forked from shadowsocks/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (95 loc) · 2.74 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
package main
import (
"encoding/hex"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/riobard/go-shadowsocks2/cipher"
)
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
Socks string
RedirTCP string
RedirTCP6 string
TCPTun string
UDPTun string
}
flag.BoolVar(&config.Verbose, "verbose", false, "verbose mode")
flag.StringVar(&flags.Cipher, "cipher", "", "cipher")
flag.StringVar(&flags.Key, "key", "", "secret key in hexadecimal")
flag.StringVar(&flags.Server, "s", "", "server listen address")
flag.StringVar(&flags.Client, "c", "", "client connect address")
flag.StringVar(&flags.Socks, "socks", ":1080", "(client-only) SOCKS listen address")
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.Cipher == "" {
ls := cipher.ListCiphers()
fmt.Fprintf(os.Stderr, "# available ciphers\n")
for _, each := range ls {
fmt.Fprintf(os.Stderr, "%s\n", each)
}
return
}
key, err := hex.DecodeString(flags.Key)
if err != nil {
log.Fatalf("key: %v", err)
}
streamCipher, packetCipher, err := cipher.MakeCipher(flags.Cipher, key)
if err != nil {
log.Fatalf("cipher: %v", err)
}
if flags.Client != "" { // client mode
if flags.UDPTun != "" {
for _, tun := range strings.Split(flags.UDPTun, ",") {
p := strings.Split(tun, "=")
go udpLocal(p[0], flags.Client, p[1], packetCipher)
}
}
if flags.TCPTun != "" {
for _, tun := range strings.Split(flags.TCPTun, ",") {
p := strings.Split(tun, "=")
go tcpTun(p[0], flags.Client, p[1], streamCipher)
}
}
if flags.Socks != "" {
go socksLocal(flags.Socks, flags.Client, streamCipher)
}
if flags.RedirTCP != "" {
go redirLocal(flags.RedirTCP, flags.Client, streamCipher)
}
if flags.RedirTCP6 != "" {
go redir6Local(flags.RedirTCP6, flags.Client, streamCipher)
}
} else if flags.Server != "" { // server mode
go udpRemote(flags.Server, packetCipher)
go tcpRemote(flags.Server, streamCipher)
} else {
flag.Usage()
return
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}