|
| 1 | +// JayJay service worker (Cloudflare Worker, standard Go -> WASM). |
| 2 | +// |
| 3 | +// GET /appcast.xml — macOS/Sparkle: log the profiling query params Sparkle |
| 4 | +// appends, then proxy the real appcast (APPCAST_ORIGIN). |
| 5 | +// The EdDSA signature is verified in-app, so the proxy |
| 6 | +// cannot tamper with updates. |
| 7 | +// GET /ping — GPUI (Linux/Windows): log app version + OS + arch. |
| 8 | +// |
| 9 | +// Privacy: no IP or personal data is stored. The daily-unique key is a salted |
| 10 | +// SHA-256 of (IP + day + HASH_SECRET); the raw IP never leaves this function. |
| 11 | +package main |
| 12 | + |
| 13 | +import ( |
| 14 | + "crypto/sha256" |
| 15 | + "database/sql" |
| 16 | + "encoding/hex" |
| 17 | + "io" |
| 18 | + "net/http" |
| 19 | + "strconv" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/syumai/workers" |
| 23 | + "github.com/syumai/workers/cloudflare" |
| 24 | + _ "github.com/syumai/workers/cloudflare/d1" // registers the "d1" sql driver |
| 25 | + "github.com/syumai/workers/cloudflare/fetch" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + db, err := sql.Open("d1", "DB") |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { |
| 34 | + logEvent(db, r, "gpui") |
| 35 | + w.Write([]byte("ok")) |
| 36 | + }) |
| 37 | + appcastHandler := func(w http.ResponseWriter, r *http.Request) { |
| 38 | + logEvent(db, r, "swiftui") |
| 39 | + proxyAppcast(w, r) |
| 40 | + } |
| 41 | + http.HandleFunc("/appcast.xml", appcastHandler) |
| 42 | + http.HandleFunc("/", appcastHandler) |
| 43 | + workers.Serve(nil) // use http.DefaultServeMux |
| 44 | +} |
| 45 | + |
| 46 | +func proxyAppcast(w http.ResponseWriter, r *http.Request) { |
| 47 | + req, err := fetch.NewRequest(r.Context(), http.MethodGet, cloudflare.Getenv("APPCAST_ORIGIN"), nil) |
| 48 | + if err != nil { |
| 49 | + w.WriteHeader(http.StatusInternalServerError) |
| 50 | + return |
| 51 | + } |
| 52 | + res, err := fetch.NewClient().Do(req, nil) |
| 53 | + if err != nil { |
| 54 | + w.WriteHeader(http.StatusBadGateway) |
| 55 | + return |
| 56 | + } |
| 57 | + defer res.Body.Close() |
| 58 | + w.Header().Set("content-type", "application/xml; charset=utf-8") |
| 59 | + io.Copy(w, res.Body) |
| 60 | +} |
| 61 | + |
| 62 | +func logEvent(db *sql.DB, r *http.Request, channel string) { |
| 63 | + day := time.Now().Unix() / 86400 |
| 64 | + unique := dailyUnique(r.Header.Get("CF-Connecting-IP"), day, cloudflare.Getenv("HASH_SECRET")) |
| 65 | + |
| 66 | + q := r.URL.Query() |
| 67 | + version := firstNonEmpty(q.Get("version"), q.Get("appVersionShort"), q.Get("appVersion")) |
| 68 | + osName := orDefault(q.Get("os"), appcastDefault(channel, "macos")) |
| 69 | + osVersion := firstNonEmpty(q.Get("osver"), q.Get("osVersion")) |
| 70 | + arch := q.Get("arch") |
| 71 | + if arch == "" { |
| 72 | + arch = archFromCPUType(q.Get("cputype")) |
| 73 | + } |
| 74 | + platform := orDefault(q.Get("platform"), appcastDefault(channel, "macos")) |
| 75 | + |
| 76 | + // Fire-and-forget: a logging failure must never break the update check. |
| 77 | + _, _ = db.ExecContext(r.Context(), |
| 78 | + `INSERT INTO pings (day, unique_key, channel, platform, os, os_version, arch, version, model) |
| 79 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, |
| 80 | + day, unique, channel, platform, osName, osVersion, arch, version, q.Get("model")) |
| 81 | +} |
| 82 | + |
| 83 | +func dailyUnique(ip string, day int64, secret string) string { |
| 84 | + h := sha256.Sum256([]byte(ip + "|" + strconv.FormatInt(day, 10) + "|" + secret)) |
| 85 | + return hex.EncodeToString(h[:12]) |
| 86 | +} |
| 87 | + |
| 88 | +// Sparkle sends a mach-o cputype; map the common ones, pass anything else through. |
| 89 | +func archFromCPUType(cputype string) string { |
| 90 | + switch cputype { |
| 91 | + case "16777223": |
| 92 | + return "x86_64" |
| 93 | + case "16777228": |
| 94 | + return "arm64" |
| 95 | + case "7": |
| 96 | + return "x86" |
| 97 | + default: |
| 98 | + return cputype |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func firstNonEmpty(vals ...string) string { |
| 103 | + for _, v := range vals { |
| 104 | + if v != "" { |
| 105 | + return v |
| 106 | + } |
| 107 | + } |
| 108 | + return "" |
| 109 | +} |
| 110 | + |
| 111 | +func orDefault(v, fallback string) string { |
| 112 | + if v == "" { |
| 113 | + return fallback |
| 114 | + } |
| 115 | + return v |
| 116 | +} |
| 117 | + |
| 118 | +func appcastDefault(channel, value string) string { |
| 119 | + if channel == "swiftui" { |
| 120 | + return value |
| 121 | + } |
| 122 | + return "" |
| 123 | +} |
0 commit comments