github.com/metacubex/mihomo@v1.18.5/listener/inner/tcp.go (about) 1 package inner 2 3 import ( 4 "errors" 5 "net" 6 "net/netip" 7 "strconv" 8 9 N "github.com/metacubex/mihomo/common/net" 10 C "github.com/metacubex/mihomo/constant" 11 ) 12 13 var tunnel C.Tunnel 14 15 func New(t C.Tunnel) { 16 tunnel = t 17 } 18 19 func HandleTcp(address string, proxy string) (conn net.Conn, err error) { 20 if tunnel == nil { 21 return nil, errors.New("tcp uninitialized") 22 } 23 // executor Parsed 24 conn1, conn2 := N.Pipe() 25 26 metadata := &C.Metadata{} 27 metadata.NetWork = C.TCP 28 metadata.Type = C.INNER 29 metadata.DNSMode = C.DNSNormal 30 metadata.Process = C.MihomoName 31 if proxy != "" { 32 metadata.SpecialProxy = proxy 33 } 34 if h, port, err := net.SplitHostPort(address); err == nil { 35 if port, err := strconv.ParseUint(port, 10, 16); err == nil { 36 metadata.DstPort = uint16(port) 37 } 38 if ip, err := netip.ParseAddr(h); err == nil { 39 metadata.DstIP = ip 40 } else { 41 metadata.Host = h 42 } 43 } 44 45 go tunnel.HandleTCPConn(conn2, metadata) 46 return conn1, nil 47 }