-
Notifications
You must be signed in to change notification settings - Fork 671
/
pmp.go
88 lines (71 loc) · 1.86 KB
/
pmp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nat
import (
"errors"
"math"
"net"
"time"
"github.com/jackpal/gateway"
natpmp "github.com/jackpal/go-nat-pmp"
)
var (
errInvalidLifetime = errors.New("invalid mapping duration range")
pmpClientTimeout = 500 * time.Millisecond
_ Router = &pmpRouter{}
)
// pmpRouter adapts the NAT-PMP protocol implementation so it conforms to the
// common interface.
type pmpRouter struct {
client *natpmp.Client
}
func (r *pmpRouter) SupportsNAT() bool {
return true
}
func (r *pmpRouter) MapPort(
networkProtocol string,
newInternalPort uint16,
newExternalPort uint16,
mappingName string,
mappingDuration time.Duration,
) error {
protocol := networkProtocol
internalPort := int(newInternalPort)
externalPort := int(newExternalPort)
// go-nat-pmp uses seconds to denote their lifetime
lifetime := mappingDuration.Seconds()
// Assumes the architecture is at least 32-bit
if lifetime < 0 || lifetime > math.MaxInt32 {
return errInvalidLifetime
}
_, err := r.client.AddPortMapping(protocol, internalPort, externalPort, int(lifetime))
return err
}
func (r *pmpRouter) UnmapPort(
networkProtocol string,
internalPort uint16,
_ uint16,
) error {
protocol := networkProtocol
internalPortInt := int(internalPort)
_, err := r.client.AddPortMapping(protocol, internalPortInt, 0, 0)
return err
}
func (r *pmpRouter) ExternalIP() (net.IP, error) {
response, err := r.client.GetExternalAddress()
if err != nil {
return nil, err
}
return response.ExternalIPAddress[:], nil
}
func getPMPRouter() *pmpRouter {
gatewayIP, err := gateway.DiscoverGateway()
if err != nil {
return nil
}
pmp := &pmpRouter{natpmp.NewClientWithTimeout(gatewayIP, pmpClientTimeout)}
if _, err := pmp.ExternalIP(); err != nil {
return nil
}
return pmp
}