2026-11-16 11:52:38 +08:00
|
|
|
// Package storage keeps saved servers in the app's store and their device
|
|
|
|
|
// tokens in the platform's secure storage.
|
2026-11-16 09:09:09 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"encoding/json"
|
2026-11-16 12:05:50 +08:00
|
|
|
"net/url"
|
2026-11-16 11:52:38 +08:00
|
|
|
"sort"
|
2026-11-16 09:09:09 +08:00
|
|
|
|
|
|
|
|
"github.com/egoist/asuka/sdk/secure"
|
|
|
|
|
"github.com/egoist/asuka/sdk/store"
|
|
|
|
|
"github.com/egoist/asuka/signal"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
// Connection is one saved server.
|
2026-11-16 09:09:09 +08:00
|
|
|
type Connection struct {
|
2026-11-16 11:52:38 +08:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
Label string `json:"label"`
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
Proxy string `json:"proxy,omitempty"`
|
|
|
|
|
Insecure bool `json:"insecure,omitempty"`
|
|
|
|
|
Fingerprint string `json:"fingerprint,omitempty"`
|
|
|
|
|
ServerID string `json:"serverId,omitempty"`
|
|
|
|
|
Passwordless bool `json:"passwordless,omitempty"`
|
|
|
|
|
LastUsedAt int64 `json:"lastUsedAt,omitempty"`
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store is the persistence the UI needs. Tests use a memory store.
|
|
|
|
|
type Store interface {
|
2026-11-16 11:52:38 +08:00
|
|
|
Connections() []Connection
|
|
|
|
|
SaveConnections([]Connection)
|
2026-11-16 09:09:09 +08:00
|
|
|
InstallID() string
|
2026-11-16 11:52:38 +08:00
|
|
|
LoadToken(c Connection) (string, bool, error)
|
|
|
|
|
SaveToken(id, token string) error
|
|
|
|
|
ClearToken(c Connection) error
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
// Persistent is the app's store: servers in store.json, tokens in the
|
2026-11-16 09:09:09 +08:00
|
|
|
// Android Keystore / Keychain.
|
|
|
|
|
type Persistent struct {
|
2026-11-16 11:52:38 +08:00
|
|
|
conns *signal.Signal[[]Connection]
|
2026-11-16 09:09:09 +08:00
|
|
|
installID *signal.Signal[string]
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
const (
|
|
|
|
|
connectionsKey = "openchamber.connections"
|
|
|
|
|
installIDKey = "openchamber.installId"
|
|
|
|
|
// legacyKey is the single-server record earlier versions wrote.
|
|
|
|
|
legacyKey = "openchamber.connection"
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
// New makes the persistent store. Call it on the main thread, while a
|
|
|
|
|
// component builds, as any signal.
|
|
|
|
|
func New() *Persistent {
|
2026-11-16 11:52:38 +08:00
|
|
|
conns := store.Signal(connectionsKey, []Connection{})
|
|
|
|
|
list := conns.Get()
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
|
list = migrateLegacy()
|
|
|
|
|
if len(list) > 0 {
|
|
|
|
|
conns.Set(list)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 09:09:09 +08:00
|
|
|
p := &Persistent{
|
2026-11-16 11:52:38 +08:00
|
|
|
conns: conns,
|
|
|
|
|
installID: store.Signal(installIDKey, ""),
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
// migrateLegacy turns the old single-connection record into a list entry.
|
|
|
|
|
func migrateLegacy() []Connection {
|
|
|
|
|
var raw string
|
|
|
|
|
if !store.Get(legacyKey, &raw) || raw == "" {
|
|
|
|
|
return nil
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
var old Connection
|
|
|
|
|
if json.Unmarshal([]byte(raw), &old) != nil || old.URL == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if old.ID == "" {
|
|
|
|
|
old.ID = newID()
|
|
|
|
|
}
|
2026-11-16 12:05:50 +08:00
|
|
|
if old.Label == "" || old.Label == old.URL {
|
|
|
|
|
if u, err := url.Parse(old.URL); err == nil && u.Host != "" {
|
|
|
|
|
old.Label = u.Host
|
|
|
|
|
} else {
|
|
|
|
|
old.Label = old.URL
|
|
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
}
|
|
|
|
|
// Clear it so the next launch does not migrate again.
|
|
|
|
|
_ = store.Set(legacyKey, "")
|
|
|
|
|
return []Connection{old}
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (p *Persistent) Connections() []Connection {
|
|
|
|
|
list := p.conns.Get()
|
|
|
|
|
out := make([]Connection, len(list))
|
|
|
|
|
copy(out, list)
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Persistent) SaveConnections(list []Connection) {
|
|
|
|
|
sort.SliceStable(list, func(i, j int) bool { return list[i].LastUsedAt > list[j].LastUsedAt })
|
|
|
|
|
p.conns.Set(list)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InstallID is a stable random id for this installation. It names the
|
|
|
|
|
// device when logging in, so repeated logins reuse one device record.
|
|
|
|
|
func (p *Persistent) InstallID() string {
|
|
|
|
|
if id := p.installID.Get(); id != "" {
|
|
|
|
|
return id
|
|
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
id := newID()
|
2026-11-16 09:09:09 +08:00
|
|
|
p.installID.Set(id)
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (p *Persistent) tokenKey(id string) string {
|
|
|
|
|
sum := sha256.Sum256([]byte(id))
|
2026-11-16 09:09:09 +08:00
|
|
|
return "openchamber.token." + hex.EncodeToString(sum[:12])
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
// legacyTokenKey is where earlier versions kept a token: by server URL.
|
|
|
|
|
func legacyTokenKey(url string) string {
|
|
|
|
|
sum := sha256.Sum256([]byte(url))
|
|
|
|
|
return "openchamber.token." + hex.EncodeToString(sum[:12])
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (p *Persistent) LoadToken(c Connection) (string, bool, error) {
|
|
|
|
|
if tok, ok, err := secure.Get(p.tokenKey(c.ID)); err != nil || ok {
|
|
|
|
|
return tok, ok, err
|
|
|
|
|
}
|
|
|
|
|
// A connection migrated from the single-server record keeps its token
|
|
|
|
|
// under the old URL-based key.
|
|
|
|
|
return secure.Get(legacyTokenKey(c.URL))
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (p *Persistent) SaveToken(id, token string) error {
|
|
|
|
|
return secure.Set(p.tokenKey(id), token)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
|
|
|
|
|
func (p *Persistent) ClearToken(c Connection) error {
|
|
|
|
|
err := secure.Delete(p.tokenKey(c.ID))
|
|
|
|
|
_ = secure.Delete(legacyTokenKey(c.URL))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newID() string {
|
|
|
|
|
var b [8]byte
|
|
|
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
|
|
|
return "srv_" + hex.EncodeToString(b[:4])
|
|
|
|
|
}
|
|
|
|
|
return "srv_" + hex.EncodeToString(b[:])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewID returns a fresh connection id.
|
|
|
|
|
func NewID() string { return newID() }
|