forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
161 lines (135 loc) · 3.05 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
package main
import (
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"github.com/shadowsocks/go-shadowsocks2/core"
"github.com/shadowsocks/go-shadowsocks2/socks"
"io"
"log"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
)
func main() {
var flags *Configure
var filePath = flag.String("path", "/etc/shadowsocks/conf.client.json", "shadowsocks configure file path")
flag.Parse()
if flags = loadConfigure(*filePath); flags == nil {
return
}
if flags.Keygen > 0 {
key := make([]byte, flags.Keygen)
io.ReadFull(rand.Reader, key)
fmt.Println(base64.URLEncoding.EncodeToString(key))
return
}
if flags.Client == "" && flags.Server == "" {
flag.Usage()
return
}
var key []byte
if flags.Key != "" {
k, err := base64.URLEncoding.DecodeString(flags.Key)
if err != nil {
log.Fatal(err)
}
key = k
}
if flags.Client != "" { // client mode
addr := flags.Client
cipher := flags.Cipher
password := flags.Password
var err error
if strings.HasPrefix(addr, "ss://") {
addr, cipher, password, err = parseURL(addr)
if err != nil {
log.Fatal(err)
}
}
udpAddr := addr
ciph, err := core.PickCipher(cipher, key, password)
if err != nil {
log.Fatal(err)
}
if flags.Plugin != "" {
addr, err = startPlugin(flags.Plugin, flags.PluginOpts, addr, false)
if err != nil {
log.Fatal(err)
}
}
if flags.UDPTun != "" {
for _, tun := range strings.Split(flags.UDPTun, ",") {
p := strings.Split(tun, "=")
go udpLocal(p[0], udpAddr, p[1], ciph.PacketConn)
}
}
if flags.TCPTun != "" {
for _, tun := range strings.Split(flags.TCPTun, ",") {
p := strings.Split(tun, "=")
go tcpTun(p[0], addr, p[1], ciph.StreamConn)
}
}
if flags.Socks != "" {
socks.UDPEnabled = flags.UDPSocks
go socksLocal(flags.Socks, addr, ciph.StreamConn)
if flags.UDPSocks {
go udpSocksLocal(flags.Socks, udpAddr, ciph.PacketConn)
}
}
if flags.RedirTCP != "" {
go redirLocal(flags.RedirTCP, addr, ciph.StreamConn)
}
if flags.RedirTCP6 != "" {
go redir6Local(flags.RedirTCP6, addr, ciph.StreamConn)
}
}
if flags.Server != "" { // server mode
addr := flags.Server
cipher := flags.Cipher
password := flags.Password
var err error
if strings.HasPrefix(addr, "ss://") {
addr, cipher, password, err = parseURL(addr)
if err != nil {
log.Fatal(err)
}
}
udpAddr := addr
if flags.Plugin != "" {
addr, err = startPlugin(flags.Plugin, flags.PluginOpts, addr, true)
if err != nil {
log.Fatal(err)
}
}
ciph, err := core.PickCipher(cipher, key, password)
if err != nil {
log.Fatal(err)
}
if flags.UDP {
go udpRemote(udpAddr, ciph.PacketConn)
}
if flags.TCP {
go tcpRemote(addr, ciph.StreamConn)
}
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
killPlugin()
}
func parseURL(s string) (addr, cipher, password string, err error) {
u, err := url.Parse(s)
if err != nil {
return
}
addr = u.Host
if u.User != nil {
cipher = u.User.Username()
password, _ = u.User.Password()
}
return
}