-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
277 lines (224 loc) · 6.58 KB
/
main.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Modpack Updater
Copyright (C) 2023 Marfjeh
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"archive/zip"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
)
type modpack struct {
Name string `json:"Name"`
URL string `json:"URL"`
AutoUpdater bool `json:"AutoUpdater"`
Version string `json:"Version"`
Sha1sum string `json:"sha1sum"`
}
type modpackupdate struct {
Version string `json:"version"`
Sha1 string `json:"sha1"`
}
func main() {
var modpack modpack
args := os.Args[1:]
if len(args) != 0 {
if os.Args[1] == "-?" {
fmt.Println("Command arguments:")
fmt.Println("-f to force update.")
os.Exit(0)
}
}
//Read the local modpack file.
data, err := ioutil.ReadFile("./modpack.json")
err = json.Unmarshal(data, &modpack)
//Print logo.
fmt.Println("Modpack Updater Version 1.2\n" +
"Github: https://github.com/Marfjeh/Modpack-Updater\n" +
"-----------------------------------------------------------------------------\n" +
"Modpack: " + modpack.Name + "\n" +
"Modpack version: " + modpack.Version + "\n" +
"-----------------------------------------------------------------------------\n" +
"Copyright (C) 2023 Marfjeh\n" +
"This program comes with ABSOLUTELY NO WARRANTY; for details see the git repo.\n" +
"This is free software, and you are welcome to redistribute it\n" +
"under certain conditions; see the git repo for details.\n" +
"-----------------------------------------------------------------------------\n")
//Init Update check
fmt.Printf("Checking for updated version... ")
if !modpack.AutoUpdater {
fmt.Println(" [ FAILED ]")
fmt.Println("Auto update is disabled in modpack.json exiting.")
fmt.Println("If you want to re-enable auto update, please set AutoUpdate to true in modpack.json file.")
os.Exit(0)
}
Modpackupdate := checkupdate(modpack.Sha1sum, modpack.Version)
err = downloadFile("deployment.zip", modpack.URL)
fmt.Println(" [ OK ]")
fmt.Printf("Verifying checksum of deployment...")
ziphash := getHash()
if ziphash != Modpackupdate.Sha1 {
fmt.Println(" [ FAILED ]")
fmt.Println("Detected checksum: " + ziphash)
fmt.Println("Expected checksum: " + ziphash)
os.Exit(1)
} else {
fmt.Println(" [ OK ]")
fmt.Println("Detected checksum: " + ziphash)
fmt.Println("Expected checksum: " + ziphash)
}
fmt.Printf("Cleaning mods folder...")
err = cleanFolders()
fmt.Println(" [ OK ]")
fmt.Printf("Extracting update package...")
_, err = extractZip()
fmt.Println(" [ OK ]")
fmt.Printf("Cleaning up... ")
err = os.Remove("deployment.zip")
fmt.Println(" [ OK ]")
fmt.Println("Updating is complete. you can now start up your modpack.")
if err != nil {
failExit(err)
}
os.Exit(0)
}
func failExit(err error) {
log.Fatal("--------- ERROR ---------\n" + err.Error())
os.Exit(1)
}
func checkupdate(url string, version string) modpackupdate {
args := os.Args[1:]
ModpackUpdate := modpackupdate{}
getJSON(url, &ModpackUpdate)
if len(args) != 0 {
if os.Args[1] == "-f" {
version = "force update"
}
}
if version == ModpackUpdate.Version {
fmt.Println("[ Already up-to-date ]")
fmt.Println("Exiting, you can now play the modpack!")
os.Exit(0)
} else {
fmt.Println("[ Update found ]")
}
return ModpackUpdate
}
var myClient = &http.Client{Timeout: 60 * time.Second}
func getJSON(url string, target interface{}) error {
r, err := myClient.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
// downloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func downloadFile(filepath string, url string) error {
fmt.Printf("Downloading update. This can take some time depending on your internet speed...")
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
// Clean the mods folder so we can move in the new mods.
func cleanFolders() error {
moddir, err := ioutil.ReadDir("mods/")
for _, d := range moddir {
err = os.RemoveAll(path.Join([]string{"mods", d.Name()}...))
if err != nil {
return err
}
}
return nil
}
// Extract the zip containing the files.
func extractZip() ([]string, error) {
var filenames []string
r, err := zip.OpenReader("deployment.zip")
if err != nil {
failExit(err)
}
defer r.Close()
for _, f := range r.File {
dest, _ := os.Getwd()
fpath := filepath.Join(string(dest), f.Name)
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
continue
}
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
rc, err := f.Open()
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
rc.Close()
if err != nil {
return filenames, err
}
}
return filenames, nil
}
// Calcuate the hash of the deployment.zip to verify that the zip is downloaded successfully.
// So people with awful internet connection wont get currupted files.
func getHash() string {
f, err := os.Open("deployment.zip")
if err != nil {
fmt.Println(" [ Failed ]")
failExit(err)
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
fmt.Println(" [ Failed ]")
failExit(err)
}
return hex.EncodeToString(h.Sum(nil))
}