@@ -12,6 +12,7 @@ import (
12
12
"os/signal"
13
13
"path/filepath"
14
14
"syscall"
15
+ "time"
15
16
)
16
17
17
18
func usage () {
@@ -125,6 +126,9 @@ func runWarpInWarp(bindAddress, endpoint string, verbose bool) {
125
126
// run secondary warp
126
127
runWarp (warpBindAddress , endpoint , "./secondary/wgcf-profile.ini" , verbose , false )
127
128
129
+ // wait for secondary warp
130
+ waitForPortToGetsOpenOrTimeout (warpBindAddress )
131
+
128
132
// run virtual endpoint
129
133
virtualEndpointBindAddress , err := findFreePort ("udp" )
130
134
if err != nil {
@@ -218,3 +222,34 @@ func makeDirs() {
218
222
}
219
223
log .Println ("Changed working directory to 'stuff'" )
220
224
}
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