Skip to content

Commit

Permalink
Fix #104: add alternate hostname to server connection config
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Apr 19, 2023
1 parent 5782c97 commit ec3cb0c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [#117](https://github.com/dweymouth/supersonic/issues/117) Add (optional) system tray menu and close to tray support
- [#55](https://github.com/dweymouth/supersonic/issues/55) Show disc number and disc count for multi-disc albums
- [#115](https://github.com/dweymouth/supersonic/issues/115) Add search bar to artist, genres, and playlists pages
- [#104](https://github.com/dweymouth/supersonic/issues/104) Add alternate (e.g. external) hostname to server connection config

### Fixes
- **todo-commithash** Don't show update available prompt if the found version is the same as the running app version
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Screenshots of Supersonic running against the Navidrome [demo server](https://ww
<a href="https://raw.githubusercontent.com/dweymouth/supersonic/main/res/screenshots/FavoriteSongsView.png"><img src="https://raw.github.com/dweymouth/supersonic/main/res/screenshots/FavoriteSongsView.png" width="49.5%"/></a>

## Features
* [x] Fast, lightweight, native UI
* [x] Fast, lightweight, native UI with infinite scrolling
* [x] High-quality gapless audio playback powered by MPV, with optional audio exclusive mode
* [x] ReplayGain support (depends on files being tagged on server)
* [x] Infinite scrolling
* [x] Scrobble plays to server, with configurable criteria
* [x] Primary and alternate server hostnames, e.g. for internal and external URLs
* [x] Browse by albums, artists, genres, playlists
* [x] Album and playlist views with tracklist and cover image
* [x] Artist view with biography, image, similar artists, and discography
Expand Down
27 changes: 15 additions & 12 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import (
"github.com/pelletier/go-toml"
)

type ServerConnection struct {
Hostname string
AltHostname string
Username string
LegacyAuth bool
}

type ServerConfig struct {
ID uuid.UUID
Nickname string
Hostname string
Username string
LegacyAuth bool
Default bool
ServerConnection
ID uuid.UUID
Nickname string
Default bool
}

type AppConfig struct {
Expand Down Expand Up @@ -172,13 +177,11 @@ func (c *Config) SetDefaultServer(serverID uuid.UUID) {
}
}

func (c *Config) AddServer(nickname, hostname, username string, legacyAuth bool) *ServerConfig {
func (c *Config) AddServer(nickname string, connection ServerConnection) *ServerConfig {
s := &ServerConfig{
ID: uuid.New(),
Nickname: nickname,
Hostname: hostname,
Username: username,
LegacyAuth: legacyAuth,
ID: uuid.New(),
Nickname: nickname,
ServerConnection: connection,
}
c.Servers = append(c.Servers, s)
return s
Expand Down
59 changes: 47 additions & 12 deletions backend/servermanager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend

import (
"context"
"errors"
"net/http"
"time"
Expand All @@ -26,7 +27,7 @@ func NewServerManager(appName string) *ServerManager {
}

func (s *ServerManager) ConnectToServer(conf *ServerConfig, password string) error {
cli, err := s.testConnectionAndCreateClient(conf.Hostname, conf.Username, password, conf.LegacyAuth)
cli, err := s.testConnectionAndCreateClient(conf.ServerConnection, password)
if err != nil {
return err
}
Expand All @@ -39,12 +40,12 @@ func (s *ServerManager) ConnectToServer(conf *ServerConfig, password string) err
}

func (s *ServerManager) TestConnectionAndAuth(
hostname, username, password string, legacyAuth bool, timeout time.Duration,
connection ServerConnection, password string, timeout time.Duration,
) error {
err := ErrUnreachable
done := make(chan bool)
go func() {
_, err = s.testConnectionAndCreateClient(hostname, username, password, legacyAuth)
_, err = s.testConnectionAndCreateClient(connection, password)
close(done)
}()
t := time.NewTimer(timeout)
Expand All @@ -57,21 +58,55 @@ func (s *ServerManager) TestConnectionAndAuth(
}
}

func (s *ServerManager) testConnectionAndCreateClient(hostname, username, password string, legacyAuth bool) (*subsonic.Client, error) {
func (s *ServerManager) testConnectionAndCreateClient(connection ServerConnection, password string) (*subsonic.Client, error) {
cli, err := s.connect(connection, password)
if err != nil {
return nil, err
}
if err := cli.Authenticate(password); err != nil {
return nil, err
}
return cli, nil
}

func (s *ServerManager) connect(connection ServerConnection, password string) (*subsonic.Client, error) {
cli := &subsonic.Client{
Client: &http.Client{},
BaseUrl: hostname,
User: username,
PasswordAuth: legacyAuth,
BaseUrl: connection.Hostname,
User: connection.Username,
PasswordAuth: connection.LegacyAuth,
ClientName: "supersonic",
}
if !cli.Ping() {
return nil, ErrUnreachable
altCli := &subsonic.Client{
Client: &http.Client{},
BaseUrl: connection.AltHostname,
User: connection.Username,
PasswordAuth: connection.LegacyAuth,
ClientName: "supersonic",
}
if err := cli.Authenticate(password); err != nil {
return nil, err
pingChan := make(chan bool, 2) // false for primary hostname, true for alternate
pingFunc := func(delay time.Duration, cli *subsonic.Client, val bool) {
<-time.After(delay)
if cli.Ping() {
pingChan <- val
}
}
go pingFunc(0, cli, false)
if connection.AltHostname != "" {
go pingFunc(333*time.Millisecond, altCli, true) // give primary hostname ping a head start
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
select {
case <-ctx.Done():
return nil, ErrUnreachable
case altPing := <-pingChan:
if altPing {
return altCli, nil
}
return cli, nil
}
return cli, nil
}

func (s *ServerManager) Logout() {
Expand Down
21 changes: 17 additions & 4 deletions ui/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ func (m *Controller) PromptForFirstServer() {
// connection is good
pop.Hide()
m.doModalClosed()
server := m.App.Config.AddServer(d.Nickname, d.Host, d.Username, d.LegacyAuth)
conn := backend.ServerConnection{
Hostname: d.Host,
AltHostname: d.AltHost,
Username: d.Username,
LegacyAuth: d.LegacyAuth,
}
server := m.App.Config.AddServer(d.Nickname, conn)
if err := m.App.ServerManager.SetServerPassword(server, d.Password); err != nil {
log.Printf("error setting keyring credentials: %v", err)
// TODO: handle?
Expand Down Expand Up @@ -254,7 +260,7 @@ func (m *Controller) PromptForLoginAndConnect() {
d.DisableSubmit()
d.SetInfoText("Testing connection...")
go func() {
err := m.App.ServerManager.TestConnectionAndAuth(server.Hostname, server.Username, password, server.LegacyAuth, 5*time.Second)
err := m.App.ServerManager.TestConnectionAndAuth(server.ServerConnection, password, 5*time.Second)
if err == backend.ErrUnreachable {
d.SetErrorText("Server unreachable")
} else if err != nil {
Expand All @@ -278,6 +284,7 @@ func (m *Controller) PromptForLoginAndConnect() {
// connection is good
editPop.Hide()
server.Hostname = editD.Host
server.AltHostname = editD.AltHost
server.Nickname = editD.Nickname
server.Username = editD.Username
server.LegacyAuth = editD.LegacyAuth
Expand Down Expand Up @@ -343,7 +350,7 @@ func (c *Controller) trySetPasswordAndConnectToServer(server *backend.ServerConf
}

func (c *Controller) tryConnectToServer(server *backend.ServerConfig, password string) error {
if err := c.App.ServerManager.TestConnectionAndAuth(server.Hostname, server.Username, password, server.LegacyAuth, 10*time.Second); err != nil {
if err := c.App.ServerManager.TestConnectionAndAuth(server.ServerConnection, password, 10*time.Second); err != nil {
return err
}
if err := c.App.ServerManager.ConnectToServer(server, password); err != nil {
Expand All @@ -355,7 +362,13 @@ func (c *Controller) tryConnectToServer(server *backend.ServerConfig, password s

func (c *Controller) testConnectionAndUpdateDialogText(dlg *dialogs.AddEditServerDialog) bool {
dlg.SetInfoText("Testing connection...")
err := c.App.ServerManager.TestConnectionAndAuth(dlg.Host, dlg.Username, dlg.Password, dlg.LegacyAuth, 5*time.Second)
conn := backend.ServerConnection{
Hostname: dlg.Host,
AltHostname: dlg.AltHost,
Username: dlg.Username,
LegacyAuth: dlg.LegacyAuth,
}
err := c.App.ServerManager.TestConnectionAndAuth(conn, dlg.Password, 5*time.Second)
if err == backend.ErrUnreachable {
dlg.SetErrorText("Could not reach server (wrong hostname?)")
return false
Expand Down
8 changes: 7 additions & 1 deletion ui/dialogs/addeditserverdialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type AddEditServerDialog struct {

Nickname string
Host string
AltHost string
Username string
Password string
LegacyAuth bool
Expand All @@ -34,6 +35,7 @@ func NewAddEditServerDialog(title string, prefillServer *backend.ServerConfig) *
if prefillServer != nil {
a.Nickname = prefillServer.Nickname
a.Host = prefillServer.Hostname
a.AltHost = prefillServer.AltHostname
a.Username = prefillServer.Username
a.LegacyAuth = prefillServer.LegacyAuth
}
Expand All @@ -44,6 +46,8 @@ func NewAddEditServerDialog(title string, prefillServer *backend.ServerConfig) *
nickField.SetPlaceHolder("My Server")
hostField := widget.NewEntryWithData(binding.BindString(&a.Host))
hostField.SetPlaceHolder("http://localhost:4533")
altHostField := widget.NewEntryWithData(binding.BindString(&a.AltHost))
altHostField.SetPlaceHolder("(optional) https://my-external-domain.net/music")
userField := widget.NewEntryWithData(binding.BindString(&a.Username))
passField := widget.NewPasswordEntry()
a.submitBtn = widget.NewButton("Enter", func() {
Expand All @@ -64,6 +68,8 @@ func NewAddEditServerDialog(title string, prefillServer *backend.ServerConfig) *
nickField,
widget.NewLabel("Hostname"),
hostField,
widget.NewLabel("Alt. Hostname"),
altHostField,
widget.NewLabel("Username"),
userField,
widget.NewLabel("Password"),
Expand Down Expand Up @@ -111,7 +117,7 @@ func (a *AddEditServerDialog) doSetPromptText(text string, color fyne.ThemeColor

func (a *AddEditServerDialog) MinSize() fyne.Size {
a.ExtendBaseWidget(a)
return fyne.NewSize(450, a.container.MinSize().Height)
return fyne.NewSize(475, a.container.MinSize().Height)
}

func (a *AddEditServerDialog) CreateRenderer() fyne.WidgetRenderer {
Expand Down

0 comments on commit ec3cb0c

Please sign in to comment.