forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (149 loc) · 4.12 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
package main
import (
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/shadowsocks/go-shadowsocks2/core"
"github.com/shadowsocks/go-shadowsocks2/socks"
)
var config struct {
Verbose bool
UDPTimeout time.Duration
}
func logf(f string, v ...interface{}) {
if config.Verbose {
log.Printf(f, v...)
}
}
func main() {
var flags struct {
Client string
Server string
Cipher string
Key string
Password string
Keygen int
Socks string
RedirTCP string
RedirTCP6 string
TCPTun string
UDPTun string
UDPSocks bool
}
flag.BoolVar(&config.Verbose, "verbose", false, "verbose mode")
flag.StringVar(&flags.Cipher, "cipher", "AEAD_CHACHA20_POLY1305", "available ciphers: "+strings.Join(core.ListCipher(), " "))
flag.StringVar(&flags.Key, "key", "", "base64url-encoded key (derive from password if empty)")
flag.IntVar(&flags.Keygen, "keygen", 0, "generate a base64url-encoded random key of given length in byte")
flag.StringVar(&flags.Password, "password", "", "password")
flag.StringVar(&flags.Server, "s", "", "server listen address or url")
flag.StringVar(&flags.Client, "c", "", "client connect address or url")
flag.StringVar(&flags.Socks, "socks", "", "(client-only) SOCKS listen address")
flag.BoolVar(&flags.UDPSocks, "u", false, "(client-only) Enable UDP support for SOCKS")
flag.StringVar(&flags.RedirTCP, "redir", "", "(client-only) redirect TCP from this address")
flag.StringVar(&flags.RedirTCP6, "redir6", "", "(client-only) redirect TCP IPv6 from this address")
flag.StringVar(&flags.TCPTun, "tcptun", "", "(client-only) TCP tunnel (laddr1=raddr1,laddr2=raddr2,...)")
flag.StringVar(&flags.UDPTun, "udptun", "", "(client-only) UDP tunnel (laddr1=raddr1,laddr2=raddr2,...)")
flag.DurationVar(&config.UDPTimeout, "udptimeout", 5*time.Minute, "UDP tunnel timeout")
flag.Parse()
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)
}
}
ciph, err := core.PickCipher(cipher, key, password)
if err != nil {
log.Fatal(err)
}
if flags.UDPTun != "" {
for _, tun := range strings.Split(flags.UDPTun, ",") {
p := strings.Split(tun, "=")
go udpLocal(p[0], addr, 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, addr, 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)
}
}
ciph, err := core.PickCipher(cipher, key, password)
if err != nil {
log.Fatal(err)
}
go udpRemote(addr, ciph.PacketConn)
go tcpRemote(addr, ciph.StreamConn)
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}
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
}