Files
openchamber-android-native/internal/storage/storage.go
T
blumia 254d08b1bd feat: bootstrap OpenChamber native Android client
Add a Go client for OpenChamber built with Asuka's native widgets.

- internal/ocapi: health, password-to-device-token auth, session and
  message paging, prompt/interrupt/model/agent, permissions, SSE events,
  HTTP/SOCKS5 proxies and certificate pinning
- internal/storage: connection settings plus a device token in the
  platform keystore, with a per-install id for server-side dedupe
- internal/appui: connect, session list and chat screens with streaming
  Markdown, reasoning/tool folding, permission cards and session actions
- netcfg: native module that enables cleartext HTTP in release builds
- cmd/ocprobe: terminal probe for the API
- headless UI tests (uitest) and the research/plan document
2026-11-16 09:09:09 +08:00

114 lines
3.1 KiB
Go

// Package storage keeps the connection settings in the app's store and the
// device token in the platform's secure storage.
package storage
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"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.
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"`
}
// Store is the persistence the UI needs. Tests use a memory store.
type Store interface {
Connection() (Connection, bool)
SaveConnection(Connection)
ClearConnection()
InstallID() string
LoadToken(server string) (string, bool, error)
SaveToken(server, token string) error
ClearToken(server string) error
}
// Persistent is the app's store: settings in store.json, tokens in the
// Android Keystore / Keychain.
type Persistent struct {
conn *signal.Signal[Connection]
hasConn *signal.Signal[bool]
installID *signal.Signal[string]
}
// 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 != ""
p := &Persistent{
conn: signal.NewFunc(c, nil),
hasConn: signal.New(ok),
installID: store.Signal("openchamber.installId", ""),
}
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
}
// Keep it in the store as JSON; the signal above stays the source of
// truth for the UI.
store.Set("openchamber.connection", string(buf))
}
func (p *Persistent) ClearConnection() {
p.conn.Set(Connection{})
p.hasConn.Set(false)
store.Set("openchamber.connection", "")
}
// 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
}
var b [16]byte
if _, err := rand.Read(b[:]); err != nil {
return "openchamber-" + hex.EncodeToString(b[:8])
}
id := hex.EncodeToString(b[:])
p.installID.Set(id)
return id
}
func (p *Persistent) tokenKey(server string) string {
sum := sha256.Sum256([]byte(server))
return "openchamber.token." + hex.EncodeToString(sum[:12])
}
func (p *Persistent) LoadToken(server string) (string, bool, error) {
return secure.Get(p.tokenKey(server))
}
func (p *Persistent) SaveToken(server, token string) error {
return secure.Set(p.tokenKey(server), token)
}
func (p *Persistent) ClearToken(server string) error {
return secure.Delete(p.tokenKey(server))
}