-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.go
521 lines (486 loc) · 12.3 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package main
import (
"bufio"
"crypto/md5"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"github.com/XiaoMengXinX/Music163bot-Go/v2/bot"
"github.com/XiaoMengXinX/Music163bot-Go/v2/symbols"
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
"github.com/traefik/yaegi/stdlib/syscall"
"github.com/traefik/yaegi/stdlib/unsafe"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"reflect"
"runtime"
"strings"
"time"
)
var config map[string]string
var (
_ConfigPath *string
_NoUpdate *bool
_NoMD5Check *bool
_EnableExt *bool
_SrcPath *string
_BotEntry *string
_ExtEntry *string
_ExtPath *string
)
var (
runtimeVer = fmt.Sprintf(runtime.Version()) // 编译环境
_VersionName = "" // 程序版本
_VersionCode = 0
commitSHA = "" // 编译哈希
buildTime = "" // 编译日期
buildOS = "" // 编译系统
buildArch = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH) // 运行环境
repo = "" // 项目地址
)
type metadata struct {
Version string `json:"version"`
VersionCode int `json:"version_code"`
Files []struct {
File string `json:"file"`
Md5 string `json:"md5"`
} `json:"files"`
}
type versions struct {
Version string `json:"version"`
VersionCode int `json:"version_code"`
CommitSha string `json:"commit_sha"`
}
// LogFormatter 自定义 log 格式
type LogFormatter struct{}
// Format 自定义 log 格式
func (s *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
timestamp := time.Now().Local().Format("2006/01/02 15:04:05")
var msg string
msg = fmt.Sprintf("%s [%s] %s (%s:%d)\n", timestamp, strings.ToUpper(entry.Level.String()), entry.Message, path.Base(entry.Caller.File), entry.Caller.Line)
return []byte(msg), nil
}
func init() {
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
FullTimestamp: true,
DisableLevelTruncation: true,
PadLevelText: true,
})
logrus.SetFormatter(new(LogFormatter))
logrus.SetReportCaller(true)
for _, v := range []string{`panic`, `fatal`, `error`, `warn`, `info`, `debug`, `trace`} {
v = config["LogLevel"]
level, err := logrus.ParseLevel(v)
if err != nil {
logrus.SetLevel(level)
}
}
dirExists("./log")
timeStamp := time.Now().Local().Format("2006-01-02")
logFile := fmt.Sprintf("./log/%v.log", timeStamp)
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
logrus.Errorln(err)
}
output := io.MultiWriter(os.Stdout, file)
logrus.SetOutput(output)
f := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
_ConfigPath = f.String("c", "config.ini", "配置文件")
_NoUpdate = f.Bool("no-update", false, "关闭更新检测")
_NoMD5Check = f.Bool("no-md5-check", false, "关闭 md5 效验")
_EnableExt = f.Bool("enable-ext", false, "启用插件加载")
_SrcPath = f.String("path", "./src", "自定义更新下载/加载路径")
_ExtPath = f.String("ext-path", "./ext", "自定义插件路径")
_BotEntry = f.String("bot-entry", "bot.Start", "自定义动态加载入口")
_ExtEntry = f.String("ext-entry", "ext.CustomScript", "自定义插件加载入口")
_ = f.Parse(os.Args[1:])
logrus.Printf("Music163bot-Go %s(%d)", _VersionName, _VersionCode)
conf, err := readConfig(*_ConfigPath)
if err != nil {
logrus.Errorln("读取配置文件失败,请检查配置文件")
logrus.Fatal(err)
}
config = conf
config["BinVersionName"] = _VersionName
config["BinVersionCode"] = fmt.Sprintf("%d", _VersionCode)
config["runtimeVer"] = runtimeVer
config["buildTime"] = buildTime
config["commitSHA"] = commitSHA
config["buildOS"] = buildOS
config["buildArch"] = buildArch
if config["AutoUpdate"] == "false" {
*_NoUpdate = true
}
if config["CheckMD5"] == "false" {
*_NoMD5Check = true
}
if config["SrcPath"] != "" {
*_SrcPath = config["SrcPath"]
} else {
config["SrcPath"] = *_SrcPath
}
if config["BotEntry"] != "" {
*_BotEntry = config["BotEntry"]
}
if config["ExtPath"] != "" {
*_ExtPath = config["ExtPath"]
} else {
config["ExtPath"] = *_ExtPath
}
if config["ExtEntry"] != "" {
*_ExtEntry = config["ExtEntry"]
} else {
config["ExtEntry"] = *_ExtEntry
}
if config["EnableExt"] == "true" {
*_EnableExt = true
}
}
func main() {
var meta metadata
var actionCode = -1 // actionCode: 0 exit, 1 error exit, 2 reload src, 3 update src
for true {
err := func() (err error) {
if *_NoUpdate && actionCode != 3 {
data, err := getLocalVersion()
if err != nil {
return err
}
meta = data
if !*_NoMD5Check && data.VersionCode != 0 {
logrus.Println("正在校验文件MD5")
err := checkMD5(data)
if err != nil {
return err
}
logrus.Println("MD5校验成功")
}
} else {
logrus.Printf("正在检查更新中")
versionData, err := getVersions()
if err == nil {
data, err := getUpdate(versionData)
meta = data
if err != nil {
logrus.Errorln(err)
return err
}
if !*_NoMD5Check {
logrus.Println("正在校验文件MD5")
err := checkMD5(meta)
if err != nil {
logrus.Errorln(err)
return err
}
logrus.Println("MD5校验成功")
}
} else {
logrus.Errorln(err)
return err
}
}
return err
}()
var ext func(*tgbotapi.BotAPI, tgbotapi.Update) error
if func() bool {
if err == nil {
v, err := loadDyn(meta)
if err == nil {
if *_EnableExt {
ext, err = loadExt()
if err != nil {
logrus.Errorln(err)
}
}
start, ok := v.Interface().(func(map[string]string, func(*tgbotapi.BotAPI, tgbotapi.Update) error) int)
if ok {
config["VersionName"] = meta.Version
config["VersionCode"] = fmt.Sprintf("%d", meta.VersionCode)
if *_NoUpdate && *_NoMD5Check {
logrus.Printf("加载自定义源码中")
} else {
logrus.Printf("加载版本 %s(%d) 中", meta.Version, meta.VersionCode)
}
actionCode = start(config, ext)
} else {
return true
}
} else {
logrus.Errorln(err)
return true
}
} else {
logrus.Errorln(err)
return true
}
return false
}() {
if *_EnableExt {
ext, err = loadExt()
if err != nil {
logrus.Errorln(err)
}
}
logrus.Printf("加载内置版本 %s(%d) 中", _VersionName, _VersionCode)
actionCode = bot.Start(config, ext)
}
switch actionCode {
case 0:
break
case 1:
logrus.Fatal("Unexpected error")
break
case 2:
time.Sleep(2 * time.Second)
continue
case 3:
time.Sleep(2 * time.Second)
continue
}
}
}
func loadDyn(meta metadata) (res reflect.Value, err error) {
defer func() {
e := recover()
if e != nil {
err = fmt.Errorf("%v", e)
}
}()
i := interp.New(interp.Options{})
_ = i.Use(unsafe.Symbols)
_ = i.Use(stdlib.Symbols)
_ = i.Use(syscall.Symbols)
_ = i.Use(symbols.Symbols)
if *_NoUpdate && *_NoMD5Check {
files, err := ioutil.ReadDir(*_SrcPath)
if err != nil {
return reflect.Value{}, err
}
for _, f := range files {
if strings.Contains(f.Name(), ".go") {
_, err := i.EvalPath(fmt.Sprintf("%s/%s", *_SrcPath, f.Name()))
if err != nil {
return reflect.Value{}, err
}
}
}
} else {
for _, f := range meta.Files {
if strings.Contains(path.Base(f.File), ".go") {
_, err := i.EvalPath(fmt.Sprintf("%s/%s", *_SrcPath, path.Base(f.File)))
if err != nil {
return reflect.Value{}, err
}
}
}
}
res, err = i.Eval(*_BotEntry)
if err != nil {
return reflect.Value{}, err
}
return res, err
}
func loadExt() (ext func(*tgbotapi.BotAPI, tgbotapi.Update) error, err error) {
dirExists(*_ExtPath)
defer func() {
e := recover()
if e != nil {
err = fmt.Errorf("%v", e)
}
}()
i := interp.New(interp.Options{})
_ = i.Use(unsafe.Symbols)
_ = i.Use(stdlib.Symbols)
_ = i.Use(syscall.Symbols)
_ = i.Use(symbols.Symbols)
files, _ := ioutil.ReadDir(*_ExtPath)
if len(files) == 0 {
return
}
for _, f := range files {
if strings.Contains(f.Name(), ".go") {
_, err := i.EvalPath(fmt.Sprintf("%s/%s", *_ExtPath, f.Name()))
if err != nil {
return ext, err
}
}
}
res, err := i.Eval(*_ExtEntry)
if err != nil {
return ext, err
}
ext, ok := res.Interface().(func(*tgbotapi.BotAPI, tgbotapi.Update) error)
if ok {
return ext, err
}
return ext, err
}
func getLocalVersion() (meta metadata, err error) {
if fileExists(fmt.Sprintf("%s/version.json", *_SrcPath)) {
content, err := ioutil.ReadFile(fmt.Sprintf("%s/version.json", *_SrcPath))
if err != nil {
return meta, err
}
err = json.Unmarshal(content, &meta)
return meta, err
}
return meta, err
}
func getUpdate(versionData []versions) (meta metadata, err error) {
dirExists(*_SrcPath)
var versionName string
var versionCode int
currentVersion, _ := getLocalVersion()
if currentVersion.VersionCode != 0 {
versionCode = currentVersion.VersionCode
versionName = currentVersion.Version
meta = currentVersion
} else {
versionCode = _VersionCode
versionName = _VersionName
}
latest := func() versions {
for _, v := range versionData {
if v.VersionCode > versionCode {
return v
}
}
return versions{}
}()
if latest.VersionCode == 0 {
logrus.Printf("%s(%d) 已是最新版本", versionName, versionCode)
return meta, err
}
logrus.Printf("检测到版本更新: %s(%d), 正在获取更新", latest.Version, latest.VersionCode)
dataFile, err := getFile(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/metadata.json", repo, latest.CommitSha))
if err != nil {
return meta, err
}
err = ioutil.WriteFile(fmt.Sprintf("%s/version.json", *_SrcPath), dataFile, 0644)
if err != nil {
return meta, err
}
_ = json.Unmarshal(dataFile, &meta)
for _, v := range meta.Files {
srcFile, err := getFile(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s", repo, latest.CommitSha, v.File))
if err != nil {
return meta, err
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", *_SrcPath, path.Base(v.File)), srcFile, 0644)
if err != nil {
return meta, err
}
}
logrus.Println("更新下载完成")
return meta, err
}
func getVersions() (versionData []versions, err error) {
updateData, err := getFile(fmt.Sprintf("https://raw.githubusercontent.com/%s/v2/versions.json", repo))
if err != nil {
return versionData, err
}
err = json.Unmarshal(updateData, &versionData)
if err != nil {
return versionData, err
}
return versionData, err
}
func checkMD5(data metadata) (err error) {
for _, f := range data.Files {
file, err := ioutil.ReadFile(f.File)
if err != nil {
return err
}
md5Data := md5.Sum(file)
if hex.EncodeToString(md5Data[:]) != f.Md5 {
return fmt.Errorf("文件: %s MD5效验失败 ", f.File)
}
}
return err
}
func readConfig(path string) (map[string]string, error) {
config := make(map[string]string)
f, err := os.Open(path)
if err != nil {
return config, err
//panic(err)
}
defer f.Close()
r := bufio.NewReader(f)
for {
b, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
return config, err
//panic(err)
}
s := strings.TrimSpace(string(b))
index := strings.Index(s, "=")
if index < 0 {
continue
}
key := strings.TrimSpace(s[:index])
if len(key) == 0 {
continue
}
value := strings.TrimSpace(s[index+1:])
if len(value) == 0 {
continue
}
config[key] = value
}
return config, err
}
func getFile(url string) (body []byte, err error) {
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return body, err
}
res, err := client.Do(req)
if err != nil {
return body, err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return body, err
}
return body, err
}
func fileExists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func dirExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
err := os.Mkdir(path, os.ModePerm)
if err != nil {
logrus.Errorf("mkdir %v failed: %v\n", path, err)
}
return false
}
logrus.Errorf("Error: %v\n", err)
return false
}