Skip to content

Commit

Permalink
add fault-tolerant load balancing
Browse files Browse the repository at this point in the history
  • Loading branch information
cmason3 committed Feb 19, 2024
1 parent 86921d4 commit b28a947
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ A simple TCP and UDP based port forwarder which supports concurrent connections
-udp [bind_host:]<listen_port>:<remote_host>:<remote_port>
-logfile <portfwd.log>
-config <portfwd.conf>
-fault-tolerant
-ft-tcp
```

You can specify as many TCP and/or UDP forwarders as you wish on the command line - if you omit `bind_host` then it defaults to `127.0.0.1` - to listen on all IPs use `0.0.0.0`. If you duplicate `bind_host` and `listen_port` then it will load balance between the destinations (round-robin by default). For TCP connections instead of round-robin load balancing you can specify `-fault-tolerant`, which will keep using the same destination until it fails and will then move to the next.
You can specify as many TCP and/or UDP forwarders as you wish on the command line - if you omit `bind_host` then it defaults to `127.0.0.1` - to listen on all IPs use `0.0.0.0`. If you duplicate `bind_host` and `listen_port` then it will load balance between the destinations (round-robin by default). For TCP connections instead of round-robin load balancing you can specify `-ft-tcp`, which will keep using the same destination until it fails and will then move to the next (fault tolerant).

You also have the option of specifying multiple TCP and/or UDP forwarders (one per line) within a configuration file, e.g:

Expand Down
8 changes: 4 additions & 4 deletions portfwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func main() {
fmt.Fprintf(os.Stderr, " -udp [bind_host:]<listen_port>:<remote_host>:<remote_port>\n")
fmt.Fprintf(os.Stderr, " -logfile <portfwd.log>\n")
fmt.Fprintf(os.Stderr, " -config <portfwd.conf>\n")
fmt.Fprintf(os.Stderr, " -fault-tolerant\n")
fmt.Fprintf(os.Stderr, " -ft-tcp\n")
}
os.Exit(1)
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func parseArgs() (Args, error) {
} else {
return args, fmt.Errorf("invalid argument: %s", os.Args[i])
}
} else if smatch(os.Args[i], "-fault-tolerant", 2) {
} else if smatch(os.Args[i], "-ft-tcp", 2) {
args.mode = "FT"

} else {
Expand Down Expand Up @@ -333,7 +333,7 @@ func udpForwarder(fwdr string, targets []string, wgf *sync.WaitGroup, args *Args
defer wgc.Done()
buf := make([]byte, bufSize)

for {
for {
if n, _, err := u.dst.ReadFrom(buf); err == nil {
s.WriteToUDP(buf[:n], addr)
udpConnsMutex.Lock()
Expand Down Expand Up @@ -434,7 +434,7 @@ func tcpForwarder(fwdr string, targets []string, wgf *sync.WaitGroup, args *Args
if args.mode == "FT" {
targetMutex.Lock()
for i, t := range targets {
if t == target {
if (t == target) && (i != (len(targets) - 1)) {
copy(targets[i:], targets[i + 1:])
targets[len(targets) - 1] = t
break
Expand Down

0 comments on commit b28a947

Please sign in to comment.