From a27848979df040a4306b696760d45466d2f5b63c Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Tue, 13 Apr 2021 21:15:44 +0800 Subject: [PATCH] remove global/config.go --- coolq/cqcode.go | 18 +++++++++++------- global/config.go | 30 ------------------------------ global/net.go | 21 ++++++--------------- main.go | 24 +++++++++++++++--------- 4 files changed, 32 insertions(+), 61 deletions(-) delete mode 100644 global/config.go diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 5fd35dc..4e782b2 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -1091,10 +1091,11 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) ( return maxImageSize }() thread, _ := strconv.Atoi(c) - if global.PathExists(cacheFile) && cache == "1" { + exist := global.PathExists(cacheFile) + if exist && cache == "1" { goto hasCacheFile } - if global.PathExists(cacheFile) { + if exist { _ = os.Remove(cacheFile) } if err := global.DownloadFileMultiThreading(f, cacheFile, maxSize, thread, nil); err != nil { @@ -1153,22 +1154,25 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) ( return &LocalVideoElement{File: rawPath}, nil } if strings.HasPrefix(f, "base64") { - b, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", "")) + b, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(f, "base64://")) if err != nil { return nil, err } return &LocalImageElement{Stream: bytes.NewReader(b)}, nil } - if !global.PathExists(rawPath) && global.PathExists(path.Join(global.ImagePathOld, f)) { + exist := global.PathExists(rawPath) + if !exist && global.PathExists(path.Join(global.ImagePathOld, f)) { + exist = true rawPath = path.Join(global.ImagePathOld, f) } - if !global.PathExists(rawPath) && global.PathExists(rawPath+".cqimg") { + if !exist && global.PathExists(rawPath+".cqimg") { + exist = true rawPath += ".cqimg" } - if !global.PathExists(rawPath) && d["url"] != "" { + if !exist && d["url"] != "" { return bot.makeImageOrVideoElem(map[string]string{"file": d["url"]}, false, group) } - if global.PathExists(rawPath) { + if exist { file, err := os.Open(rawPath) if err != nil { return nil, err diff --git a/global/config.go b/global/config.go deleted file mode 100644 index f83abab..0000000 --- a/global/config.go +++ /dev/null @@ -1,30 +0,0 @@ -package global - -// AccountToken 存储AccountToken供登录使用 -var AccountToken []byte - -// PasswordHash 存储QQ密码哈希供登录使用 -var PasswordHash [16]byte - -/* -// GetCurrentPath 预留,获取当前目录地址 -func GetCurrentPath() (string, error) { - file, err := exec.LookPath(os.Args[0]) - if err != nil { - return "", err - } - fpath, err := filepath.Abs(file) - if err != nil { - return "", err - } - if runtime.GOOS == "windows" { - // fpath = strings.Replace(fpath, "\\", "/", -1) - fpath = strings.ReplaceAll(fpath, "\\", "/") - } - i := strings.LastIndex(fpath, "/") - if i < 0 { - return "", errors.New("system/path_error,Can't find '/' or '\\'") - } - return fpath[0 : i+1], nil -} -*/ diff --git a/global/net.go b/global/net.go index 0b74a37..d3b5d1b 100644 --- a/global/net.go +++ b/global/net.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "io/ioutil" - "math/rand" "net/http" "net/url" "os" @@ -49,22 +48,14 @@ var ( // GetBytes 对给定URL发送Get请求,返回响应主体 func GetBytes(url string) ([]byte, error) { - req, err := http.NewRequest("GET", url, nil) + reader, err := HTTPGetReadCloser(url) if err != nil { return nil, err } - req.Header["User-Agent"] = []string{UserAgent} - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") { - r, _ := gzip.NewReader(resp.Body) - defer r.Close() - return ioutil.ReadAll(r) - } - return ioutil.ReadAll(resp.Body) + defer func() { + _ = reader.Close() + }() + return ioutil.ReadAll(reader) } // DownloadFile 将给定URL对应的文件下载至给定Path @@ -311,7 +302,7 @@ func NewGzipReadCloser(reader io.ReadCloser) (io.ReadCloser, error) { // Read impls io.Reader func (g *gzipCloser) Read(p []byte) (n int, err error) { - return rand.Read(p) + return g.r.Read(p) } // Close impls io.Closer diff --git a/main.go b/main.go index 3a01f42..3df3d2f 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,12 @@ var ( d bool h bool + // PasswordHash 存储QQ密码哈希供登录使用 + PasswordHash [16]byte + + // AccountToken 存储AccountToken供登录使用 + AccountToken []byte + // 允许通过配置文件设置的状态列表 allowStatus = [...]client.UserOnlineStatus{ client.StatusOnline, client.StatusAway, client.StatusInvisible, client.StatusBusy, @@ -174,8 +180,8 @@ func main() { } log.Infof("密码加密已启用, 请输入Key对密码进行加密: (Enter 提交)") byteKey, _ = term.ReadPassword(int(os.Stdin.Fd())) - global.PasswordHash = md5.Sum([]byte(conf.Account.Password)) - _ = os.WriteFile("password.encrypt", []byte(PasswordHashEncrypt(global.PasswordHash[:], byteKey)), 0644) + PasswordHash = md5.Sum([]byte(conf.Account.Password)) + _ = os.WriteFile("password.encrypt", []byte(PasswordHashEncrypt(PasswordHash[:], byteKey)), 0644) log.Info("密码已加密,为了您的账号安全,请删除配置文件中的密码后重新启动.") readLine() os.Exit(0) @@ -212,10 +218,10 @@ func main() { if err != nil { log.Fatalf("加密存储的密码损坏,请尝试重新配置密码") } - copy(global.PasswordHash[:], ph) + copy(PasswordHash[:], ph) } } else { - global.PasswordHash = md5.Sum([]byte(conf.Account.Password)) + PasswordHash = md5.Sum([]byte(conf.Account.Password)) } if !isFastStart { log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.") @@ -236,9 +242,9 @@ func main() { return "未知" }()) cli = client.NewClientEmpty() - if conf.Account.Uin != 0 && global.PasswordHash != [16]byte{} { + if conf.Account.Uin != 0 && PasswordHash != [16]byte{} { cli.Uin = conf.Account.Uin - cli.PasswordMd5 = global.PasswordHash + cli.PasswordMd5 = PasswordHash } cli.OnLog(func(c *client.QQClient, e *client.LogEvent) { switch e.Type { @@ -270,8 +276,8 @@ func main() { isQRCodeLogin := (conf.Account.Uin == 0 || len(conf.Account.Password) == 0) && !conf.Account.Encrypt isTokenLogin := false saveToken := func() { - global.AccountToken = cli.GenToken() - _ = ioutil.WriteFile("session.token", global.AccountToken, 0677) + AccountToken = cli.GenToken() + _ = ioutil.WriteFile("session.token", AccountToken, 0677) } if global.PathExists("session.token") { token, err := ioutil.ReadFile("session.token") @@ -337,7 +343,7 @@ func main() { time.Sleep(time.Second) } log.Warnf("尝试重连...") - err := cli.TokenLogin(global.AccountToken) + err := cli.TokenLogin(AccountToken) if err == nil { saveToken() return