Add ipfilter to exclude ip from candidates gather (#1052)

This commit is contained in:
cnderrauber
2022-09-29 14:40:38 +08:00
committed by GitHub
parent 9c8d9fca0c
commit 4b630d2156
5 changed files with 73 additions and 18 deletions
+43
View File
@@ -227,6 +227,49 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err
})
}
if len(rtcConf.IPs.Includes) != 0 || len(rtcConf.IPs.Excludes) != 0 {
var ipnets [2][]*net.IPNet
for i, ips := range [][]string{rtcConf.IPs.Includes, rtcConf.IPs.Excludes} {
ipnets[i], err = func(fromIPs []string) ([]*net.IPNet, error) {
var toNets []*net.IPNet
for _, ip := range fromIPs {
_, ipnet, err := net.ParseCIDR(ip)
if err != nil {
return nil, err
}
toNets = append(toNets, ipnet)
}
return toNets, nil
}(ips)
if err != nil {
return nil, err
}
}
includes, excludes := ipnets[0], ipnets[1]
s.SetIPFilter(func(ip net.IP) bool {
if len(includes) > 0 {
for _, ipn := range includes {
if ipn.Contains(ip) {
return true
}
}
return false
}
if len(excludes) > 0 {
for _, ipn := range excludes {
if ipn.Contains(ip) {
return false
}
}
}
return true
})
}
return &WebRTCConfig{
Configuration: c,
SettingEngine: s,