forked from zema1/suo5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (150 loc) · 3.63 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"
"fmt"
log "github.com/kataras/golog"
"github.com/urfave/cli/v2"
"github.com/zema1/suo5/ctrl"
"os"
"os/signal"
"strings"
)
func main() {
log.Default.SetTimeFormat("01-02 15:04")
app := cli.NewApp()
app.Name = "suo5"
app.Usage = "A super http proxy tunnel"
app.Version = "v0.3.0"
defaultConfig := ctrl.DefaultSuo5Config()
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Usage: "set the remote server url, ex: http://localhost:8080/tomcat_debug_war_exploded/",
Value: defaultConfig.Target,
Required: true,
},
&cli.StringFlag{
Name: "listen",
Aliases: []string{"l"},
Usage: "set the listen address of socks5 server",
Value: defaultConfig.Listen,
},
&cli.StringFlag{
Name: "method",
Aliases: []string{"m"},
Usage: "http request method",
Value: defaultConfig.Method,
},
&cli.BoolFlag{
Name: "no-auth",
Usage: "disable socks5 authentication",
Value: defaultConfig.NoAuth,
},
&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",
Value: string(defaultConfig.Mode),
},
&cli.StringFlag{
Name: "ua",
Usage: "the user-agent used to send request",
Value: defaultConfig.UserAgent,
},
&cli.IntFlag{
Name: "timeout",
Usage: "http request timeout in seconds",
Value: defaultConfig.Timeout,
},
&cli.IntFlag{
Name: "buf-size",
Usage: "set the request max body size",
Value: defaultConfig.BufferSize,
},
&cli.StringFlag{
Name: "proxy",
Usage: "use upstream socks5 proxy",
Value: defaultConfig.UpstreamProxy,
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "debug the traffic, print more details",
Value: defaultConfig.Debug,
},
}
app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
log.Default.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")
noAuth := c.Bool("no-auth")
auth := c.String("auth")
mode := ctrl.ConnectionType(c.String("mode"))
ua := c.String("ua")
bufSize := c.Int("buf-size")
timeout := c.Int("timeout")
debug := c.Bool("debug")
proxy := c.String("proxy")
method := c.String("method")
var username, password string
if auth == "" {
username = "suo5"
password = ctrl.RandString(8)
} else {
parts := strings.Split(auth, ":")
if len(parts) != 2 {
return fmt.Errorf("invalid socks credentials, expected username:password")
}
username = parts[0]
password = parts[1]
}
if !(mode == ctrl.AutoDuplex || mode == ctrl.FullDuplex || mode == ctrl.HalfDuplex) {
return fmt.Errorf("invalid mode, expected auto or full or half")
}
if bufSize < 512 || bufSize > 1024000 {
return fmt.Errorf("inproper buffer size, 512~1024000")
}
config := &ctrl.Suo5Config{
Listen: listen,
Target: target,
NoAuth: noAuth,
Username: username,
Password: password,
Mode: mode,
UserAgent: ua,
BufferSize: bufSize,
Timeout: timeout,
Debug: debug,
UpstreamProxy: proxy,
Method: method,
}
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
}