Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-nat
Commits
51722233
Unverified
Commit
51722233
authored
Mar 02, 2019
by
Simon Menke
Committed by
GitHub
Mar 02, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7 from ryanskidmore/master
Added method to generically discover IG Devices
parents
e3ba0d89
06c184b5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
0 deletions
+62
-0
nat.go
nat.go
+2
-0
upnp.go
upnp.go
+60
-0
No files found.
nat.go
View file @
51722233
...
@@ -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
)
:
...
...
upnp.go
View file @
51722233
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment