Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/rtcconfig/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/pion/stun/v3"
"github.com/pkg/errors"
"golang.org/x/exp/slices"

"github.com/livekit/protocol/logger"
)
Expand Down Expand Up @@ -54,7 +53,7 @@ func (conf *RTCConfig) determineIP() (string, error) {
}

// use local ip instead
addresses, err := GetLocalIPAddresses(false, true, nil)
addresses, err := GetLocalIPAddresses(false, true, nil, nil)
if len(addresses) > 0 {
// prefer IPv4 address if available
for _, addr := range addresses {
Expand All @@ -67,15 +66,15 @@ func (conf *RTCConfig) determineIP() (string, error) {
return "", err
}

func GetLocalIPAddresses(includeLoopback bool, includeV6 bool, preferredInterfaces []string) ([]string, error) {
func GetLocalIPAddresses(includeLoopback bool, includeV6 bool, ifFilter func(string) bool, ipFilter func(net.IP) bool) ([]string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
loopBacks := make([]string, 0)
addresses := make([]string, 0)
for _, iface := range ifaces {
if len(preferredInterfaces) != 0 && !slices.Contains(preferredInterfaces, iface.Name) {
if ifFilter != nil && !ifFilter(iface.Name) {
continue
}

Expand Down Expand Up @@ -104,6 +103,12 @@ func GetLocalIPAddresses(includeLoopback bool, includeV6 bool, preferredInterfac
if ip == nil {
continue
}
if ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
continue
}
if ipFilter != nil && !ipFilter(ip) {
continue
}
if ip.IsLoopback() {
loopBacks = append(loopBacks, ip.String())
} else {
Expand Down
10 changes: 3 additions & 7 deletions pkg/rtcconfig/webrtc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewWebRTCConfig(rtcConf *RTCConfig, development bool) (*WebRTCConfig, error
// force it to the node IPs that the user has set
if !rtcConf.NodeIP.IsEmpty() && (rtcConf.UseExternalIP || !rtcConf.NodeIPAutoGenerated) {
if rtcConf.UseExternalIP {
ips, newFilter, err := getNAT1to1IPsForConf(rtcConf, ipFilter)
ips, newFilter, err := getNAT1to1IPsForConf(rtcConf, ifFilter, ipFilter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -237,12 +237,12 @@ func iceServerForStunServers(servers []string) webrtc.ICEServer {
return iceServer
}

func getNAT1to1IPsForConf(rtcConf *RTCConfig, ipFilter func(net.IP) bool) ([]string, func(net.IP) bool, error) {
func getNAT1to1IPsForConf(rtcConf *RTCConfig, ifFilter func(string) bool, ipFilter func(net.IP) bool) ([]string, func(net.IP) bool, error) {
stunServers := rtcConf.STUNServers
if len(stunServers) == 0 {
stunServers = DefaultStunServers
}
localIPs, err := GetLocalIPAddresses(rtcConf.EnableLoopbackCandidate, true, nil)
localIPs, err := GetLocalIPAddresses(rtcConf.EnableLoopbackCandidate, true, ifFilter, ipFilter)
if err != nil {
return nil, ipFilter, err
}
Expand All @@ -267,10 +267,6 @@ func getNAT1to1IPsForConf(rtcConf *RTCConfig, ipFilter func(net.IP) bool) ([]str
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
for _, ip := range localIPs {
if ipFilter != nil && !ipFilter(net.ParseIP(ip)) {
continue
}

wg.Add(1)
go func(localIP string) {
defer wg.Done()
Expand Down
Loading