ARTEX-English/selfupdate/github.go
skilfoy 7120c7344f Rewrite remaining package comments into natural English
Rewrite selfupdate comments and user-facing error text from machine English into the meaning of the upstream Chinese.
2026-10-06 23:20:43 +00:00

144 lines
4.8 KiB
Go

package selfupdate
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
// Repo is the release source. It is hard-coded rather than configurable:
// a configurable update source would be a remote code-execution channel for
// anyone who can change settings, and a penetration-testing platform cannot
// leave that open.
const Repo = "skilfoy/ARTEX-English"
// latestURL is GitHub's "latest stable release" endpoint. It skips prereleases and drafts.
const latestURL = "https://api.github.com/repos/" + Repo + "/releases/latest"
// allowedHosts limits which hosts the upgrade path may contact. Together with
// checkRedirect below, any hop redirected to a host outside this list fails
// immediately. That is the first gate against DNS poisoning or a
// man-in-the-middle swapping the binary; the second is the SHA256SUMS check.
var allowedHosts = map[string]bool{
"api.github.com": true,
"github.com": true,
"objects.githubusercontent.com": true, // object storage where release assets actually live
"release-assets.githubusercontent.com": true,
"raw.githubusercontent.com": true,
}
// Release is the subset of a GitHub Release this package cares about.
type Release struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
PublishedAt time.Time `json:"published_at"`
HTMLURL string `json:"html_url"`
Assets []Asset `json:"assets"`
}
// Asset is one file attached to a Release.
type Asset struct {
Name string `json:"name"`
URL string `json:"browser_download_url"`
Size int64 `json:"size"`
}
// NewClient builds an HTTP client that only accepts GitHub hosts. An empty proxy dials directly.
//
// The default Transport is deliberately not reused: the upgrade path must use
// TLS and verify certificates, and must not inherit InsecureSkipVerify (or
// similar) set somewhere else.
func NewClient(proxy string) *http.Client {
tr := &http.Transport{
ForceAttemptHTTP2: true,
TLSHandshakeTimeout: 15 * time.Second,
}
if p := strings.TrimSpace(proxy); p != "" {
if pu, err := url.Parse(p); err == nil {
tr.Proxy = http.ProxyURL(pu)
}
}
return &http.Client{
Transport: tr,
Timeout: 30 * time.Minute, // whole-archive download; a per-request timeout would abort it
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return fmt.Errorf("too many redirects")
}
return checkURL(req.URL)
},
}
}
// checkURL requires https and a host on the allowlist.
func checkURL(u *url.URL) error {
if u.Scheme != "https" {
return fmt.Errorf("refusing non-HTTPS URL: %s", u.Scheme+"://"+u.Host)
}
if !allowedHosts[strings.ToLower(u.Hostname())] {
return fmt.Errorf("refusing non-GitHub host: %s", u.Hostname())
}
return nil
}
// FetchLatest queries the latest stable release.
func FetchLatest(ctx context.Context, c *http.Client) (*Release, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, latestURL, nil)
if err != nil {
return nil, err
}
if err := checkURL(req.URL); err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "artex-selfupdate")
resp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to reach GitHub (a global proxy can be set in system settings): %w", err)
}
defer resp.Body.Close()
switch {
case resp.StatusCode == http.StatusForbidden, resp.StatusCode == http.StatusTooManyRequests:
// Unauthenticated GitHub API access is 60 requests per IP per hour, which
// a shared egress IP hits easily.
return nil, fmt.Errorf("GitHub API rate limited (60 requests per hour); try again later")
case resp.StatusCode == http.StatusNotFound:
return nil, fmt.Errorf("repository %s has not published a stable release", Repo)
case resp.StatusCode != http.StatusOK:
return nil, fmt.Errorf("GitHub returned %d", resp.StatusCode)
}
var rel Release
if err := json.NewDecoder(resp.Body).Decode(&rel); err != nil {
return nil, fmt.Errorf("failed to parse release: %w", err)
}
if strings.TrimSpace(rel.TagName) == "" {
return nil, fmt.Errorf("release is missing a tag")
}
return &rel, nil
}
// AssetName returns the archive name for the current platform, matching
// package_binary in build.sh: artex-<version>-<os>-<arch>.zip (version without a v prefix).
func AssetName(tag, goos, goarch string) string {
return fmt.Sprintf("artex-%s-%s-%s.zip", strings.TrimPrefix(tag, "v"), goos, goarch)
}
// FindAsset looks up an asset by name.
func (r *Release) FindAsset(name string) (Asset, bool) {
for _, a := range r.Assets {
if strings.EqualFold(a.Name, name) {
return a, true
}
}
return Asset{}, false
}