-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.go
410 lines (390 loc) · 9.88 KB
/
app.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/xbclub/BilibiliDanmuRobot-Core/config"
"github.com/xbclub/BilibiliDanmuRobot-Core/entity"
"github.com/xbclub/BilibiliDanmuRobot-Core/http"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
"gopkg.in/yaml.v3"
"io"
httpx "net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
// App struct
type App struct {
Vserion string
ctx context.Context
login chan bool
loginurl *entity.LoginUrl
loginstatus int
channelisrun bool
loginCtx context.Context
loginCancel context.CancelFunc
}
// NewApp creates a new App application struct
func NewApp(version string) *App {
http.InitHttpClient()
return &App{Vserion: version}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (l *App) Userlogin() *entity.LoginUrl {
if l.GetloginStatus() {
return nil
}
var err error
if l.login != nil && l.channelisrun {
l.Stopwork()
}
l.login = make(chan bool)
if l.loginurl, err = http.GetLoginUrl(); err != nil {
logx.Error(err)
return nil
}
l.loginstatus = 0
l.loginCtx, l.loginCancel = context.WithCancel(context.Background())
go l.work(l.loginCtx)
l.channelisrun = true
return l.loginurl
//if err = utiles.GenerateQr(loginUrl.Data.Url); err != nil {
// logx.Error(err)
// return err
//}
}
func (l *App) Getlogin() int {
if l.loginstatus == 1 {
l.Stopwork()
return 1
} else if l.loginstatus == 3 {
l.Stopwork()
return 3
}
return 2
}
func (l *App) work(ctx context.Context) {
var err error
var url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + l.loginurl.Data.OauthKey
var resp *resty.Response
var data *entity.LoginInfoData
var file *os.File
var CookieStr string
var CookieList = make(map[string]string)
cli := resty.New()
pre := &entity.LoginInfoPre{}
logx.Info("等待扫码登录...")
nologin := true
var w = 1 * time.Second
var t = time.NewTimer(w)
defer t.Stop()
for nologin {
select {
case <-ctx.Done():
//l.channelisrun = false
logx.Info("登录线程退出")
nologin = false
case <-t.C:
logx.Info("等待扫码中")
if resp, err = cli.R().
SetHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36").
Get(url); err != nil {
logx.Error("请求getLoginInfo失败:", err)
l.loginstatus = 3
}
if err = json.Unmarshal(resp.Body(), pre); err != nil {
logx.Error("Unmarshal失败:", err, "body:", string(resp.Body()))
l.loginstatus = 3
}
if pre.Code == 0 {
data = &entity.LoginInfoData{}
if err = json.Unmarshal(resp.Body(), data); err != nil {
logx.Error("Unmarshal失败:", err, "body:", string(resp.Body()))
l.loginstatus = 3
}
if data.Data.Code == 86038 {
logx.Error(data.Data.Message)
}
if data.Data.Code == 0 {
logx.Info("登录成功!")
for _, v := range resp.Header().Values("Set-Cookie") {
pair := strings.Split(v, ";")
kv := strings.Split(pair[0], "=")
CookieList[kv[0]] = kv[1]
CookieStr += pair[0] + ";"
}
//使用追加模式打开文件
file, err = os.OpenFile("token/bili_token.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
logx.Errorf("打开文件错误:", err)
}
file.WriteString(CookieStr)
file.Close()
//使用追加模式打开文件
file, err = os.OpenFile("token/bili_token.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
logx.Errorf("打开文件错误:", err)
l.loginstatus = 3
}
tokenstr, _ := json.Marshal(CookieList)
file.WriteString(string(tokenstr))
file.Close()
l.loginstatus = 1
l.Stopwork()
}
//l.Stopwork()
}
t.Reset(w)
}
}
}
func (l *App) Stopwork() {
if l.loginCancel != nil {
l.loginCancel()
}
}
func (l *App) Onstop() bool {
return true
}
func (l *App) GetloginStatus() bool {
if http.FileExists("token/bili_token.txt") && http.FileExists("token/bili_token.json") {
err := http.SetHistoryCookie()
if err != nil {
logx.Error(err)
return false
}
status := http.GetUserInfo()
return status.Islogin
} else {
return false
}
}
func (l *App) GetUserInfo() *entity.UserinfoLite {
return http.GetUserInfo()
}
func (l *App) WriteConfig(data string) *ConfigResponse {
var c config.Config
resp := new(ConfigResponse)
err := json.Unmarshal([]byte(data), &c)
if err != nil {
logx.Error(err)
resp.Code = false
resp.Msg = err.Error()
return resp
}
c.Log = logx.LogConf{
ServiceName: "",
Mode: "file",
Encoding: "plain",
Path: fmt.Sprintf("./logs/%v", c.RoomId),
TimeFormat: "",
Level: "info",
MaxContentLength: 0,
Compress: false,
Stat: true,
KeepDays: 0,
StackCooldownMillis: 100,
MaxBackups: 0,
MaxSize: 0,
Rotation: "daily",
}
yamlBytes, err := yaml.Marshal(&c)
if err != nil {
logx.Error("Failed to marshal YAML: %v", err)
resp.Code = false
resp.Msg = err.Error()
return resp
}
if _, err = os.Stat("./etc"); os.IsNotExist(err) {
// Directory does not exist, create it
err = os.Mkdir("./etc", 0755)
if err != nil {
logx.Error(err)
resp.Code = false
resp.Msg = err.Error()
return resp
}
}
file, err := os.OpenFile("etc/bilidanmaku-api.yaml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
logx.Errorf("打开文件错误:", err)
resp.Code = false
resp.Msg = "打开文件错误:" + err.Error()
return resp
}
_, err = file.Write(yamlBytes)
if err != nil {
logx.Errorf("文件写入错误:", err)
resp.Code = false
resp.Msg = "文件写入错误:" + err.Error()
return resp
}
file.Close()
err = Mustload(&c)
if err != nil {
logx.Error(err)
resp.Code = false
resp.Msg = err.Error()
return resp
}
resp.Code = true
return resp
}
func Mustload(c *config.Config) (err error) {
defer func() {
if r := recover(); r != nil {
// 处理panic错误
// 如果需要,将错误转换为适当的错误类型
// 设置错误消息或执行任何必要的错误处理
// ...
// 返回错误
// 可以返回自定义的错误消息,或者根据捕获到的panic错误创建一个新的错误对象
errMsg := fmt.Sprintf("发生了panic错误:%v", r)
// 或者创建一个新的错误对象:err = errors.New("发生了panic错误")
// 返回错误
// someFunction的调用者将接收到该错误
err = errors.New(errMsg)
}
}()
conf.MustLoad("etc/bilidanmaku-api.yaml", c, conf.UseEnv())
return nil
}
func (l *App) ReadConfig() *ConfigResponse {
resp := new(ConfigResponse)
var c config.Config
err := Mustload(&c)
if err != nil {
logx.Error(err)
resp.Code = false
resp.Msg = err.Error()
return resp
}
resp.Code = true
resp.Form = c
return resp
}
type UpdateResponse struct {
Version string `json:"version"`
Link string `json:"link"`
ChangeLog string `json:"changeLog"`
}
func (l *App) GetVersion() string {
return l.Vserion
}
func (l *App) CheckUpdate() *VersionResponse {
versionresp := VersionResponse{Code: 2}
resp, err := httpx.Get("https://danmuji.neuedu.work/getUpdate")
if err != nil {
logx.Error(err)
versionresp.Msg = "链接更新服务器失败"
return &versionresp
}
if resp.StatusCode != httpx.StatusOK {
logx.Error(resp.StatusCode)
versionresp.Msg = "链接更新服务器失败"
return &versionresp
}
updateResp := &UpdateResponse{}
err = json.NewDecoder(resp.Body).Decode(updateResp)
if err != nil {
logx.Error(err)
versionresp.Msg = "更信息读取失败"
return &versionresp
}
if l.Vserion == updateResp.Version {
versionresp.Code = 0
versionresp.Msg = "无需更新"
return &versionresp
}
versionresp.Code = 1
versionresp.Msg = updateResp.Version
versionresp.Content = updateResp.ChangeLog
return &versionresp
}
func (l *App) GetUpdateUpgrader() string {
resp, err := httpx.Get("https://danmuji.neuedu.work/getUpgraderUpdate")
if err != nil {
logx.Error(err)
return "链接更新服务器失败"
}
defer resp.Body.Close()
if resp.StatusCode == httpx.StatusOK {
updateResp := &UpdateResponse{}
err := json.NewDecoder(resp.Body).Decode(updateResp)
if err != nil {
logx.Error(err)
return "更信息读取失败"
}
err = downloadAndExtract(updateResp.Link)
if err != nil {
logx.Error("Error:", err)
return "下载更新程序失败"
}
} else {
logx.Errorf("Request failed with status code: %d\n", resp.StatusCode)
return "更新服务器链接失败"
}
go func() {
// 唤起upgrade.exe start是非阻塞 /B是隐藏窗口
cmd := exec.Command("cmd.exe", "/C", "start", "upgrader.exe")
if err := cmd.Start(); err != nil {
fmt.Println(err)
}
fmt.Println("exit")
os.Exit(0)
}()
return "升级程序初始化成功,即将开始升级"
}
// func (l *App) StartProgram() bool {
// err := l.program.Start()
// if err != nil {
// return false
// }
// return true
// }
//
// func (l *App) GetProgramStatus() bool {
// return l.GetProgramStatus()
// }
type ConfigResponse struct {
Code bool
Msg string
Form config.Config
}
type VersionResponse struct {
Code int
Msg string
Content string
}
func downloadAndExtract(link string) error {
resp, err := httpx.Get(link)
logx.Info(link)
defer resp.Body.Close()
if err != nil {
return err
}
extractedFile, err := os.OpenFile(filepath.Base("upgrader.exe"), os.O_WRONLY|os.O_CREATE, 0755)
defer extractedFile.Close()
if err != nil {
return err
}
_, err = io.Copy(extractedFile, resp.Body)
if err != nil {
return err
}
return nil
}