From e4c321de0235cae98db4261c07515905678cada9 Mon Sep 17 00:00:00 2001 From: cnderrauber Date: Fri, 11 Mar 2022 14:08:41 +0800 Subject: [PATCH] add interface filter config (#502) * add interface filter config * Update config-sample.yaml Co-authored-by: David Zhao * better description Co-authored-by: David Zhao --- config-sample.yaml | 9 +++++++++ pkg/config/config.go | 24 +++++++++++++++--------- pkg/rtc/config.go | 28 +++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/config-sample.yaml b/config-sample.yaml index fc88b9b82..4dfef4da7 100644 --- a/config-sample.yaml +++ b/config-sample.yaml @@ -61,6 +61,15 @@ rtc: # low_quality: 500ms # mid_quality: 1s # high_quality: 1s + # # network interface filter. If the machine has more than one network interface and you'd like it to use or skip specific interfaces + # # both inclusion and exclusion filters can be used together. If neither is defined (default), all interfaces on the machine will be used. + # # If both of them are set, then only interfaces that satisfy both filters will be used. + # # If an interface is present in both lists, include takes precedence. + # interfaces: + # includes: + # - en0 + # excludes: + # - docker0 # when enabled, LiveKit will expose prometheus metrics on :6789/metrics # prometheus_port: 6789 diff --git a/pkg/config/config.go b/pkg/config/config.go index 8333b5de6..52ee14730 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -46,15 +46,16 @@ type Config struct { } type RTCConfig struct { - UDPPort uint32 `yaml:"udp_port,omitempty"` - TCPPort uint32 `yaml:"tcp_port,omitempty"` - ICEPortRangeStart uint32 `yaml:"port_range_start,omitempty"` - ICEPortRangeEnd uint32 `yaml:"port_range_end,omitempty"` - NodeIP string `yaml:"node_ip,omitempty"` - STUNServers []string `yaml:"stun_servers,omitempty"` - TURNServers []TURNServer `yaml:"turn_servers,omitempty"` - UseExternalIP bool `yaml:"use_external_ip"` - UseICELite bool `yaml:"use_ice_lite,omitempty"` + UDPPort uint32 `yaml:"udp_port,omitempty"` + TCPPort uint32 `yaml:"tcp_port,omitempty"` + ICEPortRangeStart uint32 `yaml:"port_range_start,omitempty"` + ICEPortRangeEnd uint32 `yaml:"port_range_end,omitempty"` + NodeIP string `yaml:"node_ip,omitempty"` + STUNServers []string `yaml:"stun_servers,omitempty"` + TURNServers []TURNServer `yaml:"turn_servers,omitempty"` + UseExternalIP bool `yaml:"use_external_ip"` + UseICELite bool `yaml:"use_ice_lite,omitempty"` + Interfaces InterfacesConfig `yaml:"interfaces"` // Number of packets to buffer for NACK PacketBufferSize int `yaml:"packet_buffer_size,omitempty"` @@ -92,6 +93,11 @@ type CongestionControlConfig struct { ProbeMode CongestionControlProbeMode `yaml:"padding_mode,omitempty"` } +type InterfacesConfig struct { + Includes []string `yaml:"includes"` + Excludes []string `yaml:"excludes"` +} + type AudioConfig struct { // minimum level to be considered active, 0-127, where 0 is loudest ActiveLevel uint8 `yaml:"active_level"` diff --git a/pkg/rtc/config.go b/pkg/rtc/config.go index 188b13aa5..a1d9629b1 100644 --- a/pkg/rtc/config.go +++ b/pkg/rtc/config.go @@ -64,7 +64,7 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err LoggerFactory: logging.NewLoggerFactory(logger.GetLogger()), } - if externalIP != "" { + if conf.RTC.UseExternalIP && externalIP != "" { s.SetNAT1To1IPs([]string{externalIP}, webrtc.ICECandidateTypeHost) } @@ -180,6 +180,32 @@ func NewWebRTCConfig(conf *config.Config, externalIP string) (*WebRTCConfig, err s.SetLite(true) } + if len(rtcConf.Interfaces.Includes) != 0 || len(rtcConf.Interfaces.Excludes) != 0 { + includes := rtcConf.Interfaces.Includes + excludes := rtcConf.Interfaces.Excludes + s.SetInterfaceFilter(func(s string) bool { + //filter by include interfaces + if len(includes) > 0 { + for _, iface := range includes { + if iface == s { + return true + } + } + return false + } + + // filter by exclude interfaces + if len(excludes) > 0 { + for _, iface := range excludes { + if iface == s { + return false + } + } + } + return true + }) + } + return &WebRTCConfig{ Configuration: c, SettingEngine: s,