196 lines
5.2 KiB
Go
196 lines
5.2 KiB
Go
package installer
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// PinnedAsset describes a file that upstream `reims-util` caches under
|
|
// `$XDG_CACHE_HOME/reims-util/<Name>` and verifies by SHA-256 before
|
|
// downloading (reims-util 7867384 src/cache.rs:20-51).
|
|
type PinnedAsset struct {
|
|
Name string
|
|
URL string
|
|
SHA256 string
|
|
Step string
|
|
}
|
|
|
|
// OcBinaryDataAsset matches upstream reims-util 7867384
|
|
// (src/opencore/mod.rs:31-38). GitHub's `/archive/<commit>.zip` ignores Range
|
|
// requests (always answering HTTP 200 without Content-Length), which interacts
|
|
// with reims-util's 180s SEGMENT_BUDGET (src/http.rs:25, 291-297, 342-346) to
|
|
// silently truncate and restart from 0 every 3 minutes on slow connections.
|
|
// Pre-fetching it with an idle-read timeout avoids that loop while keeping
|
|
// exact SHA-256 verification.
|
|
var OcBinaryDataAsset = PinnedAsset{
|
|
Name: "OcBinaryData-32500d5e3313f11cad00351ba1c6f9a4aa6cb0f7.zip",
|
|
URL: "https://github.com/acidanthera/OcBinaryData/archive/32500d5e3313f11cad00351ba1c6f9a4aa6cb0f7.zip",
|
|
SHA256: "1ced15b42352a3a6c1b76ca95951f337fa79899acfb4ed80f329191716623741",
|
|
Step: "downloading OpenCore's picker icons and boot chime",
|
|
}
|
|
|
|
// ShouldPrefetchOcBinaryData returns true when the installed runtime uses the
|
|
// reims-util commit (7867384 / 0.1.0) whose pinned OcBinaryData matches
|
|
// OcBinaryDataAsset.
|
|
func ShouldPrefetchOcBinaryData(utilCommit, utilVersion string) bool {
|
|
c := strings.ToLower(strings.TrimSpace(utilCommit))
|
|
if c != "" {
|
|
return strings.HasPrefix(c, "7867384")
|
|
}
|
|
return strings.Contains(utilVersion, "0.1.0")
|
|
}
|
|
|
|
// EnsureCachedAsset verifies that `<xdgCacheDir>/reims-util/<asset.Name>`
|
|
// exists with the expected SHA-256, downloading it via HTTP with a per-read
|
|
// idle timeout (instead of a total-body timeout) if missing or corrupt.
|
|
func EnsureCachedAsset(ctx context.Context, xdgCacheDir string, asset PinnedAsset, client *http.Client, onUpdate func(Update)) error {
|
|
if strings.TrimSpace(xdgCacheDir) == "" {
|
|
return nil
|
|
}
|
|
cacheDir := filepath.Join(xdgCacheDir, "reims-util")
|
|
dest := filepath.Join(cacheDir, asset.Name)
|
|
if ok, _ := fileMatchesSHA256(dest, asset.SHA256); ok {
|
|
return nil
|
|
}
|
|
if err := os.MkdirAll(cacheDir, 0o755); err != nil {
|
|
return err
|
|
}
|
|
if client == nil {
|
|
client = newIdleHTTPClient(60 * time.Second)
|
|
}
|
|
if onUpdate != nil {
|
|
onUpdate(Update{Kind: KindStep, Message: asset.Step, Percent: -1})
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, asset.URL, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("User-Agent", "reims-go")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("%s answered HTTP %d", asset.URL, resp.StatusCode)
|
|
}
|
|
|
|
part := dest + ".part"
|
|
f, err := os.Create(part)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
h := sha256.New()
|
|
total := resp.ContentLength
|
|
if total < 0 {
|
|
total = 0
|
|
}
|
|
buf := make([]byte, 64*1024)
|
|
var done int64
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
f.Close()
|
|
_ = os.Remove(part)
|
|
return err
|
|
}
|
|
n, rerr := resp.Body.Read(buf)
|
|
if n > 0 {
|
|
if _, werr := f.Write(buf[:n]); werr != nil {
|
|
f.Close()
|
|
_ = os.Remove(part)
|
|
return werr
|
|
}
|
|
_, _ = h.Write(buf[:n])
|
|
done += int64(n)
|
|
if onUpdate != nil {
|
|
onUpdate(Update{
|
|
Kind: KindProgress,
|
|
Step: asset.Step,
|
|
Done: done,
|
|
Total: total,
|
|
Percent: percentOf(done, total),
|
|
})
|
|
}
|
|
}
|
|
if rerr == io.EOF {
|
|
break
|
|
}
|
|
if rerr != nil {
|
|
f.Close()
|
|
_ = os.Remove(part)
|
|
return rerr
|
|
}
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
_ = os.Remove(part)
|
|
return err
|
|
}
|
|
|
|
got := hex.EncodeToString(h.Sum(nil))
|
|
if !strings.EqualFold(got, asset.SHA256) {
|
|
_ = os.Remove(part)
|
|
return fmt.Errorf("%s SHA-256 mismatch: got %s, want %s", asset.Name, got, asset.SHA256)
|
|
}
|
|
_ = os.Remove(dest)
|
|
if err := os.Rename(part, dest); err != nil {
|
|
_ = os.Remove(part)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func fileMatchesSHA256(path, want string) (bool, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer f.Close()
|
|
h := sha256.New()
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
return false, err
|
|
}
|
|
return strings.EqualFold(hex.EncodeToString(h.Sum(nil)), want), nil
|
|
}
|
|
|
|
// idleTimeoutConn resets the socket read deadline before every Read, so a slow
|
|
// but steadily progressing stream is never killed by a fixed total duration.
|
|
type idleTimeoutConn struct {
|
|
net.Conn
|
|
idle time.Duration
|
|
}
|
|
|
|
func (c *idleTimeoutConn) Read(p []byte) (int, error) {
|
|
if c.idle > 0 {
|
|
_ = c.Conn.SetReadDeadline(time.Now().Add(c.idle))
|
|
}
|
|
return c.Conn.Read(p)
|
|
}
|
|
|
|
func newIdleHTTPClient(idle time.Duration) *http.Client {
|
|
dialer := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
|
|
tr := &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
conn, err := dialer.DialContext(ctx, network, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &idleTimeoutConn{Conn: conn, idle: idle}, nil
|
|
},
|
|
TLSHandshakeTimeout: 30 * time.Second,
|
|
ResponseHeaderTimeout: idle,
|
|
}
|
|
return &http.Client{Transport: tr}
|
|
}
|