2021-09-23 19:16:26 +02:00
|
|
|
//go:build !windows
|
2019-05-31 11:15:59 +02:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jedisct1/dlog"
|
|
|
|
)
|
|
|
|
|
2020-08-03 18:05:42 +02:00
|
|
|
func NetProbe(proxy *Proxy, address string, timeout int) error {
|
2023-06-24 22:08:01 +02:00
|
|
|
if len(address) <= 0 || timeout == 0 {
|
2019-05-31 11:15:59 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-03 18:05:42 +02:00
|
|
|
if captivePortalHandler, err := ColdStart(proxy); err == nil {
|
2020-08-04 00:50:59 +02:00
|
|
|
if captivePortalHandler != nil {
|
|
|
|
defer captivePortalHandler.Stop()
|
|
|
|
}
|
2020-08-03 18:05:42 +02:00
|
|
|
} else {
|
|
|
|
dlog.Critical(err)
|
|
|
|
}
|
2019-05-31 11:15:59 +02:00
|
|
|
remoteUDPAddr, err := net.ResolveUDPAddr("udp", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
retried := false
|
|
|
|
if timeout < 0 {
|
|
|
|
timeout = MaxTimeout
|
|
|
|
} else {
|
2019-10-22 11:50:08 +02:00
|
|
|
timeout = Min(MaxTimeout, timeout)
|
2019-05-31 11:15:59 +02:00
|
|
|
}
|
|
|
|
for tries := timeout; tries > 0; tries-- {
|
|
|
|
pc, err := net.DialUDP("udp", nil, remoteUDPAddr)
|
|
|
|
if err != nil {
|
|
|
|
if !retried {
|
|
|
|
retried = true
|
|
|
|
dlog.Notice("Network not available yet -- waiting...")
|
|
|
|
}
|
|
|
|
dlog.Debug(err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pc.Close()
|
|
|
|
dlog.Notice("Network connectivity detected")
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-03 14:32:32 +02:00
|
|
|
dlog.Error("Timeout while waiting for network connectivity")
|
|
|
|
return nil
|
2019-05-31 11:15:59 +02:00
|
|
|
}
|