Skip to content

Commit

Permalink
Use named return val for IP/if filter
Browse files Browse the repository at this point in the history
This should make it clear that you need to return `true`
to keep it and `false` to exclude.

Relates to pion/webrtc#2958
  • Loading branch information
WofWca authored and Sean-Der committed Nov 26, 2024
1 parent 1c850ea commit 8b8fffd
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ type Agent struct {
udpMux UDPMux
udpMuxSrflx UniversalUDPMux

interfaceFilter func(string) bool
ipFilter func(net.IP) bool
interfaceFilter func(string) (keep bool)
ipFilter func(net.IP) (keep bool)
includeLoopback bool

insecureSkipVerify bool
Expand Down
4 changes: 2 additions & 2 deletions agent_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ type AgentConfig struct {

// InterfaceFilter is a function that you can use in order to whitelist or blacklist
// the interfaces which are used to gather ICE candidates.
InterfaceFilter func(string) bool
InterfaceFilter func(string) (keep bool)

// IPFilter is a function that you can use in order to whitelist or blacklist
// the ips which are used to gather ICE candidates.
IPFilter func(net.IP) bool
IPFilter func(net.IP) (keep bool)

// InsecureSkipVerify controls if self-signed certificates are accepted when connecting
// to TURN servers via TLS or DTLS
Expand Down
6 changes: 3 additions & 3 deletions gather_vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func TestVNetGatherWithInterfaceFilter(t *testing.T) {
t.Run("InterfaceFilter should exclude the interface", func(t *testing.T) {
a, err := NewAgent(&AgentConfig{
Net: nw,
InterfaceFilter: func(interfaceName string) bool {
InterfaceFilter: func(interfaceName string) (keep bool) {
require.Equal(t, "eth0", interfaceName)
return false
},
Expand All @@ -408,7 +408,7 @@ func TestVNetGatherWithInterfaceFilter(t *testing.T) {
t.Run("IPFilter should exclude the IP", func(t *testing.T) {
a, err := NewAgent(&AgentConfig{
Net: nw,
IPFilter: func(ip net.IP) bool {
IPFilter: func(ip net.IP) (keep bool) {
require.Equal(t, net.IP{1, 2, 3, 1}, ip)
return false
},
Expand All @@ -429,7 +429,7 @@ func TestVNetGatherWithInterfaceFilter(t *testing.T) {
t.Run("InterfaceFilter should not exclude the interface", func(t *testing.T) {
a, err := NewAgent(&AgentConfig{
Net: nw,
InterfaceFilter: func(interfaceName string) bool {
InterfaceFilter: func(interfaceName string) (keep bool) {
require.Equal(t, "eth0", interfaceName)
return true
},
Expand Down
4 changes: 2 additions & 2 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func isZeros(ip net.IP) bool {
//nolint:gocognit
func localInterfaces(
n transport.Net,
interfaceFilter func(string) bool,
ipFilter func(net.IP) bool,
interfaceFilter func(string) (keep bool),
ipFilter func(net.IP) (keep bool),
networkTypes []NetworkType,
includeLoopback bool,
) ([]*transport.Interface, []netip.Addr, error) {
Expand Down
2 changes: 1 addition & 1 deletion net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestCreateAddr(t *testing.T) {
require.Equal(t, &net.TCPAddr{IP: ipv6.AsSlice(), Port: port}, createAddr(NetworkTypeTCP6, ipv6, port))
}

func problematicNetworkInterfaces(s string) bool {
func problematicNetworkInterfaces(s string) (keep bool) {
defaultDockerBridgeNetwork := strings.Contains(s, "docker")
customDockerBridgeNetwork := strings.Contains(s, "br-")

Expand Down
8 changes: 4 additions & 4 deletions udp_mux_multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ type UDPMuxFromPortOption interface {
}

type multiUDPMuxFromPortParam struct {
ifFilter func(string) bool
ipFilter func(ip net.IP) bool
ifFilter func(string) (keep bool)
ipFilter func(ip net.IP) (keep bool)
networks []NetworkType
readBufferSize int
writeBufferSize int
Expand All @@ -160,7 +160,7 @@ func (o *udpMuxFromPortOption) apply(p *multiUDPMuxFromPortParam) {
}

// UDPMuxFromPortWithInterfaceFilter set the filter to filter out interfaces that should not be used
func UDPMuxFromPortWithInterfaceFilter(f func(string) bool) UDPMuxFromPortOption {
func UDPMuxFromPortWithInterfaceFilter(f func(string) (keep bool)) UDPMuxFromPortOption {
return &udpMuxFromPortOption{
f: func(p *multiUDPMuxFromPortParam) {
p.ifFilter = f
Expand All @@ -169,7 +169,7 @@ func UDPMuxFromPortWithInterfaceFilter(f func(string) bool) UDPMuxFromPortOption
}

// UDPMuxFromPortWithIPFilter set the filter to filter out IP addresses that should not be used
func UDPMuxFromPortWithIPFilter(f func(ip net.IP) bool) UDPMuxFromPortOption {
func UDPMuxFromPortWithIPFilter(f func(ip net.IP) (keep bool)) UDPMuxFromPortOption {
return &udpMuxFromPortOption{
f: func(p *multiUDPMuxFromPortParam) {
p.ipFilter = f
Expand Down

0 comments on commit 8b8fffd

Please sign in to comment.