A connection saved without a name (including the one migrated from the single-server record) showed its URL as both title and subtitle. Fall back to the host, and migrate old records that stored the URL as the name.
161 lines
4.2 KiB
Go
161 lines
4.2 KiB
Go
// Package storage keeps saved servers in the app's store and their device
|
|
// tokens in the platform's secure storage.
|
|
package storage
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/url"
|
|
"sort"
|
|
|
|
"github.com/egoist/asuka/sdk/secure"
|
|
"github.com/egoist/asuka/sdk/store"
|
|
"github.com/egoist/asuka/signal"
|
|
)
|
|
|
|
// Connection is one saved server.
|
|
type Connection struct {
|
|
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"`
|
|
}
|
|
|
|
// Store is the persistence the UI needs. Tests use a memory store.
|
|
type Store interface {
|
|
Connections() []Connection
|
|
SaveConnections([]Connection)
|
|
InstallID() string
|
|
LoadToken(c Connection) (string, bool, error)
|
|
SaveToken(id, token string) error
|
|
ClearToken(c Connection) error
|
|
}
|
|
|
|
// Persistent is the app's store: servers in store.json, tokens in the
|
|
// Android Keystore / Keychain.
|
|
type Persistent struct {
|
|
conns *signal.Signal[[]Connection]
|
|
installID *signal.Signal[string]
|
|
}
|
|
|
|
const (
|
|
connectionsKey = "openchamber.connections"
|
|
installIDKey = "openchamber.installId"
|
|
// legacyKey is the single-server record earlier versions wrote.
|
|
legacyKey = "openchamber.connection"
|
|
)
|
|
|
|
// New makes the persistent store. Call it on the main thread, while a
|
|
// component builds, as any signal.
|
|
func New() *Persistent {
|
|
conns := store.Signal(connectionsKey, []Connection{})
|
|
list := conns.Get()
|
|
if len(list) == 0 {
|
|
list = migrateLegacy()
|
|
if len(list) > 0 {
|
|
conns.Set(list)
|
|
}
|
|
}
|
|
p := &Persistent{
|
|
conns: conns,
|
|
installID: store.Signal(installIDKey, ""),
|
|
}
|
|
return p
|
|
}
|
|
|
|
// 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
|
|
}
|
|
var old Connection
|
|
if json.Unmarshal([]byte(raw), &old) != nil || old.URL == "" {
|
|
return nil
|
|
}
|
|
if old.ID == "" {
|
|
old.ID = newID()
|
|
}
|
|
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
|
|
}
|
|
}
|
|
// Clear it so the next launch does not migrate again.
|
|
_ = store.Set(legacyKey, "")
|
|
return []Connection{old}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
id := newID()
|
|
p.installID.Set(id)
|
|
return id
|
|
}
|
|
|
|
func (p *Persistent) tokenKey(id string) string {
|
|
sum := sha256.Sum256([]byte(id))
|
|
return "openchamber.token." + hex.EncodeToString(sum[:12])
|
|
}
|
|
|
|
// 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])
|
|
}
|
|
|
|
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))
|
|
}
|
|
|
|
func (p *Persistent) SaveToken(id, token string) error {
|
|
return secure.Set(p.tokenKey(id), token)
|
|
}
|
|
|
|
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() }
|