mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
* fix: skip callback error * update: update comment * change the logic of callback and auto-register * add token update prompt. * fix log buffer string * fix #2368 增加对 client 的利用,避免创建过多 clients * refactor: wrap sign request * feat: impl additional sign servers configuration * fix error in using configurations. * fix lint error * 支持切换回主签名服务器 * feat: support different key and auth * optimize: find avaliable sign-server * fix: register instance after server is changed * fix lint error * update: add config 'sync-check-servers' * update: first check master sign-server, or wait 3s * add checking log & optimize wait for checking done * fix wrong judge * add config: rule for changing sign server * optimize registration logic after changing server * add some log * fix #2390 * resolve requested changes in #2389 * update dependency * fix lint error 'idx is unused' * refactor: extract sync check and async check logic * delete async check sign-server
166 lines
4.7 KiB
Go
166 lines
4.7 KiB
Go
// Package config 包含go-cqhttp操作配置文件的相关函数
|
|
package config
|
|
|
|
import (
|
|
"bufio"
|
|
_ "embed" // embed the default config file
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// defaultConfig 默认配置文件
|
|
//
|
|
//go:embed default_config.yml
|
|
var defaultConfig string
|
|
|
|
// Reconnect 重连配置
|
|
type Reconnect struct {
|
|
Disabled bool `yaml:"disabled"`
|
|
Delay uint `yaml:"delay"`
|
|
MaxTimes uint `yaml:"max-times"`
|
|
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"`
|
|
AllowTempSession bool `yaml:"allow-temp-session"`
|
|
SignServers []SignServer `yaml:"sign-servers"`
|
|
RuleChangeSignServer int `yaml:"rule-change-sign-server"`
|
|
MaxCheckCount int `yaml:"max-check-count"`
|
|
SignServerTimeout int `yaml:"sign-server-timeout"`
|
|
IsBelow110 bool `yaml:"is-below-110"`
|
|
AutoRegister bool `yaml:"auto-register"`
|
|
AutoRefreshToken bool `yaml:"auto-refresh-token"`
|
|
RefreshInterval int64 `yaml:"refresh-interval"`
|
|
}
|
|
|
|
// SignServer 签名服务器
|
|
type SignServer struct {
|
|
URL string `yaml:"url"`
|
|
Key string `yaml:"key"`
|
|
Authorization string `yaml:"authorization"`
|
|
}
|
|
|
|
// Config 总配置文件
|
|
type Config struct {
|
|
Account *Account `yaml:"account"`
|
|
Heartbeat struct {
|
|
Disabled bool `yaml:"disabled"`
|
|
Interval int `yaml:"interval"`
|
|
} `yaml:"heartbeat"`
|
|
|
|
Message struct {
|
|
PostFormat string `yaml:"post-format"`
|
|
ProxyRewrite string `yaml:"proxy-rewrite"`
|
|
IgnoreInvalidCQCode bool `yaml:"ignore-invalid-cqcode"`
|
|
ForceFragment bool `yaml:"force-fragment"`
|
|
FixURL bool `yaml:"fix-url"`
|
|
ReportSelfMessage bool `yaml:"report-self-message"`
|
|
RemoveReplyAt bool `yaml:"remove-reply-at"`
|
|
ExtraReplyData bool `yaml:"extra-reply-data"`
|
|
SkipMimeScan bool `yaml:"skip-mime-scan"`
|
|
ConvertWebpImage bool `yaml:"convert-webp-image"`
|
|
HTTPTimeout int `yaml:"http-timeout"`
|
|
} `yaml:"message"`
|
|
|
|
Output struct {
|
|
LogLevel string `yaml:"log-level"`
|
|
LogAging int `yaml:"log-aging"`
|
|
LogForceNew bool `yaml:"log-force-new"`
|
|
LogColorful *bool `yaml:"log-colorful"`
|
|
Debug bool `yaml:"debug"`
|
|
} `yaml:"output"`
|
|
|
|
Servers []map[string]yaml.Node `yaml:"servers"`
|
|
Database map[string]yaml.Node `yaml:"database"`
|
|
}
|
|
|
|
// Server 的简介和初始配置
|
|
type Server struct {
|
|
Brief string
|
|
Default string
|
|
}
|
|
|
|
// Parse 从默认配置文件路径中获取
|
|
func Parse(path string) *Config {
|
|
file, err := os.ReadFile(path)
|
|
config := &Config{}
|
|
if err == nil {
|
|
err = yaml.NewDecoder(strings.NewReader(expand(string(file), os.Getenv))).Decode(config)
|
|
if err != nil {
|
|
log.Fatal("配置文件不合法!", err)
|
|
}
|
|
} else {
|
|
generateConfig()
|
|
os.Exit(0)
|
|
}
|
|
return config
|
|
}
|
|
|
|
var serverconfs []*Server
|
|
|
|
// AddServer 添加该服务的简介和默认配置
|
|
func AddServer(s *Server) {
|
|
serverconfs = append(serverconfs, s)
|
|
}
|
|
|
|
// generateConfig 生成配置文件
|
|
func generateConfig() {
|
|
fmt.Println("未找到配置文件,正在为您生成配置文件中!")
|
|
sb := strings.Builder{}
|
|
sb.WriteString(defaultConfig)
|
|
hint := "请选择你需要的通信方式:"
|
|
for i, s := range serverconfs {
|
|
hint += fmt.Sprintf("\n> %d: %s", i, s.Brief)
|
|
}
|
|
hint += `
|
|
请输入你需要的编号(0-9),可输入多个,同一编号也可输入多个(如: 233)
|
|
您的选择是:`
|
|
fmt.Print(hint)
|
|
input := bufio.NewReader(os.Stdin)
|
|
readString, err := input.ReadString('\n')
|
|
if err != nil {
|
|
log.Fatal("输入不合法: ", err)
|
|
}
|
|
rmax := len(serverconfs)
|
|
if rmax > 10 {
|
|
rmax = 10
|
|
}
|
|
for _, r := range readString {
|
|
r -= '0'
|
|
if r >= 0 && r < rune(rmax) {
|
|
sb.WriteString(serverconfs[r].Default)
|
|
}
|
|
}
|
|
_ = os.WriteFile("config.yml", []byte(sb.String()), 0o644)
|
|
fmt.Println("默认配置文件已生成,请修改 config.yml 后重新启动!")
|
|
_, _ = input.ReadString('\n')
|
|
}
|
|
|
|
// expand 使用正则进行环境变量展开
|
|
// os.ExpandEnv 字符 $ 无法逃逸
|
|
// https://github.com/golang/go/issues/43482
|
|
func expand(s string, mapping func(string) string) string {
|
|
r := regexp.MustCompile(`\${([a-zA-Z_]+[a-zA-Z0-9_:/.]*)}`)
|
|
return r.ReplaceAllStringFunc(s, func(s string) string {
|
|
s = strings.Trim(s, "${}")
|
|
before, after, ok := strings.Cut(s, ":")
|
|
m := mapping(before)
|
|
if ok && m == "" {
|
|
return after
|
|
}
|
|
return m
|
|
})
|
|
}
|