Move common option list parsing code to own function.

This commit is contained in:
Joachim Bauch 2023-12-07 13:33:54 +01:00
parent 0f83392e2d
commit e61845b086
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
5 changed files with 123 additions and 20 deletions

57
config.go Normal file
View file

@ -0,0 +1,57 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2023 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @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 <http://www.gnu.org/licenses/>.
*/
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
}

50
config_test.go Normal file
View file

@ -0,0 +1,50 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2023 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @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 <http://www.gnu.org/licenses/>.
*/
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)
}
}

17
hub.go
View file

@ -262,20 +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 {
value, err := config.GetString("geoip-overrides", option)
if err != nil {
var ge goconf.GetError
if errors.As(err, &ge) && ge.Reason == goconf.OptionNotFound {
// Skip options from "default" section.
continue
}
return nil, err
}
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, "/") {

View file

@ -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) {

View file

@ -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)