-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
200 lines (160 loc) · 4.02 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"flag"
"fmt"
"io"
"net"
"os"
"os/signal"
"syscall"
"github.com/xjdrew/go-udtwrapper/udt"
)
var udtaddr = flag.String("udtaddr", "", "Remote UDT address (host:port) to connect.")
var udtport = flag.Int("udtport", 0, "Local UDT port to listen.")
var tcpaddr = flag.String("tcpaddr", "", "Remote TCP address (host:port) to connect.")
var tcpport = flag.Int("tcpport", 0, "Local TCP port to listen.")
var bufsize = flag.Int("bufsize", 1024*1024, "Send/receive buffer size.")
var stopc chan struct{}
var stopped bool = false
func exitOnError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(-1)
}
}
type Dialer interface {
Dial(network, address string) (net.Conn, error)
}
func main() {
flag.Parse()
if flag.NFlag() == 0 {
flag.Usage()
os.Exit(0)
}
if *udtaddr != "" && *tcpaddr != "" {
fmt.Fprintf(os.Stderr, "both udt and tcp address shouldn't be specified.\n")
os.Exit(0)
}
if *udtaddr == "" && *tcpaddr == "" {
fmt.Fprintf(os.Stderr, "either udt or tcp address should be specified.\n")
os.Exit(0)
}
if *udtaddr != "" && *tcpport == 0 {
fmt.Fprintf(os.Stderr, "tcp port should be specified for listening.\n")
os.Exit(0)
}
if *tcpaddr != "" && *udtport == 0 {
fmt.Fprintf(os.Stderr, "udt port should be specified for listening.\n")
os.Exit(0)
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
stopc = make(chan struct{}, 1)
go func() {
<-sigc
stopped = true
close(stopc)
}()
var ln net.Listener
var d Dialer
var err error
var network, raddr string
if *udtaddr != "" {
fmt.Printf("listening on tcp port %d...\n", *tcpport)
ln, err = net.Listen("tcp", fmt.Sprintf(":%d", *tcpport))
exitOnError(err)
d = &udt.Dialer{}
network = "udt"
raddr = *udtaddr
} else {
fmt.Printf("listening on udt port %d...\n", *tcpport)
ln, err = udt.Listen("udt", fmt.Sprintf(":%d", *udtport))
exitOnError(err)
d = &net.Dialer{}
network = "tcp"
raddr = *tcpaddr
}
go func() {
<-stopc
ln.Close()
}()
for {
fmt.Printf("waiting for local connection...\n")
c, err := ln.Accept()
if stopped {
break
}
exitOnError(err)
fmt.Printf("new local connection from %s.\n", c.RemoteAddr())
go handle(d, network, raddr, c)
}
}
func handle(d Dialer, network, raddr string, l net.Conn) {
fmt.Printf("connecting to remote address %s...\n", raddr)
r, err := d.Dial(network, raddr)
exitOnError(err)
fmt.Printf("tunneling between %s and %s...\n", l.RemoteAddr(), r.RemoteAddr())
tunnel(l, r)
l.Close()
r.Close()
}
func tunnel(l, r net.Conn) {
donec := make(chan struct{}, 2)
go func() {
buf := make([]byte, *bufsize)
n := 0
for {
nr, rerr := r.Read(buf)
if rerr != nil && rerr != io.EOF {
fmt.Fprintf(os.Stderr, "reading from %s failed: %s\n", r.RemoteAddr(), rerr.Error())
break
}
fmt.Printf("read %d bytes from remote\n", nr)
if nr > 0 {
nw, werr := l.Write(buf[:nr])
if werr != nil {
fmt.Fprintf(os.Stderr, "writing to %s failed: %s\n", l.RemoteAddr(), werr.Error())
break
}
fmt.Printf("written %d bytes to local\n", nw)
n += nw
}
if rerr == io.EOF {
break
}
}
fmt.Printf("tunnel %s<->%s received %d bytes.\n", l.RemoteAddr(), r.RemoteAddr(), n)
donec <- struct{}{}
}()
go func() {
buf := make([]byte, *bufsize)
n := 0
for {
nr, rerr := l.Read(buf)
if rerr != nil && rerr != io.EOF {
fmt.Fprintf(os.Stderr, "reading from %s failed: %s\n", l.RemoteAddr(), rerr.Error())
break
}
fmt.Printf("read %d bytes from local\n", nr)
if nr > 0 {
nw, werr := r.Write(buf[:nr])
if werr != nil {
fmt.Fprintf(os.Stderr, "writing %s remote failed: %s\n", r.RemoteAddr(), werr.Error())
break
}
fmt.Printf("written %d bytes to remote\n", nw)
n += nw
}
if rerr == io.EOF {
break
}
}
fmt.Printf("tunnel %s<->%s sent %d bytes.\n", l.RemoteAddr(), r.RemoteAddr(), n)
donec <- struct{}{}
}()
select {
case <-donec:
case <-stopc:
return
}
}