feat: port the client to MyGo native UI (0.3.0)

Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
This commit is contained in:
OpenChamber committed 2026-11-18 13:22:59 +08:00
1 parent 2fb4a8ea79
commit 6c0ee61750
2538 files changed
+675779 -4792

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
// Package schemehost maps URLs of the app's custom schemes to the
// http://*.localhost URLs that serve them where the webview can't load a
// custom scheme itself (WebView2), and back.
//
// <scheme>://localhost/… is served at http://<scheme>.localhost/…, and
// <scheme>://<host>/… at http://<host>.<scheme>.localhost/…. Chromium
// treats every *.localhost host as a secure origin on the loopback
// interface, so each host stays its own origin, as with the custom schemes
// of the other webviews.
//
// Hosts are names of DNS labels (letters, digits and hyphens, at most 63
// each, not starting or ending with a hyphen) separated by dots, as
// Chromium needs them in an http URL. They are lower-cased: hosts are
// case-insensitive, and the webview reports them in lower case. URLs with
// another host, a port, user info or no host aren't mapped, so the webview
// loads them as they are.
package schemehost
import (
"net/url"
"strings"
)
const suffix = ".localhost"
// WebURL maps raw, a URL of a scheme that has reports as the app's, to
// the http URL that serves it. Other URLs are returned as they are.
func WebURL(raw string, has func(scheme string) bool) string { return WebURLOf(raw, "http", has) }
// WebURLOf is WebURL with the scheme of the URLs that serve the app's,
// http or https: Android's web view serves them at https URLs, which are
// secure contexts and take no exception for cleartext traffic.
func WebURLOf(raw, webScheme string, has func(scheme string) bool) string {
u, err := url.Parse(raw)
if err != nil || u.Opaque != "" || u.User != nil || !has(u.Scheme) {
return raw
}
host := strings.ToLower(u.Host)
if host != "localhost" && !validHost(host) {
return raw // a port, no host, or a host that isn't a DNS name
}
web := u.Scheme + suffix
if host != "localhost" {
web = host + "." + web
}
// The mapping must come back: with schemes a and b.a, b://x/ and
// a://x.b/ would both be http://x.b.a.localhost/.
if s, h, ok := AppHost(web, has); !ok || s != u.Scheme || h != host {
return raw
}
u.Scheme, u.Host = webScheme, web
return u.String()
}
// AppURL maps raw, an http URL that serves a scheme that has reports as
// the app's, back to the URL of that scheme. Other URLs are returned as
// they are.
func AppURL(raw string, has func(scheme string) bool) string { return AppURLOf(raw, "http", has) }
// AppURLOf is AppURL for URLs served at URLs of webScheme (WebURLOf).
func AppURLOf(raw, webScheme string, has func(scheme string) bool) string {
u, err := url.Parse(raw)
if err != nil || u.Scheme != webScheme || u.User != nil || u.Port() != "" {
return raw
}
scheme, host, ok := AppHost(u.Hostname(), has)
if !ok {
return raw
}
u.Scheme, u.Host = scheme, host
return u.String()
}
// AppOrigin maps origin, the origin of a page that an http URL serving a
// scheme of the app loaded, back to the origin of that scheme, such as
// http://abc.myapp.localhost to myapp://abc. Other origins are returned
// as they are.
func AppOrigin(origin string, has func(scheme string) bool) string {
u, err := url.Parse(origin)
if err != nil || u.Scheme != "http" || u.User != nil || u.Port() != "" || (u.Path != "" && u.Path != "/") {
return origin
}
scheme, host, ok := AppHost(u.Hostname(), has)
if !ok {
return origin
}
return scheme + "://" + host
}
// AppHost splits webHost, the host of an http URL, into the scheme and
// host of the app's URL it serves: <scheme>.localhost is <scheme>://localhost,
// and <host>.<scheme>.localhost is <scheme>://<host>. The longest scheme
// that has reports wins.
func AppHost(webHost string, has func(scheme string) bool) (scheme, host string, ok bool) {
rest, found := strings.CutSuffix(strings.ToLower(webHost), suffix)
if !found || rest == "" {
return "", "", false
}
for i := 0; ; {
scheme = rest[i:]
if has(scheme) {
if i == 0 {
return scheme, "localhost", true
}
host = rest[:i-1]
return scheme, host, validHost(host)
}
j := strings.IndexByte(scheme, '.')
if j < 0 {
return "", "", false
}
i += j + 1
}
}
// validHost reports whether host is lower-case DNS labels separated by
// dots.
func validHost(host string) bool {
if host == "" || len(host) > 253 {
return false
}
for _, label := range strings.Split(host, ".") {
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
return false
}
for i := 0; i < len(label); i++ {
c := label[i]
if !('a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-') {
return false
}
}
}
return true
}
+121
View File
@@ -0,0 +1,121 @@
package schemehost
import "testing"
func schemes(names ...string) func(string) bool {
return func(s string) bool {
for _, n := range names {
if n == s {
return true
}
}
return false
}
}
func TestWebURL(t *testing.T) {
has := schemes("myapp", "mygo", "com.example.app")
for _, c := range []struct{ in, want string }{
{"myapp://localhost/index.html?a=1#x", "http://myapp.localhost/index.html?a=1#x"},
{"myapp://abc/page", "http://abc.myapp.localhost/page"},
{"myapp://ABC/Page", "http://abc.myapp.localhost/Page"},
{"myapp://a-1.b2/", "http://a-1.b2.myapp.localhost/"},
{"myapp://0123456789abcdef/x", "http://0123456789abcdef.myapp.localhost/x"},
{"com.example.app://localhost/", "http://com.example.app.localhost/"},
{"com.example.app://win/", "http://win.com.example.app.localhost/"},
// Not mapped.
{"myapp://abc:8080/", "myapp://abc:8080/"},
{"myapp://localhost:1/", "myapp://localhost:1/"},
{"myapp://u@abc/", "myapp://u@abc/"},
{"myapp:///path", "myapp:///path"},
{"myapp:opaque", "myapp:opaque"},
{"myapp://-abc/", "myapp://-abc/"},
{"myapp://ab_c/", "myapp://ab_c/"},
{"myapp://a..b/", "myapp://a..b/"},
{"myapp://%C3%A9t%C3%A9/", "myapp://%C3%A9t%C3%A9/"},
{"other://abc/", "other://abc/"},
{"https://abc/", "https://abc/"},
} {
if got := WebURL(c.in, has); got != c.want {
t.Errorf("WebURL(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestWebURLAmbiguous(t *testing.T) {
// b.a://x/ and a://x.b/ would share http://x.b.a.localhost/: the longer
// scheme keeps it.
has := schemes("a", "b.a")
if got := WebURL("b.a://x/", has); got != "http://x.b.a.localhost/" {
t.Errorf("WebURL(b.a://x/) = %q", got)
}
if got := WebURL("a://x.b/", has); got != "a://x.b/" {
t.Errorf("WebURL(a://x.b/) = %q, want it unmapped", got)
}
if got := WebURL("a://b/", has); got != "a://b/" { // http://b.a.localhost/ is b.a://localhost/
t.Errorf("WebURL(a://b/) = %q, want it unmapped", got)
}
}
func TestAppURL(t *testing.T) {
has := schemes("myapp", "com.example.app")
for _, c := range []struct{ in, want string }{
{"http://myapp.localhost/index.html?a=1#x", "myapp://localhost/index.html?a=1#x"},
{"http://abc.myapp.localhost/page", "myapp://abc/page"},
{"http://ABC.MyApp.localhost/page", "myapp://abc/page"},
{"http://a.b.myapp.localhost/", "myapp://a.b/"},
{"http://com.example.app.localhost/", "com.example.app://localhost/"},
{"http://win.com.example.app.localhost/", "com.example.app://win/"},
// Not ours.
{"http://other.localhost/", "http://other.localhost/"},
{"http://abc.other.localhost/", "http://abc.other.localhost/"},
{"http://localhost/", "http://localhost/"},
{"http://myapp.localhost:8080/", "http://myapp.localhost:8080/"},
{"http://abc.myapp.localhost:8080/", "http://abc.myapp.localhost:8080/"},
{"https://abc.myapp.localhost/", "https://abc.myapp.localhost/"},
{"http://myapp.example.com/", "http://myapp.example.com/"},
{"http://a_b.myapp.localhost/", "http://a_b.myapp.localhost/"},
} {
if got := AppURL(c.in, has); got != c.want {
t.Errorf("AppURL(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestRoundTrip(t *testing.T) {
has := schemes("myapp", "com.example.app")
for _, u := range []string{
"myapp://localhost/",
"myapp://abc/page?q=1#h",
"myapp://a.b.c/x/y",
"myapp://localhost.abc/",
"com.example.app://win-2/",
} {
web := WebURL(u, has)
if web == u {
t.Errorf("WebURL(%q) not mapped", u)
}
if got := AppURL(web, has); got != u {
t.Errorf("AppURL(WebURL(%q)) = %q", u, got)
}
}
}
func TestAppOrigin(t *testing.T) {
has := schemes("myapp", "mygo")
for _, c := range []struct{ in, want string }{
{"http://mygo.localhost", "mygo://localhost"},
{"http://myapp.localhost", "myapp://localhost"},
{"http://abc.myapp.localhost", "myapp://abc"},
{"http://a.b.myapp.localhost", "myapp://a.b"},
{"http://abc.myapp.localhost:5173", "http://abc.myapp.localhost:5173"},
{"http://abc.other.localhost", "http://abc.other.localhost"},
{"https://abc.myapp.localhost", "https://abc.myapp.localhost"},
{"http://localhost:5173", "http://localhost:5173"},
{"null", "null"},
} {
if got := AppOrigin(c.in, has); got != c.want {
t.Errorf("AppOrigin(%q) = %q, want %q", c.in, got, c.want)
}
}
}