-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathmain.go
More file actions
297 lines (280 loc) · 7.5 KB
/
Copy pathmain.go
File metadata and controls
297 lines (280 loc) · 7.5 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"strings"
"github.com/zema1/suo5/suo5"
// _ "github.com/chainreactors/proxyclient/extend"
log "github.com/kataras/golog"
"github.com/urfave/cli/v2"
"github.com/zema1/suo5/ctrl"
)
var Version = "v0.0.0"
func main() {
ctrl.InitDefaultLog(os.Stdout)
app := cli.NewApp()
app.Name = "suo5"
app.Usage = "A high-performance http tunnel"
app.Version = Version
defaultConfig := suo5.DefaultSuo5Config()
app.DisableSliceFlagSeparator = true
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "the filepath for json config file",
Value: "",
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Usage: "the remote server url, ex: http://localhost:8080/suo5.jsp",
Value: defaultConfig.Target,
Required: true,
},
&cli.StringFlag{
Name: "listen",
Aliases: []string{"l"},
Usage: "listen address of socks5 server",
Value: defaultConfig.Listen,
},
&cli.StringFlag{
Name: "method",
Aliases: []string{"m"},
Usage: "http request method",
Value: defaultConfig.Method,
},
&cli.StringFlag{
Name: "redirect",
Aliases: []string{"r"},
Usage: "redirect to the url if host not matched, used to bypass load balance",
Value: defaultConfig.RedirectURL,
},
&cli.StringFlag{
Name: "auth",
Usage: "socks5 creds, username:password, leave empty to auto generate",
Value: "",
},
&cli.StringFlag{
Name: "mode",
Usage: "connection mode, choices are auto, full, half, classic",
Value: string(defaultConfig.Mode),
},
&cli.StringSliceFlag{
Name: "header",
Aliases: []string{"H"},
Usage: "use extra header, ex -H 'Cookie: abc'",
Value: cli.NewStringSlice(defaultConfig.RawHeader...),
},
&cli.StringFlag{
Name: "ua",
Usage: "shortcut to set the request User-Agent",
Value: "",
},
&cli.IntFlag{
Name: "timeout",
Usage: "request timeout in seconds",
Value: defaultConfig.Timeout,
},
&cli.IntFlag{
Name: "max-body-size",
Aliases: []string{"S"},
Usage: "request max body size",
Value: defaultConfig.MaxBodySize,
},
&cli.IntFlag{
Name: "retry",
Usage: "request retry",
Value: defaultConfig.RetryCount,
},
&cli.IntFlag{
Name: "classic-poll-qps",
Usage: "request poll qps, only used in classic mode",
Aliases: []string{"qps"},
Value: defaultConfig.ClassicPollQPS,
},
&cli.IntFlag{
Name: "classic-poll-interval",
Usage: "request poll interval in milliseconds, only used in classic mode",
Aliases: []string{"qi"},
Value: defaultConfig.ClassicPollInterval,
},
&cli.StringSliceFlag{
Name: "proxy",
Usage: "set upstream proxy, support socks5/http(s), eg: socks5://127.0.0.1:7890",
Value: cli.NewStringSlice(defaultConfig.UpstreamProxy...),
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "debug the traffic, print more details",
Value: defaultConfig.Debug,
},
&cli.BoolFlag{
Name: "no-heartbeat",
Aliases: []string{"nh"},
Usage: "disable heartbeat to the remote server which will send data every 5s",
Value: defaultConfig.DisableHeartbeat,
},
&cli.BoolFlag{
Name: "no-gzip",
Aliases: []string{"ng"},
Usage: "disable gzip compression, which will improve compatibility with some old servers",
Value: defaultConfig.DisableHeartbeat,
},
&cli.BoolFlag{
Name: "jar",
Usage: "enable cookiejar",
Value: defaultConfig.EnableCookieJar,
},
&cli.BoolFlag{
Name: "no-browser-headers",
Aliases: []string{"nb"},
Usage: "disable browser headers, which will not send Accept, Accept-Encoding, etc.",
Value: !defaultConfig.ImpersonateBrowser,
},
&cli.StringFlag{
Name: "test-exit",
Aliases: []string{"T"},
Usage: "test a real connection, if success exit(0), else exit(1)",
Hidden: true,
},
&cli.StringSliceFlag{
Name: "exclude-domain",
Aliases: []string{"E"},
Usage: "exclude certain domain name for proxy, ex -E 'portswigger.net'",
},
&cli.StringFlag{
Name: "exclude-domain-file",
Aliases: []string{"ef"},
Usage: "exclude certain domains for proxy in a file, one domain per line",
},
&cli.StringFlag{
Name: "forward",
Aliases: []string{"f"},
Usage: "forward target address, enable forward mode when specified",
Value: defaultConfig.ForwardTarget,
},
&cli.IntFlag{
Name: "dirty-size",
Aliases: []string{"jk"},
Usage: "dirty data size in bytes to flush buffer, useful for bypassing proxy buffering",
Value: defaultConfig.DirtySize,
},
}
app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel("debug")
}
return nil
}
app.Action = Action
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func Action(c *cli.Context) error {
listen := c.String("listen")
target := c.String("target")
auth := c.String("auth")
mode := suo5.ConnectionType(c.String("mode"))
maxBodySize := c.Int("max-body-size")
timeout := c.Int("timeout")
debug := c.Bool("debug")
proxy := c.StringSlice("proxy")
retryCount := c.Int("retry")
method := c.String("method")
redirect := c.String("redirect")
ua := c.String("ua")
header := c.StringSlice("header")
noHeartbeat := c.Bool("no-heartbeat")
noGzip := c.Bool("no-gzip")
noBrowserHeaders := c.Bool("no-browser-headers")
jar := c.Bool("jar")
testExit := c.String("test-exit")
exclude := c.StringSlice("exclude-domain")
excludeFile := c.String("exclude-domain-file")
classicQPS := c.Int("classic-poll-qps")
classicInterval := c.Int("classic-poll-interval")
forward := c.String("forward")
dirtySize := c.Int("dirty-size")
configFile := c.String("config")
if ua != "" {
header = append(header, fmt.Sprintf("User-Agent: %s", ua))
}
var username, password string
if auth != "" {
parts := strings.Split(auth, ":")
if len(parts) != 2 {
return fmt.Errorf("invalid socks credentials, expected username:password")
}
username = parts[0]
password = parts[1]
}
if excludeFile != "" {
data, err := os.ReadFile(excludeFile)
if err != nil {
return err
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
exclude = append(exclude, line)
}
}
}
config := &suo5.Suo5Config{
Listen: listen,
Target: target,
Username: username,
Password: password,
Mode: mode,
MaxBodySize: maxBodySize,
Timeout: timeout,
Debug: debug,
UpstreamProxy: proxy,
Method: method,
RedirectURL: redirect,
RawHeader: header,
DisableHeartbeat: noHeartbeat,
DisableGzip: noGzip,
EnableCookieJar: jar,
ClassicPollQPS: classicQPS,
TestExit: testExit,
ExcludeDomain: exclude,
ForwardTarget: forward,
DirtySize: dirtySize,
RetryCount: retryCount,
ClassicPollInterval: classicInterval,
ImpersonateBrowser: !noBrowserHeaders,
}
if configFile != "" {
log.Infof("loading config from: %s", configFile)
data, err := os.ReadFile(configFile)
if err != nil {
return err
}
err = json.Unmarshal(data, config)
if err != nil {
return err
}
}
ctx, cancel := signalCtx()
defer cancel()
return ctrl.Run(ctx, config)
}
func signalCtx() (context.Context, func()) {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
<-ch
cancel()
}()
return ctx, cancel
}