2018-07-04 10:51:47 +00:00
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
2019-06-09 07:24:20 +00:00
|
|
|
"context"
|
2018-07-04 10:51:47 +00:00
|
|
|
"net"
|
2019-06-09 07:24:20 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2018-07-04 10:51:47 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/huin/goupnp"
|
|
|
|
"github.com/huin/goupnp/dcps/internetgateway1"
|
|
|
|
"github.com/huin/goupnp/dcps/internetgateway2"
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
"github.com/koron/go-ssdp"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
var _ NAT = (*upnp_NAT)(nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func discoverUPNP_IG1(ctx context.Context) <-chan NAT {
|
|
|
|
res := make(chan NAT)
|
2018-07-04 10:51:47 +00:00
|
|
|
go func() {
|
2019-06-09 07:24:20 +00:00
|
|
|
defer close(res)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// find devices
|
2023-06-30 13:41:32 +00:00
|
|
|
devs, err := goupnp.DiscoverDevicesCtx(ctx, internetgateway1.URN_WANConnectionDevice_1)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dev := range devs {
|
|
|
|
if dev.Root == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dev.Root.Device.VisitServices(func(srv *goupnp.Service) {
|
2019-06-09 07:24:20 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
switch srv.ServiceType {
|
|
|
|
case internetgateway1.URN_WANIPConnection_1:
|
|
|
|
client := &internetgateway1.WANIPConnection1{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: dev.Root,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil && isNat {
|
2019-06-09 07:24:20 +00:00
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-IP1)", dev.Root}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case internetgateway1.URN_WANPPPConnection_1:
|
|
|
|
client := &internetgateway1.WANPPPConnection1{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: dev.Root,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil && isNat {
|
2019-06-09 07:24:20 +00:00
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-PPP1)", dev.Root}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func discoverUPNP_IG2(ctx context.Context) <-chan NAT {
|
|
|
|
res := make(chan NAT)
|
2018-07-04 10:51:47 +00:00
|
|
|
go func() {
|
2019-06-09 07:24:20 +00:00
|
|
|
defer close(res)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// find devices
|
2023-06-30 13:41:32 +00:00
|
|
|
devs, err := goupnp.DiscoverDevicesCtx(ctx, internetgateway2.URN_WANConnectionDevice_2)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dev := range devs {
|
|
|
|
if dev.Root == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dev.Root.Device.VisitServices(func(srv *goupnp.Service) {
|
2019-06-09 07:24:20 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
switch srv.ServiceType {
|
|
|
|
case internetgateway2.URN_WANIPConnection_1:
|
|
|
|
client := &internetgateway2.WANIPConnection1{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: dev.Root,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil && isNat {
|
2019-06-09 07:24:20 +00:00
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-IP1)", dev.Root}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case internetgateway2.URN_WANIPConnection_2:
|
|
|
|
client := &internetgateway2.WANIPConnection2{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: dev.Root,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil && isNat {
|
2019-06-09 07:24:20 +00:00
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-IP2)", dev.Root}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case internetgateway2.URN_WANPPPConnection_1:
|
|
|
|
client := &internetgateway2.WANPPPConnection1{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: dev.Root,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil && isNat {
|
2019-06-09 07:24:20 +00:00
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-PPP1)", dev.Root}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func discoverUPNP_GenIGDev(ctx context.Context) <-chan NAT {
|
|
|
|
res := make(chan NAT, 1)
|
|
|
|
go func() {
|
|
|
|
defer close(res)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-06-30 13:41:32 +00:00
|
|
|
RootDevice, err := goupnp.DeviceByURLCtx(ctx, DeviceURL)
|
2019-06-09 07:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
RootDevice.Device.VisitServices(func(srv *goupnp.Service) {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch srv.ServiceType {
|
|
|
|
case internetgateway1.URN_WANIPConnection_1:
|
|
|
|
client := &internetgateway1.WANIPConnection1{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: RootDevice,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2019-06-09 07:24:20 +00:00
|
|
|
if err == nil && isNat {
|
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-IP1)", RootDevice}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case internetgateway1.URN_WANPPPConnection_1:
|
|
|
|
client := &internetgateway1.WANPPPConnection1{ServiceClient: goupnp.ServiceClient{
|
|
|
|
SOAPClient: srv.NewSOAPClient(),
|
|
|
|
RootDevice: RootDevice,
|
|
|
|
Service: srv,
|
|
|
|
}}
|
2023-06-30 13:41:32 +00:00
|
|
|
_, isNat, err := client.GetNATRSIPStatusCtx(ctx)
|
2019-06-09 07:24:20 +00:00
|
|
|
if err == nil && isNat {
|
|
|
|
select {
|
|
|
|
case res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG1-PPP1)", RootDevice}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
type upnp_NAT_Client interface {
|
|
|
|
GetExternalIPAddress() (string, error)
|
2023-06-30 13:41:32 +00:00
|
|
|
AddPortMappingCtx(context.Context, string, uint16, string, uint16, string, bool, string, uint32) error
|
|
|
|
DeletePortMappingCtx(context.Context, string, uint16, string) error
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type upnp_NAT struct {
|
|
|
|
c upnp_NAT_Client
|
|
|
|
ports map[int]int
|
|
|
|
typ string
|
|
|
|
rootDevice *goupnp.RootDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upnp_NAT) GetExternalAddress() (addr net.IP, err error) {
|
|
|
|
ipString, err := u.c.GetExternalIPAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.ParseIP(ipString)
|
|
|
|
if ip == nil {
|
|
|
|
return nil, ErrNoExternalAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapProtocol(s string) string {
|
|
|
|
switch s {
|
|
|
|
case "udp":
|
|
|
|
return "UDP"
|
|
|
|
case "tcp":
|
|
|
|
return "TCP"
|
|
|
|
default:
|
|
|
|
panic("invalid protocol: " + s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
func (u *upnp_NAT) AddPortMapping(ctx context.Context, protocol string, internalPort int, description string, timeout time.Duration) (int, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
ip, err := u.GetInternalAddress()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
timeoutInSeconds := uint32(timeout / time.Second)
|
|
|
|
|
|
|
|
if externalPort := u.ports[internalPort]; externalPort > 0 {
|
2023-06-30 13:41:32 +00:00
|
|
|
err = u.c.AddPortMappingCtx(ctx, "", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil {
|
|
|
|
return externalPort, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
externalPort := randomPort()
|
2023-06-30 13:41:32 +00:00
|
|
|
err = u.c.AddPortMappingCtx(ctx, "", uint16(externalPort), mapProtocol(protocol), uint16(internalPort), ip.String(), true, description, timeoutInSeconds)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err == nil {
|
|
|
|
u.ports[internalPort] = externalPort
|
|
|
|
return externalPort, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-06-30 13:41:32 +00:00
|
|
|
func (u *upnp_NAT) DeletePortMapping(ctx context.Context, protocol string, internalPort int) error {
|
2018-07-04 10:51:47 +00:00
|
|
|
if externalPort := u.ports[internalPort]; externalPort > 0 {
|
|
|
|
delete(u.ports, internalPort)
|
2023-06-30 13:41:32 +00:00
|
|
|
return u.c.DeletePortMappingCtx(ctx, "", uint16(externalPort), mapProtocol(protocol))
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upnp_NAT) GetDeviceAddress() (net.IP, error) {
|
|
|
|
addr, err := net.ResolveUDPAddr("udp4", u.rootDevice.URLBase.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr.IP, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upnp_NAT) GetInternalAddress() (net.IP, error) {
|
|
|
|
devAddr, err := u.GetDeviceAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
switch x := addr.(type) {
|
|
|
|
case *net.IPNet:
|
|
|
|
if x.Contains(devAddr) {
|
|
|
|
return x.IP, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoInternalAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *upnp_NAT) Type() string { return n.typ }
|