From 3880c3c1be0f97f0f2985329f45a3c720ba58cd7 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 24 Nov 2022 12:32:35 +0800 Subject: [PATCH] chore: add retry in tunnel dial --- go.mod | 1 + go.sum | 2 ++ tunnel/tunnel.go | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 140b1be0..feef8810 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/hashicorp/golang-lru v0.5.4 github.com/insomniacslk/dhcp v0.0.0-20221001123530-5308ebe5334c + github.com/jpillora/backoff v1.0.0 github.com/lucas-clemente/quic-go v0.29.1 github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 github.com/mdlayher/netlink v1.1.1 diff --git a/go.sum b/go.sum index e85ef9b1..fe36da98 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/hugelgupf/socketpair v0.0.0-20190730060125-05d35a94e714/go.mod h1:2Go github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/insomniacslk/dhcp v0.0.0-20221001123530-5308ebe5334c h1:OCFM4+DXTWfNlyeoddrTwdup/ztkGSyAMR2UGcPckNQ= github.com/insomniacslk/dhcp v0.0.0-20221001123530-5308ebe5334c/go.mod h1:h+MxyHxRg9NH3terB1nfRIUaQEcI0XOVkdR9LNBlp8E= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a/go.mod h1:Oz+70psSo5OFh8DBl0Zv2ACw7Esh6pPUphlvZG9x7uw= github.com/jsimonetti/rtnetlink v0.0.0-20200117123717-f846d4f6c1f4/go.mod h1:WGuG/smIU4J/54PblvSbh+xvCZmpJnFgr3ds6Z55XMQ= github.com/jsimonetti/rtnetlink v0.0.0-20201009170750-9c6f07d100c1/go.mod h1:hqoO/u39cqLeBLebZ8fWdE96O7FxrAsRYhnVOdgHxok= diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 2e38da40..c602a0be 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -11,6 +11,8 @@ import ( "sync" "time" + "github.com/jpillora/backoff" + "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/component/nat" P "github.com/Dreamacro/clash/component/process" @@ -273,8 +275,9 @@ func handleUDPConn(packet *inbound.PacketAdapter) { ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout) defer cancel() - rawPc, err := proxy.ListenPacketContext(ctx, metadata.Pure()) - if err != nil { + rawPc, err := retry(ctx, func(ctx context.Context) (C.PacketConn, error) { + return proxy.ListenPacketContext(ctx, metadata.Pure()) + }, func(err error) { if rule == nil { log.Warnln( "[UDP] dial %s %s --> %s error: %s", @@ -286,6 +289,8 @@ func handleUDPConn(packet *inbound.PacketAdapter) { } else { log.Warnln("[UDP] dial %s (match %s/%s) %s --> %s error: %s", proxy.Name(), rule.RuleType().String(), rule.Payload(), metadata.SourceAddress(), metadata.RemoteAddress(), err.Error()) } + }) + if err != nil { return } pCtx.InjectPacketConn(rawPc) @@ -354,8 +359,9 @@ func handleTCPConn(connCtx C.ConnContext) { ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout) defer cancel() - remoteConn, err := proxy.DialContext(ctx, dialMetadata) - if err != nil { + remoteConn, err := retry(ctx, func(ctx context.Context) (C.Conn, error) { + return proxy.DialContext(ctx, dialMetadata) + }, func(err error) { if rule == nil { log.Warnln( "[TCP] dial %s %s --> %s error: %s", @@ -367,6 +373,8 @@ func handleTCPConn(connCtx C.ConnContext) { } else { log.Warnln("[TCP] dial %s (match %s/%s) %s --> %s error: %s", proxy.Name(), rule.RuleType().String(), rule.Payload(), metadata.SourceAddress(), metadata.RemoteAddress(), err.Error()) } + }) + if err != nil { return } @@ -473,3 +481,29 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) { return proxies["DIRECT"], nil, nil } + +func retry[T any](ctx context.Context, ft func(context.Context) (T, error), fe func(err error)) (t T, err error) { + b := &backoff.Backoff{ + Min: 10 * time.Millisecond, + Max: 1 * time.Second, + Factor: 2, + Jitter: true, + } + for i := 0; i < 10; i++ { + t, err = ft(ctx) + if err != nil { + if fe != nil { + fe(err) + } + select { + case <-time.After(b.Duration()): + continue + case <-ctx.Done(): + return + } + } else { + break + } + } + return +}