mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 12:03:50 +08:00
password encrypt supported. close #22
This commit is contained in:
parent
97f7de85a2
commit
44b26b70a3
@ -6,16 +6,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type JsonConfig struct {
|
type JsonConfig struct {
|
||||||
Uin int64 `json:"uin"`
|
Uin int64 `json:"uin"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
EnableDB bool `json:"enable_db"`
|
EncryptPassword bool `json:"encrypt_password"`
|
||||||
AccessToken string `json:"access_token"`
|
PasswordEncrypted string `json:"password_encrypted"`
|
||||||
ReLogin bool `json:"relogin"`
|
EnableDB bool `json:"enable_db"`
|
||||||
ReLoginDelay int `json:"relogin_delay"`
|
AccessToken string `json:"access_token"`
|
||||||
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
ReLogin bool `json:"relogin"`
|
||||||
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
ReLoginDelay int `json:"relogin_delay"`
|
||||||
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
|
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
||||||
Debug bool `json:"debug"`
|
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
||||||
|
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
|
||||||
|
Debug bool `json:"debug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CQHttpApiConfig struct {
|
type CQHttpApiConfig struct {
|
||||||
|
48
main.go
48
main.go
@ -3,8 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
"github.com/Mrs4s/MiraiGo/client"
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
@ -112,7 +115,7 @@ func main() {
|
|||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if conf.Uin == 0 || conf.Password == "" {
|
if conf.Uin == 0 || (conf.Password == "" && conf.PasswordEncrypted == "") {
|
||||||
log.Warnf("请修改 config.json 以添加账号密码.")
|
log.Warnf("请修改 config.json 以添加账号密码.")
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
return
|
return
|
||||||
@ -132,6 +135,24 @@ func main() {
|
|||||||
log.Fatalf("加载设备信息失败: %v", err)
|
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 取消.")
|
log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.")
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
log.Info("开始尝试登录并同步消息...")
|
log.Info("开始尝试登录并同步消息...")
|
||||||
@ -210,3 +231,28 @@ func main() {
|
|||||||
<-c
|
<-c
|
||||||
b.Release()
|
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))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user