From d55025ecaecbfada8feeab0b114db4697e90c1cc Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 28 Feb 2023 15:53:23 +0800 Subject: [PATCH] fix: udp loopback show "The requested address is not valid in its context." --- component/dialer/dialer.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/component/dialer/dialer.go b/component/dialer/dialer.go index 2d3937c8..478e9f19 100644 --- a/component/dialer/dialer.go +++ b/component/dialer/dialer.go @@ -347,7 +347,12 @@ func (d Dialer) DialContext(ctx context.Context, network, address string) (net.C } func (d Dialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) { - return ListenPacket(ctx, ParseNetwork(network, rAddrPort.Addr()), address, WithOption(d.Opt)) + opt := WithOption(d.Opt) + if rAddrPort.Addr().Unmap().IsLoopback() { + // avoid "The requested address is not valid in its context." + opt = WithInterface("") + } + return ListenPacket(ctx, ParseNetwork(network, rAddrPort.Addr()), address, opt) } func NewDialer(options ...Option) Dialer {