@@ -31,6 +31,8 @@ import (
3131const pongAckInterval = 120 * time .Second
3232
3333type Accesspoint struct {
34+ log * log.Entry
35+
3436 addr librespot.GetAddressFunc
3537
3638 nonce []byte
@@ -57,8 +59,8 @@ type Accesspoint struct {
5759 welcome * pb.APWelcome
5860}
5961
60- func NewAccesspoint (addr librespot.GetAddressFunc , deviceId string ) * Accesspoint {
61- return & Accesspoint {addr : addr , deviceId : deviceId , recvChans : make (map [PacketType ][]chan Packet )}
62+ func NewAccesspoint (log * log. Entry , addr librespot.GetAddressFunc , deviceId string ) * Accesspoint {
63+ return & Accesspoint {log : log , addr : addr , deviceId : deviceId , recvChans : make (map [PacketType ][]chan Packet )}
6264}
6365
6466func (ap * Accesspoint ) init (ctx context.Context ) (err error ) {
@@ -85,14 +87,14 @@ func (ap *Accesspoint) init(ctx context.Context) (err error) {
8587 // we assign to ap.conn after because if Dial fails we'll have a nil ap.conn which we don't want
8688 ap .conn = conn
8789 // Successfully connected.
88- log .Debugf ("connected to %s" , addr )
90+ ap . log .Debugf ("connected to %s" , addr )
8991 return nil
9092 } else if attempts >= 6 {
9193 // Only try a few times before giving up.
9294 return fmt .Errorf ("failed to connect to AP %v: %w" , addr , err )
9395 }
9496 // Try again with a different AP.
95- log .WithError (err ).Warnf ("failed to connect to AP %v, retrying with a different AP" , addr )
97+ ap . log .WithError (err ).Warnf ("failed to connect to AP %v, retrying with a different AP" , addr )
9698 }
9799}
98100
@@ -249,7 +251,7 @@ func (ap *Accesspoint) Receive(types ...PacketType) <-chan Packet {
249251
250252func (ap * Accesspoint ) startReceiving () {
251253 ap .recvLoopOnce .Do (func () {
252- log .Tracef ("starting accesspoint recv loop" )
254+ ap . log .Tracef ("starting accesspoint recv loop" )
253255 go ap .recvLoop ()
254256
255257 // set last ping in the future
@@ -268,19 +270,22 @@ loop:
268270 // no need to hold the connMu since reconnection happens in this routine
269271 pkt , payload , err := ap .encConn .receivePacket (context .TODO ())
270272 if err != nil {
271- log .WithError (err ).Errorf ("failed receiving packet" )
273+ if ! ap .stop {
274+ ap .log .WithError (err ).Errorf ("failed receiving packet" )
275+ }
276+
272277 break loop
273278 }
274279
275280 switch pkt {
276281 case PacketTypePing :
277- log .Tracef ("received accesspoint ping" )
282+ ap . log .Tracef ("received accesspoint ping" )
278283 if err := ap .encConn .sendPacket (context .TODO (), PacketTypePong , payload ); err != nil {
279- log .WithError (err ).Errorf ("failed sending Pong packet" )
284+ ap . log .WithError (err ).Errorf ("failed sending Pong packet" )
280285 break loop
281286 }
282287 case PacketTypePongAck :
283- log .Tracef ("received accesspoint pong ack" )
288+ ap . log .Tracef ("received accesspoint pong ack" )
284289 ap .lastPongAckLock .Lock ()
285290 ap .lastPongAck = time .Now ()
286291 ap .lastPongAckLock .Unlock ()
@@ -297,7 +302,7 @@ loop:
297302 }
298303
299304 if ! handled {
300- log .Debugf ("skipping packet %v, len: %d" , pkt , len (payload ))
305+ ap . log .Debugf ("skipping packet %v, len: %d" , pkt , len (payload ))
301306 }
302307 }
303308 }
@@ -310,7 +315,7 @@ loop:
310315 if ! ap .stop {
311316 ap .connMu .Lock ()
312317 if err := backoff .Retry (ap .reconnect , backoff .NewExponentialBackOff ()); err != nil {
313- log .WithError (err ).Errorf ("failed reconnecting accesspoint, bye bye" )
318+ ap . log .WithError (err ).Errorf ("failed reconnecting accesspoint, bye bye" )
314319 log .Exit (1 )
315320 }
316321 ap .connMu .Unlock ()
@@ -347,7 +352,7 @@ loop:
347352 timePassed := time .Since (ap .lastPongAck )
348353 ap .lastPongAckLock .Unlock ()
349354 if timePassed > pongAckInterval {
350- log .Errorf ("did not receive last pong ack from accesspoint, %.0fs passed" , timePassed .Seconds ())
355+ ap . log .Errorf ("did not receive last pong ack from accesspoint, %.0fs passed" , timePassed .Seconds ())
351356
352357 // closing the connection should make the read on the "recvLoop" fail,
353358 // continue hoping for a new connection
@@ -376,7 +381,7 @@ func (ap *Accesspoint) reconnect() (err error) {
376381 // if we are here the "recvLoop" has already died, restart it
377382 go ap .recvLoop ()
378383
379- log .Debugf ("re-established accesspoint connection" )
384+ ap . log .Debugf ("re-established accesspoint connection" )
380385 return nil
381386}
382387
@@ -426,7 +431,7 @@ func (ap *Accesspoint) performKeyExchange() ([]byte, error) {
426431 // exchange keys and compute shared secret
427432 ap .dh .Exchange (apResponse .Challenge .LoginCryptoChallenge .DiffieHellman .Gs )
428433
429- log .Debugf ("completed keyexchange" )
434+ ap . log .Debugf ("completed keyexchange" )
430435 return cc .Dump (), nil
431436}
432437
@@ -463,7 +468,7 @@ func (ap *Accesspoint) solveChallenge(exchangeData []byte) error {
463468 var resp pb.APResponseMessage
464469 if err := readMessage (ap .conn , & resp ); errors .Is (err , os .ErrDeadlineExceeded ) {
465470 ap .encConn = newShannonConn (ap .conn , macData [20 :52 ], macData [52 :84 ])
466- log .Debug ("completed challenge" )
471+ ap . log .Debug ("completed challenge" )
467472 return nil
468473 } else if err != nil {
469474 return fmt .Errorf ("failed reading APLoginFailed message: %w" , err )
@@ -510,7 +515,7 @@ func (ap *Accesspoint) authenticate(ctx context.Context, credentials *pb.LoginCr
510515 }
511516
512517 ap .welcome = & welcome
513- log .Infof ("authenticated AP as %s" , * welcome .CanonicalUsername )
518+ ap . log .Infof ("authenticated AP as %s" , * welcome .CanonicalUsername )
514519
515520 return nil
516521 } else if recvPkt == PacketTypeAuthFailure {
0 commit comments