feat: save several servers and pick one to connect

Match the first-party mobile client's connection screen:

- the first screen lists saved servers, the one last used leading with
  its connect button or connected state, the others below, and adding
  is one tap
- servers can be added, edited (name, address, proxy, self-signed) and
  deleted; deleting the live or last one returns to the list
- a password sheet prompts only when the server asks for one, keeps the
  error and the typed password for a retry
- while connected, the instances sheet switches servers
- proxy and self-signed options stay per server (the native app's own)
- the single-server record and its token migrate automatically
- permission cards read a plain signal, fixing a flaky update
- bump version to 0.1.2 (build 3)
This commit is contained in:
blumia committed 2026-11-16 11:52:38 +08:00
1 parent ef0b63ceee
commit 13aca21d99
12 files changed
+966 -361

No files matched your search

+98 -56
View File
@@ -1,5 +1,5 @@
// Package storage keeps the connection settings in the app's store and the
// device token in the platform's secure storage.
// Package storage keeps saved servers in the app's store and their device
// tokens in the platform's secure storage.
package storage
import (
@@ -7,77 +7,99 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"sort"
"github.com/egoist/asuka/sdk/secure"
"github.com/egoist/asuka/sdk/store"
"github.com/egoist/asuka/signal"
)
// Connection is the last server the app connected to.
// Connection is one saved server.
type Connection struct {
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 servers need no token; remembering that avoids a
// pointless password prompt on the next launch.
Passwordless bool `json:"passwordless,omitempty"`
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 {
Connection() (Connection, bool)
SaveConnection(Connection)
ClearConnection()
Connections() []Connection
SaveConnections([]Connection)
InstallID() string
LoadToken(server string) (string, bool, error)
SaveToken(server, token string) error
ClearToken(server string) error
LoadToken(c Connection) (string, bool, error)
SaveToken(id, token string) error
ClearToken(c Connection) error
}
// Persistent is the app's store: settings in store.json, tokens in the
// Persistent is the app's store: servers in store.json, tokens in the
// Android Keystore / Keychain.
type Persistent struct {
conn *signal.Signal[Connection]
hasConn *signal.Signal[bool]
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 {
raw := store.Signal("openchamber.connection", "")
var c Connection
ok := raw.Get() != "" && json.Unmarshal([]byte(raw.Get()), &c) == nil && c.URL != ""
conns := store.Signal(connectionsKey, []Connection{})
list := conns.Get()
if len(list) == 0 {
list = migrateLegacy()
if len(list) > 0 {
conns.Set(list)
}
}
p := &Persistent{
conn: signal.NewFunc(c, nil),
hasConn: signal.New(ok),
installID: store.Signal("openchamber.installId", ""),
conns: conns,
installID: store.Signal(installIDKey, ""),
}
return p
}
func (p *Persistent) Connection() (Connection, bool) {
return p.conn.Get(), p.hasConn.Get()
}
func (p *Persistent) SaveConnection(c Connection) {
p.conn.Set(c)
p.hasConn.Set(c.URL != "")
buf, err := json.Marshal(c)
if err != nil {
return
// 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
}
// Keep it in the store as JSON; the signal above stays the source of
// truth for the UI.
store.Set("openchamber.connection", string(buf))
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
}
// Clear it so the next launch does not migrate again.
_ = store.Set(legacyKey, "")
return []Connection{old}
}
func (p *Persistent) ClearConnection() {
p.conn.Set(Connection{})
p.hasConn.Set(false)
store.Set("openchamber.connection", "")
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
@@ -86,28 +108,48 @@ func (p *Persistent) InstallID() string {
if id := p.installID.Get(); id != "" {
return id
}
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
return "openchamber-" + hex.EncodeToString(b[:8])
}
id := hex.EncodeToString(b[:])
id := newID()
p.installID.Set(id)
return id
}
func (p *Persistent) tokenKey(server string) string {
sum := sha256.Sum256([]byte(server))
func (p *Persistent) tokenKey(id string) string {
sum := sha256.Sum256([]byte(id))
return "openchamber.token." + hex.EncodeToString(sum[:12])
}
func (p *Persistent) LoadToken(server string) (string, bool, error) {
return secure.Get(p.tokenKey(server))
// 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) SaveToken(server, token string) error {
return secure.Set(p.tokenKey(server), token)
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) ClearToken(server string) error {
return secure.Delete(p.tokenKey(server))
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() }