Skip to content

Commit

Permalink
fixed warp in warp port racing
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Jan 31, 2024
1 parent 7242f5b commit c63ed38
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/signal"
"path/filepath"
"syscall"
"time"
)

func usage() {
Expand Down Expand Up @@ -125,6 +126,9 @@ func runWarpInWarp(bindAddress, endpoint string, verbose bool) {
// run secondary warp
runWarp(warpBindAddress, endpoint, "./secondary/wgcf-profile.ini", verbose, false)

// wait for secondary warp
waitForPortToGetsOpenOrTimeout(warpBindAddress)

// run virtual endpoint
virtualEndpointBindAddress, err := findFreePort("udp")
if err != nil {
Expand Down Expand Up @@ -218,3 +222,34 @@ func makeDirs() {
}
log.Println("Changed working directory to 'stuff'")
}
func isPortOpen(address string, timeout time.Duration) bool {
// Try to establish a connection
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
return false
}
defer conn.Close()

return true
}

func waitForPortToGetsOpenOrTimeout(addressToCheck string) {
timeout := 5 * time.Second
checkInterval := 500 * time.Millisecond

// Set a deadline for when to stop checking
deadline := time.Now().Add(timeout)

for {
if time.Now().After(deadline) {
log.Fatalf("Timeout reached, port %s is not open", addressToCheck)
}

if isPortOpen(addressToCheck, checkInterval) {
log.Printf("Port %s is now open", addressToCheck)
break
}

time.Sleep(checkInterval)
}
}

0 comments on commit c63ed38

Please sign in to comment.