-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
206 lines (194 loc) · 5.62 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
package main
import (
"flag"
"log"
"os"
"sync"
"time"
"github.com/ccsexyz/shadowsocks-go/internal/utils"
ss "github.com/ccsexyz/shadowsocks-go/shadowsocks"
"github.com/fsnotify/fsnotify"
)
func main() {
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime | log.Lmicroseconds)
var c ss.Config
var target string
var configfile string
var pprofaddr string
flag.StringVar(&c.Type, "type", "", "server type(eg: server, local)")
flag.StringVar(&c.Remoteaddr, "s", "", "remote server address")
flag.StringVar(&c.Localaddr, "l", "", "local listen address")
flag.StringVar(&target, "t", "", "target address(for tcptun and udptun)")
flag.StringVar(&configfile, "c", "", "the configuration file path")
flag.StringVar(&c.Method, "m", "aes-256-cfb", "crypt method")
flag.StringVar(&c.Password, "p", "you need a password", "password")
flag.BoolVar(&c.Nonop, "nonop", false, "enable this to be compatiable with official ss servers(client only)")
flag.BoolVar(&c.UDPRelay, "udprelay", false, "relay udp packets")
flag.StringVar(&c.Nickname, "name", "", "nickname for logging")
flag.BoolVar(&c.Obfs, "obfs", false, "enable obfs mode")
flag.StringVar(&c.LogFile, "log", "", "set the path of logfile")
flag.BoolVar(&c.Verbose, "verbose", false, "show verbose log")
flag.BoolVar(&c.Debug, "debug", false, "show debug log")
flag.StringVar(&pprofaddr, "pprof", "", "the pprof listen address")
flag.IntVar(&c.Timeout, "timeout", 0, "set the timeout of tcp connection")
flag.BoolVar(&c.Safe, "safe", false, "runs under safe mode, server won't validate iv if safe is enabled")
flag.BoolVar(&c.MITM, "mitm", false, "enable MITM-based http/https proxy")
flag.IntVar(&c.DataShard, "ds", 0, "set datashard - fec")
flag.IntVar(&c.ParityShard, "ps", 0, "set parityshard - fec")
flag.StringVar(&c.ObfsMethod, "om", "", "set the method for obfs(http/websocket/tls)")
flag.BoolVar(&c.SSProxy, "ssproxy", false, "enable ss proxy for local server")
flag.BoolVar(&c.AllowHTTP, "allow_http", false, "allow http wstunel connection")
flag.Parse()
if len(os.Args) == 1 {
flag.Usage()
return
}
if len(os.Args) == 2 {
configfile = os.Args[1]
}
if len(pprofaddr) != 0 {
if utils.PprofEnabled() {
log.Println("run pprof http server at", pprofaddr)
go func() {
utils.RunProfileHTTPServer(pprofaddr)
}()
} else {
log.Println("set pprof but pprof isn't compiled")
}
}
if len(configfile) == 0 {
if len(target) != 0 {
c.Backend = &ss.Config{Method: c.Method, Password: c.Password, Remoteaddr: c.Remoteaddr}
c.Remoteaddr = target
}
ss.CheckConfig(&c)
runServer(&c)
return
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
configs, err := ss.ReadConfig(configfile)
if err != nil {
log.Fatal(err)
}
for {
die := make(chan bool)
var wg sync.WaitGroup
for _, c := range configs {
wg.Add(1)
go func(c *ss.Config) {
defer wg.Done()
runServer(c)
}(c)
}
oldConfigs := configs
go func() {
<-die
for _, c := range oldConfigs {
c.Close()
}
}()
go func() {
err = watcher.Add(configfile)
if err != nil {
log.Println(err)
return
}
defer watcher.Remove(configfile)
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Rename == fsnotify.Rename {
newConfigs, err := ss.ReadConfig(configfile)
if err != nil {
continue
}
configs = newConfigs
close(die)
return
} else if event.Op&fsnotify.Remove == fsnotify.Remove {
return
}
case <-watcher.Errors:
// close(die)
// return
case <-die:
return
}
}
}()
wg.Wait()
select {
case <-die:
default:
return
}
time.Sleep(time.Second)
}
}
func runServer(c *ss.Config) {
switch c.Type {
default:
log.Println("unsupported server type:", c.Type)
case "local":
c.Log("run client at", c.Localaddr, "with method", c.Method)
if c.UDPRelay {
c.Log("run udp local server at", c.Localaddr, "with method", c.Method)
go RunUDPLocalServer(c)
}
RunTCPLocalServer(c)
case "redir":
c.Log("run redir at", c.Localaddr)
if c.UDPRelay {
c.Log("run udp redir server at", c.Localaddr)
go RunUDPRedirServer(c)
}
RunTCPRedirServer(c)
case "server":
c.Log("run server at", c.Localaddr, "with method", c.Method)
if c.UDPRelay {
c.Log("run udp remote server at", c.Localaddr, "with method", c.Method)
go RunUDPRemoteServer(c)
}
RunTCPRemoteServer(c)
case "multiserver":
c.Log("run multi server at", c.Localaddr)
if c.UDPRelay {
c.Log("run multi udp remote server at", c.Localaddr)
go RunMultiUDPRemoteServer(c)
}
RunMultiTCPRemoteServer(c)
case "ssproxy":
if c.UDPRelay {
c.Log("run udp remote proxy server at", c.Localaddr)
go RunUDPRemoteServer(c)
}
c.Log("run ss proxy at", c.Localaddr, "with method", c.Method)
RunSSProxyServer(c)
case "socksproxy":
if c.UDPRelay {
c.Log("run udp local proxy server at", c.Localaddr, "with method", c.Method)
go RunUDPLocalServer(c)
}
c.Log("run socks proxy at", c.Localaddr, "with method", c.Method)
RunSocksProxyServer(c)
case "tcptun":
if len(c.Localaddr) == 0 || c.Backend == nil || len(c.Backend.Remoteaddr) == 0 {
break
}
c.Log("run tcp tunnel at", c.Localaddr, "to", c.Remoteaddr)
RunTCPTunServer(c)
case "udptun":
if len(c.Localaddr) == 0 || c.Backend == nil || (len(c.Backend.Remoteaddr) == 0 && len(c.Backend.FakeTCPAddr) == 0) {
break
}
c.Log("run udp tunnel at", c.Localaddr, "to", c.Remoteaddr)
RunUDPTunServer(c)
case "wstunnel":
c.Log("run wstunnel server at", c.Localaddr, "with method", c.Method)
RunWstunnelRemoteServer(c)
}
}