From e9f0e3bbc77e370cb87b85b1ff147bebd46cec84 Mon Sep 17 00:00:00 2001 From: johlanse Date: Fri, 26 Aug 2022 23:00:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=A2=98=E5=BA=93=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9B=AE=E5=BD=95=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E9=A2=98=E5=BA=93=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- conf/config.go | 3 ++ conf/config_default.yml | 3 ++ main.go | 6 ++++ model/model.go | 2 +- utils/client.go | 2 ++ utils/update/update.go | 3 ++ utils/util.go | 63 +++++++++++++++++++++++++++++++++++++++-- 8 files changed, 80 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index ff99892..de1fb91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ RUN apt-get -qq update && \ mkdir /opt/config/ COPY conf/config_default.yml /opt/config/config.yml -COPY conf/QuestionBank.db /opt/config/QuestionBank.db +COPY conf/QuestionBank.db /opt/QuestionBank.db COPY ./dist/docker_linux_$TARGETARCH*/study_xxqg /opt/study_xxqg diff --git a/conf/config.go b/conf/config.go index f8be011..71ee193 100644 --- a/conf/config.go +++ b/conf/config.go @@ -89,6 +89,9 @@ type Config struct { SuperUser string `json:"super_user" yaml:"super_user"` SuperPassword string `json:"super_password" yaml:"super_password"` + // github的代理地址,用于检查更新或者其他的 + GithubProxy string `json:"github_proxy" yaml:"github_proxy"` + version string } diff --git a/conf/config_default.yml b/conf/config_default.yml index b29a9b4..0afefb4 100644 --- a/conf/config_default.yml +++ b/conf/config_default.yml @@ -116,6 +116,9 @@ special_min_score: 10 # 题目搜索的顺序,为true则从2018年最开始搜题,否则从现在最新开始搜题 reverse_order: false +# github的代理地址 +github_proxy: https://github.com + weekly_min_score: ji_guang_push: diff --git a/main.go b/main.go index 7c324f6..6404654 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,8 @@ import ( easy "github.com/t-tomalak/logrus-easy-formatter" "github.com/johlanse/study_xxqg/conf" + "github.com/johlanse/study_xxqg/utils" + // "github.com/johlanse/study_xxqg/gui" "github.com/johlanse/study_xxqg/lib" "github.com/johlanse/study_xxqg/model" @@ -59,6 +61,10 @@ func init() { log.SetLevel(log.DebugLevel) } log.SetLevel(level) + if !utils.CheckQuestionDB() { + utils.DownloadDbFile() + log.Errorln("题库文件不存在或已损坏,请手动前往 https://github.com/johlanse/study_xxqg/blob/main/conf/QuestionBank.db 下载并放入程序根目录") + } } func init() { diff --git a/model/model.go b/model/model.go index 761838a..330e495 100644 --- a/model/model.go +++ b/model/model.go @@ -43,7 +43,7 @@ func initQuestionDb() { once := sync.Once{} once.Do(func() { var err error - db1, err = sql.Open("sqlite", "./config/QuestionBank.db") + db1, err = sql.Open("sqlite", "./QuestionBank.db") if err != nil { log.Errorln("题目数据库打开失败,请检查config目录权限") log.Panicln(err.Error()) diff --git a/utils/client.go b/utils/client.go index 2293126..77c482e 100644 --- a/utils/client.go +++ b/utils/client.go @@ -15,7 +15,9 @@ func init() { if log.GetLevel() == log.DebugLevel { client.DebugLog = true client = client.DevMode() + } + client.EnableForceHTTP1() client.SetLogger(&myLog{}) client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36") } diff --git a/utils/update/update.go b/utils/update/update.go index c702aa7..b56a33a 100644 --- a/utils/update/update.go +++ b/utils/update/update.go @@ -17,6 +17,8 @@ import ( "github.com/kardianos/osext" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" + + "github.com/johlanse/study_xxqg/conf" ) // CheckUpdate 检查更新 @@ -156,6 +158,7 @@ func wait() { // SelfUpdate 自更新 func SelfUpdate(github string, version string) { + github = conf.GetConfig().GithubProxy if github == "" { github = "https://github.com" } diff --git a/utils/util.go b/utils/util.go index 9d5257b..2b72f6c 100644 --- a/utils/util.go +++ b/utils/util.go @@ -1,12 +1,15 @@ package utils import ( + "crypto/sha256" + "fmt" + "io" "net/http" "os" "os/exec" "github.com/imroc/req/v3" - "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "github.com/johlanse/study_xxqg/conf" @@ -44,7 +47,7 @@ func CheckUserCookie(cookies []*http.Cookie) bool { client := req.C().DevMode() response, err := client.R().SetCookies(cookies...).Get("https://pc-api.xuexi.cn/open/api/score/get") if err != nil { - logrus.Errorln("获取用户总分错误" + err.Error()) + log.Errorln("获取用户总分错误" + err.Error()) return false } if !gjson.GetBytes(response.Bytes(), "ok").Bool() { @@ -52,3 +55,59 @@ func CheckUserCookie(cookies []*http.Cookie) bool { } return true } + +var ( + dbSum = "a71c289c9423dd71a88d1fd9db48d51479a4beed8e013f9519c691f524613cff" +) + +// 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 + +} + +func DownloadDbFile() { + log.Infoln("正在从github下载题库文件!") + response, err := http.Get("https://github.com/johlanse/study_xxqg/releases/download/v1.0.34/QuestionBank.db") + 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 + } +}