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 {
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()
}
}

View File

@ -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
}
}

View File

@ -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)
}

View File

@ -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)
}