-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
321 lines (303 loc) · 7.22 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (C) 2023 Kevin Z <zyxkad@gmail.com>
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
)
var BuildVersion string = "dev"
var (
printLicense bool = false
noOutputLicense bool = false
udpMode bool = false
binaryMode bool = false
proxyAddr string = ""
bufferLen int = 8
linebreak string = "lf"
linebreakCh string
listenAt string = ""
targetAddr string
)
func init() {
flag.BoolVar(&printLicense, "license", printLicense, "Print the license")
flag.BoolVar(&noOutputLicense, "no-license", noOutputLicense, "Do not output the license (but you still have to follow it)")
flag.BoolVar(&udpMode, "udp", udpMode, "Use UDP mode instead of default TCP")
flag.BoolVar(&udpMode, "u", udpMode, "Use UDP mode instead of default TCP")
flag.BoolVar(&binaryMode, "binary", binaryMode, `Use hex format input/output for binary data, aka "-b"`)
flag.BoolVar(&binaryMode, "b", binaryMode, `Use hex format input/output for binary data, aka "-binary"`)
flag.IntVar(&bufferLen, "buffer", bufferLen, `The maximum output bytes per line when under binary mode, aka "-B"`)
flag.IntVar(&bufferLen, "B", bufferLen, `The maximum output bytes per line when under binary mode, aka "-buffer"`)
flag.StringVar(&proxyAddr, "proxy", proxyAddr, `The socks5 proxy address, aka "-x"`)
flag.StringVar(&proxyAddr, "x", proxyAddr, `The socks5 proxy address, aka "-proxy"`)
flag.StringVar(&linebreak, "linebreak", linebreak, `The line break, cr='\r', lf='\n', crlf='\r\n'`)
flag.StringVar(&listenAt, "listen", listenAt, `Local bind address, aka "-l"`)
flag.StringVar(&listenAt, "l", listenAt, `Local bind address, aka "-listen"`)
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
fmt.Fprint(out, helpMessage)
fmt.Fprintln(out, "Flags:")
flag.PrintDefaults()
}
}
func printErr(args ...any) {
fmt.Fprintln(os.Stderr, args...)
}
func parseFlags() {
flag.Parse()
if printLicense {
fmt.Print(LICENSE)
os.Exit(0)
}
switch linebreak {
case "cr":
linebreakCh = "\r"
default:
fallthrough
case "lf":
linebreakCh = "\n"
case "crlf":
linebreakCh = "\r\n"
}
targetAddr = flag.Arg(0)
}
type Dialer interface {
Dial(network string, addr string) (c net.Conn, err error)
ListenPacket(network string, addr string) (c net.PacketConn, err error)
}
func main() {
parseFlags()
printErr(LICENSE)
var dialer Dialer = netDialer{}
if proxyAddr != "" {
var err error
if dialer, err = NewSocks5(proxyAddr); err != nil {
printErr("Cannot create proxy:", err)
os.Exit(1)
}
}
if udpMode {
handleUDP(dialer)
} else {
handleTCP(dialer)
}
}
func handleTCP(dialer Dialer) {
conn, err := dialer.Dial("tcp", targetAddr)
if err != nil {
printErr("Err when dialing:", err)
os.Exit(1)
}
defer conn.Close()
input := bufio.NewScanner(os.Stdin)
if binaryMode {
go func() {
var err error
defer conn.Close()
for input.Scan() {
line := input.Text()
if len(line) == 0 {
continue
}
if line[0] == '!' {
if _, err = fmt.Fprintln(conn, line[1:]); err != nil {
printErr("Err when writing:", err)
os.Exit(1)
}
} else {
bts, err := parseBytes(line)
if err != nil {
printErr("Invaild input:", err)
} else if _, err = conn.Write(bts); err != nil {
printErr("Err when writing:", err)
os.Exit(1)
}
}
}
}()
buf := make([]byte, bufferLen)
var (
n int
err error
)
for {
n, err = conn.Read(buf)
if n != 0 {
fmt.Println(formatBytes(buf[:n]))
}
if err != nil {
if !errors.Is(err, io.EOF) {
printErr("Err when reading:", err)
os.Exit(1)
}
return
}
}
} else {
go func() {
var err error
defer conn.Close()
for input.Scan() {
line := input.Text()
if _, err = fmt.Fprintln(conn, line); err != nil {
printErr("Err when writing:", err)
os.Exit(1)
}
}
}()
sc := bufio.NewScanner(conn)
for sc.Scan() {
fmt.Println(sc.Text())
}
if err := sc.Err(); err != nil && !errors.Is(err, io.EOF) {
printErr("Err when reading:", err)
os.Exit(1)
}
}
}
func handleUDP(dialer Dialer) {
target := &stringAddr{"udp", targetAddr}
conn, err := dialer.ListenPacket("udp", listenAt)
if err != nil {
printErr("Err when dialing:", err)
os.Exit(1)
}
defer conn.Close()
printErr("# local addr =", conn.LocalAddr().String())
input := bufio.NewScanner(os.Stdin)
var (
n int
addr net.Addr
buf = make([]byte, 65536)
)
if binaryMode {
go func() {
var err error
defer conn.Close()
for input.Scan() {
line := input.Text()
if len(line) == 0 {
continue
}
if line[0] == '!' {
line += linebreakCh
if _, err = conn.WriteTo(([]byte)(line[1:]), target); err != nil {
printErr("Err when writing:", err)
os.Exit(1)
}
} else {
bts, err := parseBytes(line)
if err != nil {
printErr("Invaild input:", err)
} else if _, err = conn.WriteTo(bts, target); err != nil {
printErr("Err when writing:", err)
os.Exit(1)
}
}
}
}()
for {
n, addr, err = conn.ReadFrom(buf)
if n != 0 {
printErr("# recv from", addr.String(), "leng =", n)
for i, j := 0, bufferLen; i < n; i, j = j, j+bufferLen {
if j > n {
j = n
}
fmt.Println(formatBytes(buf[i:j]))
}
}
if err != nil {
if !errors.Is(err, io.EOF) {
printErr("Err when reading:", err)
os.Exit(1)
}
return
}
}
} else {
go func() {
var err error
defer conn.Close()
for input.Scan() {
line := input.Text()
line += linebreakCh
if _, err = conn.WriteTo(([]byte)(line), target); err != nil {
printErr("Err when writing:", err)
os.Exit(1)
}
}
}()
for {
n, addr, err = conn.ReadFrom(buf)
if n != 0 {
printErr("# recv from", addr.String(), "leng =", n)
fmt.Println((string)(buf[:n]))
}
if err != nil {
if !errors.Is(err, io.EOF) {
printErr("Err when reading:", err)
os.Exit(1)
}
return
}
}
}
}
func formatBytes(buf []byte) string {
s := make([]byte, len(buf)*3)
for i, v := range buf {
if i != 0 {
s = append(s, ' ')
}
if v < 16 {
s = append(s, '0')
}
s = strconv.AppendUint(s, (uint64)(v), 16)
}
return (string)(s)
}
func parseBytes(line string) (bts []byte, err error) {
fields := strings.Fields(line)
bts = make([]byte, len(fields))
for i, v := range fields {
var (
b uint64
ok bool
base = 16
)
if v, ok = strings.CutPrefix(v, "0b"); ok {
base = 2
} else if v, ok = strings.CutPrefix(v, "0o"); ok {
base = 8
} else if v, ok = strings.CutPrefix(v, "0x"); ok {
base = 16
}
b, err = strconv.ParseUint(v, base, 8)
if err != nil {
return
}
bts[i] = (byte)(b)
}
return
}
type netDialer struct{}
func (netDialer) Dial(network string, addr string) (c net.Conn, err error) {
return net.Dial(network, addr)
}
func (netDialer) ListenPacket(network string, addr string) (c net.PacketConn, err error) {
return net.ListenPacket(network, addr)
}
type stringAddr struct {
network string
addr string
}
var _ net.Addr = (*stringAddr)(nil)
func (a *stringAddr) Network() string { return a.network }
func (a *stringAddr) String() string { return a.addr }