Unverified Commit 51722233 authored by Simon Menke's avatar Simon Menke Committed by GitHub

Merge pull request #7 from ryanskidmore/master

Added method to generically discover IG Devices
parents e3ba0d89 06c184b5
...@@ -41,6 +41,8 @@ func DiscoverGateway() (NAT, error) { ...@@ -41,6 +41,8 @@ func DiscoverGateway() (NAT, error) {
return nat, nil return nat, nil
case nat := <-discoverUPNP_IG2(): case nat := <-discoverUPNP_IG2():
return nat, nil return nat, nil
case nat := <-discoverUPNP_GenIGDev():
return nat, nil
case nat := <-discoverNATPMP(): case nat := <-discoverNATPMP():
return nat, nil return nat, nil
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
......
...@@ -2,11 +2,15 @@ package nat ...@@ -2,11 +2,15 @@ package nat
import ( import (
"net" "net"
"net/url"
"strings"
"time" "time"
"github.com/huin/goupnp" "github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1" "github.com/huin/goupnp/dcps/internetgateway1"
"github.com/huin/goupnp/dcps/internetgateway2" "github.com/huin/goupnp/dcps/internetgateway2"
"github.com/koron/go-ssdp"
) )
var ( var (
...@@ -123,6 +127,62 @@ func discoverUPNP_IG2() <-chan NAT { ...@@ -123,6 +127,62 @@ func discoverUPNP_IG2() <-chan NAT {
return res 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 { type upnp_NAT_Client interface {
GetExternalIPAddress() (string, error) GetExternalIPAddress() (string, error)
AddPortMapping(string, uint16, string, uint16, string, bool, string, uint32) error AddPortMapping(string, uint16, string, uint16, string, bool, string, uint32) error
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment