diff --git a/go.mod b/go.mod index b814e3f..9abe6b0 100644 --- a/go.mod +++ b/go.mod @@ -1,18 +1,17 @@ module github.com/libp2p/go-nat require ( - github.com/huin/goupnp v1.0.0 + github.com/huin/goupnp v1.1.0 github.com/jackpal/go-nat-pmp v1.0.2 - github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d - github.com/libp2p/go-netroute v0.1.2 + github.com/koron/go-ssdp v0.0.4 + github.com/libp2p/go-netroute v0.2.1 ) require ( - github.com/google/gopacket v1.1.17 // indirect - github.com/libp2p/go-sockaddr v0.0.2 // indirect - golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect - golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 // indirect - golang.org/x/text v0.3.0 // indirect + github.com/google/gopacket v1.1.19 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect + golang.org/x/sys v0.5.0 // indirect ) go 1.19 diff --git a/go.sum b/go.sum index be2d04a..7e568f4 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,28 @@ -github.com/google/gopacket v1.1.17 h1:rMrlX2ZY2UbvT+sdz3+6J+pp2z+msCq9MxTU6ymxbBY= -github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8vaU0diBUM= -github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo= -github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= -github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= +github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8= +github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo= +github.com/huin/goupnp v1.1.0 h1:gEe0Dp/lZmPZiDFzJJaOfUpOvv2MKUkoBX8lDrn9vKU= +github.com/huin/goupnp v1.1.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8= github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= -github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ= -github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk= -github.com/libp2p/go-netroute v0.1.2 h1:UHhB35chwgvcRI392znJA3RCBtZ3MpE3ahNCN5MR4Xg= -github.com/libp2p/go-netroute v0.1.2/go.mod h1:jZLDV+1PE8y5XxBySEBgbuVAXbhtuHSdmLPL2n9MKbk= -github.com/libp2p/go-sockaddr v0.0.2 h1:tCuXfpA9rq7llM/v834RKc/Xvovy/AqM9kHvTV/jY/Q= -github.com/libp2p/go-sockaddr v0.0.2/go.mod h1:syPvOmNs24S3dFVGJA1/mrqdeijPxLV2Le3BRLKd68k= +github.com/koron/go-ssdp v0.0.4 h1:1IDwrghSKYM7yLf7XCzbByg2sJ/JcNOZRXS2jczTwz0= +github.com/koron/go-ssdp v0.0.4/go.mod h1:oDXq+E5IL5q0U8uSBcoAXzTzInwy5lEgC91HoKtbmZk= +github.com/libp2p/go-netroute v0.2.1 h1:V8kVrpD8GK0Riv15/7VN6RbUQ3URNZVosw7H2v9tksU= +github.com/libp2p/go-netroute v0.2.1/go.mod h1:hraioZr0fhBjG0ZRXJJ6Zj2IVEVNx6tDTFQfSmcq7mQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 h1:1Fzlr8kkDLQwqMP8GxrhptBLqZG/EDpiATneiZHY998= -golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/nat.go b/nat.go index 6f2ae18..6b3e19c 100644 --- a/nat.go +++ b/nat.go @@ -29,10 +29,10 @@ type NAT interface { GetInternalAddress() (addr net.IP, err error) // AddPortMapping maps a port on the local host to an external port. - AddPortMapping(protocol string, internalPort int, description string, timeout time.Duration) (mappedExternalPort int, err error) + AddPortMapping(ctx context.Context, protocol string, internalPort int, description string, timeout time.Duration) (mappedExternalPort int, err error) // DeletePortMapping removes a port mapping. - DeletePortMapping(protocol string, internalPort int) (err error) + DeletePortMapping(ctx context.Context, protocol string, internalPort int) (err error) } // DiscoverNATs returns all NATs discovered in the network. diff --git a/natpmp.go b/natpmp.go index a573f09..2378d8d 100644 --- a/natpmp.go +++ b/natpmp.go @@ -95,7 +95,7 @@ func (n *natpmpNAT) GetExternalAddress() (addr net.IP, err error) { return net.IPv4(d[0], d[1], d[2], d[3]), nil } -func (n *natpmpNAT) AddPortMapping(protocol string, internalPort int, description string, timeout time.Duration) (int, error) { +func (n *natpmpNAT) AddPortMapping(_ context.Context, protocol string, internalPort int, _ string, timeout time.Duration) (int, error) { var ( err error ) @@ -122,7 +122,7 @@ func (n *natpmpNAT) AddPortMapping(protocol string, internalPort int, descriptio return 0, err } -func (n *natpmpNAT) DeletePortMapping(protocol string, internalPort int) (err error) { +func (n *natpmpNAT) DeletePortMapping(_ context.Context, _ string, internalPort int) (err error) { delete(n.ports, internalPort) return nil } diff --git a/upnp.go b/upnp.go index ccfeb14..c50b952 100644 --- a/upnp.go +++ b/upnp.go @@ -14,9 +14,7 @@ import ( "github.com/koron/go-ssdp" ) -var ( - _ NAT = (*upnp_NAT)(nil) -) +var _ NAT = (*upnp_NAT)(nil) func discoverUPNP_IG1(ctx context.Context) <-chan NAT { res := make(chan NAT) @@ -24,7 +22,7 @@ func discoverUPNP_IG1(ctx context.Context) <-chan NAT { defer close(res) // find devices - devs, err := goupnp.DiscoverDevices(internetgateway1.URN_WANConnectionDevice_1) + devs, err := goupnp.DiscoverDevicesCtx(ctx, internetgateway1.URN_WANConnectionDevice_1) if err != nil { return } @@ -45,7 +43,7 @@ func discoverUPNP_IG1(ctx context.Context) <-chan NAT { RootDevice: dev.Root, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-IP1)", dev.Root}: @@ -59,7 +57,7 @@ func discoverUPNP_IG1(ctx context.Context) <-chan NAT { RootDevice: dev.Root, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-PPP1)", dev.Root}: @@ -81,7 +79,7 @@ func discoverUPNP_IG2(ctx context.Context) <-chan NAT { defer close(res) // find devices - devs, err := goupnp.DiscoverDevices(internetgateway2.URN_WANConnectionDevice_2) + devs, err := goupnp.DiscoverDevicesCtx(ctx, internetgateway2.URN_WANConnectionDevice_2) if err != nil { return } @@ -102,7 +100,7 @@ func discoverUPNP_IG2(ctx context.Context) <-chan NAT { RootDevice: dev.Root, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-IP1)", dev.Root}: @@ -116,7 +114,7 @@ func discoverUPNP_IG2(ctx context.Context) <-chan NAT { RootDevice: dev.Root, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-IP2)", dev.Root}: @@ -130,7 +128,7 @@ func discoverUPNP_IG2(ctx context.Context) <-chan NAT { RootDevice: dev.Root, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-PPP1)", dev.Root}: @@ -167,7 +165,7 @@ func discoverUPNP_GenIGDev(ctx context.Context) <-chan NAT { if err != nil { return } - RootDevice, err := goupnp.DeviceByURL(DeviceURL) + RootDevice, err := goupnp.DeviceByURLCtx(ctx, DeviceURL) if err != nil { return } @@ -183,7 +181,7 @@ func discoverUPNP_GenIGDev(ctx context.Context) <-chan NAT { RootDevice: RootDevice, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-IP1)", RootDevice}: @@ -197,7 +195,7 @@ func discoverUPNP_GenIGDev(ctx context.Context) <-chan NAT { RootDevice: RootDevice, Service: srv, }} - _, isNat, err := client.GetNATRSIPStatus() + _, isNat, err := client.GetNATRSIPStatusCtx(ctx) if err == nil && isNat { select { case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-PPP1)", RootDevice}: @@ -213,8 +211,8 @@ func discoverUPNP_GenIGDev(ctx context.Context) <-chan NAT { type upnp_NAT_Client interface { GetExternalIPAddress() (string, error) - AddPortMapping(string, uint16, string, uint16, string, bool, string, uint32) error - DeletePortMapping(string, uint16, string) error + AddPortMappingCtx(context.Context, string, uint16, string, uint16, string, bool, string, uint32) error + DeletePortMappingCtx(context.Context, string, uint16, string) error } type upnp_NAT struct { @@ -249,7 +247,7 @@ func mapProtocol(s string) string { } } -func (u *upnp_NAT) AddPortMapping(protocol string, internalPort int, description string, timeout time.Duration) (int, error) { +func (u *upnp_NAT) AddPortMapping(ctx context.Context, protocol string, internalPort int, description string, timeout time.Duration) (int, error) { ip, err := u.GetInternalAddress() if err != nil { return 0, nil @@ -258,7 +256,7 @@ func (u *upnp_NAT) AddPortMapping(protocol string, internalPort int, description timeoutInSeconds := uint32(timeout / time.Second) if externalPort := u.ports[internalPort]; externalPort > 0 { - err = u.c.AddPortMapping("", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds) + err = u.c.AddPortMappingCtx(ctx, "", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds) if err == nil { return externalPort, nil } @@ -266,7 +264,7 @@ func (u *upnp_NAT) AddPortMapping(protocol string, internalPort int, description for i := 0; i < 3; i++ { externalPort := randomPort() - err = u.c.AddPortMapping("", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds) + err = u.c.AddPortMappingCtx(ctx, "", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds) if err == nil { u.ports[internalPort] = externalPort return externalPort, nil @@ -276,10 +274,10 @@ func (u *upnp_NAT) AddPortMapping(protocol string, internalPort int, description return 0, err } -func (u *upnp_NAT) DeletePortMapping(protocol string, internalPort int) error { +func (u *upnp_NAT) DeletePortMapping(ctx context.Context, protocol string, internalPort int) error { if externalPort := u.ports[internalPort]; externalPort > 0 { delete(u.ports, internalPort) - return u.c.DeletePortMapping("", uint16(externalPort), mapProtocol(protocol)) + return u.c.DeletePortMappingCtx(ctx, "", uint16(externalPort), mapProtocol(protocol)) } return nil