From 44b26b70a35d358f4ecb0027cdcadd77a2b1f873 Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Mon, 10 Aug 2020 07:58:00 +0800 Subject: [PATCH] password encrypt supported. close #22 --- global/config.go | 22 ++++++++++++---------- main.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/global/config.go b/global/config.go index 6966cd154..267cd5b00 100644 --- a/global/config.go +++ b/global/config.go @@ -6,16 +6,18 @@ import ( ) type JsonConfig struct { - Uin int64 `json:"uin"` - Password string `json:"password"` - EnableDB bool `json:"enable_db"` - AccessToken string `json:"access_token"` - ReLogin bool `json:"relogin"` - ReLoginDelay int `json:"relogin_delay"` - HttpConfig *GoCQHttpConfig `json:"http_config"` - WSConfig *GoCQWebsocketConfig `json:"ws_config"` - ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"` - Debug bool `json:"debug"` + Uin int64 `json:"uin"` + Password string `json:"password"` + EncryptPassword bool `json:"encrypt_password"` + PasswordEncrypted string `json:"password_encrypted"` + EnableDB bool `json:"enable_db"` + AccessToken string `json:"access_token"` + ReLogin bool `json:"relogin"` + ReLoginDelay int `json:"relogin_delay"` + HttpConfig *GoCQHttpConfig `json:"http_config"` + WSConfig *GoCQWebsocketConfig `json:"ws_config"` + ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"` + Debug bool `json:"debug"` } type CQHttpApiConfig struct { diff --git a/main.go b/main.go index 84ed7a38f..5ad0937f7 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,11 @@ package main import ( "bufio" "bytes" + "crypto/md5" + "encoding/base64" "encoding/json" "fmt" + "github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/client" "github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/global" @@ -112,7 +115,7 @@ func main() { time.Sleep(time.Second * 5) return } - if conf.Uin == 0 || conf.Password == "" { + if conf.Uin == 0 || (conf.Password == "" && conf.PasswordEncrypted == "") { log.Warnf("请修改 config.json 以添加账号密码.") time.Sleep(time.Second * 5) return @@ -132,6 +135,24 @@ func main() { log.Fatalf("加载设备信息失败: %v", err) } } + if conf.EncryptPassword && conf.PasswordEncrypted == "" { + log.Infof("密码加密已启用, 请输入Key对密码进行加密: (Enter 提交)") + strKey, _ := console.ReadString('\n') + key := md5.Sum([]byte(strKey)) + if encrypted := EncryptPwd(conf.Password, key[:]); encrypted != "" { + conf.Password = "" + conf.PasswordEncrypted = encrypted + _ = conf.Save("config.json") + } else { + log.Warnf("加密时出现问题.") + } + } + if conf.PasswordEncrypted != "" { + log.Infof("密码加密已启用, 请输入Key对密码进行解密以继续: (Enter 提交)") + strKey, _ := console.ReadString('\n') + key := md5.Sum([]byte(strKey)) + conf.Password = DecryptPwd(conf.PasswordEncrypted, key[:]) + } log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.") time.Sleep(time.Second * 5) log.Info("开始尝试登录并同步消息...") @@ -210,3 +231,28 @@ func main() { <-c b.Release() } + +func EncryptPwd(pwd string, key []byte) string { + tea := binary.NewTeaCipher(key) + if tea == nil { + return "" + } + return base64.StdEncoding.EncodeToString(tea.Encrypt([]byte(pwd))) +} + +func DecryptPwd(ePwd string, key []byte) string { + defer func() { + if pan := recover(); pan != nil { + log.Fatalf("密码解密失败: %v", pan) + } + }() + encrypted, err := base64.StdEncoding.DecodeString(ePwd) + if err != nil { + panic(err) + } + tea := binary.NewTeaCipher(key) + if tea == nil { + panic("密钥错误") + } + return string(tea.Decrypt(encrypted)) +}