Skip to content

Commit c63ed38

Browse files
author
uoosef
committed
fixed warp in warp port racing
1 parent 7242f5b commit c63ed38

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/signal"
1313
"path/filepath"
1414
"syscall"
15+
"time"
1516
)
1617

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

129+
// wait for secondary warp
130+
waitForPortToGetsOpenOrTimeout(warpBindAddress)
131+
128132
// run virtual endpoint
129133
virtualEndpointBindAddress, err := findFreePort("udp")
130134
if err != nil {
@@ -218,3 +222,34 @@ func makeDirs() {
218222
}
219223
log.Println("Changed working directory to 'stuff'")
220224
}
225+
func isPortOpen(address string, timeout time.Duration) bool {
226+
// Try to establish a connection
227+
conn, err := net.DialTimeout("tcp", address, timeout)
228+
if err != nil {
229+
return false
230+
}
231+
defer conn.Close()
232+
233+
return true
234+
}
235+
236+
func waitForPortToGetsOpenOrTimeout(addressToCheck string) {
237+
timeout := 5 * time.Second
238+
checkInterval := 500 * time.Millisecond
239+
240+
// Set a deadline for when to stop checking
241+
deadline := time.Now().Add(timeout)
242+
243+
for {
244+
if time.Now().After(deadline) {
245+
log.Fatalf("Timeout reached, port %s is not open", addressToCheck)
246+
}
247+
248+
if isPortOpen(addressToCheck, checkInterval) {
249+
log.Printf("Port %s is now open", addressToCheck)
250+
break
251+
}
252+
253+
time.Sleep(checkInterval)
254+
}
255+
}

0 commit comments

Comments
 (0)