mihomo/listener/inner/tcp.go

48 lines
939 B
Go
Raw Normal View History

package inner
import (
"errors"
"net"
2023-10-11 13:01:14 +08:00
"net/netip"
"strconv"
2024-01-02 18:26:45 +08:00
N "github.com/metacubex/mihomo/common/net"
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
)
var tunnel C.Tunnel
func New(t C.Tunnel) {
tunnel = t
}
func HandleTcp(address string, proxy string) (conn net.Conn, err error) {
if tunnel == nil {
return nil, errors.New("tcp uninitialized")
}
// executor Parsed
2024-01-02 18:26:45 +08:00
conn1, conn2 := N.Pipe()
2023-10-11 13:01:14 +08:00
metadata := &C.Metadata{}
metadata.NetWork = C.TCP
metadata.Type = C.INNER
metadata.DNSMode = C.DNSNormal
2023-11-03 21:01:45 +08:00
metadata.Process = C.MihomoName
if proxy != "" {
metadata.SpecialProxy = proxy
}
2023-10-11 13:01:14 +08:00
if h, port, err := net.SplitHostPort(address); err == nil {
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
metadata.DstPort = uint16(port)
}
if ip, err := netip.ParseAddr(h); err == nil {
metadata.DstIP = ip
} else {
metadata.Host = h
}
}
go tunnel.HandleTCPConn(conn2, metadata)
return conn1, nil
}