Files
Reims-Go/internal/app/arm_image.go

77 lines
2.5 KiB
Go

package app
import (
"context"
"errors"
"net/http"
"os"
"path/filepath"
"time"
"github.com/Nwflower/reims-go/internal/fetch"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/netproxy"
)
const (
armImageBuild = "26B131"
armImageName = "UniversalMac_27.2.1_26B131_Restore.ipsw"
armImageURL = "https://updates.cdn-apple.com/2026FallFCS/c202a724-db7e-4d56-917b-110aa0226507/" + armImageName
armImageSHA = "233e85a09887579d47d0b195e7db72be7f204261059c6946f719acf6b300814f"
armImageSize = int64(25595954706)
)
// DownloadARMImage fetches the pinned ARM restore image from Apple into the user data folder.
func (a *App) DownloadARMImage() (string, error) {
cfg := a.runtimeConfig()
if err := cfg.EnsureDirs(); err != nil {
return "", err
}
final := filepath.Join(cfg.ImagesDir(), "arm64", armImageName)
if err := os.MkdirAll(filepath.Dir(final), 0o755); err != nil {
return "", err
}
a.ensureTaskEmitter()
var ctx context.Context
var cancel context.CancelFunc
run, id, owned := installerDownloads.reserveOrFind("arm-ipsw", armImageBuild, "installer-download", func(initialPhase string) string {
ctx, cancel = context.WithCancel(a.baseCtx())
return tasks.startWithInstaller("installer-download", "macOS 27.2.1 路 ARM64", "", "arm-ipsw", armImageBuild, initialPhase, cancel)
})
if !owned {
return id, nil
}
go func() {
defer cancel()
client := netproxy.NewClient()
client.Timeout = 0
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 || req.URL.Scheme != "https" || req.URL.Hostname() != "updates.cdn-apple.com" {
return errors.New("ARM restore image download redirected outside Apple's update server")
}
return nil
}
path, err := fetch.ResumeDirect(ctx, client, armImageURL, final, armImageSize, armImageSHA, func(done, total int64) {
update := host.Update{Kind: host.KindProgress, Done: done, Total: total, Percent: percent(done, total)}
tasks.update(id, update)
run.publish(update)
}, fetch.ResumeOptions{MaxBytes: armImageSize, StallTimeout: 45 * time.Second})
if err == nil {
if info, statErr := os.Stat(path); statErr != nil {
err = statErr
} else if info.Size() != armImageSize {
err = errors.New("ARM restore image size does not match the pinned build")
}
}
if err != nil && ctx.Err() != nil {
err = ctx.Err()
}
if err != nil {
path = ""
}
installerDownloads.completeImage(run, path, err)
tasks.finish(id, path, err)
}()
return id, nil
}