mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-09 10:40:10 +08:00
34 lines
631 B
Go
Executable File
34 lines
631 B
Go
Executable File
package proxy
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/xjasonlyu/clash/component/resolver"
|
|
)
|
|
|
|
const (
|
|
tcpConnectTimeout = 5 * time.Second
|
|
tcpKeepAlivePeriod = 30 * time.Second
|
|
)
|
|
|
|
func tcpKeepAlive(c net.Conn) {
|
|
if tcp, ok := c.(*net.TCPConn); ok {
|
|
tcp.SetKeepAlive(true)
|
|
tcp.SetKeepAlivePeriod(tcpKeepAlivePeriod)
|
|
}
|
|
}
|
|
|
|
func resolveUDPAddr(network, address string) (*net.UDPAddr, error) {
|
|
host, port, err := net.SplitHostPort(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ip, err := resolver.ResolveIP(host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
|
}
|