mihomo/listener/inner/tcp.go

44 lines
830 B
Go
Raw Permalink Normal View History

package inner
import (
"errors"
"net"
2023-10-11 13:01:14 +08:00
"net/netip"
"strconv"
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) (conn net.Conn, err error) {
if tunnel == nil {
return nil, errors.New("tcp uninitialized")
}
// executor Parsed
conn1, conn2 := net.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
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
}