[WIP] Refactoring

This commit is contained in:
Vladimir Stoilov
2024-09-20 17:36:07 +03:00
parent b3ff6f14f1
commit 89b533f949
7 changed files with 587 additions and 525 deletions

View File

@@ -1,23 +1,16 @@
package updates
import (
"archive/zip"
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/safing/portmaster/base/log"
)
const MaxUnpackSize = 1 << 30 // 2^30 == 1GB
@@ -33,6 +26,19 @@ type Artifact struct {
Version string `json:"Version,omitempty"`
}
func (a *Artifact) GetFileMode() os.FileMode {
// Special case for portmaster ui. Should be able to be executed from the regular user
if a.Platform == currentPlatform && a.Filename == "portmaster" {
return executableUIFileMode
}
if a.Platform == currentPlatform {
return executableFileMode
}
return defaultFileMode
}
type Bundle struct {
Name string `json:"Bundle"`
Version string `json:"Version"`
@@ -40,10 +46,9 @@ type Bundle struct {
Artifacts []Artifact `json:"Artifacts"`
}
func ParseBundle(dir string, indexFile string) (*Bundle, error) {
filepath := fmt.Sprintf("%s/%s", dir, indexFile)
func ParseBundle(indexFile string) (*Bundle, error) {
// Check if the file exists.
file, err := os.Open(filepath)
file, err := os.Open(indexFile)
if err != nil {
return nil, fmt.Errorf("failed to open index file: %w", err)
}
@@ -59,7 +64,7 @@ func ParseBundle(dir string, indexFile string) (*Bundle, error) {
var bundle Bundle
err = json.Unmarshal(content, &bundle)
if err != nil {
return nil, fmt.Errorf("%s %w", filepath, err)
return nil, fmt.Errorf("%s %w", indexFile, err)
}
// Filter artifacts
@@ -74,70 +79,6 @@ func ParseBundle(dir string, indexFile string) (*Bundle, error) {
return &bundle, nil
}
// CopyMatchingFilesFromCurrent check if there the current bundle files has matching files with the new bundle and copies them if they match.
func (bundle Bundle) CopyMatchingFilesFromCurrent(current Bundle, currentDir, newDir string) error {
// Make sure new dir exists
_ = os.MkdirAll(newDir, defaultDirMode)
for _, currentArtifact := range current.Artifacts {
new:
for _, newArtifact := range bundle.Artifacts {
if currentArtifact.Filename == newArtifact.Filename {
if currentArtifact.SHA256 == newArtifact.SHA256 {
// Read the content of the current file.
sourceFilePath := filepath.Join(currentDir, newArtifact.Filename)
content, err := os.ReadFile(sourceFilePath)
if err != nil {
return fmt.Errorf("failed to read file %s: %w", sourceFilePath, err)
}
// Check if the content matches the artifact hash
expectedHash, err := hex.DecodeString(newArtifact.SHA256)
if err != nil || len(expectedHash) != sha256.Size {
return fmt.Errorf("invalid artifact hash %s: %w", newArtifact.SHA256, err)
}
hash := sha256.Sum256(content)
if !bytes.Equal(expectedHash, hash[:]) {
return fmt.Errorf("expected and file hash mismatch: %s", sourceFilePath)
}
// Create new file
destFilePath := filepath.Join(newDir, newArtifact.Filename)
err = os.WriteFile(destFilePath, content, defaultFileMode)
if err != nil {
return fmt.Errorf("failed to write to file %s: %w", destFilePath, err)
}
log.Debugf("updates: file copied from current version: %s", newArtifact.Filename)
}
break new
}
}
}
return nil
}
func (bundle Bundle) DownloadAndVerify(ctx context.Context, client *http.Client, dir string) {
// Make sure dir exists
_ = os.MkdirAll(dir, defaultDirMode)
for _, artifact := range bundle.Artifacts {
filePath := filepath.Join(dir, artifact.Filename)
// Check file is already downloaded and valid.
exists, _ := checkIfFileIsValid(filePath, artifact)
if exists {
log.Debugf("updates: file already downloaded: %s", filePath)
continue
}
// Download artifact
err := processArtifact(ctx, client, artifact, filePath)
if err != nil {
log.Errorf("updates: %s", err)
}
}
}
// Verify checks if the files are present int the dataDir and have the correct hash.
func (bundle Bundle) Verify(dir string) error {
for _, artifact := range bundle.Artifacts {
@@ -179,144 +120,3 @@ func checkIfFileIsValid(filename string, artifact Artifact) (bool, error) {
}
return true, nil
}
func processArtifact(ctx context.Context, client *http.Client, artifact Artifact, filePath string) error {
providedHash, err := hex.DecodeString(artifact.SHA256)
if err != nil || len(providedHash) != sha256.Size {
return fmt.Errorf("invalid provided hash %s: %w", artifact.SHA256, err)
}
// Download
log.Debugf("updates: downloading file: %s", artifact.Filename)
content, err := downloadFile(ctx, client, artifact.URLs)
if err != nil {
return fmt.Errorf("failed to download artifact: %w", err)
}
// Decompress
if artifact.Unpack != "" {
content, err = unpack(artifact.Unpack, content)
if err != nil {
return fmt.Errorf("failed to decompress artifact: %w", err)
}
}
// Verify
hash := sha256.Sum256(content)
if !bytes.Equal(providedHash, hash[:]) {
return fmt.Errorf("failed to verify artifact: %s", artifact.Filename)
}
// Save
tmpFilename := fmt.Sprintf("%s.download", filePath)
fileMode := defaultFileMode
if artifact.Platform == currentPlatform {
fileMode = executableFileMode
}
err = os.WriteFile(tmpFilename, content, fileMode)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}
// Rename
err = os.Rename(tmpFilename, filePath)
if err != nil {
return fmt.Errorf("failed to rename file: %w", err)
}
log.Infof("updates: file downloaded and verified: %s", artifact.Filename)
return nil
}
func downloadFile(ctx context.Context, client *http.Client, urls []string) ([]byte, error) {
for _, url := range urls {
// Try to make the request
req, err := http.NewRequestWithContext(ctx, "GET", url, http.NoBody)
if err != nil {
log.Warningf("failed to create GET request to %s: %s", url, err)
continue
}
if UserAgent != "" {
req.Header.Set("User-Agent", UserAgent)
}
resp, err := client.Do(req)
if err != nil {
log.Warningf("failed a get file request to: %s", err)
continue
}
defer func() { _ = resp.Body.Close() }()
// Check if the server returned an error
if resp.StatusCode != http.StatusOK {
log.Warningf("server returned non-OK status: %d %s", resp.StatusCode, resp.Status)
continue
}
content, err := io.ReadAll(resp.Body)
if err != nil {
log.Warningf("failed to read body of response: %s", err)
continue
}
return content, nil
}
return nil, fmt.Errorf("failed to download file from the provided urls")
}
func unpack(cType string, fileBytes []byte) ([]byte, error) {
switch cType {
case "zip":
return decompressZip(fileBytes)
case "gz":
return decompressGzip(fileBytes)
default:
return nil, fmt.Errorf("unsupported compression type")
}
}
func decompressGzip(data []byte) ([]byte, error) {
// Create a gzip reader from the byte array
gzipReader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
}
defer func() { _ = gzipReader.Close() }()
var buf bytes.Buffer
_, err = io.CopyN(&buf, gzipReader, MaxUnpackSize)
if err != nil && !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("failed to read gzip file: %w", err)
}
return buf.Bytes(), nil
}
func decompressZip(data []byte) ([]byte, error) {
// Create a zip reader from the byte array
zipReader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return nil, fmt.Errorf("failed to create zip reader: %w", err)
}
// Ensure there is only one file in the zip
if len(zipReader.File) != 1 {
return nil, fmt.Errorf("zip file must contain exactly one file")
}
// Read the single file in the zip
file := zipReader.File[0]
fileReader, err := file.Open()
if err != nil {
return nil, fmt.Errorf("failed to open file in zip: %w", err)
}
defer func() { _ = fileReader.Close() }()
var buf bytes.Buffer
_, err = io.CopyN(&buf, fileReader, MaxUnpackSize)
if err != nil && !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("failed to read file in zip: %w", err)
}
return buf.Bytes(), nil
}