mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
Use net.JoinHostPort and net.SplitHostPort for IPv6-safe host:port handling (#5836)
This commit is contained in:
@@ -13,6 +13,54 @@ import (
|
||||
var logger = log.NewFromLogrus(logrus.StandardLogger())
|
||||
var flowLogger = netflow.NewManager(nil, []byte{}, nil).GetLogger()
|
||||
|
||||
func TestConnKey_String(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key ConnKey
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
name: "IPv4",
|
||||
key: ConnKey{
|
||||
SrcIP: netip.MustParseAddr("192.168.1.1"),
|
||||
DstIP: netip.MustParseAddr("10.0.0.1"),
|
||||
SrcPort: 12345,
|
||||
DstPort: 80,
|
||||
},
|
||||
expect: "192.168.1.1:12345 → 10.0.0.1:80",
|
||||
},
|
||||
{
|
||||
name: "IPv6",
|
||||
key: ConnKey{
|
||||
SrcIP: netip.MustParseAddr("2001:db8::1"),
|
||||
DstIP: netip.MustParseAddr("2001:db8::2"),
|
||||
SrcPort: 54321,
|
||||
DstPort: 443,
|
||||
},
|
||||
expect: "[2001:db8::1]:54321 → [2001:db8::2]:443",
|
||||
},
|
||||
{
|
||||
name: "IPv4-mapped IPv6 unmaps",
|
||||
key: ConnKey{
|
||||
SrcIP: netip.MustParseAddr("::ffff:10.0.0.1"),
|
||||
DstIP: netip.MustParseAddr("::ffff:10.0.0.2"),
|
||||
SrcPort: 1000,
|
||||
DstPort: 2000,
|
||||
},
|
||||
expect: "10.0.0.1:1000 → 10.0.0.2:2000",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := tc.key.String()
|
||||
if got != tc.expect {
|
||||
t.Errorf("got %q, want %q", got, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Memory pressure tests
|
||||
func BenchmarkMemoryPressure(b *testing.B) {
|
||||
b.Run("TCPHighLoad", func(b *testing.B) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -137,12 +138,12 @@ func (info ICMPInfo) parseOriginalPacket() string {
|
||||
case nftypes.TCP:
|
||||
srcPort := uint16(transportData[0])<<8 | uint16(transportData[1])
|
||||
dstPort := uint16(transportData[2])<<8 | uint16(transportData[3])
|
||||
return fmt.Sprintf("TCP %s:%d → %s:%d", srcIP, srcPort, dstIP, dstPort)
|
||||
return "TCP " + net.JoinHostPort(srcIP.String(), strconv.Itoa(int(srcPort))) + " → " + net.JoinHostPort(dstIP.String(), strconv.Itoa(int(dstPort)))
|
||||
|
||||
case nftypes.UDP:
|
||||
srcPort := uint16(transportData[0])<<8 | uint16(transportData[1])
|
||||
dstPort := uint16(transportData[2])<<8 | uint16(transportData[3])
|
||||
return fmt.Sprintf("UDP %s:%d → %s:%d", srcIP, srcPort, dstIP, dstPort)
|
||||
return "UDP " + net.JoinHostPort(srcIP.String(), strconv.Itoa(int(srcPort))) + " → " + net.JoinHostPort(dstIP.String(), strconv.Itoa(int(dstPort)))
|
||||
|
||||
case nftypes.ICMP:
|
||||
icmpType := transportData[0]
|
||||
|
||||
@@ -5,6 +5,42 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestICMPConnKey_String(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key ICMPConnKey
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
name: "IPv4",
|
||||
key: ICMPConnKey{
|
||||
SrcIP: netip.MustParseAddr("192.168.1.1"),
|
||||
DstIP: netip.MustParseAddr("10.0.0.1"),
|
||||
ID: 1234,
|
||||
},
|
||||
expect: "192.168.1.1 → 10.0.0.1 (id 1234)",
|
||||
},
|
||||
{
|
||||
name: "IPv6",
|
||||
key: ICMPConnKey{
|
||||
SrcIP: netip.MustParseAddr("2001:db8::1"),
|
||||
DstIP: netip.MustParseAddr("2001:db8::2"),
|
||||
ID: 5678,
|
||||
},
|
||||
expect: "2001:db8::1 → 2001:db8::2 (id 5678)",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := tc.key.String()
|
||||
if got != tc.expect {
|
||||
t.Errorf("got %q, want %q", got, tc.expect)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkICMPTracker(b *testing.B) {
|
||||
b.Run("TrackOutbound", func(b *testing.B) {
|
||||
tracker := NewICMPTracker(DefaultICMPTimeout, logger, flowLogger)
|
||||
|
||||
@@ -2,7 +2,9 @@ package uspfilter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/gopacket"
|
||||
@@ -443,7 +445,7 @@ func (m *Manager) handleRouteACLs(trace *PacketTrace, d *decoder, srcIP, dstIP n
|
||||
trace.AddResult(StageRouteACL, msg, allowed)
|
||||
|
||||
if allowed && m.forwarder.Load() != nil {
|
||||
m.addForwardingResult(trace, "proxy-remote", fmt.Sprintf("%s:%d", dstIP, dstPort), true)
|
||||
m.addForwardingResult(trace, "proxy-remote", net.JoinHostPort(dstIP.String(), strconv.Itoa(int(dstPort))), true)
|
||||
}
|
||||
|
||||
trace.AddResult(StageCompleted, msgProcessingCompleted, allowed)
|
||||
|
||||
Reference in New Issue
Block a user