chore: share some code

This commit is contained in:
wwqgtxx 2023-10-23 16:45:22 +08:00
parent f6f8f27668
commit 3564e96a00
6 changed files with 8 additions and 6 deletions

View file

@ -8,11 +8,11 @@ import (
) )
// NewHTTP receive normal http request and return HTTPContext // 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 := parseSocksAddr(target)
metadata.NetWork = C.TCP metadata.NetWork = C.TCP
metadata.Type = C.HTTP metadata.Type = C.HTTP
ApplyAdditions(metadata, WithSrcAddr(source), WithInAddr(conn.LocalAddr())) ApplyAdditions(metadata, WithSrcAddr(srcConn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
ApplyAdditions(metadata, additions...) ApplyAdditions(metadata, additions...)
return conn, metadata return conn, metadata
} }

View file

@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
URL "net/url" URL "net/url"
"runtime"
"strings" "strings"
"time" "time"
@ -47,6 +48,7 @@ func HttpRequest(ctx context.Context, url, method string, header map[string][]st
transport := &http.Transport{ transport := &http.Transport{
// from http.DefaultTransport // from http.DefaultTransport
DisableKeepAlives: runtime.GOOS == "android",
MaxIdleConns: 100, MaxIdleConns: 100,
IdleConnTimeout: 30 * time.Second, IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 10 * time.Second, TLSHandshakeTimeout: 10 * time.Second,

View file

@ -12,7 +12,7 @@ import (
"github.com/Dreamacro/clash/transport/socks5" "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{ return &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
// from http.DefaultTransport // from http.DefaultTransport
@ -32,7 +32,7 @@ func newClient(source net.Addr, tunnel C.Tunnel, additions ...inbound.Addition)
left, right := net.Pipe() 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 return left, nil
}, },

View file

@ -15,7 +15,7 @@ import (
) )
func HandleConn(c net.Conn, tunnel C.Tunnel, cache *cache.LruCache[string, bool], additions ...inbound.Addition) { 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() defer client.CloseIdleConnections()
conn := N.NewBufferedConn(c) conn := N.NewBufferedConn(c)

View file

@ -43,7 +43,7 @@ func handleUpgrade(conn net.Conn, request *http.Request, tunnel C.Tunnel, additi
left, right := net.Pipe() 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 var bufferedLeft *N.BufferedConn
if request.TLS != nil { if request.TLS != nil {