forked from ezdapps/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatejson.go
More file actions
182 lines (166 loc) · 4.26 KB
/
updatejson.go
File metadata and controls
182 lines (166 loc) · 4.26 KB
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
// Copyright 2016 The go-daylight Authors
// This file is part of the go-daylight library.
//
// The go-daylight library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-daylight library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-daylight library. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"archive/zip"
"bufio"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"hash/crc32"
"io/ioutil"
// "net/http"
"os"
// "os/exec"
"path/filepath"
"reflect"
// "runtime"
"strings"
"time"
"github.com/AplaProject/go-apla/packages/lib"
"github.com/AplaProject/go-apla/packages/utils"
)
const (
// Key is an encrypted private key
Key = `17a984259355caeaad8837f3fc0af12527b801c349677effdcbcaceb1398c54463d64d6cc7081bc94f0479013bf24521`
// CRC is a crc of the password
CRC = 3362054855
)
var (
options Settings
)
// Settings is a structure for saving in update.json file
type Settings struct {
Version string
Domain string
InPath string
OutPath string
File string
ZipFile string
JSONPath string
}
func exit(err error) {
if err != nil {
fmt.Println(err)
}
fmt.Println(`Press Enter to exit...`)
fmt.Scanln()
if err != nil {
os.Exit(1)
}
}
// BytesInfoHeader returns file header for zip file
func BytesInfoHeader(size int, filename string) (*zip.FileHeader, error) {
fh := &zip.FileHeader{
Name: filename,
UncompressedSize64: uint64(size),
UncompressedSize: uint32(size),
Method: zip.Deflate,
}
fh.SetModTime(time.Now())
// fh.SetMode(fi.Mode())
return fh, nil
}
func main() {
var (
settings map[string]Settings
)
out := make(map[string]utils.Update)
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
exit(err)
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter password: ")
psw, _ := reader.ReadString('\n')
if crc32.Checksum([]byte(strings.TrimSpace(psw)), crc32.MakeTable(0xD5828281)) != CRC {
exit(fmt.Errorf(`Wrong password`))
}
key, _ := hex.DecodeString(Key)
pass := md5.Sum([]byte(psw))
privKey, err := Decrypt(pass[:], key)
if err != nil {
exit(err)
}
privateKey := hex.EncodeToString(privKey)
if len(privateKey) == 0 {
exit(fmt.Errorf(`PrivateKey is unknown`))
}
params, err := ioutil.ReadFile(filepath.Join(dir, `updatejson.json`))
if err != nil {
exit(err)
}
if err = json.Unmarshal(params, &settings); err != nil {
exit(err)
}
options = settings[`default`]
// fmt.Println(options)
for key, opt := range settings {
var upd lib.Update
if key == `default` {
continue
}
set := options
r := reflect.ValueOf(opt)
for i := 0; i < r.NumField(); i++ {
val := r.Field(i).String()
if len(val) > 0 {
ro := reflect.ValueOf(&set)
ro.Elem().Field(i).SetString(val)
}
}
infile := filepath.Join(set.InPath, set.File)
md5, err := lib.CalculateMd5(infile)
if err != nil {
exit(err)
}
upd.Version = set.Version
upd.Hash = hex.EncodeToString(md5)
sign, err := lib.SignECDSA(string(privateKey), upd.Hash)
if err != nil {
exit(err)
}
upd.Sign = hex.EncodeToString(sign)
if err = os.MkdirAll(set.OutPath, 0755); err != nil {
exit(err)
}
zipname := filepath.Join(set.OutPath, set.ZipFile)
fmt.Println(`Compressing`, zipname)
zipf, err := os.Create(zipname)
if err != nil {
exit(err)
}
z := zip.NewWriter(zipf)
var outzip []byte
if outzip, err = ioutil.ReadFile(infile); err != nil {
exit(err)
}
header, _ := BytesInfoHeader(len(outzip), filepath.Base(set.ZipFile))
f, _ := z.CreateHeader(header)
f.Write(outzip)
z.Close()
zipf.Close()
upd.URL = set.Domain + set.ZipFile
out[key] = upd
}
if updjson, err := json.Marshal(out); err != nil {
exit(err)
} else if err = ioutil.WriteFile(filepath.Join(options.JSONPath, `update.json`), updjson, 0644); err != nil {
exit(err)
}
exit(nil)
}