From 06c184b582abc99b71b8c53ea783e8778759c7f5 Mon Sep 17 00:00:00 2001 From: Ryan Skidmore Date: Fri, 1 Mar 2019 15:09:57 +0000 Subject: [PATCH 1/2] Added method to generically discover IG Devices --- nat.go | 2 ++ upnp.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/nat.go b/nat.go index 3cf9648585..eb574d63c2 100644 --- a/nat.go +++ b/nat.go @@ -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): diff --git a/upnp.go b/upnp.go index 86d6e9b3ba..caad350114 100644 --- a/upnp.go +++ b/upnp.go @@ -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 ( @@ -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 From e426ef9d27106ae2b62c8284a54059ec4c9fd263 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 11 Mar 2019 18:53:11 -0700 Subject: [PATCH 2/2] gx import go-ssdp --- package.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/package.json b/package.json index 61cdf82719..b8fa3361bb 100644 --- a/package.json +++ b/package.json @@ -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",