-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupnp.go
69 lines (60 loc) · 1.53 KB
/
upnp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package nymo
import (
"context"
"errors"
"github.com/huin/goupnp/dcps/internetgateway2"
"golang.org/x/sync/errgroup"
)
type routerClient interface {
AddPortMappingCtx(
ctx context.Context,
NewRemoteHost string,
NewExternalPort uint16,
NewProtocol string,
NewInternalPort uint16,
NewInternalClient string,
NewEnabled bool,
NewPortMappingDescription string,
NewLeaseDuration uint32,
) (err error)
DeletePortMappingCtx(
ctx context.Context,
NewRemoteHost string,
NewExternalPort uint16,
NewProtocol string,
) (err error)
GetExternalIPAddressCtx(
ctx context.Context,
) (NewExternalIPAddress string, err error)
}
func pickRouterClient(ctx context.Context) (routerClient, error) {
tasks, _ := errgroup.WithContext(ctx)
var ip1Clients []*internetgateway2.WANIPConnection1
tasks.Go(func() (err error) {
ip1Clients, _, err = internetgateway2.NewWANIPConnection1Clients()
return
})
var ip2Clients []*internetgateway2.WANIPConnection2
tasks.Go(func() (err error) {
ip2Clients, _, err = internetgateway2.NewWANIPConnection2Clients()
return
})
var ppp1Clients []*internetgateway2.WANPPPConnection1
tasks.Go(func() (err error) {
ppp1Clients, _, err = internetgateway2.NewWANPPPConnection1Clients()
return
})
if err := tasks.Wait(); err != nil {
return nil, err
}
switch {
case len(ip2Clients) >= 1:
return ip2Clients[0], nil
case len(ip1Clients) >= 1:
return ip1Clients[0], nil
case len(ppp1Clients) >= 1:
return ppp1Clients[0], nil
default:
return nil, errors.New("no services found")
}
}