-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (147 loc) · 3.36 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/netip"
"os"
"os/exec"
"strconv"
"time"
"github.com/xmdhs/natupnp/natmap"
"github.com/xmdhs/natupnp/reuse"
)
var (
stun string
localAddr string
port string
test bool
target string
comm string
udp bool
)
func init() {
flag.StringVar(&stun, "s", "turn.cloudflare.com:3478", "stun")
flag.StringVar(&localAddr, "l", "", "local addr")
flag.StringVar(&port, "p", "8086", "port")
flag.StringVar(&target, "d", "", "forward to target host")
flag.BoolVar(&test, "t", false, "test server (only tcp)")
flag.StringVar(&comm, "e", "", "run script for mapped address")
flag.BoolVar(&udp, "u", false, "udp")
flag.Parse()
}
func main() {
ctx := context.Background()
if localAddr == "" {
s, err := natmap.GetLocalAddr()
if err != nil {
panic(err)
}
h, _, err := net.SplitHostPort(s.String())
if err != nil {
panic(err)
}
localAddr = h
}
portu, err := strconv.ParseUint(port, 10, 64)
if err != nil {
panic(err)
}
laddrPort := netip.AddrPortFrom(netip.MustParseAddr(localAddr), uint16(portu))
for {
err := openPort(ctx, target, laddrPort, stun, func(s netip.AddrPort) {
fmt.Println(s)
if comm != "" {
c := exec.CommandContext(ctx, comm, localAddr, port, s.Addr().String(), strconv.Itoa(int(s.Port())))
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err = c.Run()
if err != nil {
log.Println(err)
}
}
}, udp, test)
if err != nil {
log.Println(err)
}
time.Sleep(100 * time.Millisecond)
}
}
func openPort(ctx context.Context, target string, laddr netip.AddrPort,
stun string, finish func(netip.AddrPort), udp bool, testserver bool) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if target != "" {
var forward func(ctx context.Context, laddr netip.AddrPort, target string, log func(string)) (io.Closer, error)
if udp {
forward = natmap.ForwardUdp
} else {
forward = natmap.Forward
}
l, err := forward(ctx, laddr, target, func(s string) {
log.Println(s)
})
if err != nil {
return fmt.Errorf("openPort: %w", err)
}
defer l.Close()
}
if testserver {
l, err := testServer(ctx, laddr)
if err != nil {
return fmt.Errorf("openPort: %w", err)
}
defer l.Close()
}
errCh := make(chan error, 1)
var nmap func(ctx context.Context, stunAddr string, laddr netip.AddrPort, log func(error)) (*natmap.Map, netip.AddrPort, error)
if udp {
nmap = natmap.NatMapUdp
} else {
nmap = natmap.NatMap
}
m, s, err := nmap(ctx, stun, laddr, func(s error) {
cancel()
select {
case errCh <- s:
default:
}
})
if err != nil {
return fmt.Errorf("openPort: %w", err)
}
defer m.Close()
finish(s)
err = <-errCh
if err != nil {
return fmt.Errorf("openPort: %w", err)
}
return nil
}
func testServer(ctx context.Context, laddr netip.AddrPort) (net.Listener, error) {
s := http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Addr: laddr.String(),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}),
}
l, err := reuse.Listen(ctx, "tcp", laddr.String())
if err != nil {
return nil, fmt.Errorf("testServer: %w", err)
}
go func() {
err = s.Serve(l)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Println(err)
}
}()
return l, nil
}