-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
main.go
120 lines (101 loc) · 2.15 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
package main
import (
"flag"
"log"
"strings"
"github.com/riobard/go-shadowsocks2/core"
"github.com/riobard/go-shadowsocks2/listen"
)
func main() {
listCiphers := flag.Bool("cipher", false, "List supported ciphers")
flag.Parse()
if *listCiphers {
println(strings.Join(core.ListCipher(), " "))
return
}
if len(config.Client) == 0 && len(config.Server) == 0 {
flag.Usage()
return
}
if len(config.Client) > 0 {
client()
}
if len(config.Server) > 0 {
server()
}
select {}
}
func client() {
if len(config.UDPTun) > 0 { // use first server for UDP
addr, cipher, password, err := parseURL(config.Client[0])
if err != nil {
log.Fatal(err)
}
ciph, err := core.PickCipher(cipher, nil, password)
if err != nil {
log.Fatal(err)
}
for _, p := range config.UDPTun {
go udpLocal(p[0], addr, p[1], ciph.PacketConn)
}
}
d, err := fastdialer(config.Client...)
if err != nil {
log.Fatalf("failed to create dialer: %v", err)
}
if len(config.TCPTun) > 0 {
for _, p := range config.TCPTun {
l, err := listen.ListenTo("tcp", p[0], p[1])
if err != nil {
log.Fatal(err)
}
logf("tcptun %v --> %v", p[0], p[1])
go tcpLocal(l, d)
}
}
if config.Socks != "" {
l, err := listen.Listen("socks", "tcp", config.Socks)
if err != nil {
log.Fatal(err)
}
logf("socks %v", config.Socks)
go tcpLocal(l, d)
}
if config.RedirTCP != "" {
l, err := listen.Listen("redir", "tcp", config.RedirTCP)
if err != nil {
log.Fatal(err)
}
logf("redir tcp %v", config.RedirTCP)
go tcpLocal(l, d)
}
if config.TproxyTCP != "" {
l, err := listen.Listen("tproxy", "tcp", config.TproxyTCP)
if err != nil {
log.Fatal(err)
}
logf("tproxy tcp %v", config.TproxyTCP)
go tcpLocal(l, d)
}
}
func server() {
for _, each := range config.Server {
addr, cipher, password, err := parseURL(each)
if err != nil {
log.Fatal(err)
}
ciph, err := core.PickCipher(cipher, nil, password)
if err != nil {
log.Fatal(err)
}
if config.UDP {
go udpRemote(addr, ciph.PacketConn)
}
go tcpRemote(addr, ciph.StreamConn)
}
}
func logf(f string, v ...interface{}) {
if config.Verbose {
log.Printf(f, v...)
}
}