-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (88 loc) · 2.34 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"flag"
libp2pnat "github.com/libp2p/go-libp2p-nat"
gonat "github.com/libp2p/go-nat"
"log"
"time"
)
func debugNat(natGateway gonat.NAT, port int) {
log.Println("NAT网关类型:", natGateway.Type())
natDeviceAddress, e := natGateway.GetDeviceAddress()
if e != nil {
log.Fatalln(e)
}
log.Println("NAT网关IP:", natDeviceAddress.String())
natExternalAddress, e := natGateway.GetExternalAddress()
if e != nil {
log.Fatalln(e)
}
log.Println("NAT网关公网IP:", natExternalAddress.String())
//进行端口映射
mappedExternalPort, e := natGateway.AddPortMapping("udp", port, "P2P测试", time.Minute)
if e != nil {
log.Fatalln(e)
}
log.Println("NAT映射端口:", port, mappedExternalPort)
//移除端口映射
_ = natGateway.DeletePortMapping("udp", port)
}
// 目前有go-nat和go-libp2p-nat两个库可用,
// go-nat.DiscoverNATs(ctx)的网络环境适应性较好.
func main() {
log.Println("NAT示例")
natType := flag.String("type", "", "")
flag.Parse()
//内部端口
port := 60000
//创建上下文
ctx := context.Background()
if *natType == "libp2p" {
//go-libp2p-nat库
//H3C路由需要2分钟以上, 家里的路由5秒.
log.Println("使用go-libp2p-nat")
mNAT, e := libp2pnat.DiscoverNAT(ctx)
if e != nil {
log.Fatalln(e)
}
log.Println("已有NAT映射:", mNAT.Mappings())
natMapping, e := mNAT.NewMapping("udp", port)
if e != nil {
log.Fatalln(e)
}
externalAddr, e := natMapping.ExternalAddr()
if e != nil {
log.Fatalln(e)
}
// 地址形式为IP加端口, 如 8.8.8.8:38288
log.Println("NAT公网地址:", externalAddr.String())
//移除端口映射
_ = natMapping.Close()
} else {
//go-nat库
if *natType == "gateway" {
log.Println("使用go-nat DiscoverGateway()")
//H3C路由需要2分钟以上, 家里的路由5秒.
natGateway, e := gonat.DiscoverGateway()
if e != nil {
log.Fatalln(e)
}
log.Println("找到NAT网关:", natGateway)
debugNat(natGateway, port)
} else {
log.Println("使用go-nat DiscoverNATs(ctx)")
//企业路由器2秒, 家里路由器马上
natChan := gonat.DiscoverNATs(ctx)
select {
case natGateway := <-natChan:
if natGateway == nil {
log.Println("没有找到网关")
return
}
log.Println("找到NAT网关:", natGateway)
debugNat(natGateway, port)
}
}
}
}