summaryrefslogtreecommitdiff
path: root/internal/sync/updater.go
blob: 23cbd19ec0a9f0b7f577aa6a988ac24d0ed40b4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sync

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"time"
)

// Updater checks for and downloads client updates.
type Updater struct {
	fetcher    *Fetcher
	currentVer string
	dataDir    string
}

// NewUpdater creates an updater.
func NewUpdater(fetcher *Fetcher, currentVersion, dataDir string) *Updater {
	return &Updater{
		fetcher:    fetcher,
		currentVer: currentVersion,
		dataDir:    dataDir,
	}
}

// UpdateInfo describes an available update.
type UpdateInfo struct {
	Available  bool   `json:"available"`
	Version    string `json:"version"`
	Changelog  string `json:"changelog"`
	CurrentVer string `json:"current_version"`
}

// Check returns info about available updates.
func (u *Updater) Check() (*UpdateInfo, error) {
	ver, err := u.fetcher.FetchVersion()
	if err != nil {
		return nil, fmt.Errorf("check update: %w", err)
	}

	return &UpdateInfo{
		Available:  ver.Version != u.currentVer,
		Version:    ver.Version,
		Changelog:  ver.Changelog,
		CurrentVer: u.currentVer,
	}, nil
}

// Download fetches the new binary, cleans stale configs, replaces current binary and restarts.
func (u *Updater) Download() (string, error) {
	ver, err := u.fetcher.FetchVersion()
	if err != nil {
		return "", fmt.Errorf("fetch version: %w", err)
	}

	if ver.URL == "" {
		suffix := "linux-amd64"
		if runtime.GOOS == "windows" {
			suffix = "windows-amd64.exe"
		}
		ver.URL = u.fetcher.baseURL + "/releases/vpnem-" + suffix
	}

	client := &http.Client{Timeout: 5 * time.Minute}
	resp, err := client.Get(ver.URL)
	if err != nil {
		return "", fmt.Errorf("download: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("download: HTTP %d", resp.StatusCode)
	}

	ext := ""
	if runtime.GOOS == "windows" {
		ext = ".exe"
	}

	newBin := filepath.Join(u.dataDir, "vpnem-update"+ext)
	f, err := os.Create(newBin)
	if err != nil {
		return "", fmt.Errorf("create file: %w", err)
	}

	if _, err := io.Copy(f, resp.Body); err != nil {
		f.Close()
		os.Remove(newBin)
		return "", fmt.Errorf("write update: %w", err)
	}
	f.Close()

	if runtime.GOOS != "windows" {
		os.Chmod(newBin, 0o755)
	}

	// Clean stale configs so new version starts fresh
	os.Remove(filepath.Join(u.dataDir, "state.json"))
	os.Remove(filepath.Join(u.dataDir, "config.json"))
	os.Remove(filepath.Join(u.dataDir, "cache.db"))

	// Replace current binary and restart
	currentBin, _ := os.Executable()
	if currentBin != "" {
		if runtime.GOOS == "windows" {
			// Windows can't overwrite running exe — rename old, copy new, restart
			oldBin := currentBin + ".old"
			os.Remove(oldBin)
			os.Rename(currentBin, oldBin)
			copyFile(newBin, currentBin)
			// Restart: launch new binary and exit current
			cmd := exec.Command(currentBin)
			cmd.Dir = u.dataDir
			cmd.Start()
			os.Exit(0)
		} else {
			// Linux: overwrite in place, then re-exec
			copyFile(newBin, currentBin)
			os.Remove(newBin)
			cmd := exec.Command(currentBin)
			cmd.Dir = u.dataDir
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			cmd.Start()
			os.Exit(0)
		}
	}

	return newBin, nil
}

func copyFile(src, dst string) error {
	in, err := os.Open(src)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, in)
	if err != nil {
		return err
	}

	// Preserve executable permission on Linux
	if runtime.GOOS != "windows" {
		os.Chmod(dst, 0o755)
	}
	return nil
}