Skip to content

Commit

Permalink
Merge pull request #4 from libp2p/merge/upstream
Browse files Browse the repository at this point in the history
Merge/upstream
  • Loading branch information
Stebalien authored Mar 12, 2019
2 parents b1008fb + e426ef9 commit 5b3e757
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ require (
github.com/huin/goupnp v1.0.0
github.com/jackpal/gateway v1.0.5
github.com/jackpal/go-nat-pmp v1.0.1
github.com/koron/go-ssdp v0.0.0-20180514024734-4a0ed625a78b
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/jackpal/gateway v1.0.5 h1:qzXWUJfuMdlLMtt0a3Dgt+xkWQiA5itDEITVJtuSwMc
github.com/jackpal/gateway v1.0.5/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQetPfnjA=
github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA=
github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/koron/go-ssdp v0.0.0-20180514024734-4a0ed625a78b h1:wxtKgYHEncAU00muMD06dzLiahtGM1eouRNOzVV7tdQ=
github.com/koron/go-ssdp v0.0.0-20180514024734-4a0ed625a78b/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk=
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1 h1:Y/KGZSOdz/2r0WJ9Mkmz6NJBusp0kiNx1Cn82lzJQ6w=
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down
2 changes: 2 additions & 0 deletions nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func DiscoverGateway() (NAT, error) {
return nat, nil
case nat := <-discoverUPNP_IG2():
return nat, nil
case nat := <-discoverUPNP_GenIGDev():
return nat, nil
case nat := <-discoverNATPMP():
return nat, nil
case <-time.After(10 * time.Second):
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"hash": "QmYsYNh6saxUYHajdj49uiRzdxQgiFTtymrjf3d1f2Cer4",
"name": "go-nat-pmp",
"version": "1.0.1"
},
{
"author": "koron",
"hash": "QmfDrLLyLVTLbDFfYvPsMqj3hgacxFCXyShZ5YjobqUAtr",
"name": "go-ssdp",
"version": "0.0.0"
}
],
"gxVersion": "0.9.0",
Expand Down
60 changes: 60 additions & 0 deletions upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package nat

import (
"net"
"net/url"
"strings"
"time"

"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1"
"github.com/huin/goupnp/dcps/internetgateway2"

"github.com/koron/go-ssdp"
)

var (
Expand Down Expand Up @@ -123,6 +127,62 @@ func discoverUPNP_IG2() <-chan NAT {
return res
}

func discoverUPNP_GenIGDev() <-chan NAT {
res := make(chan NAT, 1)
go func() {
DeviceList, err := ssdp.Search(ssdp.All, 5, "")
if err != nil {
return
}
var gw ssdp.Service
for _, Service := range DeviceList {
if strings.Contains(Service.Type, "InternetGatewayDevice") {
gw = Service
break
}
}

DeviceURL, err := url.Parse(gw.Location)
if err != nil {
return
}
RootDevice, err := goupnp.DeviceByURL(DeviceURL)
if err != nil {
return
}

RootDevice.Device.VisitServices(func(srv *goupnp.Service) {
switch srv.ServiceType {
case internetgateway1.URN_WANIPConnection_1:
client := &internetgateway1.WANIPConnection1{ServiceClient: goupnp.ServiceClient{
SOAPClient: srv.NewSOAPClient(),
RootDevice: RootDevice,
Service: srv,
}}
_, isNat, err := client.GetNATRSIPStatus()
if err == nil && isNat {
res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-IP1)", RootDevice}
return
}

case internetgateway1.URN_WANPPPConnection_1:
client := &internetgateway1.WANPPPConnection1{ServiceClient: goupnp.ServiceClient{
SOAPClient: srv.NewSOAPClient(),
RootDevice: RootDevice,
Service: srv,
}}
_, isNat, err := client.GetNATRSIPStatus()
if err == nil && isNat {
res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-PPP1)", RootDevice}
return
}

}
})
}()
return res
}

type upnp_NAT_Client interface {
GetExternalIPAddress() (string, error)
AddPortMapping(string, uint16, string, uint16, string, bool, string, uint32) error
Expand Down

0 comments on commit 5b3e757

Please sign in to comment.