diff --git a/config.go b/config.go new file mode 100644 index 0000000..c7d316c --- /dev/null +++ b/config.go @@ -0,0 +1,57 @@ +/** + * Standalone signaling server for the Nextcloud Spreed app. + * Copyright (C) 2023 struktur AG + * + * @author Joachim Bauch + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package signaling + +import ( + "errors" + + "github.com/dlintw/goconf" +) + +func GetStringOptions(config *goconf.ConfigFile, section string, ignoreErrors bool) (map[string]string, error) { + options, _ := config.GetOptions(section) + if len(options) == 0 { + return nil, nil + } + + result := make(map[string]string) + for _, option := range options { + value, err := config.GetString(section, option) + if err != nil { + if ignoreErrors { + continue + } + + var ge goconf.GetError + if errors.As(err, &ge) && ge.Reason == goconf.OptionNotFound { + // Skip options from "default" section. + continue + } + + return nil, err + } + + result[option] = value + } + + return result, nil +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..d14ba94 --- /dev/null +++ b/config_test.go @@ -0,0 +1,50 @@ +/** + * Standalone signaling server for the Nextcloud Spreed app. + * Copyright (C) 2023 struktur AG + * + * @author Joachim Bauch + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package signaling + +import ( + "reflect" + "testing" + + "github.com/dlintw/goconf" +) + +func TestStringOptions(t *testing.T) { + expected := map[string]string{ + "one": "1", + "two": "2", + } + config := goconf.NewConfigFile() + for k, v := range expected { + config.AddOption("foo", k, v) + } + config.AddOption("default", "three", "3") + + options, err := GetStringOptions(config, "foo", false) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(expected, options) { + t.Errorf("expected %+v, got %+v", expected, options) + } +} diff --git a/hub.go b/hub.go index d6c3086..7a01e1f 100644 --- a/hub.go +++ b/hub.go @@ -262,9 +262,9 @@ func NewHub(config *goconf.ConfigFile, events AsyncEvents, rpcServer *GrpcServer log.Printf("Not using GeoIP database") } - if options, _ := config.GetOptions("geoip-overrides"); len(options) > 0 { - geoipOverrides = make(map[*net.IPNet]string) - for _, option := range options { + if options, _ := GetStringOptions(config, "geoip-overrides", true); len(options) > 0 { + geoipOverrides = make(map[*net.IPNet]string, len(options)) + for option, value := range options { var ip net.IP var ipNet *net.IPNet if strings.Contains(option, "/") { @@ -290,7 +290,6 @@ func NewHub(config *goconf.ConfigFile, events AsyncEvents, rpcServer *GrpcServer } } - value, _ := config.GetString("geoip-overrides", option) value = strings.ToUpper(strings.TrimSpace(value)) if value == "" { log.Printf("IP %s doesn't have a country assigned, skipping", option) diff --git a/mcu_proxy.go b/mcu_proxy.go index d675bd9..ff0adfd 100644 --- a/mcu_proxy.go +++ b/mcu_proxy.go @@ -1230,14 +1230,18 @@ func NewMcuProxy(config *goconf.ConfigFile, etcdClient *EtcdClient, rpcClients * } func (m *mcuProxy) loadContinentsMap(config *goconf.ConfigFile) error { - options, _ := config.GetOptions("continent-overrides") + options, err := GetStringOptions(config, "continent-overrides", false) + if err != nil { + return err + } + if len(options) == 0 { m.setContinentsMap(nil) return nil } continentsMap := make(map[string][]string) - for _, option := range options { + for option, value := range options { option = strings.ToUpper(strings.TrimSpace(option)) if !IsValidContinent(option) { log.Printf("Ignore unknown continent %s", option) @@ -1245,7 +1249,6 @@ func (m *mcuProxy) loadContinentsMap(config *goconf.ConfigFile) error { } var values []string - value, _ := config.GetString("continent-overrides", option) for _, v := range strings.Split(value, ",") { v = strings.ToUpper(strings.TrimSpace(v)) if !IsValidContinent(v) { diff --git a/proxy/proxy_tokens_static.go b/proxy/proxy_tokens_static.go index a7179d4..ac22c2b 100644 --- a/proxy/proxy_tokens_static.go +++ b/proxy/proxy_tokens_static.go @@ -30,6 +30,7 @@ import ( "github.com/dlintw/goconf" "github.com/golang-jwt/jwt/v4" + signaling "github.com/strukturag/nextcloud-spreed-signaling" ) type tokensStatic struct { @@ -60,10 +61,13 @@ func (t *tokensStatic) Get(id string) (*ProxyToken, error) { } func (t *tokensStatic) load(config *goconf.ConfigFile, ignoreErrors bool) error { + options, err := signaling.GetStringOptions(config, "tokens", ignoreErrors) + if err != nil { + return err + } + tokenKeys := make(map[string]*ProxyToken) - options, _ := config.GetOptions("tokens") - for _, id := range options { - filename, _ := config.GetString("tokens", id) + for id, filename := range options { if filename == "" { if !ignoreErrors { return fmt.Errorf("No filename given for token %s", id)