Esempio n. 1
0
func NewDefaultListener(protocol string, lAddr string, requireUPNPHairpin bool) Listener {
	// Local listen IP & port
	lAddrIP, lAddrPort := splitHostPort(lAddr)

	// Create listener
	listener, err := net.Listen(protocol, lAddr)
	if err != nil {
		panic(err)
	}
	// Actual listener local IP & port
	listenerIP, listenerPort := splitHostPort(listener.Addr().String())
	log.Debug("Local listener", "ip", listenerIP, "port", listenerPort)

	// Determine internal address...
	var intAddr *NetAddress = NewNetAddressString(lAddr)

	// Determine external address...
	var extAddr *NetAddress
	// If the lAddrIP is INADDR_ANY, try UPnP
	if lAddrIP == "" || lAddrIP == "0.0.0.0" {
		if requireUPNPHairpin {
			upnpCapabilities, err := upnp.Probe()
			if err != nil {
				log.Warn("Failed to probe UPNP", "error", err)
				goto SKIP_UPNP
			}
			if !upnpCapabilities.Hairpin {
				goto SKIP_UPNP
			}
		}
		extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
	}
SKIP_UPNP:

	// Otherwise just use the local address...
	if extAddr == nil {
		extAddr = getNaiveExternalAddress(listenerPort)
	}
	if extAddr == nil {
		panic("Could not determine external address!")
	}

	dl := &DefaultListener{
		listener:    listener,
		intAddr:     intAddr,
		extAddr:     extAddr,
		connections: make(chan net.Conn, numBufferedConnections),
	}

	go dl.listenRoutine()

	return dl
}
Esempio n. 2
0
func probe_upnp() {

	capabilities, err := upnp.Probe()
	if err != nil {
		fmt.Println("Probe failed: %v", err)
	} else {
		fmt.Println("Probe success!")
		jsonBytes, err := json.Marshal(capabilities)
		if err != nil {
			panic(err)
		}
		fmt.Println(string(jsonBytes))
	}

}