Send a byte to the netprobe IP only on Windows

This commit is contained in:
Frank Denis 2019-05-31 11:15:59 +02:00
parent cf261da79a
commit b22d6dfc96
3 changed files with 95 additions and 42 deletions

View File

@ -5,7 +5,6 @@ import (
"errors"
"flag"
"fmt"
"net"
"net/http"
"net/url"
"os"
@ -421,7 +420,7 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
netprobeTimeout = *netprobeTimeoutOverride
}
})
netProbe(config.NetprobeAddress, netprobeTimeout)
NetProbe(config.NetprobeAddress, netprobeTimeout)
if !config.OfflineMode {
if err := config.loadSources(proxy); err != nil {
return err
@ -605,43 +604,3 @@ func cdLocal() {
}
os.Chdir(filepath.Dir(exeFileName))
}
func netProbe(address string, timeout int) error {
if len(address) <= 0 || timeout <= 0 {
return nil
}
remoteUDPAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return err
}
retried := false
if timeout < 0 {
timeout = MaxTimeout
} else {
timeout = Max(MaxTimeout, timeout)
}
for tries := timeout; tries > 0; tries-- {
pc, err := net.DialUDP("udp", nil, remoteUDPAddr)
if err == nil {
// Write at least 1 byte. This ensures that sockets are ready to use for writing.
// Windows specific: during the system startup, sockets can be created but the underlying buffers may not be setup yet. If this is the case
// Write fails with WSAENOBUFS: "An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full"
_, err = pc.Write([]byte{ 0 })
}
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
}
es := "Timeout while waiting for network connectivity"
dlog.Error(es)
return errors.New(es)
}

View File

@ -0,0 +1,45 @@
// +build !windows
package main
import (
"errors"
"net"
"time"
"github.com/jedisct1/dlog"
)
func NetProbe(address string, timeout int) error {
if len(address) <= 0 || timeout <= 0 {
return nil
}
remoteUDPAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return err
}
retried := false
if timeout < 0 {
timeout = MaxTimeout
} else {
timeout = Max(MaxTimeout, timeout)
}
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
}
es := "Timeout while waiting for network connectivity"
dlog.Error(es)
return errors.New(es)
}

View File

@ -0,0 +1,49 @@
package main
import (
"errors"
"net"
"time"
"github.com/jedisct1/dlog"
)
func NetProbe(address string, timeout int) error {
if len(address) <= 0 || timeout <= 0 {
return nil
}
remoteUDPAddr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return err
}
retried := false
if timeout < 0 {
timeout = MaxTimeout
} else {
timeout = Max(MaxTimeout, timeout)
}
for tries := timeout; tries > 0; tries-- {
pc, err := net.DialUDP("udp", nil, remoteUDPAddr)
if err == nil {
// Write at least 1 byte. This ensures that sockets are ready to use for writing.
// Windows specific: during the system startup, sockets can be created but the underlying buffers may not be setup yet. If this is the case
// Write fails with WSAENOBUFS: "An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full"
_, err = pc.Write([]byte{0})
}
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
}
es := "Timeout while waiting for network connectivity"
dlog.Error(es)
return errors.New(es)
}