Anker Solix: a 403 that is not about the token spends no login

anker-solix-api v3.9.1 (2026-10-25) and ha-anker-solix 3.9.1.1 (2026-10-30).
The release is mostly PPS, F3000 and A91B2 work, and none of it reaches an
A5191. The emergency fix does. On 29 October Anker retired an endpoint by
answering it 403. Clients took every 403 for an evicted token and logged in
again on every poll, and that ended in a 24-hour IP ban, code 100041 "Hit max
login error limit" (ha-anker-solix #658, #659). This connector treated a 403
the same way. A successful login clears the backoff, so nothing would have
stopped the loop.

A refused token now costs a login only when one could help:
- A token issued less than two minutes ago was not evicted, so its refusal is
  the endpoint saying no.
- If another request has already replaced the token, the retry uses that
  token and no new login is made.
- An endpoint that refuses a fresh token is left alone for an hour. Its
  requests still go out and fail on their own, and no further logins are
  spent on it.
- Code 100041 holds the next login attempt back for an hour. More attempts
  only add to the count, and only a new public IP lifts the ban early.

Already as upstream now has it: the MQTT client does not mark a topic
subscribed when its SUBACK refuses it, and a malformed message is dropped
without touching the connection. The v3.9.1 signature comment (secret[:16]
after the handshake, presetKey only for the exchange) matches
payloadcrypto.go.

Toyota: pytoyoda has nothing after 6.0.0, which e7d2ef8 ported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5.5 committed 2026-11-18 09:44:43 +01:00
1 parent 0ea562a80a
commit 9d8636ee73
4 files changed
+196 -19

No files matched your search

@@ -42,6 +42,10 @@ type session struct {
loginRetryAt time.Time
loginFails int
// refused holds endpoints that turned a token away minutes after the login
// that issued it, until when no login is spent on them again. Guarded by mu.
refused map[string]time.Time
// The cloud MQTT half (cloudmqtt.go), under its own lock: a broker connection
// takes a TLS handshake and a fetched certificate to open, so it outlives the
// plugin instance for the same reason the token does. mqttMu guards all four
@@ -131,9 +135,43 @@ func (s *session) noteLogin(err error) {
wait = loginRetryMax
}
var rejected *loginRejected
if errors.As(err, &rejected) && rejected.code == codeSignInLocked && wait < loginLockoutWait {
wait = loginLockoutWait
if errors.As(err, &rejected) {
switch {
case rejected.code == codeSignInLocked && wait < loginLockoutWait:
wait = loginLockoutWait
case rejected.code == codeLoginBanned && wait < loginBanWait:
wait = loginBanWait
}
}
s.loginRetryAt = time.Now().Add(wait)
s.loginErr = &loginFailure{err: err, retryAt: s.loginRetryAt}
}
// endpointRefusal is how long an endpoint that refused a fresh token is left
// without a login spent on it. Its requests still go out, and fail on their own.
const endpointRefusal = time.Hour
// noteRefused records that endpoint refused a token it had no reason to.
func (s *session) noteRefused(endpoint string) {
s.mu.Lock()
defer s.mu.Unlock()
s.noteRefusedLocked(endpoint)
}
// noteRefusedLocked is noteRefused for a caller already holding s.mu.
func (s *session) noteRefusedLocked(endpoint string) {
if s.refused == nil {
s.refused = map[string]time.Time{}
}
s.refused[endpoint] = time.Now().Add(endpointRefusal)
}
// refusing reports whether endpoint is inside its refusal window. Caller holds s.mu.
func (s *session) refusing(endpoint string) bool {
until, ok := s.refused[endpoint]
if ok && !time.Now().Before(until) {
delete(s.refused, endpoint)
return false
}
return ok
}