fix: unmap 4in6 ip

This commit is contained in:
gVisor bot 2023-10-11 18:17:39 +08:00
parent 1a232b7504
commit 9bd516bc62
4 changed files with 22 additions and 36 deletions

View file

@ -38,27 +38,24 @@ func WithSpecialProxy(specialProxy string) Addition {
func WithSrcAddr(addr net.Addr) Addition { func WithSrcAddr(addr net.Addr) Addition {
return func(metadata *C.Metadata) { return func(metadata *C.Metadata) {
if addrPort, err := parseAddr(addr); err == nil { addrPort := parseAddr(addr)
metadata.SrcIP = addrPort.Addr() metadata.SrcIP = addrPort.Addr().Unmap()
metadata.SrcPort = addrPort.Port() metadata.SrcPort = addrPort.Port()
} }
} }
}
func WithDstAddr(addr net.Addr) Addition { func WithDstAddr(addr net.Addr) Addition {
return func(metadata *C.Metadata) { return func(metadata *C.Metadata) {
if addrPort, err := parseAddr(addr); err == nil { addrPort := parseAddr(addr)
metadata.DstIP = addrPort.Addr() metadata.DstIP = addrPort.Addr().Unmap()
metadata.DstPort = addrPort.Port() metadata.DstPort = addrPort.Port()
} }
} }
}
func WithInAddr(addr net.Addr) Addition { func WithInAddr(addr net.Addr) Addition {
return func(metadata *C.Metadata) { return func(metadata *C.Metadata) {
if addrPort, err := parseAddr(addr); err == nil { addrPort := parseAddr(addr)
metadata.InIP = addrPort.Addr() metadata.InIP = addrPort.Addr().Unmap()
metadata.InPort = addrPort.Port() metadata.InPort = addrPort.Port()
} }
} }
}

View file

@ -16,9 +16,9 @@ func SkipAuthPrefixes() []netip.Prefix {
} }
func SkipAuthRemoteAddr(addr net.Addr) bool { func SkipAuthRemoteAddr(addr net.Addr) bool {
if addrPort, err := parseAddr(addr); err == nil { if addrPort := parseAddr(addr); addrPort.IsValid() {
for _, prefix := range skipAuthPrefixes { for _, prefix := range skipAuthPrefixes {
if prefix.Contains(addrPort.Addr()) { if prefix.Contains(addrPort.Addr().Unmap()) {
return true return true
} }
} }

View file

@ -1,7 +1,6 @@
package inbound package inbound
import ( import (
"errors"
"net" "net"
"net/http" "net/http"
"net/netip" "net/netip"
@ -63,25 +62,23 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
return 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 // Filter when net.Addr interface is nil
if addr == nil { if addr == nil {
return netip.AddrPort{}, errors.New("nil addr") return netip.AddrPort{}
} }
if rawAddr, ok := addr.(interface{ RawAddr() net.Addr }); ok { if addr, ok := addr.(interface{ RawAddr() net.Addr }); ok {
if addrPort, err := parseAddr(rawAddr.RawAddr()); err == nil { if rawAddr := addr.RawAddr(); rawAddr != nil {
return addrPort, nil return parseAddr(rawAddr)
} }
} }
if addr, ok := addr.(interface{ AddrPort() netip.AddrPort }); ok { if addr, ok := addr.(interface{ AddrPort() netip.AddrPort }); ok {
if addrPort := addr.AddrPort(); addrPort.IsValid() { return addr.AddrPort()
return addrPort, nil
}
} }
addrStr := addr.String() addrStr := addr.String()
host, port, err := net.SplitHostPort(addrStr) host, port, err := net.SplitHostPort(addrStr)
if err != nil { if err != nil {
return netip.AddrPort{}, err return netip.AddrPort{}
} }
var uint16Port uint16 var uint16Port uint16
@ -89,6 +86,6 @@ func parseAddr(addr net.Addr) (netip.AddrPort, error) {
uint16Port = uint16(port) uint16Port = uint16(port)
} }
ip, err := netip.ParseAddr(host) ip, _ := netip.ParseAddr(host)
return netip.AddrPortFrom(ip, uint16Port), err return netip.AddrPortFrom(ip, uint16Port)
} }

View file

@ -93,12 +93,8 @@ func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, meta
NetWork: C.TCP, NetWork: C.TCP,
Type: h.Type, Type: h.Type,
Host: metadata.Destination.Fqdn, 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 { for _, addition := range additions {
addition.Apply(cMetadata) addition.Apply(cMetadata)
} }
@ -160,12 +156,8 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
NetWork: C.UDP, NetWork: C.UDP,
Type: h.Type, Type: h.Type,
Host: dest.Fqdn, 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 { for _, addition := range additions {
addition.Apply(cMetadata) addition.Apply(cMetadata)
} }