-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
243 lines (204 loc) · 4.66 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"flag"
"fmt"
"net"
"github.com/bepass-org/proxy/pkg/mixed"
"github.com/bepass-org/proxy/pkg/statute"
"github.com/onionj/websocket-mux/muxr"
)
var version = "" // set it just in Makefile
var (
authToken string
bind string
server bool
publicKey string
privateKey string
path string
client bool
serverAddr string
muxrClient *muxr.Client
bufferSize int
)
func main() {
flag.StringVar(&bind, "bind", ":1080", "The address to bind the server to")
flag.StringVar(&authToken, "token", "1234", "Auth token for improve security")
flag.IntVar(&bufferSize, "buffer-size", 4096, "default is 4096 (4KB)")
// server
flag.BoolVar(&server, "server", false, "Start a server")
flag.StringVar(&publicKey, "public-key", "", "TLS certificate public key.")
flag.StringVar(&privateKey, "private-key", "", "TLS certificate private key.")
flag.StringVar(&path, "path", "/", "Listener path")
// Client
flag.BoolVar(&client, "client", false, "Start a client")
flag.StringVar(&serverAddr, "server-address", "", "If you run code as a client, you have to set the server address")
flag.Parse()
fmt.Println("version:", version)
if client {
muxrClient = muxr.NewClient(serverAddr)
closerFunc, err := muxrClient.StartForever()
if err != nil {
fmt.Println("muxrClient err:", err)
return
}
defer closerFunc()
proxy := mixed.NewProxy(
mixed.WithBindAddress(bind),
mixed.WithUserHandler(clientHandler),
)
if err := proxy.ListenAndServe(); err != nil {
panic(err)
}
} else if server {
server := muxr.NewServer(bind)
server.Handle(path, serverHandler)
if publicKey != "" && privateKey != "" {
if err := server.ListenAndServeTLS(publicKey, privateKey); err != nil {
panic(err)
}
} else {
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}
} else {
flag.Usage()
}
}
func clientHandler(req *statute.ProxyRequest) error {
fmt.Println("handling request to", req.Network, req.Destination)
stream, err := muxrClient.Dial()
if err != nil {
fmt.Println("clientHandler, muxrClient.Dial() err:", err)
return err
}
defer stream.Close()
// Send auth token
err = stream.Write([]byte(authToken))
if err != nil {
fmt.Println(err)
return err
}
// Send destination network
err = stream.Write([]byte(req.Network))
if err != nil {
fmt.Println(err)
return err
}
// Send destination address
err = stream.Write([]byte(req.Destination))
if err != nil {
fmt.Println(err)
return err
}
wait := make(chan struct{}, 2)
// Send incoming data from the local system to the proxy server.
buf := make([]byte, bufferSize)
go func() {
for {
n, err := req.Conn.Read(buf)
if err != nil {
break
}
err = stream.Write(buf[:n])
if err != nil {
break
}
}
wait <- struct{}{}
}()
// Send incoming data from the proxy server to the local system.
go func() {
for {
data, err := stream.Read()
if err != nil {
break
}
_, err = req.Conn.Write(data)
if err != nil {
break
}
}
wait <- struct{}{}
}()
<-wait
return nil
}
func serverHandler(stream *muxr.Stream) {
token, err := stream.Read()
if err != nil {
fmt.Println(err.Error())
return
}
if authToken != string(token) {
fmt.Println("Invalid token:", string(token))
_ = stream.ConnAdaptor.Conn.Close()
return
}
networkBuffer, err := stream.Read()
if err != nil {
fmt.Println(err.Error())
return
}
network := string(networkBuffer)
destinationBuffer, err := stream.Read()
if err != nil {
fmt.Println(err.Error())
return
}
destination := string(destinationBuffer)
// check for private networks
host, _, err := net.SplitHostPort(destination)
if err == nil {
if host == "localhost" {
fmt.Println("drop request to ", destination)
return
}
ip := net.ParseIP(host)
if ip != nil && (ip.IsPrivate() || ip.IsLoopback()) {
fmt.Println("drop request to ", destination)
return
}
}
fmt.Println("handling request to", network, destination)
// Connecting to destinations
conn, err := net.Dial(network, destination)
if err != nil {
fmt.Println(err.Error())
return
}
defer func(conn net.Conn) {
_ = conn.Close()
}(conn)
wait := make(chan struct{}, 2)
// Send incoming data from the destination to the client.
buf := make([]byte, bufferSize)
go func() {
for {
n, err := conn.Read(buf)
if err != nil {
break
}
err = stream.Write(buf[:n])
if err != nil {
break
}
}
wait <- struct{}{}
}()
// Send incoming data from the client to the destination.
go func() {
for {
data, err := stream.Read()
if err != nil {
break
}
_, err = conn.Write(data)
if err != nil {
break
}
}
wait <- struct{}{}
}()
<-wait
}