github.com/chwjbn/xclash@v0.2.0/adapter/inbound/socket.go (about)

     1  package inbound
     2  
     3  import (
     4  	"github.com/chwjbn/xclash/component/auth"
     5  	"net"
     6  
     7  	C "github.com/chwjbn/xclash/constant"
     8  	"github.com/chwjbn/xclash/context"
     9  	"github.com/chwjbn/xclash/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().String()); err == nil {
    18  		metadata.SrcIP = ip
    19  		metadata.SrcPort = port
    20  	}
    21  
    22  	return context.NewConnContext(conn, metadata)
    23  }
    24  
    25  func NewSocketWithAuth(target socks5.Addr, conn net.Conn, source C.Type, authUser *auth.AuthUser) *context.ConnContext {
    26  	metadata := parseSocksAddr(target)
    27  	metadata.NetWork = C.TCP
    28  	metadata.Type = source
    29  	if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
    30  		metadata.SrcIP = ip
    31  		metadata.SrcPort = port
    32  	}
    33  
    34  	return context.NewConnContextWithAuth(conn, metadata, authUser)
    35  }