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:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
+178
@@ -0,0 +1,178 @@
|
||||
// Package nsis pins the NSIS that mygo build downloads on Windows to make
|
||||
// the installer, and installs it into the user's cache directory, where
|
||||
// makensis runs as it does from an installation. CI downloads it with
|
||||
// internal/prefetch before the tests run.
|
||||
package nsis
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/egoist/mygo/internal/download"
|
||||
)
|
||||
|
||||
// Release is the NSIS that mygo build downloads: the official zip,
|
||||
// checked against its SHA-256. The URLs are copies of that zip, tried in
|
||||
// order: the asset of the nsis-<version> release of MyGo's repository
|
||||
// first, since SourceForge, where NSIS publishes it, has been down for
|
||||
// hours at a time, its mirrors redirecting to it.
|
||||
var Release = struct {
|
||||
Version, SHA256 string
|
||||
URLs []string
|
||||
}{
|
||||
Version: "3.13",
|
||||
SHA256: "ba63dffc4410ee89193e1cb5a41989991bd77c61068da17e3156d136b7b0b3d8",
|
||||
URLs: []string{
|
||||
"https://github.com/egoist/mygo/releases/download/nsis-3.13/nsis-3.13.zip",
|
||||
"https://downloads.sourceforge.net/project/nsis/NSIS%203/3.13/nsis-3.13.zip",
|
||||
},
|
||||
}
|
||||
|
||||
// Dir is where mygo build keeps the NSIS it downloads, or "" without a
|
||||
// cache directory.
|
||||
func Dir() string {
|
||||
cache, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(cache, "mygo", "nsis-"+Release.Version)
|
||||
}
|
||||
|
||||
// Executable returns makensis in the NSIS directory dir, or "".
|
||||
func Executable(dir string) string {
|
||||
for _, p := range []string{filepath.Join(dir, "Bin", "makensis.exe"), filepath.Join(dir, "makensis.exe")} {
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Download downloads Release into dir, unless it is there already, and
|
||||
// returns its makensis; file makes the download of each URL. The archive
|
||||
// is unpacked next to dir and renamed into place, so dir is complete
|
||||
// whenever it exists, and processes that need it at the same time
|
||||
// download it once: the others wait, then find it.
|
||||
func Download(ctx context.Context, dir string, file func(url string) download.File) (string, error) {
|
||||
if p := Executable(dir); p != "" {
|
||||
return p, nil
|
||||
}
|
||||
parent := filepath.Dir(dir)
|
||||
if err := os.MkdirAll(parent, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
unlock, err := download.Lock(dir+".lock", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer unlock()
|
||||
if p := Executable(dir); p != "" {
|
||||
return p, nil
|
||||
}
|
||||
work, err := os.MkdirTemp(parent, ".nsis-")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.RemoveAll(work)
|
||||
archive := filepath.Join(work, "nsis.zip")
|
||||
var errs []error
|
||||
for _, url := range Release.URLs {
|
||||
f := file(url)
|
||||
f.SHA256 = Release.SHA256
|
||||
err := download.Get(ctx, f, archive)
|
||||
if err == nil {
|
||||
errs = nil
|
||||
break
|
||||
}
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return "", errors.Join(errs...)
|
||||
}
|
||||
unpacked := filepath.Join(work, "nsis")
|
||||
if err := unzipTop(archive, unpacked); err != nil {
|
||||
return "", fmt.Errorf("unpacking NSIS: %w", err)
|
||||
}
|
||||
if Executable(unpacked) == "" {
|
||||
return "", errors.New("unpacking NSIS: the archive holds no makensis.exe")
|
||||
}
|
||||
if err := os.Rename(unpacked, dir); err != nil {
|
||||
// Anything there is broken, or it would have been found above.
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.Rename(unpacked, dir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return Executable(dir), nil
|
||||
}
|
||||
|
||||
// unzipTop unpacks the zip archive at path, whose entries are all in one
|
||||
// top directory, into dir without that directory.
|
||||
func unzipTop(path, dir string) error {
|
||||
r, err := zip.OpenReader(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Close()
|
||||
top := ""
|
||||
for _, f := range r.File {
|
||||
first, rest, ok := strings.Cut(f.Name, "/")
|
||||
if top == "" {
|
||||
top = first
|
||||
}
|
||||
if !ok || first != top {
|
||||
return fmt.Errorf("%q is not in the top directory %q", f.Name, top)
|
||||
}
|
||||
if rest == "" {
|
||||
continue // the top directory
|
||||
}
|
||||
name := strings.TrimSuffix(rest, "/")
|
||||
if !fs.ValidPath(name) || !filepath.IsLocal(filepath.FromSlash(name)) || strings.Contains(name, `\`) {
|
||||
return fmt.Errorf("invalid path %q", f.Name)
|
||||
}
|
||||
target := filepath.Join(dir, filepath.FromSlash(name))
|
||||
switch mode := f.Mode(); {
|
||||
case mode.IsDir():
|
||||
if err := os.MkdirAll(target, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
case mode.IsRegular():
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := unzipFile(f, target); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("%q is neither a file nor a directory", f.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unzipFile(f *zip.File, target string) error {
|
||||
src, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
dst, err := os.Create(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The zip reader checks the CRC-32 of the file once it is read.
|
||||
_, err = io.Copy(dst, src)
|
||||
if cerr := dst.Close(); err == nil {
|
||||
err = cerr
|
||||
}
|
||||
return err
|
||||
}
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
package nsis
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/internal/download"
|
||||
)
|
||||
|
||||
// TestRelease checks that every copy of NSIS that mygo build tries is
|
||||
// the zip of its version, so that updating NSIS updates them all.
|
||||
func TestRelease(t *testing.T) {
|
||||
v := Release.Version
|
||||
for _, url := range Release.URLs {
|
||||
if !strings.HasSuffix(url, "/"+v+"/nsis-"+v+".zip") && !strings.HasSuffix(url, "/nsis-"+v+"/nsis-"+v+".zip") {
|
||||
t.Errorf("%s is not NSIS %s", url, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestDownload downloads NSIS from a test server into the cache once,
|
||||
// from the first copy that answers with the expected archive, and refuses
|
||||
// an archive that is not the expected one.
|
||||
func TestDownload(t *testing.T) {
|
||||
archive := zipArchive(t, "nsis-9.9/", "nsis-9.9/Bin/makensis.exe", "nsis-9.9/Include/MUI2.nsh")
|
||||
var requests atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests.Add(1)
|
||||
switch r.URL.Path {
|
||||
case "/hangs/nsis-9.9.zip":
|
||||
<-r.Context().Done()
|
||||
case "/down/nsis-9.9.zip":
|
||||
http.Error(w, "origin timeout", 522)
|
||||
case "/wrong/nsis-9.9.zip":
|
||||
io.WriteString(w, "<html>Not the archive</html>")
|
||||
default:
|
||||
w.Write(archive)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
saved, savedBackoff := Release, download.Backoff
|
||||
defer func() { Release, download.Backoff = saved, savedBackoff }()
|
||||
download.Backoff = make([]time.Duration, len(savedBackoff))
|
||||
sum := sha256.Sum256(archive)
|
||||
Release.SHA256 = hex.EncodeToString(sum[:])
|
||||
Release.URLs = []string{srv.URL + "/hangs/nsis-9.9.zip", srv.URL + "/down/nsis-9.9.zip", srv.URL + "/wrong/nsis-9.9.zip", srv.URL + "/nsis-9.9.zip"}
|
||||
// A host that takes the connection and never answers gives way to the
|
||||
// next copy, as the CLI's downloads give up after a while.
|
||||
file := func(url string) download.File {
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport.ResponseHeaderTimeout = 200 * time.Millisecond
|
||||
return download.File{URL: url, Client: &http.Client{Transport: transport}}
|
||||
}
|
||||
|
||||
cache := t.TempDir()
|
||||
dir := filepath.Join(cache, "nsis-9.9")
|
||||
tool, err := Download(context.Background(), dir, file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tool != filepath.Join(dir, "Bin", "makensis.exe") {
|
||||
t.Errorf("makensis = %s", tool)
|
||||
}
|
||||
if b, err := os.ReadFile(filepath.Join(dir, "Include", "MUI2.nsh")); err != nil || string(b) != "nsis-9.9/Include/MUI2.nsh" {
|
||||
t.Errorf("Include/MUI2.nsh = %q, %v", b, err)
|
||||
}
|
||||
// A host that answers 522 is asked again before the next copy is.
|
||||
if again, err := Download(context.Background(), dir, file); err != nil || again != tool || requests.Load() != int32(3+len(download.Backoff)+1) {
|
||||
t.Errorf("downloading again = %s, %v after %d requests", again, err, requests.Load())
|
||||
}
|
||||
|
||||
// Every copy fails, and the error says how.
|
||||
Release.SHA256 = strings.Repeat("0", 64)
|
||||
other := filepath.Join(cache, "nsis-other")
|
||||
_, err = Download(context.Background(), other, file)
|
||||
if err == nil {
|
||||
t.Fatal("downloaded an unexpected archive")
|
||||
}
|
||||
for _, s := range []string{
|
||||
srv.URL + "/hangs/nsis-9.9.zip\": net/http: timeout awaiting response headers",
|
||||
srv.URL + "/down/nsis-9.9.zip: 522",
|
||||
srv.URL + "/wrong/nsis-9.9.zip has the SHA-256",
|
||||
srv.URL + "/nsis-9.9.zip has the SHA-256",
|
||||
} {
|
||||
if !strings.Contains(err.Error(), s) {
|
||||
t.Errorf("the error does not say %q:\n%v", s, err)
|
||||
}
|
||||
}
|
||||
var names []string
|
||||
if entries, err := os.ReadDir(cache); err == nil {
|
||||
for _, e := range entries {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
}
|
||||
if want := []string{"nsis-9.9", "nsis-9.9.lock", "nsis-other.lock"}; !slices.Equal(names, want) {
|
||||
t.Errorf("the cache holds %v, want %v", names, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnzipTop unpacks the top directory of archives, and refuses entries
|
||||
// outside it.
|
||||
func TestUnzipTop(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
entries []string
|
||||
ok bool
|
||||
}{
|
||||
{[]string{"top/", "top/a/b.txt", "top/c.txt"}, true},
|
||||
{[]string{"top/a.txt", "other/b.txt"}, false},
|
||||
{[]string{"top/a.txt", "b.txt"}, false},
|
||||
{[]string{"top/../evil.txt"}, false},
|
||||
{[]string{"top/a/../../evil.txt"}, false},
|
||||
} {
|
||||
path := filepath.Join(t.TempDir(), "archive.zip")
|
||||
if err := os.WriteFile(path, zipArchive(t, tc.entries...), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), "out")
|
||||
err := unzipTop(path, dir)
|
||||
if (err == nil) != tc.ok {
|
||||
t.Errorf("%q: %v", tc.entries, err)
|
||||
continue
|
||||
}
|
||||
if tc.ok {
|
||||
for _, name := range []string{"a/b.txt", "c.txt"} {
|
||||
if b, err := os.ReadFile(filepath.Join(dir, filepath.FromSlash(name))); err != nil || string(b) != "top/"+name {
|
||||
t.Errorf("%s = %q, %v", name, b, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// zipArchive makes a zip archive of the named entries: directories end with
|
||||
// a slash, and files hold their names.
|
||||
func zipArchive(t *testing.T, names ...string) []byte {
|
||||
t.Helper()
|
||||
var b bytes.Buffer
|
||||
zw := zip.NewWriter(&b)
|
||||
for _, name := range names {
|
||||
w, err := zw.Create(name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasSuffix(name, "/") {
|
||||
io.WriteString(w, name)
|
||||
}
|
||||
}
|
||||
if err := zw.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
Reference in new issue
Block a user