-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
177 lines (145 loc) · 3.35 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
175
176
177
// KumaSocks project main.go
package main
import (
"flag"
"io"
"io/ioutil"
"log"
"net"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/BurntSushi/toml"
"golang.org/x/net/proxy"
)
var (
configPath string
conf Config
proxyDialer proxy.Dialer
running bool
)
type Config struct {
ListenAddr string `toml:"listen-addr"`
ProxyAddr string `toml:"proxy-addr"`
IOCopyHack bool `toml:"io-copy-hack"`
}
func readConf(path string) string {
buf, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("[ERROR] Read config error: %s.\n", err.Error())
return ""
}
return string(buf)
}
func customCopy(dst io.Writer, src io.Reader, buf []byte) (err error) {
for {
nr, er := src.Read(buf)
if nr > 0 {
_, ew := dst.Write(buf[0:nr])
if ew != nil {
err = ew
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return err
}
func handleConnection(conn *net.TCPConn) {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(20 * time.Second)
conn.SetNoDelay(true)
dstAddr, err := GetOriginalDST(conn)
if err != nil {
log.Printf("[ERROR] GetOriginalDST error: %s\n", err.Error())
return
}
log.Printf("[INFO] Connect to %s.\n", dstAddr.String())
proxy, err := proxyDialer.Dial("tcp", dstAddr.String())
if err != nil {
log.Printf("[ERROR] Dial proxy error: %s.\n", err.Error())
return
}
proxy.(*net.TCPConn).SetKeepAlive(true)
proxy.(*net.TCPConn).SetKeepAlivePeriod(20 * time.Second)
proxy.(*net.TCPConn).SetNoDelay(true)
copyEnd := false
go func() {
buf := make([]byte, 32*1024)
var err error
if conf.IOCopyHack {
err = customCopy(proxy, conn, buf)
} else {
_, err = io.CopyBuffer(proxy, conn, buf)
}
if err != nil && !copyEnd {
log.Printf("[ERROR] Copy error: %s.\n", err.Error())
}
copyEnd = true
conn.Close()
proxy.Close()
}()
buf := make([]byte, 32*1024)
if conf.IOCopyHack {
err = customCopy(conn, proxy, buf)
} else {
_, err = io.CopyBuffer(conn, proxy, buf)
}
if err != nil && !copyEnd {
log.Printf("[ERROR] Copy error: %s.\n", err.Error())
}
copyEnd = true
conn.Close()
proxy.Close()
}
func main() {
flag.StringVar(&configPath, "c", "/etc/kumasocks.toml", "Config file path")
flag.Parse()
configStr := readConf(configPath)
if _, err := toml.Decode(configStr, &conf); err != nil {
log.Fatalf("[ERROR] Decode config error: %s.\n", err.Error())
}
listener, err := net.Listen("tcp", conf.ListenAddr)
if err != nil {
log.Fatalf("[ERROR] Listen tcp error: %s.\n", err.Error())
}
defer listener.Close()
proxyURL, err := url.Parse(conf.ProxyAddr)
if err != nil {
log.Fatalf("[ERROR] Parse proxy address error: %s.\n", err.Error())
}
proxyDialer, err = proxy.FromURL(proxyURL, &net.Dialer{})
if err != nil {
log.Fatalf("[ERROR] Create proxy error: %s.\n", err.Error())
}
log.Println("[INFO] Starting Kumasocks...")
if conf.IOCopyHack {
log.Println("[INFO] Using io.Copy hack.")
}
running = true
go func() {
for running {
conn, err := listener.Accept()
if err != nil {
if running {
log.Printf("[ERROR] TCP accept error: %s.\n", err.Error())
}
continue
}
go handleConnection(conn.(*net.TCPConn))
}
}()
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-c
log.Println("[INFO] Exiting Kumasocks...")
running = false
listener.Close()
}