From 3564e96a0010a415e5dd22fc9deeb7efd381dcb5 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 23 Oct 2023 16:45:22 +0800 Subject: [PATCH] chore: share some code --- adapter/inbound/http.go | 4 ++-- component/http/http.go | 2 ++ dns/{patch.go => local.go} | 0 listener/http/client.go | 4 ++-- listener/http/proxy.go | 2 +- listener/http/upgrade.go | 2 +- 6 files changed, 8 insertions(+), 6 deletions(-) rename dns/{patch.go => local.go} (100%) diff --git a/adapter/inbound/http.go b/adapter/inbound/http.go index 2a6050e5..7f3b143f 100644 --- a/adapter/inbound/http.go +++ b/adapter/inbound/http.go @@ -8,11 +8,11 @@ import ( ) // NewHTTP receive normal http request and return HTTPContext -func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn, additions ...Addition) (net.Conn, *C.Metadata) { +func NewHTTP(target socks5.Addr, srcConn net.Conn, conn net.Conn, additions ...Addition) (net.Conn, *C.Metadata) { metadata := parseSocksAddr(target) metadata.NetWork = C.TCP metadata.Type = C.HTTP - ApplyAdditions(metadata, WithSrcAddr(source), WithInAddr(conn.LocalAddr())) + ApplyAdditions(metadata, WithSrcAddr(srcConn.RemoteAddr()), WithInAddr(conn.LocalAddr())) ApplyAdditions(metadata, additions...) return conn, metadata } diff --git a/component/http/http.go b/component/http/http.go index 8e682e94..073f0237 100644 --- a/component/http/http.go +++ b/component/http/http.go @@ -7,6 +7,7 @@ import ( "net" "net/http" URL "net/url" + "runtime" "strings" "time" @@ -47,6 +48,7 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st transport := &http.Transport{ // from http.DefaultTransport + DisableKeepAlives: runtime.GOOS == "android", MaxIdleConns: 100, IdleConnTimeout: 30 * time.Second, TLSHandshakeTimeout: 10 * time.Second, diff --git a/dns/patch.go b/dns/local.go similarity index 100% rename from dns/patch.go rename to dns/local.go diff --git a/listener/http/client.go b/listener/http/client.go index 76c7c8eb..84c284ff 100644 --- a/listener/http/client.go +++ b/listener/http/client.go @@ -12,7 +12,7 @@ import ( "github.com/Dreamacro/clash/transport/socks5" ) -func newClient(source net.Addr, tunnel C.Tunnel, additions ...inbound.Addition) *http.Client { +func newClient(srcConn net.Conn, tunnel C.Tunnel, additions ...inbound.Addition) *http.Client { return &http.Client{ Transport: &http.Transport{ // from http.DefaultTransport @@ -32,7 +32,7 @@ func newClient(source net.Addr, tunnel C.Tunnel, additions ...inbound.Addition) left, right := net.Pipe() - go tunnel.HandleTCPConn(inbound.NewHTTP(dstAddr, source, right, additions...)) + go tunnel.HandleTCPConn(inbound.NewHTTP(dstAddr, srcConn, right, additions...)) return left, nil }, diff --git a/listener/http/proxy.go b/listener/http/proxy.go index 44ff04c7..fa1d8f88 100644 --- a/listener/http/proxy.go +++ b/listener/http/proxy.go @@ -15,7 +15,7 @@ import ( ) func HandleConn(c net.Conn, tunnel C.Tunnel, cache *cache.LruCache[string, bool], additions ...inbound.Addition) { - client := newClient(c.RemoteAddr(), tunnel, additions...) + client := newClient(c, tunnel, additions...) defer client.CloseIdleConnections() conn := N.NewBufferedConn(c) diff --git a/listener/http/upgrade.go b/listener/http/upgrade.go index e67928ce..6e4f063d 100644 --- a/listener/http/upgrade.go +++ b/listener/http/upgrade.go @@ -43,7 +43,7 @@ func handleUpgrade(conn net.Conn, request *http.Request, tunnel C.Tunnel, additi left, right := net.Pipe() - go tunnel.HandleTCPConn(inbound.NewHTTP(dstAddr, conn.RemoteAddr(), right, additions...)) + go tunnel.HandleTCPConn(inbound.NewHTTP(dstAddr, conn, right, additions...)) var bufferedLeft *N.BufferedConn if request.TLS != nil {