github.com/igoogolx/clash@v1.19.8/adapter/inbound/socket.go (about)

     1  package inbound
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	C "github.com/igoogolx/clash/constant"
     8  	"github.com/igoogolx/clash/context"
     9  	"github.com/igoogolx/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 addrPort, err := netip.ParseAddrPort(conn.LocalAddr().String()); err == nil {
    22  		metadata.OriginDst = addrPort
    23  	}
    24  	return context.NewConnContext(conn, metadata)
    25  }