2022-01-18 21:09:36 +08:00
|
|
|
package inner
|
|
|
|
|
|
|
|
import (
|
2023-05-01 21:27:55 +08:00
|
|
|
"errors"
|
|
|
|
"net"
|
2023-10-11 13:01:14 +08:00
|
|
|
"net/netip"
|
|
|
|
"strconv"
|
2023-05-01 21:27:55 +08:00
|
|
|
|
2022-01-18 21:09:36 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
var tunnel C.Tunnel
|
2022-01-18 21:09:36 +08:00
|
|
|
|
2023-09-28 18:59:31 +08:00
|
|
|
func New(t C.Tunnel) {
|
|
|
|
tunnel = t
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
|
|
|
|
2023-05-01 21:27:55 +08:00
|
|
|
func HandleTcp(address string) (conn net.Conn, err error) {
|
2023-09-28 18:59:31 +08:00
|
|
|
if tunnel == nil {
|
2023-05-01 21:27:55 +08:00
|
|
|
return nil, errors.New("tcp uninitialized")
|
|
|
|
}
|
|
|
|
// executor Parsed
|
2022-01-18 21:09:36 +08:00
|
|
|
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
|
|
|
|
metadata.Process = C.ClashName
|
2023-10-23 16:52:19 +08:00
|
|
|
metadata.RawSrcAddr = conn1.RemoteAddr()
|
|
|
|
metadata.RawDstAddr = conn1.LocalAddr()
|
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)
|
2023-05-01 21:27:55 +08:00
|
|
|
return conn1, nil
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|