chore: add retry in tunnel dial
This commit is contained in:
parent
add8f84da3
commit
4adfea87f7
3 changed files with 41 additions and 4 deletions
1
go.mod
1
go.mod
|
@ -16,6 +16,7 @@ require (
|
||||||
github.com/gorilla/websocket v1.5.0
|
github.com/gorilla/websocket v1.5.0
|
||||||
github.com/hashicorp/golang-lru v0.5.4
|
github.com/hashicorp/golang-lru v0.5.4
|
||||||
github.com/insomniacslk/dhcp v0.0.0-20221001123530-5308ebe5334c
|
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/lucas-clemente/quic-go v0.29.1
|
||||||
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
|
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
|
||||||
github.com/mdlayher/netlink v1.1.1
|
github.com/mdlayher/netlink v1.1.1
|
||||||
|
|
2
go.sum
2
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/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 h1:OCFM4+DXTWfNlyeoddrTwdup/ztkGSyAMR2UGcPckNQ=
|
||||||
github.com/insomniacslk/dhcp v0.0.0-20221001123530-5308ebe5334c/go.mod h1:h+MxyHxRg9NH3terB1nfRIUaQEcI0XOVkdR9LNBlp8E=
|
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-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-20200117123717-f846d4f6c1f4/go.mod h1:WGuG/smIU4J/54PblvSbh+xvCZmpJnFgr3ds6Z55XMQ=
|
||||||
github.com/jsimonetti/rtnetlink v0.0.0-20201009170750-9c6f07d100c1/go.mod h1:hqoO/u39cqLeBLebZ8fWdE96O7FxrAsRYhnVOdgHxok=
|
github.com/jsimonetti/rtnetlink v0.0.0-20201009170750-9c6f07d100c1/go.mod h1:hqoO/u39cqLeBLebZ8fWdE96O7FxrAsRYhnVOdgHxok=
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jpillora/backoff"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/inbound"
|
"github.com/Dreamacro/clash/adapter/inbound"
|
||||||
"github.com/Dreamacro/clash/component/nat"
|
"github.com/Dreamacro/clash/component/nat"
|
||||||
P "github.com/Dreamacro/clash/component/process"
|
P "github.com/Dreamacro/clash/component/process"
|
||||||
|
@ -273,8 +275,9 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
rawPc, err := proxy.ListenPacketContext(ctx, metadata.Pure())
|
rawPc, err := retry(ctx, func(ctx context.Context) (C.PacketConn, error) {
|
||||||
if err != nil {
|
return proxy.ListenPacketContext(ctx, metadata.Pure())
|
||||||
|
}, func(err error) {
|
||||||
if rule == nil {
|
if rule == nil {
|
||||||
log.Warnln(
|
log.Warnln(
|
||||||
"[UDP] dial %s %s --> %s error: %s",
|
"[UDP] dial %s %s --> %s error: %s",
|
||||||
|
@ -286,6 +289,8 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
|
||||||
} else {
|
} 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())
|
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
|
return
|
||||||
}
|
}
|
||||||
pCtx.InjectPacketConn(rawPc)
|
pCtx.InjectPacketConn(rawPc)
|
||||||
|
@ -354,8 +359,9 @@ func handleTCPConn(connCtx C.ConnContext) {
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
remoteConn, err := proxy.DialContext(ctx, dialMetadata)
|
remoteConn, err := retry(ctx, func(ctx context.Context) (C.Conn, error) {
|
||||||
if err != nil {
|
return proxy.DialContext(ctx, dialMetadata)
|
||||||
|
}, func(err error) {
|
||||||
if rule == nil {
|
if rule == nil {
|
||||||
log.Warnln(
|
log.Warnln(
|
||||||
"[TCP] dial %s %s --> %s error: %s",
|
"[TCP] dial %s %s --> %s error: %s",
|
||||||
|
@ -367,6 +373,8 @@ func handleTCPConn(connCtx C.ConnContext) {
|
||||||
} else {
|
} 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())
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,3 +481,29 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
||||||
|
|
||||||
return proxies["DIRECT"], nil, nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue