2020-01-19 12:30:09 +13:00
package config
import (
2021-04-05 08:37:33 +12:00
"encoding/json"
2020-01-27 22:00:49 +13:00
"strconv"
"strings"
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/utils"
2021-06-26 23:44:41 +12:00
"github.com/rs/zerolog/log"
2020-01-19 12:30:09 +13:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2021-04-05 05:37:07 +12:00
"github.com/pion/webrtc/v3"
2020-01-19 12:30:09 +13:00
)
type WebRTC struct {
2020-04-05 19:07:45 +12:00
ICELite bool
2021-04-05 05:37:07 +12:00
ICEServers [ ] webrtc . ICEServer
2020-02-10 20:13:40 +13:00
EphemeralMin uint16
EphemeralMax uint16
NAT1To1IPs [ ] string
2021-12-04 11:54:07 +13:00
TCPMUX int
UDPMUX int
2021-12-12 02:34:28 +13:00
ImplicitControl bool
2020-01-19 12:30:09 +13:00
}
func ( WebRTC ) Init ( cmd * cobra . Command ) error {
2020-01-29 14:27:59 +13:00
cmd . PersistentFlags ( ) . String ( "epr" , "59000-59100" , "limits the pool of ephemeral ports that ICE UDP connections can allocate from" )
2020-01-27 22:00:49 +13:00
if err := viper . BindPFlag ( "epr" , cmd . PersistentFlags ( ) . Lookup ( "epr" ) ) ; err != nil {
return err
}
2020-02-14 16:55:36 +13:00
cmd . PersistentFlags ( ) . StringSlice ( "nat1to1" , [ ] string { } , "sets a list of external IP addresses of 1:1 (D)NAT and a candidate type for which the external IP address is used" )
if err := viper . BindPFlag ( "nat1to1" , cmd . PersistentFlags ( ) . Lookup ( "nat1to1" ) ) ; err != nil {
2020-02-10 20:13:40 +13:00
return err
}
2021-12-04 11:54:07 +13:00
cmd . PersistentFlags ( ) . Int ( "tcpmux" , 0 , "single TCP mux port for all peers" )
if err := viper . BindPFlag ( "tcpmux" , cmd . PersistentFlags ( ) . Lookup ( "tcpmux" ) ) ; err != nil {
return err
}
cmd . PersistentFlags ( ) . Int ( "udpmux" , 0 , "single UDP mux port for all peers" )
if err := viper . BindPFlag ( "udpmux" , cmd . PersistentFlags ( ) . Lookup ( "udpmux" ) ) ; err != nil {
return err
}
2021-06-26 23:44:41 +12:00
cmd . PersistentFlags ( ) . String ( "ipfetch" , "http://checkip.amazonaws.com" , "automatically fetch IP address from given URL when nat1to1 is not present" )
if err := viper . BindPFlag ( "ipfetch" , cmd . PersistentFlags ( ) . Lookup ( "ipfetch" ) ) ; err != nil {
return err
}
2020-04-05 19:50:36 +12:00
cmd . PersistentFlags ( ) . Bool ( "icelite" , false , "configures whether or not the ice agent should be a lite agent" )
2020-04-05 19:07:45 +12:00
if err := viper . BindPFlag ( "icelite" , cmd . PersistentFlags ( ) . Lookup ( "icelite" ) ) ; err != nil {
return err
}
2020-04-05 19:50:36 +12:00
cmd . PersistentFlags ( ) . StringSlice ( "iceserver" , [ ] string { "stun:stun.l.google.com:19302" } , "describes a single STUN and TURN server that can be used by the ICEAgent to establish a connection with a peer" )
2020-04-05 19:07:45 +12:00
if err := viper . BindPFlag ( "iceserver" , cmd . PersistentFlags ( ) . Lookup ( "iceserver" ) ) ; err != nil {
return err
}
2021-04-05 05:37:07 +12:00
cmd . PersistentFlags ( ) . String ( "iceservers" , "" , "describes a single STUN and TURN server that can be used by the ICEAgent to establish a connection with a peer" )
2021-04-05 08:37:33 +12:00
if err := viper . BindPFlag ( "iceservers" , cmd . PersistentFlags ( ) . Lookup ( "iceservers" ) ) ; err != nil {
return err
}
2021-04-05 05:37:07 +12:00
2021-12-12 02:34:28 +13:00
// TODO: Should be moved to session config.
2021-12-12 03:15:29 +13:00
cmd . PersistentFlags ( ) . Bool ( "implicit_control" , false , "if enabled members can gain control implicitly" )
if err := viper . BindPFlag ( "implicit_control" , cmd . PersistentFlags ( ) . Lookup ( "implicit_control" ) ) ; err != nil {
2021-12-12 02:34:28 +13:00
return err
}
2020-01-19 12:30:09 +13:00
return nil
}
func ( s * WebRTC ) Set ( ) {
2021-04-05 08:48:54 +12:00
s . NAT1To1IPs = viper . GetStringSlice ( "nat1to1" )
2021-12-04 11:54:07 +13:00
s . TCPMUX = viper . GetInt ( "tcpmux" )
s . UDPMUX = viper . GetInt ( "udpmux" )
2020-04-05 19:07:45 +12:00
s . ICELite = viper . GetBool ( "icelite" )
2021-04-05 08:48:54 +12:00
s . ICEServers = [ ] webrtc . ICEServer { }
iceServersJson := viper . GetString ( "iceservers" )
if iceServersJson != "" {
err := json . Unmarshal ( [ ] byte ( iceServersJson ) , & s . ICEServers )
2021-04-05 08:37:33 +12:00
if err != nil {
2021-06-26 23:45:22 +12:00
log . Panic ( ) . Err ( err ) . Msg ( "failed to process iceservers" )
2021-04-05 05:37:07 +12:00
}
}
2021-04-05 08:48:54 +12:00
iceServerSlice := viper . GetStringSlice ( "iceserver" )
if len ( iceServerSlice ) > 0 {
s . ICEServers = append ( s . ICEServers , webrtc . ICEServer { URLs : iceServerSlice } )
}
2020-02-10 20:13:40 +13:00
2020-02-14 16:50:53 +13:00
if len ( s . NAT1To1IPs ) == 0 {
2021-06-26 23:44:41 +12:00
ipfetch := viper . GetString ( "ipfetch" )
ip , err := utils . GetIP ( ipfetch )
if err != nil {
log . Panic ( ) . Err ( err ) . Str ( "ipfetch" , ipfetch ) . Msg ( "failed to fetch ip address" )
2020-02-14 16:50:53 +13:00
}
2021-06-26 23:44:41 +12:00
s . NAT1To1IPs = append ( s . NAT1To1IPs , ip )
2020-02-10 20:13:40 +13:00
}
2020-01-27 22:00:49 +13:00
2020-02-10 20:13:40 +13:00
min := uint16 ( 59000 )
max := uint16 ( 59100 )
2020-01-27 22:00:49 +13:00
epr := viper . GetString ( "epr" )
ports := strings . SplitN ( epr , "-" , - 1 )
2020-03-15 11:48:17 +13:00
if len ( ports ) > 1 {
start , err := strconv . ParseUint ( ports [ 0 ] , 10 , 16 )
2020-01-27 22:00:49 +13:00
if err == nil {
2020-02-10 20:13:40 +13:00
min = uint16 ( start )
2020-01-27 22:00:49 +13:00
}
2020-02-10 20:13:40 +13:00
2020-03-15 11:48:17 +13:00
end , err := strconv . ParseUint ( ports [ 1 ] , 10 , 16 )
2020-01-27 22:00:49 +13:00
if err == nil {
2020-02-10 20:13:40 +13:00
max = uint16 ( end )
2020-01-27 22:00:49 +13:00
}
}
2020-02-10 20:13:40 +13:00
if min > max {
s . EphemeralMin = max
s . EphemeralMax = min
} else {
s . EphemeralMin = min
s . EphemeralMax = max
}
2021-12-12 02:34:28 +13:00
// TODO: Should be moved to session config.
2021-12-12 03:15:29 +13:00
s . ImplicitControl = viper . GetBool ( "implicit_control" )
2020-01-19 12:30:09 +13:00
}