-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
95 lines (84 loc) · 2.26 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
package main
import (
"crypto/aes"
"crypto/cipher"
"flag"
"github.com/kless/goconfig/config"
. "hyq/newsocks/handler"
"log"
"net"
)
var mode, server, client_listen, server_listen, ck, sk, http_proxy string
var clientCipher, serverCipher cipher.Block
var handler Handler
func loadConfig() {
//read config
c, _ := config.ReadDefault("config.ini")
mode, _ = c.String("main", "mode")
server, _ = c.String("client", "server")
client_listen, _ = c.String("client", "listen")
server_listen, _ = c.String("server", "listen")
ck, _ = c.String("encrypto", "client-key")
sk, _ = c.String("encrypto", "server-key")
http_proxy, _ = c.String("client", "http-proxy")
}
func loadFlags() {
flag.StringVar(&mode, "mode", mode, "server or client")
flag.StringVar(&server, "server", server, "the remote server")
flag.StringVar(&client_listen, "client-listen", client_listen, "the ip and port of client to bind")
flag.StringVar(&server_listen, "server-listen", server_listen, "the ip and port of server to bind")
flag.StringVar(&ck, "client-key", ck, "the client key")
flag.StringVar(&sk, "server-key", sk, "the server key")
flag.StringVar(&http_proxy, "http-proxy", http_proxy, "the ip and port of http proxy server ")
flag.Parse()
log.Printf("mode:%s", mode)
if mode == "client" {
log.Printf("server:%s", server)
log.Printf("client-listen:%s", client_listen)
if http_proxy != "" {
log.Printf("http-proxy:%s", http_proxy)
}
} else {
log.Printf("server-listen:%s", server_listen)
}
log.Printf("client-key:%s", ck)
log.Printf("server-key:%s", sk)
}
func main() {
loadConfig()
loadFlags()
clientCipher, _ = aes.NewCipher([]byte(ck))
serverCipher, _ = aes.NewCipher([]byte(sk))
var listen string
switch mode {
case "server":
handler = &Server{
ClientCipher: clientCipher,
ServerCipher: serverCipher,
}
listen = server_listen
case "client":
handler = &Client{
Server: server,
ClientCipher: clientCipher,
ServerCipher: serverCipher,
}
listen = client_listen
}
lsn, e := net.Listen("tcp", listen)
if e != nil {
log.Fatal(e)
}
defer lsn.Close()
if mode == "client" && http_proxy != "" {
go startHTTPProxyServer(http_proxy)
}
for {
conn, e := lsn.Accept()
if e != nil {
log.Print(e)
continue
}
go handler.Handle(conn)
}
}