mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
Merge branch 'dev' into database-support
# Conflicts: # coolq/bot.go # modules/config/config.go
This commit is contained in:
commit
c4d34fa14f
20
coolq/bot.go
20
coolq/bot.go
@ -21,8 +21,8 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/base"
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CQBot CQBot结构体,存储Bot实例相关配置
|
// CQBot CQBot结构体,存储Bot实例相关配置
|
||||||
@ -66,12 +66,12 @@ func (e *Event) JSONString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewQQBot 初始化一个QQBot实例
|
// NewQQBot 初始化一个QQBot实例
|
||||||
func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
func NewQQBot(cli *client.QQClient) *CQBot {
|
||||||
bot := &CQBot{
|
bot := &CQBot{
|
||||||
Client: cli,
|
Client: cli,
|
||||||
}
|
}
|
||||||
levelNode, levelDB := conf.Database["leveldb"]
|
levelNode, levelDB := base.Database["leveldb"]
|
||||||
mongoNode, mongoDB := conf.Database["mongodb"]
|
mongoNode, mongoDB := base.Database["mongodb"]
|
||||||
multiDB := db.NewMultiDatabase()
|
multiDB := db.NewMultiDatabase()
|
||||||
if levelDB {
|
if levelDB {
|
||||||
lconf := new(config.LevelDBConfig)
|
lconf := new(config.LevelDBConfig)
|
||||||
@ -96,7 +96,7 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
|||||||
bot.db = multiDB
|
bot.db = multiDB
|
||||||
bot.Client.OnPrivateMessage(bot.privateMessageEvent)
|
bot.Client.OnPrivateMessage(bot.privateMessageEvent)
|
||||||
bot.Client.OnGroupMessage(bot.groupMessageEvent)
|
bot.Client.OnGroupMessage(bot.groupMessageEvent)
|
||||||
if conf.Message.ReportSelfMessage {
|
if base.ReportSelfMessage {
|
||||||
bot.Client.OnSelfPrivateMessage(bot.privateMessageEvent)
|
bot.Client.OnSelfPrivateMessage(bot.privateMessageEvent)
|
||||||
bot.Client.OnSelfGroupMessage(bot.groupMessageEvent)
|
bot.Client.OnSelfGroupMessage(bot.groupMessageEvent)
|
||||||
}
|
}
|
||||||
@ -121,15 +121,11 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
|||||||
bot.Client.OnOtherClientStatusChanged(bot.otherClientStatusChangedEvent)
|
bot.Client.OnOtherClientStatusChanged(bot.otherClientStatusChangedEvent)
|
||||||
bot.Client.OnGroupDigest(bot.groupEssenceMsg)
|
bot.Client.OnGroupDigest(bot.groupEssenceMsg)
|
||||||
go func() {
|
go func() {
|
||||||
i := conf.Heartbeat.Interval
|
if base.HeartbeatInterval == 0 {
|
||||||
if i < 0 || conf.Heartbeat.Disabled {
|
|
||||||
log.Warn("警告: 心跳功能已关闭,若非预期,请检查配置文件。")
|
log.Warn("警告: 心跳功能已关闭,若非预期,请检查配置文件。")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if i == 0 {
|
t := time.NewTicker(base.HeartbeatInterval)
|
||||||
i = 5
|
|
||||||
}
|
|
||||||
t := time.NewTicker(time.Second * time.Duration(i))
|
|
||||||
for {
|
for {
|
||||||
<-t.C
|
<-t.C
|
||||||
bot.dispatchEventMessage(global.MSG{
|
bot.dispatchEventMessage(global.MSG{
|
||||||
@ -138,7 +134,7 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
|||||||
"post_type": "meta_event",
|
"post_type": "meta_event",
|
||||||
"meta_event_type": "heartbeat",
|
"meta_event_type": "heartbeat",
|
||||||
"status": bot.CQGetStatus()["data"],
|
"status": bot.CQGetStatus()["data"],
|
||||||
"interval": 1000 * i,
|
"interval": base.HeartbeatInterval.Milliseconds(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -3,11 +3,14 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// command flags
|
// command flags
|
||||||
@ -27,28 +30,36 @@ var (
|
|||||||
SplitURL bool // 是否分割URL
|
SplitURL bool // 是否分割URL
|
||||||
ForceFragmented bool // 是否启用强制分片
|
ForceFragmented bool // 是否启用强制分片
|
||||||
SkipMimeScan bool // 是否跳过Mime扫描
|
SkipMimeScan bool // 是否跳过Mime扫描
|
||||||
|
ReportSelfMessage bool // 是否上报自身消息
|
||||||
UseSSOAddress bool // 是否使用服务器下发的新地址进行重连
|
UseSSOAddress bool // 是否使用服务器下发的新地址进行重连
|
||||||
LogForceNew bool // 是否在每次启动时强制创建全新的文件储存日志
|
LogForceNew bool // 是否在每次启动时强制创建全新的文件储存日志
|
||||||
FastStart bool // 是否为快速启动
|
FastStart bool // 是否为快速启动
|
||||||
|
|
||||||
PostFormat string // 上报格式 string or array
|
PostFormat string // 上报格式 string or array
|
||||||
Proxy string // 存储 proxy_rewrite,用于设置代理
|
Proxy string // 存储 proxy_rewrite,用于设置代理
|
||||||
PasswordHash [16]byte // 存储QQ密码哈希供登录使用
|
PasswordHash [16]byte // 存储QQ密码哈希供登录使用
|
||||||
AccountToken []byte // 存储 AccountToken 供登录使用
|
AccountToken []byte // 存储 AccountToken 供登录使用
|
||||||
Reconnect *config.Reconnect // 重连配置
|
Account *config.Account // 账户配置
|
||||||
LogAging = time.Hour * 24 * 365 // 日志时效
|
Reconnect *config.Reconnect // 重连配置
|
||||||
|
LogLevel string // 日志等级
|
||||||
|
LogAging = time.Hour * 24 * 365 // 日志时效
|
||||||
|
HeartbeatInterval = time.Second * 5 // 心跳间隔
|
||||||
|
|
||||||
|
Servers []map[string]yaml.Node // 连接服务列表
|
||||||
|
Database map[string]yaml.Node // 数据库列表
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse parse flags
|
// Parse parse flags
|
||||||
func Parse() {
|
func Parse() {
|
||||||
flag.StringVar(&LittleC, "c", config.DefaultConfigFile, "configuration filename")
|
wd, _ := os.Getwd()
|
||||||
|
dc := path.Join(wd, "config.yml")
|
||||||
|
flag.StringVar(&LittleC, "c", dc, "configuration filename")
|
||||||
flag.BoolVar(&LittleD, "d", false, "running as a daemon")
|
flag.BoolVar(&LittleD, "d", false, "running as a daemon")
|
||||||
flag.BoolVar(&LittleH, "h", false, "this help")
|
flag.BoolVar(&LittleH, "h", false, "this help")
|
||||||
flag.StringVar(&LittleWD, "w", "", "cover the working directory")
|
flag.StringVar(&LittleWD, "w", "", "cover the working directory")
|
||||||
d := flag.Bool("D", false, "debug mode")
|
d := flag.Bool("D", false, "debug mode")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
config.DefaultConfigFile = LittleC // cover config file
|
|
||||||
if *d {
|
if *d {
|
||||||
Debug = true
|
Debug = true
|
||||||
}
|
}
|
||||||
@ -56,7 +67,7 @@ func Parse() {
|
|||||||
|
|
||||||
// Init read config from yml file
|
// Init read config from yml file
|
||||||
func Init() {
|
func Init() {
|
||||||
conf := config.Get()
|
conf := config.Parse(LittleC)
|
||||||
{ // bool config
|
{ // bool config
|
||||||
if conf.Output.Debug {
|
if conf.Output.Debug {
|
||||||
Debug = true
|
Debug = true
|
||||||
@ -67,11 +78,16 @@ func Init() {
|
|||||||
ExtraReplyData = conf.Message.ExtraReplyData
|
ExtraReplyData = conf.Message.ExtraReplyData
|
||||||
ForceFragmented = conf.Message.ForceFragment
|
ForceFragmented = conf.Message.ForceFragment
|
||||||
SkipMimeScan = conf.Message.SkipMimeScan
|
SkipMimeScan = conf.Message.SkipMimeScan
|
||||||
|
ReportSelfMessage = conf.Message.ReportSelfMessage
|
||||||
UseSSOAddress = conf.Account.UseSSOAddress
|
UseSSOAddress = conf.Account.UseSSOAddress
|
||||||
}
|
}
|
||||||
{ // others
|
{ // others
|
||||||
Proxy = conf.Message.ProxyRewrite
|
Proxy = conf.Message.ProxyRewrite
|
||||||
|
Account = conf.Account
|
||||||
Reconnect = conf.Account.ReLogin
|
Reconnect = conf.Account.ReLogin
|
||||||
|
Servers = conf.Servers
|
||||||
|
Database = conf.Database
|
||||||
|
LogLevel = conf.Output.LogLevel
|
||||||
if conf.Message.PostFormat != "string" && conf.Message.PostFormat != "array" {
|
if conf.Message.PostFormat != "string" && conf.Message.PostFormat != "array" {
|
||||||
log.Warnf("post-format 配置错误, 将自动使用 string")
|
log.Warnf("post-format 配置错误, 将自动使用 string")
|
||||||
PostFormat = "string"
|
PostFormat = "string"
|
||||||
@ -81,5 +97,11 @@ func Init() {
|
|||||||
if conf.Output.LogAging > 0 {
|
if conf.Output.LogAging > 0 {
|
||||||
LogAging = time.Hour * 24 * time.Duration(conf.Output.LogAging)
|
LogAging = time.Hour * 24 * time.Duration(conf.Output.LogAging)
|
||||||
}
|
}
|
||||||
|
if conf.Heartbeat.Interval > 0 {
|
||||||
|
HeartbeatInterval = time.Second * time.Duration(conf.Heartbeat.Interval)
|
||||||
|
}
|
||||||
|
if conf.Heartbeat.Disabled || conf.Heartbeat.Interval < 0 {
|
||||||
|
HeartbeatInterval = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
main.go
43
main.go
@ -28,10 +28,10 @@ import (
|
|||||||
|
|
||||||
"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/Mrs4s/go-cqhttp/global/config"
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global/terminal"
|
"github.com/Mrs4s/go-cqhttp/global/terminal"
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/base"
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/selfupdate"
|
"github.com/Mrs4s/go-cqhttp/internal/selfupdate"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
"github.com/Mrs4s/go-cqhttp/server"
|
"github.com/Mrs4s/go-cqhttp/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,9 +56,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
base.Init()
|
base.Init()
|
||||||
|
|
||||||
// todo: do all config parse in internal/base?
|
|
||||||
conf := config.Get()
|
|
||||||
|
|
||||||
rotateOptions := []rotatelogs.Option{
|
rotateOptions := []rotatelogs.Option{
|
||||||
rotatelogs.WithRotationTime(time.Hour * 24),
|
rotatelogs.WithRotationTime(time.Hour * 24),
|
||||||
}
|
}
|
||||||
@ -71,7 +68,7 @@ func main() {
|
|||||||
log.Errorf("rotatelogs init err: %v", err)
|
log.Errorf("rotatelogs init err: %v", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
log.AddHook(global.NewLocalHook(w, global.LogFormat{}, global.GetLogLevel(conf.Output.LogLevel)...))
|
log.AddHook(global.NewLocalHook(w, global.LogFormat{}, global.GetLogLevel(base.LogLevel)...))
|
||||||
|
|
||||||
mkCacheDir := func(path string, _type string) {
|
mkCacheDir := func(path string, _type string) {
|
||||||
if !global.PathExists(path) {
|
if !global.PathExists(path) {
|
||||||
@ -117,7 +114,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (conf.Account.Uin == 0 || (conf.Account.Password == "" && !conf.Account.Encrypt)) && !global.PathExists("session.token") {
|
if (base.Account.Uin == 0 || (base.Account.Password == "" && !base.Account.Encrypt)) && !global.PathExists("session.token") {
|
||||||
log.Warn("账号密码未配置, 将使用二维码登录.")
|
log.Warn("账号密码未配置, 将使用二维码登录.")
|
||||||
if !base.FastStart {
|
if !base.FastStart {
|
||||||
log.Warn("将在 5秒 后继续.")
|
log.Warn("将在 5秒 后继续.")
|
||||||
@ -145,22 +142,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.Account.Encrypt {
|
if base.Account.Encrypt {
|
||||||
if !global.PathExists("password.encrypt") {
|
if !global.PathExists("password.encrypt") {
|
||||||
if conf.Account.Password == "" {
|
if base.Account.Password == "" {
|
||||||
log.Error("无法进行加密,请在配置文件中的添加密码后重新启动.")
|
log.Error("无法进行加密,请在配置文件中的添加密码后重新启动.")
|
||||||
readLine()
|
readLine()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
log.Infof("密码加密已启用, 请输入Key对密码进行加密: (Enter 提交)")
|
log.Infof("密码加密已启用, 请输入Key对密码进行加密: (Enter 提交)")
|
||||||
byteKey, _ = term.ReadPassword(int(os.Stdin.Fd()))
|
byteKey, _ = term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
base.PasswordHash = md5.Sum([]byte(conf.Account.Password))
|
base.PasswordHash = md5.Sum([]byte(base.Account.Password))
|
||||||
_ = os.WriteFile("password.encrypt", []byte(PasswordHashEncrypt(base.PasswordHash[:], byteKey)), 0o644)
|
_ = os.WriteFile("password.encrypt", []byte(PasswordHashEncrypt(base.PasswordHash[:], byteKey)), 0o644)
|
||||||
log.Info("密码已加密,为了您的账号安全,请删除配置文件中的密码后重新启动.")
|
log.Info("密码已加密,为了您的账号安全,请删除配置文件中的密码后重新启动.")
|
||||||
readLine()
|
readLine()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
} else {
|
} else {
|
||||||
if conf.Account.Password != "" {
|
if base.Account.Password != "" {
|
||||||
log.Error("密码已加密,为了您的账号安全,请删除配置文件中的密码后重新启动.")
|
log.Error("密码已加密,为了您的账号安全,请删除配置文件中的密码后重新启动.")
|
||||||
readLine()
|
readLine()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
@ -194,8 +191,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
copy(base.PasswordHash[:], ph)
|
copy(base.PasswordHash[:], ph)
|
||||||
}
|
}
|
||||||
} else if len(conf.Account.Password) > 0 {
|
} else if len(base.Account.Password) > 0 {
|
||||||
base.PasswordHash = md5.Sum([]byte(conf.Account.Password))
|
base.PasswordHash = md5.Sum([]byte(base.Account.Password))
|
||||||
}
|
}
|
||||||
if !base.FastStart {
|
if !base.FastStart {
|
||||||
log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.")
|
log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.")
|
||||||
@ -218,7 +215,7 @@ func main() {
|
|||||||
return "未知"
|
return "未知"
|
||||||
}())
|
}())
|
||||||
cli = newClient()
|
cli = newClient()
|
||||||
isQRCodeLogin := (conf.Account.Uin == 0 || len(conf.Account.Password) == 0) && !conf.Account.Encrypt
|
isQRCodeLogin := (base.Account.Uin == 0 || len(base.Account.Password) == 0) && !base.Account.Encrypt
|
||||||
isTokenLogin := false
|
isTokenLogin := false
|
||||||
saveToken := func() {
|
saveToken := func() {
|
||||||
base.AccountToken = cli.GenToken()
|
base.AccountToken = cli.GenToken()
|
||||||
@ -227,11 +224,11 @@ func main() {
|
|||||||
if global.PathExists("session.token") {
|
if global.PathExists("session.token") {
|
||||||
token, err := os.ReadFile("session.token")
|
token, err := os.ReadFile("session.token")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if conf.Account.Uin != 0 {
|
if base.Account.Uin != 0 {
|
||||||
r := binary.NewReader(token)
|
r := binary.NewReader(token)
|
||||||
cu := r.ReadInt64()
|
cu := r.ReadInt64()
|
||||||
if cu != conf.Account.Uin {
|
if cu != base.Account.Uin {
|
||||||
log.Warnf("警告: 配置文件内的QQ号 (%v) 与缓存内的QQ号 (%v) 不相同", conf.Account.Uin, cu)
|
log.Warnf("警告: 配置文件内的QQ号 (%v) 与缓存内的QQ号 (%v) 不相同", base.Account.Uin, cu)
|
||||||
log.Warnf("1. 使用会话缓存继续.")
|
log.Warnf("1. 使用会话缓存继续.")
|
||||||
log.Warnf("2. 删除会话缓存并重启.")
|
log.Warnf("2. 删除会话缓存并重启.")
|
||||||
log.Warnf("请选择: (5秒后自动选1)")
|
log.Warnf("请选择: (5秒后自动选1)")
|
||||||
@ -255,8 +252,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if conf.Account.Uin != 0 && base.PasswordHash != [16]byte{} {
|
if base.Account.Uin != 0 && base.PasswordHash != [16]byte{} {
|
||||||
cli.Uin = conf.Account.Uin
|
cli.Uin = base.Account.Uin
|
||||||
cli.PasswordMd5 = base.PasswordHash
|
cli.PasswordMd5 = base.PasswordHash
|
||||||
}
|
}
|
||||||
if !isTokenLogin {
|
if !isTokenLogin {
|
||||||
@ -329,12 +326,12 @@ func main() {
|
|||||||
log.Infof("开始加载群列表...")
|
log.Infof("开始加载群列表...")
|
||||||
global.Check(cli.ReloadGroupList(), true)
|
global.Check(cli.ReloadGroupList(), true)
|
||||||
log.Infof("共加载 %v 个群.", len(cli.GroupList))
|
log.Infof("共加载 %v 个群.", len(cli.GroupList))
|
||||||
if uint(conf.Account.Status) >= uint(len(allowStatus)) {
|
if uint(base.Account.Status) >= uint(len(allowStatus)) {
|
||||||
conf.Account.Status = 0
|
base.Account.Status = 0
|
||||||
}
|
}
|
||||||
cli.SetOnlineStatus(allowStatus[conf.Account.Status])
|
cli.SetOnlineStatus(allowStatus[base.Account.Status])
|
||||||
bot := coolq.NewQQBot(cli, conf)
|
bot := coolq.NewQQBot(cli)
|
||||||
for _, m := range conf.Servers {
|
for _, m := range base.Servers {
|
||||||
if h, ok := m["http"]; ok {
|
if h, ok := m["http"]; ok {
|
||||||
hc := new(config.HTTPServer)
|
hc := new(config.HTTPServer)
|
||||||
if err := h.Decode(hc); err != nil {
|
if err := h.Decode(hc); err != nil {
|
||||||
|
@ -6,10 +6,8 @@ import (
|
|||||||
_ "embed" // embed the default config file
|
_ "embed" // embed the default config file
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/param"
|
"github.com/Mrs4s/go-cqhttp/internal/param"
|
||||||
|
|
||||||
@ -21,11 +19,6 @@ import (
|
|||||||
//go:embed default_config.yml
|
//go:embed default_config.yml
|
||||||
var defaultConfig string
|
var defaultConfig string
|
||||||
|
|
||||||
var currentPath = getCurrentPath()
|
|
||||||
|
|
||||||
// DefaultConfigFile 默认配置文件路径
|
|
||||||
var DefaultConfigFile = path.Join(currentPath, "config.yml")
|
|
||||||
|
|
||||||
// Reconnect 重连配置
|
// Reconnect 重连配置
|
||||||
type Reconnect struct {
|
type Reconnect struct {
|
||||||
Disabled bool `yaml:"disabled"`
|
Disabled bool `yaml:"disabled"`
|
||||||
@ -34,17 +27,19 @@ type Reconnect struct {
|
|||||||
Interval int `yaml:"interval"`
|
Interval int `yaml:"interval"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Account 账号配置
|
||||||
|
type Account struct {
|
||||||
|
Uin int64 `yaml:"uin"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
Encrypt bool `yaml:"encrypt"`
|
||||||
|
Status int `yaml:"status"`
|
||||||
|
ReLogin *Reconnect `yaml:"relogin"`
|
||||||
|
UseSSOAddress bool `yaml:"use-sso-address"`
|
||||||
|
}
|
||||||
|
|
||||||
// Config 总配置文件
|
// Config 总配置文件
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Account struct {
|
Account *Account `yaml:"account"`
|
||||||
Uin int64 `yaml:"uin"`
|
|
||||||
Password string `yaml:"password"`
|
|
||||||
Encrypt bool `yaml:"encrypt"`
|
|
||||||
Status int `yaml:"status"`
|
|
||||||
ReLogin *Reconnect `yaml:"relogin"`
|
|
||||||
UseSSOAddress bool `yaml:"use-sso-address"`
|
|
||||||
} `yaml:"account"`
|
|
||||||
|
|
||||||
Heartbeat struct {
|
Heartbeat struct {
|
||||||
Disabled bool `yaml:"disabled"`
|
Disabled bool `yaml:"disabled"`
|
||||||
Interval int `yaml:"interval"`
|
Interval int `yaml:"interval"`
|
||||||
@ -52,10 +47,10 @@ type Config struct {
|
|||||||
|
|
||||||
Message struct {
|
Message struct {
|
||||||
PostFormat string `yaml:"post-format"`
|
PostFormat string `yaml:"post-format"`
|
||||||
|
ProxyRewrite string `yaml:"proxy-rewrite"`
|
||||||
IgnoreInvalidCQCode bool `yaml:"ignore-invalid-cqcode"`
|
IgnoreInvalidCQCode bool `yaml:"ignore-invalid-cqcode"`
|
||||||
ForceFragment bool `yaml:"force-fragment"`
|
ForceFragment bool `yaml:"force-fragment"`
|
||||||
FixURL bool `yaml:"fix-url"`
|
FixURL bool `yaml:"fix-url"`
|
||||||
ProxyRewrite string `yaml:"proxy-rewrite"`
|
|
||||||
ReportSelfMessage bool `yaml:"report-self-message"`
|
ReportSelfMessage bool `yaml:"report-self-message"`
|
||||||
RemoveReplyAt bool `yaml:"remove-reply-at"`
|
RemoveReplyAt bool `yaml:"remove-reply-at"`
|
||||||
ExtraReplyData bool `yaml:"extra-reply-data"`
|
ExtraReplyData bool `yaml:"extra-reply-data"`
|
||||||
@ -148,113 +143,97 @@ type MongoDBConfig struct {
|
|||||||
Database string `yaml:"database"`
|
Database string `yaml:"database"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// Parse 从默认配置文件路径中获取
|
||||||
config *Config
|
func Parse(path string) *Config {
|
||||||
once sync.Once
|
fromEnv := os.Getenv("GCQ_UIN") != ""
|
||||||
)
|
|
||||||
|
|
||||||
// Get 从默认配置文件路径中获取
|
file, err := os.Open(path)
|
||||||
func Get() *Config {
|
config := &Config{}
|
||||||
once.Do(func() {
|
if err == nil {
|
||||||
hasEnvironmentConf := os.Getenv("GCQ_UIN") != ""
|
defer func() { _ = file.Close() }()
|
||||||
|
if err = yaml.NewDecoder(file).Decode(config); err != nil && !fromEnv {
|
||||||
file, err := os.Open(DefaultConfigFile)
|
log.Fatal("配置文件不合法!", err)
|
||||||
config = &Config{}
|
|
||||||
if err == nil {
|
|
||||||
defer func() { _ = file.Close() }()
|
|
||||||
if err = yaml.NewDecoder(file).Decode(config); err != nil && !hasEnvironmentConf {
|
|
||||||
log.Fatal("配置文件不合法!", err)
|
|
||||||
}
|
|
||||||
} else if !hasEnvironmentConf {
|
|
||||||
generateConfig()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
if hasEnvironmentConf {
|
} else if !fromEnv {
|
||||||
// type convert tools
|
generateConfig()
|
||||||
toInt64 := func(str string) int64 {
|
os.Exit(0)
|
||||||
i, _ := strconv.ParseInt(str, 10, 64)
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
|
|
||||||
// load config from environment variable
|
|
||||||
param.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0))
|
|
||||||
param.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "")
|
|
||||||
param.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0))
|
|
||||||
param.SetAtDefault(&config.Account.ReLogin.Disabled, !param.EnsureBool(os.Getenv("GCQ_RELOGIN_DISABLED"), true), false)
|
|
||||||
param.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0))
|
|
||||||
param.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0))
|
|
||||||
dbConf := &LevelDBConfig{Enable: param.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)}
|
|
||||||
if config.Database == nil {
|
|
||||||
config.Database = make(map[string]yaml.Node)
|
|
||||||
}
|
|
||||||
config.Database["leveldb"] = func() yaml.Node {
|
|
||||||
n := &yaml.Node{}
|
|
||||||
_ = n.Encode(dbConf)
|
|
||||||
return *n
|
|
||||||
}()
|
|
||||||
accessTokenEnv := os.Getenv("GCQ_ACCESS_TOKEN")
|
|
||||||
if os.Getenv("GCQ_HTTP_PORT") != "" {
|
|
||||||
node := &yaml.Node{}
|
|
||||||
httpConf := &HTTPServer{
|
|
||||||
Host: "0.0.0.0",
|
|
||||||
Port: 5700,
|
|
||||||
MiddleWares: MiddleWares{
|
|
||||||
AccessToken: accessTokenEnv,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
param.SetExcludeDefault(&httpConf.Disabled, param.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false)
|
|
||||||
param.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "")
|
|
||||||
param.SetExcludeDefault(&httpConf.Port, int(toInt64(os.Getenv("GCQ_HTTP_PORT"))), 0)
|
|
||||||
if os.Getenv("GCQ_HTTP_POST_URL") != "" {
|
|
||||||
httpConf.Post = append(httpConf.Post, struct {
|
|
||||||
URL string `yaml:"url"`
|
|
||||||
Secret string `yaml:"secret"`
|
|
||||||
}{os.Getenv("GCQ_HTTP_POST_URL"), os.Getenv("GCQ_HTTP_POST_SECRET")})
|
|
||||||
}
|
|
||||||
_ = node.Encode(httpConf)
|
|
||||||
config.Servers = append(config.Servers, map[string]yaml.Node{"http": *node})
|
|
||||||
}
|
|
||||||
if os.Getenv("GCQ_WS_PORT") != "" {
|
|
||||||
node := &yaml.Node{}
|
|
||||||
wsServerConf := &WebsocketServer{
|
|
||||||
Host: "0.0.0.0",
|
|
||||||
Port: 6700,
|
|
||||||
MiddleWares: MiddleWares{
|
|
||||||
AccessToken: accessTokenEnv,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
param.SetExcludeDefault(&wsServerConf.Disabled, param.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false)
|
|
||||||
param.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "")
|
|
||||||
param.SetExcludeDefault(&wsServerConf.Port, int(toInt64(os.Getenv("GCQ_WS_PORT"))), 0)
|
|
||||||
_ = node.Encode(wsServerConf)
|
|
||||||
config.Servers = append(config.Servers, map[string]yaml.Node{"ws": *node})
|
|
||||||
}
|
|
||||||
if os.Getenv("GCQ_RWS_API") != "" || os.Getenv("GCQ_RWS_EVENT") != "" || os.Getenv("GCQ_RWS_UNIVERSAL") != "" {
|
|
||||||
node := &yaml.Node{}
|
|
||||||
rwsConf := &WebsocketReverse{
|
|
||||||
MiddleWares: MiddleWares{
|
|
||||||
AccessToken: accessTokenEnv,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
param.SetExcludeDefault(&rwsConf.Disabled, param.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false)
|
|
||||||
param.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "")
|
|
||||||
param.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "")
|
|
||||||
param.SetExcludeDefault(&rwsConf.Universal, os.Getenv("GCQ_RWS_UNIVERSAL"), "")
|
|
||||||
_ = node.Encode(rwsConf)
|
|
||||||
config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
// getCurrentPath 获取当前文件的路径,直接返回string
|
|
||||||
func getCurrentPath() string {
|
|
||||||
cwd, e := os.Getwd()
|
|
||||||
if e != nil {
|
|
||||||
panic(e)
|
|
||||||
}
|
}
|
||||||
return cwd
|
if fromEnv {
|
||||||
|
// type convert tools
|
||||||
|
toInt64 := func(str string) int64 {
|
||||||
|
i, _ := strconv.ParseInt(str, 10, 64)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// load config from environment variable
|
||||||
|
param.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0))
|
||||||
|
param.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "")
|
||||||
|
param.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0))
|
||||||
|
param.SetAtDefault(&config.Account.ReLogin.Disabled, !param.EnsureBool(os.Getenv("GCQ_RELOGIN_DISABLED"), true), false)
|
||||||
|
param.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0))
|
||||||
|
param.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0))
|
||||||
|
dbConf := &LevelDBConfig{Enable: param.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)}
|
||||||
|
if config.Database == nil {
|
||||||
|
config.Database = make(map[string]yaml.Node)
|
||||||
|
}
|
||||||
|
config.Database["leveldb"] = func() yaml.Node {
|
||||||
|
n := &yaml.Node{}
|
||||||
|
_ = n.Encode(dbConf)
|
||||||
|
return *n
|
||||||
|
}()
|
||||||
|
accessTokenEnv := os.Getenv("GCQ_ACCESS_TOKEN")
|
||||||
|
if os.Getenv("GCQ_HTTP_PORT") != "" {
|
||||||
|
node := &yaml.Node{}
|
||||||
|
httpConf := &HTTPServer{
|
||||||
|
Host: "0.0.0.0",
|
||||||
|
Port: 5700,
|
||||||
|
MiddleWares: MiddleWares{
|
||||||
|
AccessToken: accessTokenEnv,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
param.SetExcludeDefault(&httpConf.Disabled, param.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false)
|
||||||
|
param.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "")
|
||||||
|
param.SetExcludeDefault(&httpConf.Port, int(toInt64(os.Getenv("GCQ_HTTP_PORT"))), 0)
|
||||||
|
if os.Getenv("GCQ_HTTP_POST_URL") != "" {
|
||||||
|
httpConf.Post = append(httpConf.Post, struct {
|
||||||
|
URL string `yaml:"url"`
|
||||||
|
Secret string `yaml:"secret"`
|
||||||
|
}{os.Getenv("GCQ_HTTP_POST_URL"), os.Getenv("GCQ_HTTP_POST_SECRET")})
|
||||||
|
}
|
||||||
|
_ = node.Encode(httpConf)
|
||||||
|
config.Servers = append(config.Servers, map[string]yaml.Node{"http": *node})
|
||||||
|
}
|
||||||
|
if os.Getenv("GCQ_WS_PORT") != "" {
|
||||||
|
node := &yaml.Node{}
|
||||||
|
wsServerConf := &WebsocketServer{
|
||||||
|
Host: "0.0.0.0",
|
||||||
|
Port: 6700,
|
||||||
|
MiddleWares: MiddleWares{
|
||||||
|
AccessToken: accessTokenEnv,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
param.SetExcludeDefault(&wsServerConf.Disabled, param.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false)
|
||||||
|
param.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "")
|
||||||
|
param.SetExcludeDefault(&wsServerConf.Port, int(toInt64(os.Getenv("GCQ_WS_PORT"))), 0)
|
||||||
|
_ = node.Encode(wsServerConf)
|
||||||
|
config.Servers = append(config.Servers, map[string]yaml.Node{"ws": *node})
|
||||||
|
}
|
||||||
|
if os.Getenv("GCQ_RWS_API") != "" || os.Getenv("GCQ_RWS_EVENT") != "" || os.Getenv("GCQ_RWS_UNIVERSAL") != "" {
|
||||||
|
node := &yaml.Node{}
|
||||||
|
rwsConf := &WebsocketReverse{
|
||||||
|
MiddleWares: MiddleWares{
|
||||||
|
AccessToken: accessTokenEnv,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
param.SetExcludeDefault(&rwsConf.Disabled, param.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false)
|
||||||
|
param.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "")
|
||||||
|
param.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "")
|
||||||
|
param.SetExcludeDefault(&rwsConf.Universal, os.Getenv("GCQ_RWS_UNIVERSAL"), "")
|
||||||
|
_ = node.Encode(rwsConf)
|
||||||
|
config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateConfig 生成配置文件
|
// generateConfig 生成配置文件
|
@ -21,7 +21,7 @@ import (
|
|||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpServer struct {
|
type httpServer struct {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunPprofServer 启动 pprof 性能分析服务器
|
// RunPprofServer 启动 pprof 性能分析服务器
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
|
|
||||||
"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/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type lambdaClient struct {
|
type lambdaClient struct {
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
"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/Mrs4s/go-cqhttp/global/config"
|
"github.com/Mrs4s/go-cqhttp/modules/config"
|
||||||
|
|
||||||
"github.com/Mrs4s/MiraiGo/utils"
|
"github.com/Mrs4s/MiraiGo/utils"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user