Clash.Meta/test/util.go

51 lines
854 B
Go
Raw Normal View History

2021-09-04 22:44:18 +08:00
package main
import (
"context"
"net"
"time"
)
func Listen(network, address string) (net.Listener, error) {
lc := net.ListenConfig{}
var lastErr error
for i := 0; i < 5; i++ {
l, err := lc.Listen(context.Background(), network, address)
if err == nil {
return l, nil
}
lastErr = err
time.Sleep(time.Millisecond * 200)
}
return nil, lastErr
}
func ListenPacket(network, address string) (net.PacketConn, error) {
var lastErr error
for i := 0; i < 5; i++ {
l, err := net.ListenPacket(network, address)
if err == nil {
return l, nil
}
lastErr = err
time.Sleep(time.Millisecond * 200)
}
return nil, lastErr
}
2022-05-21 17:37:06 +08:00
func TCPing(addr string) bool {
for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", addr)
if err == nil {
conn.Close()
return true
}
time.Sleep(time.Millisecond * 500)
}
return false
}