2026-11-18 13:00:20 +08:00
|
|
|
// Package storage keeps saved servers in the app's data directory 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-18 13:00:20 +08:00
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-11-16 11:52:38 +08:00
|
|
|
"sort"
|
2026-11-18 13:00:20 +08:00
|
|
|
"sync"
|
2026-11-16 09:09:09 +08:00
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
"github.com/egoist/mygo"
|
2026-11-16 09:09:09 +08:00
|
|
|
)
|
|
|
|
|
|
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-18 13:00:20 +08:00
|
|
|
// Persistent is the app's store: servers in connections.json, tokens in
|
|
|
|
|
// the platform's secure store (the Android Keystore / iOS Keychain).
|
2026-11-16 09:09:09 +08:00
|
|
|
type Persistent struct {
|
2026-11-18 13:00:20 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
|
path string
|
|
|
|
|
conns []Connection
|
|
|
|
|
installID string
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
// New makes the persistent store. It reads the servers file once; call it
|
|
|
|
|
// on startup, before any window shows.
|
2026-11-16 09:09:09 +08:00
|
|
|
func New() *Persistent {
|
2026-11-18 13:00:20 +08:00
|
|
|
p := &Persistent{}
|
|
|
|
|
dir, err := mygo.App.Path(mygo.PathUserData)
|
|
|
|
|
if err == nil {
|
|
|
|
|
p.path = filepath.Join(dir, "connections.json")
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
p.load()
|
2026-11-16 09:09:09 +08:00
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (p *Persistent) load() {
|
|
|
|
|
if p.path == "" {
|
|
|
|
|
return
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
data, err := os.ReadFile(p.path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2026-11-16 11:52:38 +08:00
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
var list []Connection
|
|
|
|
|
if json.Unmarshal(data, &list) == nil {
|
|
|
|
|
p.conns = list
|
2026-11-16 11:52:38 +08:00
|
|
|
}
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func (p *Persistent) save() {
|
|
|
|
|
if p.path == "" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(p.path), 0o700); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, _ := json.Marshal(p.conns)
|
|
|
|
|
_ = os.WriteFile(p.path, data, 0o600)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// migrateLegacy is gone: the earlier Asuka builds kept their data in
|
|
|
|
|
// Asuka's own stores, which this app cannot read.
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (p *Persistent) Connections() []Connection {
|
2026-11-18 13:00:20 +08:00
|
|
|
p.mu.Lock()
|
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
out := make([]Connection, len(p.conns))
|
|
|
|
|
copy(out, p.conns)
|
2026-11-16 11:52:38 +08:00
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Persistent) SaveConnections(list []Connection) {
|
|
|
|
|
sort.SliceStable(list, func(i, j int) bool { return list[i].LastUsedAt > list[j].LastUsedAt })
|
2026-11-18 13:00:20 +08:00
|
|
|
p.mu.Lock()
|
|
|
|
|
p.conns = list
|
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
p.save()
|
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 {
|
2026-11-18 13:00:20 +08:00
|
|
|
p.mu.Lock()
|
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
if p.installID != "" {
|
|
|
|
|
return p.installID
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
p.installID = NewID()
|
|
|
|
|
return p.installID
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
func (p *Persistent) LoadToken(c Connection) (string, bool, error) {
|
2026-11-18 13:00:20 +08:00
|
|
|
tok, err := mygo.Secrets.Get(p.tokenKey(c.ID))
|
|
|
|
|
if errors.Is(err, mygo.ErrSecretNotFound) {
|
|
|
|
|
return "", false, nil
|
2026-11-16 11:52:38 +08:00
|
|
|
}
|
2026-11-18 13:00:20 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return "", false, err
|
|
|
|
|
}
|
|
|
|
|
return string(tok), true, nil
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (p *Persistent) SaveToken(id, token string) error {
|
2026-11-18 13:00:20 +08:00
|
|
|
return mygo.Secrets.Set(p.tokenKey(id), []byte(token))
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
|
|
|
|
|
func (p *Persistent) ClearToken(c Connection) error {
|
2026-11-18 13:00:20 +08:00
|
|
|
return mygo.Secrets.Delete(p.tokenKey(c.ID))
|
2026-11-16 11:52:38 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 13:00:20 +08:00
|
|
|
func NewID() string {
|
2026-11-16 11:52:38 +08:00
|
|
|
var b [8]byte
|
|
|
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
|
|
|
return "srv_" + hex.EncodeToString(b[:4])
|
|
|
|
|
}
|
|
|
|
|
return "srv_" + hex.EncodeToString(b[:])
|
|
|
|
|
}
|