Skip to content

Commit

Permalink
fix: Make sure DNS domain names properly parse as a CDS server URI
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Feb 7, 2024
1 parent 530320c commit 65a9f1b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/stunnerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

func main() {
os.Args[0] = "stunnerd"
var config = flag.StringP("config", "c", "", "Config origin, either a valid IP address or URL to the CDS server, or a file name (overrides: STUNNER_CONFIG_ORIGIN)")
var config = flag.StringP("config", "c", "", "Config origin, either a valid address in the format IP:port, or HTTP URL to the CDS server, or a proper file name URI in the format file://<path-to-config-file> (overrides: STUNNER_CONFIG_ORIGIN)")
var level = flag.StringP("log", "l", "", "Log level (format: <scope>:<level>, overrides: PION_LOG_*, default: all:INFO)")
var id = flag.StringP("id", "i", "", "Id for identifying with the CDS server (format: <namespace>/<name>, overrides: STUNNER_NAMESPACE/STUNNER_NAME, default: <default/stunnerd-hostname>)")
var watch = flag.BoolP("watch", "w", false, "Watch config file for updates (default: false)")
Expand Down
7 changes: 5 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func TestStunnerConfigFileWatcher(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = stunner.WatchConfig(ctx, file, conf)
url := "file://" + file
err = stunner.WatchConfig(ctx, url, conf)
assert.NoError(t, err, "creating config watcher")

// nothing should happen here: wait a bit so that the watcher has comfortable time to start
Expand Down Expand Up @@ -266,7 +267,9 @@ func TestStunnerConfigFileWatcherMultiVersion(t *testing.T) {
log.Debug("init watcher with nonexistent config file")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = stunner.WatchConfig(ctx, file, conf)

url := "file://" + file
err = stunner.WatchConfig(ctx, url, conf)
assert.NoError(t, err, "creating config watcher")

// nothing should happen here: wait a bit so that the watcher has comfortable time to start
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ type Client interface {
fmt.Stringer
}

// New creates a generic config client.
// New creates a generic config client. Origin is either a network address in the form
// "<IP>:<port>" or a proper HTTP/WS URI, in which case a CDS client is returned, or a proper file
// URL "file://<path>/<filename>" in which case a config file watcher is returned.
func New(origin string, id string, logger logging.LoggerFactory) (Client, error) {
u, err := getURI(origin)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestParseURI(t *testing.T) {
// file
u, err := getURI("/tmp/a")
assert.NoError(t, err, "file URI parse")
assert.Equal(t, "", u.Scheme, "file URI scheme")
assert.Equal(t, "http", u.Scheme, "file URI scheme")
assert.Equal(t, "", u.Host, "file URI host")
assert.Equal(t, "/tmp/a", u.Path, "file URI path")

Expand Down
3 changes: 3 additions & 0 deletions pkg/config/client/file_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/fsnotify/fsnotify"
Expand All @@ -25,6 +26,8 @@ type ConfigFileClient struct {
// NewConfigFileClient creates a client that load or watch STUNner configurations from a local
// file.
func NewConfigFileClient(origin, id string, logger logging.LeveledLogger) (Client, error) {
origin = strings.TrimPrefix(origin, "file://") // returns original if there is no "file://" prefix

return &ConfigFileClient{
configFile: origin,
id: id,
Expand Down
15 changes: 9 additions & 6 deletions pkg/config/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"net/url"
"strings"

stnrv1 "github.com/l7mp/stunner/pkg/apis/v1"
)
Expand All @@ -28,14 +29,16 @@ func decodeConfigList(r []byte) ([]*stnrv1.StunnerConfig, error) {

// getURI tries to parse an address or an URL or a file name into an URL.
func getURI(addr string) (*url.URL, error) {
// make sure we have a working HTTP scheme
if !strings.HasPrefix(addr, "http://") && !strings.HasPrefix(addr, "https://") &&
!strings.HasPrefix(addr, "ws://") && !strings.HasPrefix(addr, "wss://") &&
!strings.HasPrefix(addr, "file://") {
addr = "http://" + addr
}

url, err := url.Parse(addr)
if err != nil {
// try to parse with a http scheme as a last resort
u, err2 := url.Parse("http://" + addr)
if err2 != nil {
return nil, err
}
url = u
return nil, err
}
return url, nil
}
Expand Down
1 change: 0 additions & 1 deletion stunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var stunnerTestLoglevel string = "all:ERROR"
// var stunnerTestLoglevel string = stnrv1.DefaultLogLevel
// var stunnerTestLoglevel string = "all:INFO"
// var stunnerTestLoglevel string = "all:TRACE"

// var stunnerTestLoglevel string = "all:TRACE,vnet:INFO,turn:ERROR,turnc:ERROR"

var certPem, keyPem, _ = GenerateSelfSignedKey()
Expand Down

0 comments on commit 65a9f1b

Please sign in to comment.