Skip to content

Commit

Permalink
Support specifying domain names for load balancers in tunnel mode, cl…
Browse files Browse the repository at this point in the history
…oses #34
  • Loading branch information
extremecoders-re committed Dec 17, 2023
1 parent 0f70e53 commit c38366e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ D:\>go-dispatch-proxy.exe -lhost 192.168.1.2 -lport 5566 10.81.177.215 192.168.1
[INFO] SOCKS server started at 192.168.1.2:5566
```

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.
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.

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.

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

Both the IP and port must be mentioned while specifying the load balancer addresses.
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.

```
D:\> go-dispatch-proxy.exe -tunnel proxy1.com:7777 proxy2.com:7778
```

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

Expand Down
47 changes: 25 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var lb_list []load_balancer
var mutex *sync.Mutex

/*
Get a load balancer according to contention ratio
Get a load balancer according to contention ratio
*/
func get_load_balancer() *load_balancer {
mutex.Lock()
Expand All @@ -49,7 +49,7 @@ func get_load_balancer() *load_balancer {
}

/*
Joins the local and remote connections together
Joins the local and remote connections together
*/
func pipe_connections(local_conn, remote_conn net.Conn) {
go func() {
Expand All @@ -72,7 +72,7 @@ func pipe_connections(local_conn, remote_conn net.Conn) {
}

/*
Handle connections in tunnel mode
Handle connections in tunnel mode
*/
func handle_tunnel_connection(conn net.Conn) {
load_balancer := get_load_balancer()
Expand All @@ -91,7 +91,7 @@ func handle_tunnel_connection(conn net.Conn) {
}

/*
Calls the apprpriate handle_connections based on tunnel mode
Calls the apprpriate handle_connections based on tunnel mode
*/
func handle_connection(conn net.Conn, tunnel bool) {
if tunnel {
Expand All @@ -102,8 +102,8 @@ func handle_connection(conn net.Conn, tunnel bool) {
}

/*
Detect the addresses which can be used for dispatching in non-tunnelling mode.
Alternate to ipconfig/ifconfig
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")
Expand All @@ -125,7 +125,7 @@ func detect_interfaces() {
}

/*
Gets the interface associated with the IP
Gets the interface associated with the IP
*/
func get_iface_from_ip(ip string) string {
ifaces, _ := net.Interfaces()
Expand All @@ -148,7 +148,7 @@ func get_iface_from_ip(ip string) string {
}

/*
Parses the command line arguements to obtain the list of load balancers
Parses the command line arguements to obtain the list of load balancers
*/
func parse_load_balancers(args []string, tunnel bool) {
if len(args) == 0 {
Expand All @@ -160,55 +160,58 @@ func parse_load_balancers(args []string, tunnel bool) {
for idx, a := range args {
splitted := strings.Split(a, "@")
iface := ""
var lb_ip string
// 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_port := strings.Split(splitted[0], ":")
if len(ip_port) != 2 {
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 = ip_port[0]
lb_port, err = strconv.Atoi(ip_port[1])
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 = splitted[0]
lb_ip_or_fqdn = splitted[0]
lb_port = 0
}

if net.ParseIP(lb_ip).To4() == nil {
log.Fatal("[FATAL] Invalid address ", lb_ip)
// 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)
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)
iface = get_iface_from_ip(lb_ip_or_fqdn)
if iface == "" {
log.Fatal("[FATAL] IP address not associated with an interface ", lb_ip)
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, cont_ratio)
lb_list[idx] = load_balancer{address: fmt.Sprintf("%s:%d", lb_ip, lb_port), iface: iface, contention_ratio: cont_ratio, current_connections: 0}
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
Main function
*/
func main() {
var lhost = flag.String("lhost", "127.0.0.1", "The host to listen for SOCKS connection")
Expand Down

0 comments on commit c38366e

Please sign in to comment.