Files
magpie/internal/update/fallback.go
T
yetone 65e1422c4a update: fall back to the GitHub release when usemagpie.ai can't be reached (ssharpcc on X)
A company network that allows github.com and no other domain could not
update magpie at all: the feed (usemagpie.ai/api/latest) failed and that
was the end. When the default feed fails (20s limit, so dropped packets
don't hold the check for 10 minutes), the newest release is now read from
yetone/magpie-releases: the API, or the release page's redirect when the
API is rate-limited, plus SHA256SUMS and SHA256SUMS.sig from github.com
(or the API route when github.com's address is busy). The result goes
through Release.verify like the feed's: ed25519 signature against the
embedded key, the version inside the signed message, every hash from the
signed list. Unlike the feed, a release read this way must be signed
whatever its version. Files download from github.com (Asset.GitHub),
checked by hash as before; feed files named on the site now also get a
github.com address, so a download falls back too. Covers the app,
`magpie update` and the Windows and Linux builds (all go through
latestAt). Not on Stable (the site's choice) nor on MAGPIE_UPDATE_FEED.

Run (mica, tree at this head): gofmt clean; go vet update/cli/gui, GOOS=windows
and linux vet of update; go test internal/update, internal/cli, internal/gui all ok.
New tests use a feed port nobody listens on and a fake GitHub (API and
rate-limited redirect paths): update succeeds and stages the file by hash;
wrong key, swapped sums, missing and junk signature, signature for another
version, unsigned old version are refused with no download. Broken by hand:
with verify() removed from the fallback, 4 of those tests go red.
Not tried: the real github.com, a real allow-listed network, Windows run.

Other usemagpie.ai-only on such a network: install.sh, plugin catalog
(falls to raw.githubusercontent.com, ok), npm registry for plugin
installs, models.dev catalog, partners/icons, telemetry.
2026-11-12 17:19:40 +08:00

208 lines
6.6 KiB
Go

package update
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/yetone/magpie/internal/source"
)
// A network that lets github.com through and nothing else (a company's
// allow list: ssharpcc on X) can't reach usemagpie.ai's feed. When the
// default feed fails, the newest release is read from GitHub itself:
// yetone/magpie-releases' API, or, when the API is rate-limited, the
// release page's redirect and the SHA256SUMS beside the files. Nothing
// is configured. The result goes through Release.verify like the feed's,
// and here a release must be signed whatever its version: the feed is the
// only thing that may carry an unsigned old release, and this isn't the
// feed. The files themselves are on github.com (Asset.GitHub), which
// download already takes, with its API route.
//
// The Stable channel is the site's choice (a release a week), which
// GitHub can't give: on Stable a failed feed stays a failed feed.
// releaseRepo is where releases are published.
const releaseRepo = "yetone/magpie-releases"
// feedWait is how long the feed has to answer when GitHub stands behind
// it: a network that drops usemagpie.ai's packets would otherwise hold
// the check for the client's ten minutes. A var for tests.
var feedWait = 20 * time.Second
// siteFeed is the default feed; a var for tests.
var siteFeed = Site + "/api/latest"
// maxSmall bounds SHA256SUMS and its signature.
const maxSmall = 1 << 20
// fallsBack says whether a failed feed goes on to GitHub: the default
// feed, not the Stable channel, and not an ask already cancelled.
func fallsBack(ctx context.Context) bool {
return ctx.Err() == nil && Feed() == siteFeed && !Stable()
}
// githubLatest is the newest release as GitHub gives it, verified.
func githubLatest(ctx context.Context, lang string) (*Release, error) {
tag, notes, page, files, err := githubTag(ctx)
if err != nil {
return nil, err
}
base := fmt.Sprintf("%s/%s/releases/download/%s/", githubWeb, releaseRepo, tag)
sums, err := githubFile(ctx, base+"SHA256SUMS", files["SHA256SUMS"].api)
if err != nil {
return nil, err
}
sig, err := githubFile(ctx, base+"SHA256SUMS.sig", files["SHA256SUMS.sig"].api)
if err != nil {
return nil, fmt.Errorf("%s has no signature: %w", tag, err)
}
rel := &Release{
Version: strings.TrimPrefix(tag, "v"),
Notes: InLang(notes, lang),
URL: page,
Assets: map[string]Asset{},
Signed: &Signed{Sums: sums, Sig: strings.TrimSpace(sig)},
}
if parse(rel.Version) == nil {
return nil, fmt.Errorf("GitHub's release tag %q is no version", tag)
}
// the names come from the signed list; verify() drops any other and
// gives each file its hash
for _, line := range strings.Split(sums, "\n") {
f := strings.Fields(line)
if len(f) != 2 {
continue
}
name := strings.TrimPrefix(f[1], "*")
if name == "" || strings.ContainsAny(name, `/\`) || name == "SHA256SUMS" || name == "SHA256SUMS.sig" {
continue
}
u := base + url.PathEscape(name)
rel.Assets[name] = Asset{URL: u, GitHub: u, API: files[name].api, Size: files[name].size}
}
if err := rel.verify(); err != nil {
return nil, err
}
return rel, nil
}
type ghFile struct {
api string
size int64
}
// githubTag is the newest release's tag, notes, page and the files the
// API lists. The API is asked first; limited or unreachable, the release
// page's redirect gives the tag alone.
func githubTag(ctx context.Context) (tag, notes, page string, files map[string]ghFile, err error) {
files = map[string]ghFile{}
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/repos/%s/releases/latest", githubAPI, releaseRepo), nil)
if err != nil {
return
}
req.Header.Set("Accept", "application/vnd.github+json")
res, aerr := source.Do(client, req)
if aerr == nil {
defer res.Body.Close()
if res.StatusCode == 200 {
var gh struct {
Tag string `json:"tag_name"`
Body string `json:"body"`
Page string `json:"html_url"`
Assets []struct {
Name string `json:"name"`
URL string `json:"url"`
Size int64 `json:"size"`
} `json:"assets"`
}
if derr := json.NewDecoder(io.LimitReader(res.Body, 8<<20)).Decode(&gh); derr == nil && gh.Tag != "" {
for _, a := range gh.Assets {
if strings.HasPrefix(a.URL, githubAPI+"/") {
files[a.Name] = ghFile{api: a.URL, size: a.Size}
}
}
return gh.Tag, gh.Body, gh.Page, files, nil
}
} else {
aerr = fmt.Errorf("GitHub's API: %s", res.Status)
}
}
// the release page: /releases/latest redirects to /releases/tag/<tag>
c := *client
c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
pr, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/%s/releases/latest", githubWeb, releaseRepo), nil)
if err != nil {
return
}
pres, perr := source.Do(&c, pr)
if perr != nil {
err = fmt.Errorf("GitHub: %w", errors.Join(aerr, perr))
return
}
defer pres.Body.Close()
loc, _ := url.Parse(pres.Header.Get("Location"))
if loc != nil {
if i := strings.LastIndex(loc.Path, "/releases/tag/"); i >= 0 {
tag = strings.Trim(loc.Path[i+len("/releases/tag/"):], "/")
}
}
if tag == "" || strings.Contains(tag, "/") {
tag = ""
err = fmt.Errorf("GitHub's release page named no release (%s)", pres.Status)
if aerr != nil {
err = fmt.Errorf("%w; the API: %v", err, aerr)
}
return
}
return tag, "", fmt.Sprintf("%s/%s/releases/tag/%s", githubWeb, releaseRepo, tag), files, nil
}
// githubFile reads a small release file from its github.com address (the
// mirror the user gave in front of it), else through the API route when
// that is known. Its bytes are trusted for nothing until the signature
// over them is checked.
func githubFile(ctx context.Context, raw, api string) (string, error) {
var errs []error
tries := []struct {
u string
api bool
}{{mirrored(mirrorOf(ctx), raw), false}, {api, true}}
for _, try := range tries {
if try.u == "" {
continue
}
req, err := http.NewRequestWithContext(ctx, "GET", try.u, nil)
if err != nil {
return "", err
}
if try.api {
req.Header.Set("Accept", "application/octet-stream")
}
res, err := source.Do(client, req)
if err != nil {
errs = append(errs, err)
continue
}
b, err := io.ReadAll(io.LimitReader(res.Body, maxSmall+1))
res.Body.Close()
switch {
case res.StatusCode != 200:
errs = append(errs, fmt.Errorf("%s: %s", hostOf(try.u), res.Status))
case err != nil:
errs = append(errs, err)
case len(b) > maxSmall:
errs = append(errs, fmt.Errorf("%s: too large", raw))
default:
return string(b), nil
}
}
return "", errors.Join(errs...)
}