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

+150 -80
View File
@@ -16,71 +16,77 @@ import (
// memStore is a storage.Store that keeps everything in memory.
type memStore struct {
mu sync.Mutex
conn storage.Connection
has bool
conns []storage.Connection
tokens map[string]string
id string
}
func (m *memStore) Connection() (storage.Connection, bool) {
func (m *memStore) Connections() []storage.Connection {
m.mu.Lock()
defer m.mu.Unlock()
return m.conn, m.has
out := make([]storage.Connection, len(m.conns))
copy(out, m.conns)
return out
}
func (m *memStore) SaveConnection(c storage.Connection) {
func (m *memStore) SaveConnections(list []storage.Connection) {
m.mu.Lock()
defer m.mu.Unlock()
m.conn, m.has = c, c.URL != ""
}
func (m *memStore) ClearConnection() {
m.mu.Lock()
defer m.mu.Unlock()
m.conn, m.has = storage.Connection{}, false
out := make([]storage.Connection, len(list))
copy(out, list)
m.conns = out
}
func (m *memStore) InstallID() string {
m.mu.Lock()
defer m.mu.Unlock()
if m.id == "" {
m.id = "test-install"
}
return m.id
}
func (m *memStore) LoadToken(server string) (string, bool, error) {
func (m *memStore) LoadToken(c storage.Connection) (string, bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
t, ok := m.tokens[server]
t, ok := m.tokens[c.ID]
return t, ok, nil
}
func (m *memStore) SaveToken(server, token string) error {
func (m *memStore) SaveToken(id, token string) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.tokens == nil {
m.tokens = map[string]string{}
}
m.tokens[server] = token
return nil
}
func (m *memStore) ClearToken(server string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.tokens, server)
m.tokens[id] = token
return nil
}
func (m *memStore) token(server string) string {
func (m *memStore) ClearToken(c storage.Connection) error {
m.mu.Lock()
defer m.mu.Unlock()
return m.tokens[server]
delete(m.tokens, c.ID)
return nil
}
func (m *memStore) connection() storage.Connection {
func (m *memStore) token(id string) string {
m.mu.Lock()
defer m.mu.Unlock()
return m.conn
return m.tokens[id]
}
func (m *memStore) connections() []storage.Connection {
return m.Connections()
}
// fakeServer stands in for OpenChamber: /health, /auth/session and the
// session list. password "" with validToken "" makes it passwordless.
func fakeServer(t *testing.T, password, validToken string) *httptest.Server {
// session list. An empty password makes it passwordless.
func fakeServer(t *testing.T, password, validToken, sessionTitle string) *httptest.Server {
t.Helper()
if sessionTitle == "" {
sessionTitle = "First session"
}
mux := http.NewServeMux()
authorized := func(r *http.Request) bool {
if validToken == "" {
@@ -117,15 +123,16 @@ func fakeServer(t *testing.T, password, validToken string) *httptest.Server {
fmt.Fprintf(w, `{"authenticated":true,"clientToken":%q}`, validToken)
}
})
mux.HandleFunc("/api/location", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"directory":"/home/me/project","project":{"id":"p1"}}`)
})
mux.HandleFunc("/api/session", func(w http.ResponseWriter, r *http.Request) {
if !authorized(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}
fmt.Fprint(w, `{"data":[
{"id":"ses_1","title":"First session","time":{"created":1,"updated":2},
"location":{"directory":"/home/me/project"},"model":{"id":"m","providerID":"p"}}],
"cursor":{}}`)
fmt.Fprintf(w, `{"data":[{"id":"ses_1","title":%q,"time":{"created":1,"updated":2},
"location":{"directory":"/home/me/project"},"model":{"id":"m","providerID":"p"}}],"cursor":{}}`, sessionTitle)
})
mux.HandleFunc("/api/session/active", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"data":{"ses_1":{"type":"running"}}}`)
@@ -151,86 +158,149 @@ func mount(t *testing.T, st *memStore) *uitest.App {
return uitest.Mount(t, func() ui.Node { return rootWithStore(st) }, uitest.Platform(ui.PlatformAndroid))
}
func TestConnectWithPassword(t *testing.T) {
srv := fakeServer(t, "secret", "oc_client_test")
// connectViaForm adds a server from the first screen (or the saved list)
// and taps Connect.
func connectViaForm(t *testing.T, a *uitest.App, url string) {
t.Helper()
if a.Has("手动输入地址") {
a.Tap("手动输入地址")
} else {
a.Tap("添加服务器")
}
a.Type("http://192.168.1.5:3000", url)
a.Tap("连接")
}
func unlockWith(t *testing.T, a *uitest.App, password string) {
t.Helper()
a.WaitFor("这台服务器需要密码。")
a.Type("密码", password)
a.Tap("解锁")
}
func TestConnectWithPassword(t *testing.T) {
srv := fakeServer(t, "secret", "oc_client_test", "")
st := &memStore{}
a := mount(t, st)
a.Expect("连接到 OpenChamber")
a.Type("http://192.168.1.5:3000", srv.URL)
a.Type("密码(可选)", "secret")
a.Tap("连接")
connectViaForm(t, a, srv.URL)
unlockWith(t, a, "secret")
a.WaitFor("First session")
a.ExpectNo("密码(可选)")
if got := st.connection().URL; got != srv.URL {
t.Errorf("connection not saved: %q", got)
conns := st.connections()
if len(conns) != 1 || conns[0].URL != srv.URL {
t.Fatalf("connections = %+v", conns)
}
if got := st.token(srv.URL); got != "oc_client_test" {
t.Errorf("token not saved: %q", got)
if got := st.token(conns[0].ID); got != "oc_client_test" {
t.Errorf("token = %q", got)
}
if conns[0].Passwordless {
t.Error("password server marked passwordless")
}
}
func TestWrongPassword(t *testing.T) {
srv := fakeServer(t, "secret", "oc_client_test")
a := mount(t, &memStore{})
a.Type("http://192.168.1.5:3000", srv.URL)
a.Type("密码(可选)", "nope")
a.Tap("连接")
func TestWrongPasswordThenRetry(t *testing.T) {
srv := fakeServer(t, "secret", "oc_client_test", "")
st := &memStore{}
a := mount(t, st)
connectViaForm(t, a, srv.URL)
unlockWith(t, a, "nope")
a.WaitFor("密码错误")
// The prompt stays up so the password can be retried.
a.Type("密码", "secret")
a.Tap("解锁")
a.WaitFor("First session")
}
func TestAutoReconnectWithStoredToken(t *testing.T) {
srv := fakeServer(t, "secret", "oc_client_test")
srv := fakeServer(t, "secret", "oc_client_test", "")
st := &memStore{
conn: storage.Connection{URL: srv.URL},
has: true,
tokens: map[string]string{srv.URL: "oc_client_test"},
conns: []storage.Connection{{ID: "a", URL: srv.URL, Label: "Home", LastUsedAt: 1}},
tokens: map[string]string{"a": "oc_client_test"},
}
a := mount(t, st)
a.WaitFor("First session")
a.ExpectNo("连接到 OpenChamber")
a.ExpectNo("手动输入地址")
a.ExpectNo("这台服务器需要密码。")
}
func TestExpiredTokenAsksForPassword(t *testing.T) {
srv := fakeServer(t, "secret", "oc_client_test")
srv := fakeServer(t, "secret", "oc_client_test", "")
st := &memStore{
conn: storage.Connection{URL: srv.URL},
has: true,
tokens: map[string]string{srv.URL: "revoked"},
conns: []storage.Connection{{ID: "a", URL: srv.URL, Label: "Home", LastUsedAt: 1}},
tokens: map[string]string{"a": "revoked"},
}
a := mount(t, st)
a.WaitFor("登录已失效,请输入密码重新连接")
a.Expect("连接到 OpenChamber")
a.Until("the revoked token to be dropped", func() bool { return st.token(srv.URL) == "" })
a.WaitFor("登录已失效,请重新输入密码")
a.Until("the revoked token to be dropped", func() bool { return st.token("a") == "" })
unlockWith(t, a, "secret")
a.WaitFor("First session")
}
func TestPasswordlessServer(t *testing.T) {
srv := fakeServer(t, "", "")
srv := fakeServer(t, "", "", "")
st := &memStore{}
a := mount(t, st)
a.Type("http://192.168.1.5:3000", srv.URL)
a.Tap("连接")
connectViaForm(t, a, srv.URL)
a.WaitFor("First session")
if got := st.connection(); !got.Passwordless {
t.Errorf("passwordless not remembered: %+v", got)
conns := st.connections()
if len(conns) != 1 || !conns[0].Passwordless {
t.Fatalf("connections = %+v", conns)
}
if got := st.token(conns[0].ID); got != "" {
t.Errorf("unexpected token %q", got)
}
}
func TestAutoReconnectPasswordlessWithoutToken(t *testing.T) {
srv := fakeServer(t, "", "")
t.Cleanup(srv.Close)
st := &memStore{
conn: storage.Connection{URL: srv.URL, Passwordless: true},
has: true,
}
func TestMultipleConnectionsAndSwitch(t *testing.T) {
srvA := fakeServer(t, "", "", "Session A")
srvB := fakeServer(t, "", "", "Session B")
st := &memStore{conns: []storage.Connection{
{ID: "a", URL: srvA.URL, Label: "A", Passwordless: true, LastUsedAt: 2},
{ID: "b", URL: srvB.URL, Label: "B", Passwordless: true, LastUsedAt: 1},
}}
a := mount(t, st)
a.WaitFor("First session")
a.ExpectNo("请重新输入密码")
a.WaitFor("Session A")
a.Tap("断开")
a.Expect("A")
a.Expect("B")
a.Tap("B")
a.WaitFor("Session B")
}
func TestAddAnotherConnection(t *testing.T) {
srvA := fakeServer(t, "", "", "Session A")
srvB := fakeServer(t, "", "", "Session B")
st := &memStore{conns: []storage.Connection{
{ID: "a", URL: srvA.URL, Label: "A", Passwordless: true, LastUsedAt: 1},
}}
a := mount(t, st)
a.WaitFor("Session A")
a.Tap("断开")
a.Tap("添加服务器")
a.Type("http://192.168.1.5:3000", srvB.URL)
a.Type("名称(可选)", "B")
a.Tap("连接")
a.WaitFor("Session B")
if conns := st.connections(); len(conns) != 2 {
t.Fatalf("connections = %+v", conns)
}
}
func TestDeleteConnection(t *testing.T) {
srv := fakeServer(t, "", "", "Session A")
st := &memStore{conns: []storage.Connection{
{ID: "a", URL: srv.URL, Label: "A", Passwordless: true, LastUsedAt: 1},
}}
a := mount(t, st)
a.WaitFor("Session A")
a.Tap("断开")
a.Tap("操作")
a.Tap("删除")
a.Tap("删除")
a.Until("the connection list to empty", func() bool { return len(st.connections()) == 0 })
a.Expect("手动输入地址")
}