[lbry] upnp: brought in upnp fix from dcrd
This commit is contained in:
parent
0c8cf5dea0
commit
ed5dd41a2a
1 changed files with 18 additions and 8 deletions
26
upnp.go
26
upnp.go
|
@ -39,7 +39,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -126,8 +126,9 @@ func Discover() (nat NAT, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var serviceIP string = getServiceIP(serviceURL)
|
||||||
var ourIP string
|
var ourIP string
|
||||||
ourIP, err = getOurIP()
|
ourIP, err = getOurIP(serviceIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -212,13 +213,22 @@ func getChildService(d *device, serviceType string) *service {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getOurIP returns a best guess at what the local IP is.
|
func getServiceIP(serviceURL string) (routerIP string) {
|
||||||
func getOurIP() (ip string, err error) {
|
url, _ := url.Parse(serviceURL)
|
||||||
hostname, err := os.Hostname()
|
return url.Hostname()
|
||||||
if err != nil {
|
}
|
||||||
return
|
|
||||||
|
// getOurIP returns the local IP that is on the same subnet as the serviceIP.
|
||||||
|
func getOurIP(serviceIP string) (ip string, err error) {
|
||||||
|
_, serviceNet, _ := net.ParseCIDR(serviceIP + "/24")
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ip, _, _ := net.ParseCIDR(addr.String())
|
||||||
|
if serviceNet.Contains(ip) {
|
||||||
|
return ip.String(), nil
|
||||||
}
|
}
|
||||||
return net.LookupCNAME(hostname)
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// getServiceURL parses the xml description at the given root url to find the
|
// getServiceURL parses the xml description at the given root url to find the
|
||||||
|
|
Loading…
Reference in a new issue