forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
65 lines (57 loc) · 1.64 KB
/
router.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// Package nat performs network address translation and provides helpers for
// routing ports.
package nat
import (
"net"
"time"
)
// NetworkProtocol is a protocol that will be used through a port
type NetworkProtocol string
// Available protocol
const (
TCP NetworkProtocol = "TCP"
UDP NetworkProtocol = "UDP"
)
// Router provides a standard NAT router functions. Specifically, allowing the
// fetching of public IPs and port forwarding to this computer.
type Router interface {
// mapPort creates a mapping between a port on the local computer to an
// external port on the router.
//
// The mappingName is something displayed on the router, so it is included
// for completeness.
MapPort(
networkProtocol NetworkProtocol,
newInternalPort uint16,
newExternalPort uint16,
mappingName string,
mappingDuration time.Duration) error
// UnmapPort clears a mapping that was previous made by a call to MapPort
UnmapPort(
networkProtocol NetworkProtocol,
internalPort uint16,
externalPort uint16) error
// Returns the routers IP address on the network the router considers
// external
IP() (net.IP, error)
}
// NewRouter returns a new router discovered on the local network
func NewRouter() Router {
routers := make(chan Router)
// Because getting a router can take a noticeable amount of time to error,
// we run these requests in parallel
go func() {
routers <- getUPnPRouter()
}()
go func() {
routers <- getPMPRouter()
}()
for i := 0; i < 2; i++ {
if router := <-routers; router != nil {
return router
}
}
return noRouter{}
}