mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2026-04-23 00:07:30 +08:00
41 lines
992 B
Go
41 lines
992 B
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
|
|
M "github.com/xjasonlyu/tun2socks/v2/metadata"
|
|
"github.com/xjasonlyu/tun2socks/v2/transport/socks5"
|
|
)
|
|
|
|
const (
|
|
tcpConnectTimeout = 5 * time.Second
|
|
tcpKeepAlivePeriod = 30 * time.Second
|
|
)
|
|
|
|
// SetKeepAlive sets tcp keepalive option for tcp connection.
|
|
func SetKeepAlive(c net.Conn) {
|
|
if tcp, ok := c.(*net.TCPConn); ok {
|
|
tcp.SetKeepAlive(true)
|
|
tcp.SetKeepAlivePeriod(tcpKeepAlivePeriod)
|
|
}
|
|
}
|
|
|
|
// SafeConnClose closes tcp connection safely.
|
|
func SafeConnClose(c net.Conn, err error) {
|
|
if c != nil && err != nil {
|
|
c.Close()
|
|
}
|
|
}
|
|
|
|
// SerializeSocksAddr serializes metadata to SOCKSv5 address.
|
|
func SerializeSocksAddr(m *M.Metadata) socks5.Addr {
|
|
return socks5.SerializeAddr("", m.DstIP, m.DstPort)
|
|
}
|
|
|
|
// WithTCPConnectTimeout returns a derived context with the default TCP connect timeout.
|
|
func WithTCPConnectTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
return context.WithTimeout(ctx, tcpConnectTimeout)
|
|
}
|