1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

feather: use startup params to decrypt password

This commit is contained in:
Ink-33 2020-11-16 22:34:05 +08:00
parent 79ba9474a3
commit 0f0f74ece8
No known key found for this signature in database
GPG Key ID: 5D8B1D036EFB0D2E

75
main.go
View File

@ -2,17 +2,16 @@ package main
import ( import (
"bufio" "bufio"
"context"
"crypto/md5" "crypto/md5"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Mrs4s/go-cqhttp/server"
"github.com/guonaihong/gout"
"github.com/tidwall/gjson"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"path" "path"
"runtime" "runtime"
@ -20,15 +19,19 @@ import (
"strings" "strings"
"time" "time"
"github.com/Mrs4s/go-cqhttp/server"
"github.com/guonaihong/gout"
"github.com/tidwall/gjson"
"github.com/Mrs4s/MiraiGo/binary" "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"
"github.com/getlantern/go-update" "github.com/getlantern/go-update"
"github.com/lestrrat-go/file-rotatelogs" rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook" "github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/t-tomalak/logrus-easy-formatter" easy "github.com/t-tomalak/logrus-easy-formatter"
) )
func init() { func init() {
@ -93,13 +96,26 @@ func init() {
func main() { func main() {
console := bufio.NewReader(os.Stdin) console := bufio.NewReader(os.Stdin)
var strKey string
arg := os.Args arg := os.Args
if len(arg) > 1 && arg[1] == "update" { fmt.Println(arg)
if len(arg) > 2 { if len(arg) > 1 {
selfUpdate(arg[2]) for i := range arg {
} else { switch arg[i] {
selfUpdate("") case "update":
if len(arg) > i+1 {
selfUpdate(arg[i+2])
} else {
selfUpdate("")
}
case "key":
if len(arg) > i+1 {
b := []byte(arg[i+1])
b = append(b, 13, 10)
strKey = string(b[:])
fmt.Println(b)
}
}
} }
} }
@ -216,8 +232,24 @@ func main() {
} }
} }
if conf.PasswordEncrypted != "" { if conf.PasswordEncrypted != "" {
log.Infof("密码加密已启用, 请输入Key对密码进行解密以继续: (Enter 提交)") if strKey == "" {
strKey, _ := console.ReadString('\n') log.Infof("密码加密已启用, 请输入Key对密码进行解密以继续: (Enter 提交)")
ctx := context.Background()
go func(ctx context.Context) {
select {
case <-ctx.Done():
return
case <-time.After(time.Second * 45):
log.Infof("解密key输入超时")
time.Sleep(3 * time.Second)
os.Exit(0)
}
}(ctx)
strKey, _ = console.ReadString('\n')
ctx.Done()
} else {
log.Infof("密码加密已启用, 使用运行时传递的参数进行解密,按 Ctrl+C 取消.")
}
key := md5.Sum([]byte(strKey)) key := md5.Sum([]byte(strKey))
conf.Password = DecryptPwd(conf.PasswordEncrypted, key[:]) conf.Password = DecryptPwd(conf.PasswordEncrypted, key[:])
} }
@ -284,10 +316,23 @@ func main() {
} }
b := server.WebServer.Run(fmt.Sprintf("%s:%d", conf.WebUi.Host, conf.WebUi.WebUiPort), cli) b := server.WebServer.Run(fmt.Sprintf("%s:%d", conf.WebUi.Host, conf.WebUi.WebUiPort), cli)
c := server.Console c := server.Console
r := server.Restart
go checkUpdate() go checkUpdate()
signal.Notify(c, os.Interrupt, os.Kill) signal.Notify(c, os.Interrupt, os.Kill)
<-c select {
b.Release() case <-c:
b.Release()
case <-r:
b.Release()
cmd := &exec.Cmd{
Path: arg[0],
Args: arg[1:],
Stderr: os.Stderr,
Stdout: os.Stdout,
}
server.HttpServer.ShutDown()
cmd.Start()
}
} }
func EncryptPwd(pwd string, key []byte) string { func EncryptPwd(pwd string, key []byte) string {