Skip to content

Commit

Permalink
Add synchronization for TCP handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Jul 28, 2024
1 parent 6f42590 commit 27476a8
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions sshmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"slices"
"sync"
"time"

"github.com/pires/go-proxyproto"
Expand Down Expand Up @@ -359,7 +360,15 @@ func sendLogAndClose(logMessage *LogMessage, session *ssh.PipeSession, logCh cha
logCh <- *logMessage
}

func sshmuxListenAddr(address string, sshConfig *ssh.ServerConfig, proxy bool, proxyMux bool) {
func sshmuxListenAddr(address string, waitgroup *sync.WaitGroup, sshConfig *ssh.ServerConfig, proxy bool, proxyMux bool) {
// configure waitgroup callback
defer func() {
if waitgroup != nil {
waitgroup.Done()
}
}()

// set up TCP listener
listener, err := net.Listen("tcp", address)
if err != nil {
log.Fatal(err)
Expand All @@ -378,8 +387,12 @@ func sshmuxListenAddr(address string, sshConfig *ssh.ServerConfig, proxy bool, p
}
}
defer listener.Close()

// set up log channel
logCh := make(chan LogMessage, 256)
go runLogger(logCh)

// main handler loop
for {
conn, err := listener.Accept()
if err != nil {
Expand Down Expand Up @@ -427,21 +440,24 @@ func sshmuxServer(configFile string) {
}
sshConfig.AddHostKey(key)
}
waitgroup := sync.WaitGroup{}
waitgroup.Add(1)
if config.Address == config.ProxiedAddress {
if config.Address == "" {
log.Println("No address specified, defaulting to 0.0.0.0:8022")
go sshmuxListenAddr("0.0.0.0:8022", sshConfig, false, false)
go sshmuxListenAddr("0.0.0.0:8022", &waitgroup, sshConfig, false, false)
} else {
go sshmuxListenAddr(config.Address, sshConfig, true, true)
go sshmuxListenAddr(config.Address, &waitgroup, sshConfig, true, true)
}
} else {
if config.Address != "" {
go sshmuxListenAddr(config.Address, sshConfig, false, false)
go sshmuxListenAddr(config.Address, &waitgroup, sshConfig, false, false)
}
if config.ProxiedAddress != "" {
go sshmuxListenAddr(config.ProxiedAddress, sshConfig, true, false)
go sshmuxListenAddr(config.ProxiedAddress, &waitgroup, sshConfig, true, false)
}
}
waitgroup.Wait()
}

func main() {
Expand Down

0 comments on commit 27476a8

Please sign in to comment.