| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package upnp |
| 27 |
|
| 28 |
import ( |
| 29 |
"context" |
| 30 |
"errors" |
| 31 |
"net" |
| 32 |
"net/url" |
| 33 |
"time" |
| 34 |
|
| 35 |
"github.com/NebulousLabs/fastrand" |
| 36 |
"github.com/NebulousLabs/go-upnp/goupnp" |
| 37 |
"github.com/NebulousLabs/go-upnp/goupnp/dcps/internetgateway1" |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
type IGD struct { |
| 43 |
|
| 44 |
|
| 45 |
client interface { |
| 46 |
GetExternalIPAddress() (string, error) |
| 47 |
AddPortMapping(string, uint16, string, uint16, string, bool, string, uint32) error |
| 48 |
DeletePortMapping(string, uint16, string) error |
| 49 |
GetServiceClient() *goupnp.ServiceClient |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func (d *IGD) ExternalIP() (string, error) { |
| 55 |
return d.client.GetExternalIPAddress() |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func (d *IGD) Forward(port uint16, desc string) error { |
| 61 |
ip, err := d.getInternalIP() |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
|
| 66 |
err = d.client.AddPortMapping("", port, "TCP", port, ip, true, desc, 0) |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
return d.client.AddPortMapping("", port, "UDP", port, ip, true, desc, 0) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (d *IGD) Clear(port uint16) error { |
| 75 |
tcpErr := d.client.DeletePortMapping("", port, "TCP") |
| 76 |
udpErr := d.client.DeletePortMapping("", port, "UDP") |
| 77 |
|
| 78 |
if tcpErr != nil && udpErr != nil { |
| 79 |
return tcpErr |
| 80 |
} |
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func (d *IGD) Location() string { |
| 86 |
return d.client.GetServiceClient().Location.String() |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func (d *IGD) getInternalIP() (string, error) { |
| 91 |
host, _, _ := net.SplitHostPort(d.client.GetServiceClient().RootDevice.URLBase.Host) |
| 92 |
devIP := net.ParseIP(host) |
| 93 |
if devIP == nil { |
| 94 |
return "", errors.New("could not determine router's internal IP") |
| 95 |
} |
| 96 |
|
| 97 |
ifaces, err := net.Interfaces() |
| 98 |
if err != nil { |
| 99 |
return "", err |
| 100 |
} |
| 101 |
|
| 102 |
for _, iface := range ifaces { |
| 103 |
addrs, err := iface.Addrs() |
| 104 |
if err != nil { |
| 105 |
return "", err |
| 106 |
} |
| 107 |
|
| 108 |
for _, addr := range addrs { |
| 109 |
if x, ok := addr.(*net.IPNet); ok && x.Contains(devIP) { |
| 110 |
return x.IP.String(), nil |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return "", errors.New("could not determine internal IP") |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func Discover() (*IGD, error) { |
| 120 |
return DiscoverCtx(context.Background()) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
func DiscoverCtx(ctx context.Context) (*IGD, error) { |
| 129 |
|
| 130 |
|
| 131 |
maxTries := 3 |
| 132 |
sleepTime := time.Millisecond * time.Duration(fastrand.Intn(5000)) |
| 133 |
for try := 0; try < maxTries; try++ { |
| 134 |
pppclients, _, _ := internetgateway1.NewWANPPPConnection1Clients(ctx) |
| 135 |
if len(pppclients) > 0 { |
| 136 |
return &IGD{pppclients[0]}, nil |
| 137 |
} |
| 138 |
ipclients, _, _ := internetgateway1.NewWANIPConnection1Clients(ctx) |
| 139 |
if len(ipclients) > 0 { |
| 140 |
return &IGD{ipclients[0]}, nil |
| 141 |
} |
| 142 |
select { |
| 143 |
case <-ctx.Done(): |
| 144 |
return nil, context.Canceled |
| 145 |
case <-time.After(sleepTime): |
| 146 |
} |
| 147 |
sleepTime *= 2 |
| 148 |
} |
| 149 |
return nil, errors.New("no UPnP-enabled gateway found") |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
func Load(rawurl string) (*IGD, error) { |
| 156 |
loc, err := url.Parse(rawurl) |
| 157 |
if err != nil { |
| 158 |
return nil, err |
| 159 |
} |
| 160 |
pppclients, _ := internetgateway1.NewWANPPPConnection1ClientsByURL(loc) |
| 161 |
if len(pppclients) > 0 { |
| 162 |
return &IGD{pppclients[0]}, nil |
| 163 |
} |
| 164 |
ipclients, _ := internetgateway1.NewWANIPConnection1ClientsByURL(loc) |
| 165 |
if len(ipclients) > 0 { |
| 166 |
return &IGD{ipclients[0]}, nil |
| 167 |
} |
| 168 |
return nil, errors.New("no UPnP-enabled gateway found at URL " + rawurl) |
| 169 |
} |