Skip to content

Commit

Permalink
[lbry] brought in upnp fix from dcrd
Browse files Browse the repository at this point in the history
  • Loading branch information
BrannonKing committed Aug 17, 2021
1 parent 540f4cb commit 5cb92dd
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"fmt"
"net"
"net/http"
"os"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -126,8 +126,9 @@ func Discover() (nat NAT, err error) {
if err != nil {
return
}
var serviceIP string = getServiceIP(serviceURL)
var ourIP string
ourIP, err = getOurIP()
ourIP, err = getOurIP(serviceIP)
if err != nil {
return
}
Expand Down Expand Up @@ -212,13 +213,22 @@ func getChildService(d *device, serviceType string) *service {
return nil
}

// getOurIP returns a best guess at what the local IP is.
func getOurIP() (ip string, err error) {
hostname, err := os.Hostname()
if err != nil {
return
func getServiceIP(serviceURL string) (routerIP string) {
url, _ := url.Parse(serviceURL)
return url.Hostname()
}

// 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
Expand Down

0 comments on commit 5cb92dd

Please sign in to comment.