mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2024-05-17 21:16:33 +02:00
Move common option list parsing code to own function.
This commit is contained in:
parent
0f83392e2d
commit
e61845b086
57
config.go
Normal file
57
config.go
Normal 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
50
config_test.go
Normal 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
17
hub.go
|
@ -262,20 +262,9 @@ func NewHub(config *goconf.ConfigFile, events AsyncEvents, rpcServer *GrpcServer
|
||||||
log.Printf("Not using GeoIP database")
|
log.Printf("Not using GeoIP database")
|
||||||
}
|
}
|
||||||
|
|
||||||
if options, _ := config.GetOptions("geoip-overrides"); len(options) > 0 {
|
if options, _ := GetStringOptions(config, "geoip-overrides", true); len(options) > 0 {
|
||||||
geoipOverrides = make(map[*net.IPNet]string)
|
geoipOverrides = make(map[*net.IPNet]string, len(options))
|
||||||
for _, option := range options {
|
for option, value := 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
|
|
||||||
}
|
|
||||||
|
|
||||||
var ip net.IP
|
var ip net.IP
|
||||||
var ipNet *net.IPNet
|
var ipNet *net.IPNet
|
||||||
if strings.Contains(option, "/") {
|
if strings.Contains(option, "/") {
|
||||||
|
|
|
@ -1230,14 +1230,18 @@ func NewMcuProxy(config *goconf.ConfigFile, etcdClient *EtcdClient, rpcClients *
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mcuProxy) loadContinentsMap(config *goconf.ConfigFile) error {
|
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 {
|
if len(options) == 0 {
|
||||||
m.setContinentsMap(nil)
|
m.setContinentsMap(nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
continentsMap := make(map[string][]string)
|
continentsMap := make(map[string][]string)
|
||||||
for _, option := range options {
|
for option, value := range options {
|
||||||
option = strings.ToUpper(strings.TrimSpace(option))
|
option = strings.ToUpper(strings.TrimSpace(option))
|
||||||
if !IsValidContinent(option) {
|
if !IsValidContinent(option) {
|
||||||
log.Printf("Ignore unknown continent %s", option)
|
log.Printf("Ignore unknown continent %s", option)
|
||||||
|
@ -1245,7 +1249,6 @@ func (m *mcuProxy) loadContinentsMap(config *goconf.ConfigFile) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
var values []string
|
var values []string
|
||||||
value, _ := config.GetString("continent-overrides", option)
|
|
||||||
for _, v := range strings.Split(value, ",") {
|
for _, v := range strings.Split(value, ",") {
|
||||||
v = strings.ToUpper(strings.TrimSpace(v))
|
v = strings.ToUpper(strings.TrimSpace(v))
|
||||||
if !IsValidContinent(v) {
|
if !IsValidContinent(v) {
|
||||||
|
|
|
@ -30,6 +30,7 @@ import (
|
||||||
|
|
||||||
"github.com/dlintw/goconf"
|
"github.com/dlintw/goconf"
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
signaling "github.com/strukturag/nextcloud-spreed-signaling"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tokensStatic struct {
|
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 {
|
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)
|
tokenKeys := make(map[string]*ProxyToken)
|
||||||
options, _ := config.GetOptions("tokens")
|
for id, filename := range options {
|
||||||
for _, id := range options {
|
|
||||||
filename, _ := config.GetString("tokens", id)
|
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
if !ignoreErrors {
|
if !ignoreErrors {
|
||||||
return fmt.Errorf("No filename given for token %s", id)
|
return fmt.Errorf("No filename given for token %s", id)
|
||||||
|
|
Loading…
Reference in a new issue