package headlessapi import ( "context" "fmt" "io" "net" "net/netip" "strconv" "strings" "sync" "time" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/transport/call/common" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" "github.com/pion/transport/v4" "github.com/pion/transport/v4/stdnet" ) type iceNetwork struct { local *stdnet.Net interfaceMutex sync.Mutex dialer N.Dialer dnsRouter adapter.DNSRouter } func (n *iceNetwork) ListenPacket(network, address string) (net.PacketConn, error) { localAddress, err := n.ResolveUDPAddr(network, address) if err != nil { return nil, err } return n.local.ListenUDP(network, localAddress) } func (n *iceNetwork) ListenUDP(network string, address *net.UDPAddr) (transport.UDPConn, error) { return n.local.ListenUDP(network, address) } func (n *iceNetwork) ListenTCP(network string, address *net.TCPAddr) (transport.TCPListener, error) { return n.local.ListenTCP(network, address) } func (n *iceNetwork) Dial(network, address string) (net.Conn, error) { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() return common.DialContext(n.dialer)(ctx, network, address) } func (n *iceNetwork) DialUDP(network string, _ *net.UDPAddr, remoteAddress *net.UDPAddr) (transport.UDPConn, error) { connection, err := n.Dial(network, remoteAddress.String()) if err != nil { return nil, err } if datagramConnection, loaded := connection.(transport.UDPConn); loaded { return datagramConnection, nil } return &iceDatagramConnection{connection: connection, remoteAddress: remoteAddress}, nil } func (n *iceNetwork) DialTCP(network string, _ *net.TCPAddr, remoteAddress *net.TCPAddr) (transport.TCPConn, error) { connection, err := n.Dial(network, remoteAddress.String()) if err != nil { return nil, err } if streamConnection, loaded := connection.(transport.TCPConn); loaded { return streamConnection, nil } return &iceStreamConnection{connection: connection}, nil } func (n *iceNetwork) ResolveIPAddr(network, host string) (*net.IPAddr, error) { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() addresses, err := common.ResolveHost(ctx, n.dnsRouter, n.dialer, host) if err != nil { return nil, err } for _, address := range addresses { if strings.HasSuffix(network, "4") && !address.Is4() || strings.HasSuffix(network, "6") && !address.Is6() { continue } return &net.IPAddr{IP: address.AsSlice(), Zone: address.Zone()}, nil } return nil, fmt.Errorf("call: no %s address for %s", network, host) } func (n *iceNetwork) ResolveUDPAddr(network, address string) (*net.UDPAddr, error) { destination, err := n.resolveAddress(network, address) if err != nil { return nil, err } return destination.UDPAddr(), nil } func (n *iceNetwork) ResolveTCPAddr(network, address string) (*net.TCPAddr, error) { destination, err := n.resolveAddress(network, address) if err != nil { return nil, err } return destination.TCPAddr(), nil } func (n *iceNetwork) Interfaces() ([]*transport.Interface, error) { n.interfaceMutex.Lock() defer n.interfaceMutex.Unlock() if err := n.local.UpdateInterfaces(); err != nil { return nil, err } return n.local.Interfaces() } func (n *iceNetwork) InterfaceByIndex(index int) (*transport.Interface, error) { n.interfaceMutex.Lock() defer n.interfaceMutex.Unlock() return n.local.InterfaceByIndex(index) } func (n *iceNetwork) InterfaceByName(name string) (*transport.Interface, error) { n.interfaceMutex.Lock() defer n.interfaceMutex.Unlock() return n.local.InterfaceByName(name) } func (n *iceNetwork) CreateDialer(_ *net.Dialer) transport.Dialer { return n } func (n *iceNetwork) CreateListenConfig(config *net.ListenConfig) transport.ListenConfig { return n.local.CreateListenConfig(config) } func (n *iceNetwork) resolveAddress(network, address string) (M.Socksaddr, error) { host, portString, err := net.SplitHostPort(address) if err != nil { return M.Socksaddr{}, err } port, err := strconv.ParseUint(portString, 10, 16) if err != nil { return M.Socksaddr{}, err } if host == "" { return M.Socksaddr{Port: uint16(port)}, nil } resolved, err := n.ResolveIPAddr(network, host) if err != nil { return M.Socksaddr{}, err } ipAddress, _ := netip.AddrFromSlice(resolved.IP) return M.SocksaddrFrom(ipAddress.WithZone(resolved.Zone), uint16(port)), nil } type iceStreamConnection struct { connection net.Conn } func (c *iceStreamConnection) Read(buffer []byte) (int, error) { return c.connection.Read(buffer) } func (c *iceStreamConnection) Write(buffer []byte) (int, error) { return c.connection.Write(buffer) } func (c *iceStreamConnection) Close() error { return c.connection.Close() } func (c *iceStreamConnection) LocalAddr() net.Addr { return M.SocksaddrFromNet(c.connection.LocalAddr()).TCPAddr() } func (c *iceStreamConnection) RemoteAddr() net.Addr { return c.connection.RemoteAddr() } func (c *iceStreamConnection) SetDeadline(deadline time.Time) error { return c.connection.SetDeadline(deadline) } func (c *iceStreamConnection) SetReadDeadline(deadline time.Time) error { return c.connection.SetReadDeadline(deadline) } func (c *iceStreamConnection) SetWriteDeadline(deadline time.Time) error { return c.connection.SetWriteDeadline(deadline) } func (c *iceStreamConnection) CloseRead() error { return fmt.Errorf("call: TCP half-close is unavailable") } func (c *iceStreamConnection) CloseWrite() error { return fmt.Errorf("call: TCP half-close is unavailable") } func (c *iceStreamConnection) ReadFrom(reader io.Reader) (int64, error) { return io.Copy(c.connection, reader) } func (c *iceStreamConnection) SetLinger(_ int) error { return nil } func (c *iceStreamConnection) SetKeepAlive(_ bool) error { return nil } func (c *iceStreamConnection) SetKeepAlivePeriod(_ time.Duration) error { return nil } func (c *iceStreamConnection) SetNoDelay(_ bool) error { return nil } func (c *iceStreamConnection) SetReadBuffer(_ int) error { return nil } func (c *iceStreamConnection) SetWriteBuffer(_ int) error { return nil } type iceDatagramConnection struct { connection net.Conn remoteAddress *net.UDPAddr } func (c *iceDatagramConnection) Read(buffer []byte) (int, error) { return c.connection.Read(buffer) } func (c *iceDatagramConnection) Write(buffer []byte) (int, error) { return c.connection.Write(buffer) } func (c *iceDatagramConnection) Close() error { return c.connection.Close() } func (c *iceDatagramConnection) LocalAddr() net.Addr { return M.SocksaddrFromNet(c.connection.LocalAddr()).UDPAddr() } func (c *iceDatagramConnection) RemoteAddr() net.Addr { return c.remoteAddress } func (c *iceDatagramConnection) SetDeadline(deadline time.Time) error { return c.connection.SetDeadline(deadline) } func (c *iceDatagramConnection) SetReadDeadline(deadline time.Time) error { return c.connection.SetReadDeadline(deadline) } func (c *iceDatagramConnection) SetWriteDeadline(deadline time.Time) error { return c.connection.SetWriteDeadline(deadline) } func (c *iceDatagramConnection) SetReadBuffer(_ int) error { return nil } func (c *iceDatagramConnection) SetWriteBuffer(_ int) error { return nil } func (c *iceDatagramConnection) ReadFrom(buffer []byte) (int, net.Addr, error) { length, err := c.connection.Read(buffer) return length, c.remoteAddress, err } func (c *iceDatagramConnection) ReadFromUDP(buffer []byte) (int, *net.UDPAddr, error) { length, err := c.connection.Read(buffer) return length, c.remoteAddress, err } func (c *iceDatagramConnection) ReadMsgUDP(buffer, _ []byte) (int, int, int, *net.UDPAddr, error) { length, err := c.connection.Read(buffer) return length, 0, 0, c.remoteAddress, err } func (c *iceDatagramConnection) WriteTo(buffer []byte, address net.Addr) (int, error) { if address != nil && address.String() != c.remoteAddress.String() { return 0, fmt.Errorf("call: UDP connection is bound to %s", c.remoteAddress) } return c.connection.Write(buffer) } func (c *iceDatagramConnection) WriteToUDP(buffer []byte, address *net.UDPAddr) (int, error) { if address == nil { return c.connection.Write(buffer) } return c.WriteTo(buffer, address) } func (c *iceDatagramConnection) WriteMsgUDP(buffer, control []byte, address *net.UDPAddr) (int, int, error) { if len(control) != 0 { return 0, 0, fmt.Errorf("call: UDP control messages are unavailable") } length, err := c.WriteToUDP(buffer, address) return length, 0, err }