-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmain.go
More file actions
594 lines (492 loc) · 17.5 KB
/
main.go
File metadata and controls
594 lines (492 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package main
import (
"context"
"encoding/hex"
"errors"
"fmt"
"math"
"net/http"
"os"
"path/filepath"
"strings"
"time"
librespot "github.com/devgianlu/go-librespot"
"github.com/devgianlu/go-librespot/mpris"
"github.com/devgianlu/go-librespot/apresolve"
"github.com/devgianlu/go-librespot/player"
devicespb "github.com/devgianlu/go-librespot/proto/spotify/connectstate/devices"
"github.com/devgianlu/go-librespot/session"
"github.com/devgianlu/go-librespot/zeroconf"
"github.com/gofrs/flock"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/rand"
"github.com/knadh/koanf/v2"
flag "github.com/spf13/pflag"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
)
var errAlreadyRunning = errors.New("go-librespot is already running")
type App struct {
log librespot.Logger
cfg *Config
client *http.Client
resolver *apresolve.ApResolver
deviceId string
deviceType devicespb.DeviceType
clientToken string
state librespot.AppState
server ApiServer
mpris mpris.Server
logoutCh chan *AppPlayer
}
func parseDeviceType(val string) (devicespb.DeviceType, error) {
valEnum, ok := devicespb.DeviceType_value[strings.ToUpper(val)]
if !ok {
return 0, fmt.Errorf("invalid device type: %s", val)
}
return devicespb.DeviceType(valEnum), nil
}
func NewApp(cfg *Config) (app *App, err error) {
app = &App{cfg: cfg, logoutCh: make(chan *AppPlayer)}
logger := log.StandardLogger()
logger.SetFormatter(&log.TextFormatter{
DisableTimestamp: cfg.LogDisableTimestamp,
})
app.log = &LogrusAdapter{log.NewEntry(logger)}
app.client = &http.Client{Timeout: 30 * time.Second}
app.deviceType, err = parseDeviceType(cfg.DeviceType)
if err != nil {
return nil, err
}
app.state.SetLogger(app.log)
if err := app.state.Read(cfg.ConfigDir); err != nil {
return nil, err
}
app.resolver = apresolve.NewApResolver(app.log, app.client)
if cfg.DeviceId != "" {
// Use configured device ID.
app.deviceId = cfg.DeviceId
} else if app.state.DeviceId != "" {
// Use device ID generated in a previous run.
app.deviceId = app.state.DeviceId
} else {
// Generate a new device ID.
deviceIdBytes := make([]byte, 20)
_, _ = rand.Read(deviceIdBytes)
app.deviceId = hex.EncodeToString(deviceIdBytes)
log.Infof("generated new device id: %s", app.deviceId)
// Save device ID so we can reuse it next time.
app.state.DeviceId = app.deviceId
if err := app.state.Write(); err != nil {
return nil, err
}
}
if cfg.ClientToken != "" {
app.clientToken = cfg.ClientToken
}
return app, nil
}
func (app *App) newAppPlayer(ctx context.Context, creds any) (_ *AppPlayer, err error) {
appPlayer := &AppPlayer{
app: app,
stop: make(chan struct{}, 1),
logout: app.logoutCh,
countryCode: new(string),
volumeUpdate: make(chan float32, 1),
}
appPlayer.prefetchTimer = time.NewTimer(math.MaxInt64)
appPlayer.prefetchTimer.Stop()
if appPlayer.sess, err = session.NewSessionFromOptions(ctx, &session.Options{
Log: app.log,
DeviceType: app.deviceType,
DeviceId: app.deviceId,
ClientToken: app.clientToken,
Resolver: app.resolver,
Client: app.client,
AppState: &app.state,
Credentials: creds,
}); err != nil {
return nil, err
}
appPlayer.initState()
if appPlayer.player, err = player.NewPlayer(&player.Options{
Spclient: appPlayer.sess.Spclient(),
AudioKey: appPlayer.sess.AudioKey(),
Events: appPlayer.sess.Events(),
Log: app.log,
NormalisationEnabled: !app.cfg.NormalisationDisabled,
NormalisationUseAlbumGain: app.cfg.NormalisationUseAlbumGain,
NormalisationPregain: app.cfg.NormalisationPregain,
CountryCode: appPlayer.countryCode,
AudioBackend: app.cfg.AudioBackend,
AudioDevice: app.cfg.AudioDevice,
MixerDevice: app.cfg.MixerDevice,
MixerControlName: app.cfg.MixerControlName,
AudioBufferTime: app.cfg.AudioBufferTime,
AudioPeriodCount: app.cfg.AudioPeriodCount,
ExternalVolume: app.cfg.ExternalVolume,
VolumeUpdate: appPlayer.volumeUpdate,
AudioOutputPipe: app.cfg.AudioOutputPipe,
AudioOutputPipeFormat: app.cfg.AudioOutputPipeFormat,
},
); err != nil {
return nil, fmt.Errorf("failed initializing player: %w", err)
}
return appPlayer, nil
}
func (app *App) Zeroconf(ctx context.Context) error {
return app.withAppPlayer(ctx, func(ctx context.Context) (*AppPlayer, error) {
if app.cfg.Credentials.Zeroconf.PersistCredentials && len(app.state.Credentials.Data) > 0 {
app.log.WithField("username", librespot.ObfuscateUsername(app.state.Credentials.Username)).
Infof("loading previously persisted zeroconf credentials")
return app.newAppPlayer(ctx, session.StoredCredentials{
Username: app.state.Credentials.Username,
Data: app.state.Credentials.Data,
})
}
return nil, nil
})
}
func (app *App) SpotifyToken(ctx context.Context, username, token string) error {
return app.withCredentials(ctx, session.SpotifyTokenCredentials{Username: username, Token: token})
}
func (app *App) Interactive(ctx context.Context, callbackPort int) error {
return app.withCredentials(ctx, session.InteractiveCredentials{CallbackPort: callbackPort})
}
func (app *App) withCredentials(ctx context.Context, creds any) (err error) {
return app.withAppPlayer(ctx, func(ctx context.Context) (*AppPlayer, error) {
if len(app.state.Credentials.Data) > 0 {
return app.newAppPlayer(ctx, session.StoredCredentials{
Username: app.state.Credentials.Username,
Data: app.state.Credentials.Data,
})
} else {
appPlayer, err := app.newAppPlayer(ctx, creds)
if err != nil {
return nil, err
}
// store credentials outside this context in case we get called again
app.state.Credentials.Username = appPlayer.sess.Username()
app.state.Credentials.Data = appPlayer.sess.StoredCredentials()
if err = app.state.Write(); err != nil {
return nil, err
}
app.log.WithField("username", librespot.ObfuscateUsername(appPlayer.sess.Username())).
Debugf("stored credentials")
return appPlayer, nil
}
})
}
func (app *App) withAppPlayer(ctx context.Context, appPlayerFunc func(context.Context) (*AppPlayer, error)) (err error) {
// if zeroconf is disabled, there is not much we need to do
if !app.cfg.ZeroconfEnabled {
appPlayer, err := appPlayerFunc(ctx)
if err != nil {
return err
} else if appPlayer == nil {
panic("zeroconf is disabled and no credentials are present")
}
appPlayer.Run(ctx, app.server.Receive(), app.mpris.Receive())
return nil
}
// pre fetch resolver endpoints
if err := app.resolver.FetchAll(ctx); err != nil {
return fmt.Errorf("failed getting endpoints from resolver: %w", err)
}
// start zeroconf server and dispatch
z, err := zeroconf.NewZeroconf(zeroconf.Options{
Log: app.log,
Port: app.cfg.ZeroconfPort,
DeviceName: app.cfg.DeviceName,
DeviceId: app.deviceId,
DeviceType: app.deviceType,
DiscoveryImplementation: app.cfg.ZeroconfImplementation,
InterfacesToAdvertise: app.cfg.ZeroconfInterfacesToAdvertise,
})
if err != nil {
return fmt.Errorf("failed initializing zeroconf: %w", err)
}
var apiCh chan ApiRequest
currentPlayer, err := appPlayerFunc(ctx)
if err != nil {
return err
}
// set current player to the provided one if we have it
if currentPlayer != nil {
app.log.WithField("username", librespot.ObfuscateUsername(currentPlayer.sess.Username())).
Debugf("initializing zeroconf session")
apiCh = make(chan ApiRequest)
go currentPlayer.Run(ctx, apiCh, app.mpris.Receive())
// let zeroconf know that we already have a user
z.SetCurrentUser(currentPlayer.sess.Username())
}
// forward API requests to proper channel only if a session is present
go func() {
for {
select {
case req := <-app.server.Receive():
if currentPlayer == nil {
req.Reply(nil, ErrNoSession)
break
}
// if we are here the channel must exist
apiCh <- req
}
}
}()
// listen for logout events and unset session when that happens
go func() {
for {
select {
case p := <-app.logoutCh:
// check that the logout request is for the current player
if p != currentPlayer {
continue
}
currentPlayer.Close()
currentPlayer = nil
// close the channel after setting the current session to nil
close(apiCh)
// restore the session if there is one.
// we will restore the session even if it's for the same user, but it shouldn't be an issue
newAppPlayer, err := appPlayerFunc(ctx)
if err != nil {
log.WithError(err).Errorf("failed restoring session after logout")
} else if newAppPlayer == nil {
// unset the zeroconf user
z.SetCurrentUser("")
} else {
// first create the channel and then assign the current session
apiCh = make(chan ApiRequest)
currentPlayer = newAppPlayer
go newAppPlayer.Run(ctx, apiCh, app.mpris.Receive())
// let zeroconf know that we already have a user
z.SetCurrentUser(newAppPlayer.sess.Username())
app.log.WithField("username", librespot.ObfuscateUsername(currentPlayer.sess.Username())).
Debugf("restored session after logout")
}
}
}
}()
return z.Serve(func(req zeroconf.NewUserRequest) bool {
if currentPlayer != nil {
currentPlayer.Close()
currentPlayer = nil
// close the channel after setting the current session to nil
close(apiCh)
// no need to unset the zeroconf user here as the new one will overwrite it anyway
}
newAppPlayer, err := app.newAppPlayer(ctx, session.BlobCredentials{
Username: req.Username,
Blob: req.AuthBlob,
})
if err != nil {
app.log.WithError(err).WithField("username", librespot.ObfuscateUsername(req.Username)).
Errorf("failed creating new session from %s", req.DeviceName)
return false
}
// first create the channel and then assign the current session
apiCh = make(chan ApiRequest)
currentPlayer = newAppPlayer
if app.cfg.Credentials.Zeroconf.PersistCredentials {
app.state.Credentials.Username = newAppPlayer.sess.Username()
app.state.Credentials.Data = newAppPlayer.sess.StoredCredentials()
if err := app.state.Write(); err != nil {
log.WithError(err).Errorf("failed persisting zeroconf credentials")
}
app.log.WithField("username", librespot.ObfuscateUsername(newAppPlayer.sess.Username())).
Debugf("persisted zeroconf credentials")
}
go newAppPlayer.Run(ctx, apiCh, app.mpris.Receive())
return true
})
}
type Config struct {
ConfigDir string `koanf:"config_dir"`
// We need to keep this object around, otherwise it gets GC'd and the
// finalizer will run, probably closing the lock.
configLock *flock.Flock
LogLevel log.Level `koanf:"log_level"`
LogDisableTimestamp bool `koanf:"log_disable_timestamp"`
DeviceId string `koanf:"device_id"`
DeviceName string `koanf:"device_name"`
DeviceType string `koanf:"device_type"`
ClientToken string `koanf:"client_token"`
AudioBackend string `koanf:"audio_backend"`
AudioDevice string `koanf:"audio_device"`
MixerDevice string `koanf:"mixer_device"`
MixerControlName string `koanf:"mixer_control_name"`
AudioBufferTime int `koanf:"audio_buffer_time"`
AudioPeriodCount int `koanf:"audio_period_count"`
AudioOutputPipe string `koanf:"audio_output_pipe"`
AudioOutputPipeFormat string `koanf:"audio_output_pipe_format"`
Bitrate int `koanf:"bitrate"`
VolumeSteps uint32 `koanf:"volume_steps"`
InitialVolume uint32 `koanf:"initial_volume"`
IgnoreLastVolume bool `koanf:"ignore_last_volume"`
NormalisationDisabled bool `koanf:"normalisation_disabled"`
NormalisationUseAlbumGain bool `koanf:"normalisation_use_album_gain"`
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"`
ZeroconfImplementation string `koanf:"zeroconf_implementation"`
ZeroconfInterfacesToAdvertise []string `koanf:"zeroconf_interfaces_to_advertise"`
MprisEnabled bool `koanf:"mpris_enabled"`
Server struct {
Enabled bool `koanf:"enabled"`
Address string `koanf:"address"`
Port int `koanf:"port"`
AllowOrigin string `koanf:"allow_origin"`
CertFile string `koanf:"cert_file"`
KeyFile string `koanf:"key_file"`
} `koanf:"server"`
Credentials struct {
Type string `koanf:"type"`
Interactive struct {
CallbackPort int `koanf:"callback_port"`
} `koanf:"interactive"`
SpotifyToken struct {
Username string `koanf:"username"`
AccessToken string `koanf:"access_token"`
} `koanf:"spotify_token"`
Zeroconf struct {
PersistCredentials bool `koanf:"persist_credentials"`
} `koanf:"zeroconf"`
} `koanf:"credentials"`
}
func loadConfig(cfg *Config) error {
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
userConfigDir, err := os.UserConfigDir()
if err != nil {
return err
}
defaultConfigDir := filepath.Join(userConfigDir, "go-librespot")
f.StringVar(&cfg.ConfigDir, "config_dir", defaultConfigDir, "the configuration directory")
err = f.Parse(os.Args[1:])
if err != nil {
return err
}
// Make config directory if needed.
err = os.MkdirAll(cfg.ConfigDir, 0o700)
if err != nil {
return fmt.Errorf("failed creating config directory: %w", err)
}
// Lock the config directory (to ensure multiple instances won't clobber
// each others state).
lockFilePath := filepath.Join(cfg.ConfigDir, "lockfile")
cfg.configLock = flock.New(lockFilePath)
if locked, err := cfg.configLock.TryLock(); err != nil {
return fmt.Errorf("could not lock config directory: %w", err)
} else if !locked {
// Lock already taken! Looks like go-librespot is already running.
return fmt.Errorf("%w (lockfile: %s)", errAlreadyRunning, lockFilePath)
}
k := koanf.New(".")
// load default configuration
_ = k.Load(confmap.Provider(map[string]interface{}{
"log_level": log.InfoLevel,
"device_type": "computer",
"bitrate": 160,
"audio_backend": "alsa",
"audio_device": "default",
"audio_output_pipe_format": "s16le",
"mixer_control_name": "Master",
"volume_steps": 100,
"initial_volume": 100,
"credentials.type": "zeroconf",
"server.address": "localhost",
}, "."), nil)
// load file configuration (if available)
var configPath string
if _, err := os.Stat(filepath.Join(cfg.ConfigDir, "config.yaml")); os.IsNotExist(err) {
configPath = filepath.Join(cfg.ConfigDir, "config.yml")
} else {
configPath = filepath.Join(cfg.ConfigDir, "config.yaml")
}
if err := k.Load(file.Provider(configPath), yaml.Parser()); err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed reading configuration file: %w", err)
}
}
// load command line configuration
if err := k.Load(posflag.Provider(f, ".", k), nil); err != nil {
return fmt.Errorf("failed loading command line configuration: %w", err)
}
// unmarshal configuration
if err := k.Unmarshal("", &cfg); err != nil {
return fmt.Errorf("failed to unmarshal configuration: %w", err)
}
if cfg.DeviceName == "" {
cfg.DeviceName = "go-librespot"
hostname, _ := os.Hostname()
if hostname != "" {
cfg.DeviceName += " " + hostname
}
}
return nil
}
func main() {
rand.Seed(uint64(time.Now().UnixNano()))
var cfg Config
if err := loadConfig(&cfg); err != nil {
if errors.Is(err, errAlreadyRunning) {
// Print a nice error message instead of a harder-to-read log message.
_, _ = fmt.Fprintln(os.Stderr, "could not start:", err)
os.Exit(1)
}
log.WithError(err).Fatal("failed loading config")
}
// set log level
log.SetLevel(cfg.LogLevel)
log.Infof("running go-librespot %s", librespot.VersionNumberString())
// create new app
app, err := NewApp(&cfg)
if err != nil {
log.WithError(err).Fatal("failed creating app")
}
// create api server if needed
if cfg.Server.Enabled {
app.server, err = NewApiServer(app.log, cfg.Server.Address, cfg.Server.Port, cfg.Server.AllowOrigin, cfg.Server.CertFile, cfg.Server.KeyFile)
if err != nil {
log.WithError(err).Fatal("failed creating api server")
}
} else {
app.server, _ = NewStubApiServer(app.log)
}
// create mpris server if needed
if cfg.MprisEnabled {
app.mpris, err = mpris.NewServer(app.log)
if err != nil {
log.WithError(err).Fatal("failed creating mpris server")
}
} else {
app.mpris = mpris.DummyServer{}
}
ctx := context.TODO()
switch cfg.Credentials.Type {
case "zeroconf":
// ensure zeroconf is enabled
app.cfg.ZeroconfEnabled = true
if err := app.Zeroconf(ctx); err != nil {
log.WithError(err).Fatal("failed running zeroconf")
}
case "interactive":
if err := app.Interactive(ctx, cfg.Credentials.Interactive.CallbackPort); err != nil {
log.WithError(err).Fatal("failed running with interactive auth")
}
case "spotify_token":
if err := app.SpotifyToken(ctx, cfg.Credentials.SpotifyToken.Username, cfg.Credentials.SpotifyToken.AccessToken); err != nil {
log.WithError(err).Fatal("failed running with username and spotify token")
}
default:
log.Fatalf("unknown credentials: %s", cfg.Credentials.Type)
}
}