修改题库文件目录,添加检测题库的功能

This commit is contained in:
johlanse 2022-08-26 23:00:28 +08:00
parent 7f846ef707
commit e9f0e3bbc7
8 changed files with 80 additions and 4 deletions

View File

@ -15,7 +15,7 @@ RUN apt-get -qq update && \
mkdir /opt/config/ mkdir /opt/config/
COPY conf/config_default.yml /opt/config/config.yml 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 COPY ./dist/docker_linux_$TARGETARCH*/study_xxqg /opt/study_xxqg

View File

@ -89,6 +89,9 @@ type Config struct {
SuperUser string `json:"super_user" yaml:"super_user"` SuperUser string `json:"super_user" yaml:"super_user"`
SuperPassword string `json:"super_password" yaml:"super_password"` SuperPassword string `json:"super_password" yaml:"super_password"`
// github的代理地址用于检查更新或者其他的
GithubProxy string `json:"github_proxy" yaml:"github_proxy"`
version string version string
} }

View File

@ -116,6 +116,9 @@ special_min_score: 10
# 题目搜索的顺序为true则从2018年最开始搜题否则从现在最新开始搜题 # 题目搜索的顺序为true则从2018年最开始搜题否则从现在最新开始搜题
reverse_order: false reverse_order: false
# github的代理地址
github_proxy: https://github.com
weekly_min_score: weekly_min_score:
ji_guang_push: ji_guang_push:

View File

@ -18,6 +18,8 @@ import (
easy "github.com/t-tomalak/logrus-easy-formatter" easy "github.com/t-tomalak/logrus-easy-formatter"
"github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/conf"
"github.com/johlanse/study_xxqg/utils"
// "github.com/johlanse/study_xxqg/gui" // "github.com/johlanse/study_xxqg/gui"
"github.com/johlanse/study_xxqg/lib" "github.com/johlanse/study_xxqg/lib"
"github.com/johlanse/study_xxqg/model" "github.com/johlanse/study_xxqg/model"
@ -59,6 +61,10 @@ func init() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
log.SetLevel(level) log.SetLevel(level)
if !utils.CheckQuestionDB() {
utils.DownloadDbFile()
log.Errorln("题库文件不存在或已损坏,请手动前往 https://github.com/johlanse/study_xxqg/blob/main/conf/QuestionBank.db 下载并放入程序根目录")
}
} }
func init() { func init() {

View File

@ -43,7 +43,7 @@ func initQuestionDb() {
once := sync.Once{} once := sync.Once{}
once.Do(func() { once.Do(func() {
var err error var err error
db1, err = sql.Open("sqlite", "./config/QuestionBank.db") db1, err = sql.Open("sqlite", "./QuestionBank.db")
if err != nil { if err != nil {
log.Errorln("题目数据库打开失败请检查config目录权限") log.Errorln("题目数据库打开失败请检查config目录权限")
log.Panicln(err.Error()) log.Panicln(err.Error())

View File

@ -15,7 +15,9 @@ func init() {
if log.GetLevel() == log.DebugLevel { if log.GetLevel() == log.DebugLevel {
client.DebugLog = true client.DebugLog = true
client = client.DevMode() client = client.DevMode()
} }
client.EnableForceHTTP1()
client.SetLogger(&myLog{}) 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") 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")
} }

View File

@ -17,6 +17,8 @@ import (
"github.com/kardianos/osext" "github.com/kardianos/osext"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/johlanse/study_xxqg/conf"
) )
// CheckUpdate 检查更新 // CheckUpdate 检查更新
@ -156,6 +158,7 @@ func wait() {
// SelfUpdate 自更新 // SelfUpdate 自更新
func SelfUpdate(github string, version string) { func SelfUpdate(github string, version string) {
github = conf.GetConfig().GithubProxy
if github == "" { if github == "" {
github = "https://github.com" github = "https://github.com"
} }

View File

@ -1,12 +1,15 @@
package utils package utils
import ( import (
"crypto/sha256"
"fmt"
"io"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"github.com/imroc/req/v3" "github.com/imroc/req/v3"
"github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/conf"
@ -44,7 +47,7 @@ func CheckUserCookie(cookies []*http.Cookie) bool {
client := req.C().DevMode() client := req.C().DevMode()
response, err := client.R().SetCookies(cookies...).Get("https://pc-api.xuexi.cn/open/api/score/get") response, err := client.R().SetCookies(cookies...).Get("https://pc-api.xuexi.cn/open/api/score/get")
if err != nil { if err != nil {
logrus.Errorln("获取用户总分错误" + err.Error()) log.Errorln("获取用户总分错误" + err.Error())
return false return false
} }
if !gjson.GetBytes(response.Bytes(), "ok").Bool() { if !gjson.GetBytes(response.Bytes(), "ok").Bool() {
@ -52,3 +55,59 @@ func CheckUserCookie(cookies []*http.Cookie) bool {
} }
return true 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
}
}