diff --git a/adapter/inbound/addition.go b/adapter/inbound/addition.go index 14fc303f..2754ee1e 100644 --- a/adapter/inbound/addition.go +++ b/adapter/inbound/addition.go @@ -38,27 +38,24 @@ func WithSpecialProxy(specialProxy string) Addition { func WithSrcAddr(addr net.Addr) Addition { return func(metadata *C.Metadata) { - if addrPort, err := parseAddr(addr); err == nil { - metadata.SrcIP = addrPort.Addr() - metadata.SrcPort = addrPort.Port() - } + addrPort := parseAddr(addr) + metadata.SrcIP = addrPort.Addr().Unmap() + metadata.SrcPort = addrPort.Port() } } func WithDstAddr(addr net.Addr) Addition { return func(metadata *C.Metadata) { - if addrPort, err := parseAddr(addr); err == nil { - metadata.DstIP = addrPort.Addr() - metadata.DstPort = addrPort.Port() - } + addrPort := parseAddr(addr) + metadata.DstIP = addrPort.Addr().Unmap() + metadata.DstPort = addrPort.Port() } } func WithInAddr(addr net.Addr) Addition { return func(metadata *C.Metadata) { - if addrPort, err := parseAddr(addr); err == nil { - metadata.InIP = addrPort.Addr() - metadata.InPort = addrPort.Port() - } + addrPort := parseAddr(addr) + metadata.InIP = addrPort.Addr().Unmap() + metadata.InPort = addrPort.Port() } } diff --git a/adapter/inbound/auth.go b/adapter/inbound/auth.go index 724b5b7a..93c56c99 100644 --- a/adapter/inbound/auth.go +++ b/adapter/inbound/auth.go @@ -16,9 +16,9 @@ func SkipAuthPrefixes() []netip.Prefix { } func SkipAuthRemoteAddr(addr net.Addr) bool { - if addrPort, err := parseAddr(addr); err == nil { + if addrPort := parseAddr(addr); addrPort.IsValid() { for _, prefix := range skipAuthPrefixes { - if prefix.Contains(addrPort.Addr()) { + if prefix.Contains(addrPort.Addr().Unmap()) { return true } } diff --git a/adapter/inbound/util.go b/adapter/inbound/util.go index 32ca9f05..e4d2630d 100644 --- a/adapter/inbound/util.go +++ b/adapter/inbound/util.go @@ -1,7 +1,6 @@ package inbound import ( - "errors" "net" "net/http" "net/netip" @@ -63,25 +62,23 @@ func parseHTTPAddr(request *http.Request) *C.Metadata { return metadata } -func parseAddr(addr net.Addr) (netip.AddrPort, error) { +func parseAddr(addr net.Addr) netip.AddrPort { // Filter when net.Addr interface is nil if addr == nil { - return netip.AddrPort{}, errors.New("nil addr") + return netip.AddrPort{} } - if rawAddr, ok := addr.(interface{ RawAddr() net.Addr }); ok { - if addrPort, err := parseAddr(rawAddr.RawAddr()); err == nil { - return addrPort, nil + if addr, ok := addr.(interface{ RawAddr() net.Addr }); ok { + if rawAddr := addr.RawAddr(); rawAddr != nil { + return parseAddr(rawAddr) } } if addr, ok := addr.(interface{ AddrPort() netip.AddrPort }); ok { - if addrPort := addr.AddrPort(); addrPort.IsValid() { - return addrPort, nil - } + return addr.AddrPort() } addrStr := addr.String() host, port, err := net.SplitHostPort(addrStr) if err != nil { - return netip.AddrPort{}, err + return netip.AddrPort{} } var uint16Port uint16 @@ -89,6 +86,6 @@ func parseAddr(addr net.Addr) (netip.AddrPort, error) { uint16Port = uint16(port) } - ip, err := netip.ParseAddr(host) - return netip.AddrPortFrom(ip, uint16Port), err + ip, _ := netip.ParseAddr(host) + return netip.AddrPortFrom(ip, uint16Port) } diff --git a/listener/sing/sing.go b/listener/sing/sing.go index 301dc783..d03bebb1 100644 --- a/listener/sing/sing.go +++ b/listener/sing/sing.go @@ -93,12 +93,8 @@ func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, meta NetWork: C.TCP, Type: h.Type, Host: metadata.Destination.Fqdn, - DstIP: metadata.Destination.Addr, - DstPort: metadata.Destination.Port, - SrcIP: metadata.Source.Addr, - SrcPort: metadata.Source.Port, } - additions := combineAdditions(ctx, h.Additions, inbound.WithInAddr(conn.LocalAddr())) + additions := combineAdditions(ctx, h.Additions, inbound.WithDstAddr(metadata.Destination), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr())) for _, addition := range additions { addition.Apply(cMetadata) } @@ -160,12 +156,8 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network. NetWork: C.UDP, Type: h.Type, Host: dest.Fqdn, - DstIP: dest.Addr, - DstPort: dest.Port, - SrcIP: metadata.Source.Addr, - SrcPort: metadata.Source.Port, } - additions := combineAdditions(ctx, h.Additions, inbound.WithInAddr(conn.LocalAddr())) + additions := combineAdditions(ctx, h.Additions, inbound.WithDstAddr(dest), inbound.WithSrcAddr(metadata.Source), inbound.WithInAddr(conn.LocalAddr())) for _, addition := range additions { addition.Apply(cMetadata) }