2022-08-05 09:25:12 +00:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2022-08-26 15:00:28 +00:00
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2022-08-18 07:39:03 +00:00
|
|
|
|
"net/http"
|
2022-08-05 09:25:12 +00:00
|
|
|
|
"os"
|
2022-10-31 07:10:29 +00:00
|
|
|
|
"time"
|
2022-08-05 10:08:05 +00:00
|
|
|
|
|
2022-08-26 15:00:28 +00:00
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-18 07:39:03 +00:00
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
|
2022-12-14 07:43:39 +00:00
|
|
|
|
"github.com/sjkhsl/study_xxqg/conf"
|
|
|
|
|
"github.com/sjkhsl/study_xxqg/utils/update"
|
2022-08-05 09:25:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Restart
|
|
|
|
|
/* @Description:
|
|
|
|
|
*/
|
|
|
|
|
func Restart() {
|
2022-10-03 03:05:57 +00:00
|
|
|
|
//once := sync.Once{}
|
|
|
|
|
//once.Do(func() {
|
|
|
|
|
// log.Infoln("程序启动命令: " + strings.Join(os.Args, " "))
|
|
|
|
|
// cmd := exec.Command(strings.Join(os.Args, " "))
|
|
|
|
|
// cmd.Stdout = os.Stdout
|
|
|
|
|
// cmd.Stderr = os.Stderr
|
|
|
|
|
// cmd.Stdin = os.Stdin
|
|
|
|
|
// cmd.Start()
|
|
|
|
|
// os.Exit(3)
|
|
|
|
|
//})
|
2022-10-03 03:20:56 +00:00
|
|
|
|
os.Exit(201)
|
2022-08-27 05:12:12 +00:00
|
|
|
|
|
2022-08-05 09:25:12 +00:00
|
|
|
|
}
|
2022-08-05 10:08:05 +00:00
|
|
|
|
|
|
|
|
|
func GetAbout() string {
|
|
|
|
|
msg := "study_xxqg\n程序版本:"
|
|
|
|
|
msg += conf.GetVersion()
|
|
|
|
|
msg += "\n" + update.CheckUpdate(conf.GetVersion())
|
|
|
|
|
return msg
|
|
|
|
|
}
|
2022-08-18 07:39:03 +00:00
|
|
|
|
|
|
|
|
|
// CheckUserCookie
|
|
|
|
|
/**
|
|
|
|
|
* @Description: 获取用户成绩
|
|
|
|
|
* @param user
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2022-08-30 07:13:09 +00:00
|
|
|
|
func CheckUserCookie(cookies []*http.Cookie) (bool, error) {
|
2022-09-09 15:14:06 +00:00
|
|
|
|
client := GetClient()
|
2023-08-13 17:07:05 +00:00
|
|
|
|
response, err := client.R().SetCookies(cookies...).Get("https://pc-proxy-api.xuexi.cn/delegate/score/get")
|
2022-08-18 07:39:03 +00:00
|
|
|
|
if err != nil {
|
2022-08-26 15:00:28 +00:00
|
|
|
|
log.Errorln("获取用户总分错误" + err.Error())
|
2022-08-30 07:13:09 +00:00
|
|
|
|
return true, err
|
2022-08-18 07:39:03 +00:00
|
|
|
|
}
|
2022-11-16 10:26:44 +00:00
|
|
|
|
log.Infoln(gjson.GetBytes(response.Bytes(), "@this|@pretty"))
|
|
|
|
|
if !gjson.GetBytes(response.Bytes(), "ok").Bool() &&
|
|
|
|
|
gjson.GetBytes(response.Bytes(), "code").Int() == 401 &&
|
|
|
|
|
gjson.GetBytes(response.Bytes(), "message").String() == "token check failed" {
|
2022-08-30 07:13:09 +00:00
|
|
|
|
return false, err
|
2022-08-18 07:39:03 +00:00
|
|
|
|
}
|
2022-08-30 07:13:09 +00:00
|
|
|
|
return true, err
|
2022-08-18 07:39:03 +00:00
|
|
|
|
}
|
2022-08-26 15:00:28 +00:00
|
|
|
|
|
|
|
|
|
var (
|
2022-09-22 12:17:06 +00:00
|
|
|
|
dbSum = "d6e455f03b419af108cced07ea1d17f8268400ad1b6d80cb75d58e952a5609bf"
|
2022-08-26 15:00:28 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CheckQuestionDB
|
|
|
|
|
/**
|
|
|
|
|
* @Description: 检查数据库文件完整性
|
|
|
|
|
* @param user
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
func CheckQuestionDB() bool {
|
|
|
|
|
|
|
|
|
|
if !FileIsExist("./QuestionBank.db") {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
f, err := os.Open("./QuestionBank.db")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
h := sha256.New()
|
|
|
|
|
//h := sha1.New()
|
|
|
|
|
//h := sha512.New()
|
|
|
|
|
|
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 格式化为16进制字符串
|
|
|
|
|
sha := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
|
log.Infoln("db_sha: " + sha)
|
|
|
|
|
if sha != dbSum {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 07:10:29 +00:00
|
|
|
|
/*时间戳->时间对象*/
|
|
|
|
|
func Stamp2Time(stamp int64) time.Time {
|
|
|
|
|
stampStr := Stamp2Str(stamp)
|
|
|
|
|
timer := Str2Time(stampStr)
|
|
|
|
|
return timer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**时间对象->字符串*/
|
|
|
|
|
func Time2Str() string {
|
|
|
|
|
const shortForm = "2006-01-01 15:04:05"
|
|
|
|
|
t := time.Now()
|
|
|
|
|
temp := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local)
|
|
|
|
|
str := temp.Format(shortForm)
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**字符串->时间对象*/
|
|
|
|
|
func Str2Time(formatTimeStr string) time.Time {
|
|
|
|
|
timeLayout := "2006-01-02 15:04:05"
|
|
|
|
|
loc, _ := time.LoadLocation("Local")
|
|
|
|
|
theTime, _ := time.ParseInLocation(timeLayout, formatTimeStr, loc) //使用模板在对应时区转化为time.time类型
|
|
|
|
|
|
|
|
|
|
return theTime
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*时间对象->时间戳*/
|
|
|
|
|
func Time2Stamp() int64 {
|
|
|
|
|
t := time.Now()
|
|
|
|
|
millisecond := t.UnixNano() / 1e6
|
|
|
|
|
return millisecond
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*时间戳->字符串*/
|
|
|
|
|
func Stamp2Str(stamp int64) string {
|
|
|
|
|
timeLayout := "2006-01-02 15:04:05"
|
2022-11-06 07:01:22 +00:00
|
|
|
|
str := time.Unix(stamp, 0).Format(timeLayout)
|
2022-10-31 07:10:29 +00:00
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-26 15:00:28 +00:00
|
|
|
|
func DownloadDbFile() {
|
2022-09-22 12:17:06 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
err := recover()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("下载题库文件意外错误")
|
|
|
|
|
log.Errorln(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2022-08-26 15:00:28 +00:00
|
|
|
|
log.Infoln("正在从github下载题库文件!")
|
2023-08-13 17:32:26 +00:00
|
|
|
|
response, err := http.Get("https://github.com/sjkhsl/study_xxqg/releases/download/v1.0.39/QuestionBank.db")
|
2022-08-26 15:00:28 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("下载db文件错误" + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, _ := io.ReadAll(response.Body)
|
|
|
|
|
err = os.WriteFile("./QuestionBank.db", data, 0666)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|