-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
263 lines (224 loc) · 6.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// main.go
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"strconv"
"strings"
"sync"
)
type load_balancer struct {
address string
iface string
contention_ratio int
current_connections int
}
// The load balancer used in the previous connection
var lb_index int = 0
// List of all load balancers
var lb_list []load_balancer
// Mutex to serialize access to function get_load_balancer
var mutex *sync.Mutex
/*
Get a load balancer according to contention ratio
*/
func get_load_balancer() *load_balancer {
mutex.Lock()
lb := &lb_list[lb_index]
lb.current_connections += 1
if lb.current_connections == lb.contention_ratio {
lb.current_connections = 0
lb_index += 1
if lb_index == len(lb_list) {
lb_index = 0
}
}
mutex.Unlock()
return lb
}
/*
Joins the local and remote connections together
*/
func pipe_connections(local_conn, remote_conn net.Conn) {
go func() {
defer remote_conn.Close()
defer local_conn.Close()
_, err := io.Copy(remote_conn, local_conn)
if err != nil {
return
}
}()
go func() {
defer remote_conn.Close()
defer local_conn.Close()
_, err := io.Copy(local_conn, remote_conn)
if err != nil {
return
}
}()
}
/*
Handle connections in tunnel mode
*/
func handle_tunnel_connection(conn net.Conn) {
load_balancer := get_load_balancer()
remote_addr, _ := net.ResolveTCPAddr("tcp4", load_balancer.address)
remote_conn, err := net.DialTCP("tcp4", nil, remote_addr)
if err != nil {
log.Println("[WARN]", load_balancer.address, fmt.Sprintf("{%s}", err))
conn.Close()
return
}
log.Println("[DEBUG] Tunnelled to", load_balancer.address)
pipe_connections(conn, remote_conn)
}
/*
Calls the apprpriate handle_connections based on tunnel mode
*/
func handle_connection(conn net.Conn, tunnel bool) {
if tunnel {
handle_tunnel_connection(conn)
} else if address, err := handle_socks_connection(conn); err == nil {
server_response(conn, address)
}
}
/*
Detect the addresses which can be used for dispatching in non-tunnelling mode.
Alternate to ipconfig/ifconfig
*/
func detect_interfaces() {
fmt.Println("--- Listing the available adresses for dispatching")
ifaces, _ := net.Interfaces()
for _, iface := range ifaces {
if (iface.Flags&net.FlagUp == net.FlagUp) && (iface.Flags&net.FlagLoopback != net.FlagLoopback) {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
fmt.Printf("[+] %s, IPv4:%s\n", iface.Name, ipnet.IP.String())
}
}
}
}
}
}
/*
Gets the interface associated with the IP
*/
func get_iface_from_ip(ip string) string {
ifaces, _ := net.Interfaces()
for _, iface := range ifaces {
if (iface.Flags&net.FlagUp == net.FlagUp) && (iface.Flags&net.FlagLoopback != net.FlagLoopback) {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
if ipnet.IP.String() == ip {
return iface.Name + "\x00"
}
}
}
}
}
}
return ""
}
/*
Parses the command line arguements to obtain the list of load balancers
*/
func parse_load_balancers(args []string, tunnel bool) {
if len(args) == 0 {
log.Fatal("[FATAL] Please specify one or more load balancers")
}
lb_list = make([]load_balancer, flag.NArg())
for idx, a := range args {
splitted := strings.Split(a, "@")
iface := ""
// IP address of a Fully Qualified Domain Name of the load balancer
var lb_ip_or_fqdn string
var lb_port int
var err error
if tunnel {
ip_or_fqdn_port := strings.Split(splitted[0], ":")
if len(ip_or_fqdn_port) != 2 {
log.Fatal("[FATAL] Invalid address specification ", splitted[0])
return
}
lb_ip_or_fqdn = ip_or_fqdn_port[0]
lb_port, err = strconv.Atoi(ip_or_fqdn_port[1])
if err != nil || lb_port <= 0 || lb_port > 65535 {
log.Fatal("[FATAL] Invalid port ", splitted[0])
return
}
} else {
lb_ip_or_fqdn = splitted[0]
lb_port = 0
}
// FQDN not supported for tunnel modes
if !tunnel && net.ParseIP(lb_ip_or_fqdn).To4() == nil {
log.Fatal("[FATAL] Invalid address ", lb_ip_or_fqdn)
}
var cont_ratio int = 1
if len(splitted) > 1 {
cont_ratio, err = strconv.Atoi(splitted[1])
if err != nil || cont_ratio <= 0 {
log.Fatal("[FATAL] Invalid contention ratio for ", lb_ip_or_fqdn)
}
}
// Obtaining the interface name of the load balancer IP's doesn't make sense in tunnel mode
if !tunnel {
iface = get_iface_from_ip(lb_ip_or_fqdn)
if iface == "" {
log.Fatal("[FATAL] IP address not associated with an interface ", lb_ip_or_fqdn)
}
}
log.Printf("[INFO] Load balancer %d: %s, contention ratio: %d\n", idx+1, lb_ip_or_fqdn, cont_ratio)
lb_list[idx] = load_balancer{address: fmt.Sprintf("%s:%d", lb_ip_or_fqdn, lb_port), iface: iface, contention_ratio: cont_ratio, current_connections: 0}
}
}
/*
Main function
*/
func main() {
var lhost = flag.String("lhost", "127.0.0.1", "The host to listen for SOCKS connection")
var lport = flag.Int("lport", 8080, "The local port to listen for SOCKS connection")
var detect = flag.Bool("list", false, "Shows the available addresses for dispatching (non-tunnelling mode only)")
var tunnel = flag.Bool("tunnel", false, "Use tunnelling mode (acts as a transparent load balancing proxy)")
flag.Parse()
if *detect {
detect_interfaces()
return
}
// Disable timestamp in log messages
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
// Check for valid IP
if net.ParseIP(*lhost).To4() == nil {
log.Fatal("[FATAL] Invalid host ", *lhost)
}
// Check for valid port
if *lport < 1 || *lport > 65535 {
log.Fatal("[FATAL] Invalid port ", *lport)
}
//Parse remaining string to get addresses of load balancers
parse_load_balancers(flag.Args(), *tunnel)
local_bind_address := fmt.Sprintf("%s:%d", *lhost, *lport)
// Start local server
l, err := net.Listen("tcp4", local_bind_address)
if err != nil {
log.Fatalln("[FATAL] Could not start local server on ", local_bind_address)
}
log.Println("[INFO] Local server started on ", local_bind_address)
defer l.Close()
mutex = &sync.Mutex{}
for {
conn, err := l.Accept()
if err != nil {
log.Println("[WARN] Could not accept connection")
} else {
go handle_connection(conn, *tunnel)
}
}
}