From 8b1458d2329ca161ab65f16da7834057a5f14072 Mon Sep 17 00:00:00 2001 From: devgianlu Date: Fri, 20 Dec 2024 09:43:49 +0100 Subject: [PATCH] feat: support specifying Zeroconf port Closes #154 --- README.md | 1 + cmd/daemon/main.go | 3 ++- config_schema.json | 5 +++++ zeroconf/zeroconf.go | 4 ++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b24c58b..5c29784 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ An example configuration (not required) looks like this: ```yaml zeroconf_enabled: false # Whether to keep the device discoverable at all times, even if authenticated via other means +zeroconf_port: 0 # The port to use for Zeroconf, 0 for random credentials: type: zeroconf zeroconf: diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index 844a1f2..b327420 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -214,7 +214,7 @@ func (app *App) withAppPlayer(ctx context.Context, appPlayerFunc func(context.Co } // start zeroconf server and dispatch - z, err := zeroconf.NewZeroconf(app.cfg.DeviceName, app.deviceId, app.deviceType) + z, err := zeroconf.NewZeroconf(app.cfg.ZeroconfPort, app.cfg.DeviceName, app.deviceId, app.deviceType) if err != nil { return fmt.Errorf("failed initializing zeroconf: %w", err) } @@ -359,6 +359,7 @@ type Config struct { NormalisationPregain float32 `koanf:"normalisation_pregain"` ExternalVolume bool `koanf:"external_volume"` ZeroconfEnabled bool `koanf:"zeroconf_enabled"` + ZeroconfPort int `koanf:"zeroconf_port"` DisableAutoplay bool `koanf:"disable_autoplay"` Server struct { Enabled bool `koanf:"enabled"` diff --git a/config_schema.json b/config_schema.json index 3b9f6f5..887c744 100644 --- a/config_schema.json +++ b/config_schema.json @@ -134,6 +134,11 @@ "description": "Whether Zeroconf discovery should always be enabled (even when logging in with credentials)", "default": false }, + "zeroconf_port": { + "type": "integer", + "description": "The port to use for the Zeroconf service (empty for random)", + "default": 0 + }, "credentials": { "type": "object", "properties": { diff --git a/zeroconf/zeroconf.go b/zeroconf/zeroconf.go index 163f6f5..1877bd9 100644 --- a/zeroconf/zeroconf.go +++ b/zeroconf/zeroconf.go @@ -45,7 +45,7 @@ type NewUserRequest struct { result chan bool } -func NewZeroconf(deviceName, deviceId string, deviceType devicespb.DeviceType) (_ *Zeroconf, err error) { +func NewZeroconf(port int, deviceName, deviceId string, deviceType devicespb.DeviceType) (_ *Zeroconf, err error) { z := &Zeroconf{deviceId: deviceId, deviceName: deviceName, deviceType: deviceType} z.reqsChan = make(chan NewUserRequest) @@ -54,7 +54,7 @@ func NewZeroconf(deviceName, deviceId string, deviceType devicespb.DeviceType) ( return nil, fmt.Errorf("failed initializing diffiehellman: %w", err) } - z.listener, err = net.Listen("tcp", "0.0.0.0:0") + z.listener, err = net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port)) if err != nil { return nil, fmt.Errorf("failed starting zeroconf listener: %w", err) }