Skip to content

Commit

Permalink
Merge pull request fd#7 from ryanskidmore/master
Browse files Browse the repository at this point in the history
Added method to generically discover IG Devices
  • Loading branch information
fd authored Mar 2, 2019
2 parents e3ba0d8 + 06c184b commit 5172223
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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
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 5172223

Please sign in to comment.