Skip to content

Commit c38366e

Browse files
Support specifying domain names for load balancers in tunnel mode, closes #34
1 parent 0f70e53 commit c38366e

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ D:\>go-dispatch-proxy.exe -lhost 192.168.1.2 -lport 5566 10.81.177.215 192.168.1
5757
[INFO] SOCKS server started at 192.168.1.2:5566
5858
```
5959

60-
Out of 5 consecutive connections, the first 3 are routed to `10.81.201.18` and the remaining 2 to `192.168.1.2`. The SOCKS server is started by default on `127.0.0.1:8080`. It can be changed using the `-lhost` and `lport` directive.
60+
Out of 5 consecutive connections, the first 3 are routed to `10.81.201.18` and the remaining 2 to `192.168.1.2`. The SOCKS server is started by default on `127.0.0.1:8080`. It can be changed using the `-lhost` and `-lport` directive.
6161

6262
Now change the proxy settings of your browser, download manager etc to point to the above address (eg `127.0.0.1:8080`). Be sure to add this as a SOCKS v5 proxy and NOT as a HTTP/S proxy.
6363

@@ -80,7 +80,11 @@ Next, launch go-dispatch-proxy using the `-tunnel` argument.
8080
D:\> go-dispatch-proxy.exe -tunnel 127.0.0.1:7777 127.0.0.1:7778
8181
```
8282

83-
Both the IP and port must be mentioned while specifying the load balancer addresses.
83+
Both the IP and port must be mentioned while specifying the load balancer addresses. Also instead of specifying the IP address a domain can be specified, hence the following also works.
84+
85+
```
86+
D:\> go-dispatch-proxy.exe -tunnel proxy1.com:7777 proxy2.com:7778
87+
```
8488

8589
Optionally, the listening host, port and contention ratio can also be specified like in example 2.
8690

main.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var lb_list []load_balancer
2929
var mutex *sync.Mutex
3030

3131
/*
32-
Get a load balancer according to contention ratio
32+
Get a load balancer according to contention ratio
3333
*/
3434
func get_load_balancer() *load_balancer {
3535
mutex.Lock()
@@ -49,7 +49,7 @@ func get_load_balancer() *load_balancer {
4949
}
5050

5151
/*
52-
Joins the local and remote connections together
52+
Joins the local and remote connections together
5353
*/
5454
func pipe_connections(local_conn, remote_conn net.Conn) {
5555
go func() {
@@ -72,7 +72,7 @@ func pipe_connections(local_conn, remote_conn net.Conn) {
7272
}
7373

7474
/*
75-
Handle connections in tunnel mode
75+
Handle connections in tunnel mode
7676
*/
7777
func handle_tunnel_connection(conn net.Conn) {
7878
load_balancer := get_load_balancer()
@@ -91,7 +91,7 @@ func handle_tunnel_connection(conn net.Conn) {
9191
}
9292

9393
/*
94-
Calls the apprpriate handle_connections based on tunnel mode
94+
Calls the apprpriate handle_connections based on tunnel mode
9595
*/
9696
func handle_connection(conn net.Conn, tunnel bool) {
9797
if tunnel {
@@ -102,8 +102,8 @@ func handle_connection(conn net.Conn, tunnel bool) {
102102
}
103103

104104
/*
105-
Detect the addresses which can be used for dispatching in non-tunnelling mode.
106-
Alternate to ipconfig/ifconfig
105+
Detect the addresses which can be used for dispatching in non-tunnelling mode.
106+
Alternate to ipconfig/ifconfig
107107
*/
108108
func detect_interfaces() {
109109
fmt.Println("--- Listing the available adresses for dispatching")
@@ -125,7 +125,7 @@ func detect_interfaces() {
125125
}
126126

127127
/*
128-
Gets the interface associated with the IP
128+
Gets the interface associated with the IP
129129
*/
130130
func get_iface_from_ip(ip string) string {
131131
ifaces, _ := net.Interfaces()
@@ -148,7 +148,7 @@ func get_iface_from_ip(ip string) string {
148148
}
149149

150150
/*
151-
Parses the command line arguements to obtain the list of load balancers
151+
Parses the command line arguements to obtain the list of load balancers
152152
*/
153153
func parse_load_balancers(args []string, tunnel bool) {
154154
if len(args) == 0 {
@@ -160,55 +160,58 @@ func parse_load_balancers(args []string, tunnel bool) {
160160
for idx, a := range args {
161161
splitted := strings.Split(a, "@")
162162
iface := ""
163-
var lb_ip string
163+
// IP address of a Fully Qualified Domain Name of the load balancer
164+
var lb_ip_or_fqdn string
164165
var lb_port int
165166
var err error
166167

167168
if tunnel {
168-
ip_port := strings.Split(splitted[0], ":")
169-
if len(ip_port) != 2 {
169+
ip_or_fqdn_port := strings.Split(splitted[0], ":")
170+
if len(ip_or_fqdn_port) != 2 {
170171
log.Fatal("[FATAL] Invalid address specification ", splitted[0])
171172
return
172173
}
173174

174-
lb_ip = ip_port[0]
175-
lb_port, err = strconv.Atoi(ip_port[1])
175+
lb_ip_or_fqdn = ip_or_fqdn_port[0]
176+
lb_port, err = strconv.Atoi(ip_or_fqdn_port[1])
176177
if err != nil || lb_port <= 0 || lb_port > 65535 {
177178
log.Fatal("[FATAL] Invalid port ", splitted[0])
178179
return
179180
}
180181

181182
} else {
182-
lb_ip = splitted[0]
183+
lb_ip_or_fqdn = splitted[0]
183184
lb_port = 0
184185
}
185186

186-
if net.ParseIP(lb_ip).To4() == nil {
187-
log.Fatal("[FATAL] Invalid address ", lb_ip)
187+
// FQDN not supported for tunnel modes
188+
if !tunnel && net.ParseIP(lb_ip_or_fqdn).To4() == nil {
189+
log.Fatal("[FATAL] Invalid address ", lb_ip_or_fqdn)
188190
}
191+
189192
var cont_ratio int = 1
190193
if len(splitted) > 1 {
191194
cont_ratio, err = strconv.Atoi(splitted[1])
192195
if err != nil || cont_ratio <= 0 {
193-
log.Fatal("[FATAL] Invalid contention ratio for ", lb_ip)
196+
log.Fatal("[FATAL] Invalid contention ratio for ", lb_ip_or_fqdn)
194197
}
195198
}
196199

197200
// Obtaining the interface name of the load balancer IP's doesn't make sense in tunnel mode
198201
if !tunnel {
199-
iface = get_iface_from_ip(lb_ip)
202+
iface = get_iface_from_ip(lb_ip_or_fqdn)
200203
if iface == "" {
201-
log.Fatal("[FATAL] IP address not associated with an interface ", lb_ip)
204+
log.Fatal("[FATAL] IP address not associated with an interface ", lb_ip_or_fqdn)
202205
}
203206
}
204207

205-
log.Printf("[INFO] Load balancer %d: %s, contention ratio: %d\n", idx+1, lb_ip, cont_ratio)
206-
lb_list[idx] = load_balancer{address: fmt.Sprintf("%s:%d", lb_ip, lb_port), iface: iface, contention_ratio: cont_ratio, current_connections: 0}
208+
log.Printf("[INFO] Load balancer %d: %s, contention ratio: %d\n", idx+1, lb_ip_or_fqdn, cont_ratio)
209+
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}
207210
}
208211
}
209212

210213
/*
211-
Main function
214+
Main function
212215
*/
213216
func main() {
214217
var lhost = flag.String("lhost", "127.0.0.1", "The host to listen for SOCKS connection")

0 commit comments

Comments
 (0)