Skip to content

Commit

Permalink
Move common option list parsing code to own function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Dec 7, 2023
1 parent 0f83392 commit e61845b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 20 deletions.
57 changes: 57 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2023 struktur AG
*
* @author Joachim Bauch <[email protected]>
*
* @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 changes: 50 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2023 struktur AG
*
* @author Joachim Bauch <[email protected]>
*
* @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 changes: 3 additions & 14 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/") {
Expand Down
9 changes: 6 additions & 3 deletions mcu_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,22 +1230,25 @@ 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)
continue
}

var values []string
value, _ := config.GetString("continent-overrides", option)
for _, v := range strings.Split(value, ",") {
v = strings.ToUpper(strings.TrimSpace(v))
if !IsValidContinent(v) {
Expand Down
10 changes: 7 additions & 3 deletions proxy/proxy_tokens_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e61845b

Please sign in to comment.