From 4636499439d7d549b4ddeb178f071508d9ff96e2 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 11 Oct 2023 13:01:14 +0800 Subject: [PATCH] chore: support reject proxy type --- adapter/inbound/socket.go | 22 ---------------------- adapter/outbound/reject.go | 14 ++++++++++++++ adapter/parser.go | 7 +++++++ listener/inner/tcp.go | 22 ++++++++++++++++++++-- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/adapter/inbound/socket.go b/adapter/inbound/socket.go index 65dbe0a2..dbe1712d 100644 --- a/adapter/inbound/socket.go +++ b/adapter/inbound/socket.go @@ -2,8 +2,6 @@ package inbound import ( "net" - "net/netip" - "strconv" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/socks5" @@ -21,23 +19,3 @@ func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Ad return conn, metadata } - -func NewInner(conn net.Conn, address string) (net.Conn, *C.Metadata) { - metadata := &C.Metadata{} - metadata.NetWork = C.TCP - metadata.Type = C.INNER - metadata.DNSMode = C.DNSNormal - metadata.Process = C.ClashName - 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 - } - } - - return conn, metadata -} diff --git a/adapter/outbound/reject.go b/adapter/outbound/reject.go index a72dc377..f1de3981 100644 --- a/adapter/outbound/reject.go +++ b/adapter/outbound/reject.go @@ -15,6 +15,10 @@ type Reject struct { *Base } +type RejectOption struct { + Name string `proxy:"name"` +} + // DialContext implements C.ProxyAdapter func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) { return NewConn(nopConn{}, r), nil @@ -25,6 +29,16 @@ func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, return newPacketConn(nopPacketConn{}, r), nil } +func NewRejectWithOption(option RejectOption) *Reject { + return &Reject{ + Base: &Base{ + name: option.Name, + tp: C.Direct, + udp: true, + }, + } +} + func NewReject() *Reject { return &Reject{ Base: &Base{ diff --git a/adapter/parser.go b/adapter/parser.go index eeb0fd59..43ebfe5f 100644 --- a/adapter/parser.go +++ b/adapter/parser.go @@ -120,6 +120,13 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) { break } proxy = outbound.NewDirectWithOption(*directOption) + case "reject": + rejectOption := &outbound.RejectOption{} + err = decoder.Decode(mapping, rejectOption) + if err != nil { + break + } + proxy = outbound.NewRejectWithOption(*rejectOption) default: return nil, fmt.Errorf("unsupport proxy type: %s", proxyType) } diff --git a/listener/inner/tcp.go b/listener/inner/tcp.go index cbd27cd6..8973c431 100644 --- a/listener/inner/tcp.go +++ b/listener/inner/tcp.go @@ -3,8 +3,9 @@ package inner import ( "errors" "net" + "net/netip" + "strconv" - "github.com/Dreamacro/clash/adapter/inbound" C "github.com/Dreamacro/clash/constant" ) @@ -20,6 +21,23 @@ func HandleTcp(address string) (conn net.Conn, err error) { } // executor Parsed conn1, conn2 := net.Pipe() - go tunnel.HandleTCPConn(inbound.NewInner(conn2, address)) + + metadata := &C.Metadata{} + metadata.NetWork = C.TCP + metadata.Type = C.INNER + metadata.DNSMode = C.DNSNormal + metadata.Process = C.ClashName + 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 }