github.com/yaling888/clash@v1.53.0/adapter/inbound/socket.go (about)

     1  package inbound
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	C "github.com/yaling888/clash/constant"
     8  	"github.com/yaling888/clash/context"
     9  	"github.com/yaling888/clash/transport/socks5"
    10  )
    11  
    12  // NewSocket receive TCP inbound and return ConnContext
    13  func NewSocket(target socks5.Addr, conn net.Conn, source C.Type) *context.ConnContext {
    14  	metadata := parseSocksAddr(target)
    15  	metadata.NetWork = C.TCP
    16  	metadata.Type = source
    17  	if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil {
    18  		metadata.SrcIP = ip
    19  		metadata.SrcPort = C.Port(port)
    20  	}
    21  	if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
    22  		metadata.OriginDst = netip.AddrPortFrom(ip, uint16(port))
    23  	}
    24  	return context.NewConnContext(conn, metadata)
    25  }
    26  
    27  // NewSocketBy receive TCP inbound and return ConnContext
    28  func NewSocketBy(conn net.Conn, src, dst netip.AddrPort, tp C.Type) *context.ConnContext {
    29  	metadata := &C.Metadata{}
    30  	metadata.NetWork = C.TCP
    31  	metadata.Type = tp
    32  	metadata.SrcIP = src.Addr()
    33  	metadata.SrcPort = C.Port(src.Port())
    34  	metadata.DstIP = dst.Addr()
    35  	metadata.DstPort = C.Port(dst.Port())
    36  	metadata.OriginDst = dst
    37  
    38  	return context.NewConnContext(conn, metadata)
    39  }