From d481bd7993d2241d456867e905265160fa4e205e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 25 Aug 2022 14:49:49 +0800 Subject: [PATCH] Fix bind_address --- common/dialer/default.go | 5 ++++- option/outbound.go | 16 ++++++++-------- option/types.go | 6 +++++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/common/dialer/default.go b/common/dialer/default.go index 654d13a3..b21980fb 100644 --- a/common/dialer/default.go +++ b/common/dialer/default.go @@ -113,7 +113,10 @@ func NewDefault(router adapter.Router, options option.DialerOptions) *DefaultDia } var bindUDPAddr string udpDialer := dialer - bindAddress := netip.Addr(options.BindAddress) + var bindAddress netip.Addr + if options.BindAddress != nil { + bindAddress = options.BindAddress.Build() + } if bindAddress.IsValid() { dialer.LocalAddr = &net.TCPAddr{ IP: bindAddress.AsSlice(), diff --git a/option/outbound.go b/option/outbound.go index 24629b2b..aa53a3a3 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -100,14 +100,14 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error { } type DialerOptions struct { - Detour string `json:"detour,omitempty"` - BindInterface string `json:"bind_interface,omitempty"` - BindAddress ListenAddress `json:"bind_address,omitempty"` - ProtectPath string `json:"protect_path,omitempty"` - RoutingMark int `json:"routing_mark,omitempty"` - ReuseAddr bool `json:"reuse_addr,omitempty"` - ConnectTimeout Duration `json:"connect_timeout,omitempty"` - TCPFastOpen bool `json:"tcp_fast_open,omitempty"` + Detour string `json:"detour,omitempty"` + BindInterface string `json:"bind_interface,omitempty"` + BindAddress *ListenAddress `json:"bind_address,omitempty"` + ProtectPath string `json:"protect_path,omitempty"` + RoutingMark int `json:"routing_mark,omitempty"` + ReuseAddr bool `json:"reuse_addr,omitempty"` + ConnectTimeout Duration `json:"connect_timeout,omitempty"` + TCPFastOpen bool `json:"tcp_fast_open,omitempty"` } type OutboundDialerOptions struct { diff --git a/option/types.go b/option/types.go index ee48d76c..cd400029 100644 --- a/option/types.go +++ b/option/types.go @@ -16,7 +16,7 @@ type ListenAddress netip.Addr func (a ListenAddress) MarshalJSON() ([]byte, error) { addr := netip.Addr(a) if !addr.IsValid() { - return json.Marshal("") + return nil, nil } return json.Marshal(addr.String()) } @@ -35,6 +35,10 @@ func (a *ListenAddress) UnmarshalJSON(content []byte) error { return nil } +func (a ListenAddress) Build() netip.Addr { + return (netip.Addr)(a) +} + type NetworkList string func (v *NetworkList) UnmarshalJSON(content []byte) error {