mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 19:43:49 +08:00
fix golint
This commit is contained in:
parent
d16fd6f34d
commit
61f4e7826a
10
coolq/api.go
10
coolq/api.go
@ -730,10 +730,10 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
|
||||
}
|
||||
|
||||
func (bot *CQBot) CQGetImage(file string) MSG {
|
||||
if !global.PathExists(path.Join(global.IMAGE_PATH, file)) {
|
||||
if !global.PathExists(path.Join(global.ImagePath, file)) {
|
||||
return Failed(100)
|
||||
}
|
||||
if b, err := ioutil.ReadFile(path.Join(global.IMAGE_PATH, file)); err == nil {
|
||||
if b, err := ioutil.ReadFile(path.Join(global.ImagePath, file)); err == nil {
|
||||
r := binary.NewReader(b)
|
||||
r.ReadBytes(16)
|
||||
msg := MSG{
|
||||
@ -741,7 +741,7 @@ func (bot *CQBot) CQGetImage(file string) MSG {
|
||||
"filename": r.ReadString(),
|
||||
"url": r.ReadString(),
|
||||
}
|
||||
local := path.Join(global.CACHE_PATH, file+"."+path.Ext(msg["filename"].(string)))
|
||||
local := path.Join(global.CachePath, file+"."+path.Ext(msg["filename"].(string)))
|
||||
if !global.PathExists(local) {
|
||||
if data, err := global.GetBytes(msg["url"].(string)); err == nil {
|
||||
_ = ioutil.WriteFile(local, data, 0644)
|
||||
@ -756,7 +756,7 @@ func (bot *CQBot) CQGetImage(file string) MSG {
|
||||
|
||||
func (bot *CQBot) CQDownloadFile(url string, headers map[string]string, threadCount int) MSG {
|
||||
hash := md5.Sum([]byte(url))
|
||||
file := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".cache")
|
||||
file := path.Join(global.CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
if global.PathExists(file) {
|
||||
if err := os.Remove(file); err != nil {
|
||||
log.Warnf("删除缓存文件 %v 时出现错误: %v", file, err)
|
||||
@ -867,7 +867,7 @@ func (bot *CQBot) CQReloadEventFilter() MSG {
|
||||
|
||||
func (bot *CQBot) CQSetGroupPortrait(groupId int64, file, cache string) MSG {
|
||||
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||
img, err := global.FindFile(file, cache, global.IMAGE_PATH)
|
||||
img, err := global.FindFile(file, cache, global.ImagePath)
|
||||
if err != nil {
|
||||
log.Warnf("set group portrait error: %v", err)
|
||||
return Failed(100, "LOAD_FILE_ERROR", err.Error())
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"os"
|
||||
@ -14,6 +13,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/binary"
|
||||
@ -41,7 +42,7 @@ type MSG map[string]interface{}
|
||||
|
||||
var ForceFragmented = false
|
||||
|
||||
func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
|
||||
func NewQQBot(cli *client.QQClient, conf *global.JSONConfig) *CQBot {
|
||||
bot := &CQBot{
|
||||
Client: cli,
|
||||
}
|
||||
@ -136,7 +137,7 @@ func (bot *CQBot) UploadLocalVideo(target int64, v *LocalVideoElement) (*message
|
||||
}
|
||||
defer video.Close()
|
||||
hash, _ := utils.ComputeMd5AndLength(io.MultiReader(video, v.thumb))
|
||||
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".cache")
|
||||
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
_, _ = video.Seek(0, io.SeekStart)
|
||||
_, _ = v.thumb.Seek(0, io.SeekStart)
|
||||
return bot.Client.UploadGroupShortVideo(target, video, v.thumb, cacheFile)
|
||||
@ -456,7 +457,7 @@ func (bot *CQBot) Release() {
|
||||
}
|
||||
|
||||
func (bot *CQBot) dispatchEventMessage(m MSG) {
|
||||
if global.EventFilter != nil && !global.EventFilter.Eval(global.MSG(m)){
|
||||
if global.EventFilter != nil && !global.EventFilter.Eval(global.MSG(m)) {
|
||||
log.Debug("Event filtered!")
|
||||
return
|
||||
}
|
||||
|
@ -369,7 +369,7 @@ func (bot *CQBot) ConvertStringMessage(msg string, group bool) (r []message.IMes
|
||||
saveTempText := func() {
|
||||
if len(tempText) != 0 {
|
||||
if SplitUrl {
|
||||
for _, t := range global.SplitUrl(CQCodeUnescapeValue(string(tempText))) {
|
||||
for _, t := range global.SplitURL(CQCodeUnescapeValue(string(tempText))) {
|
||||
r = append(r, message.NewText(t))
|
||||
}
|
||||
} else {
|
||||
@ -548,7 +548,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m interf
|
||||
case "text":
|
||||
if SplitUrl {
|
||||
var ret []message.IMessageElement
|
||||
for _, text := range global.SplitUrl(d["text"]) {
|
||||
for _, text := range global.SplitURL(d["text"]) {
|
||||
ret = append(ret, message.NewText(text))
|
||||
}
|
||||
return ret, nil
|
||||
@ -619,9 +619,9 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m interf
|
||||
return &message.VoiceElement{Data: data}, nil
|
||||
case "record":
|
||||
f := d["file"]
|
||||
data, err := global.FindFile(f, d["cache"], global.VOICE_PATH)
|
||||
data, err := global.FindFile(f, d["cache"], global.VoicePath)
|
||||
if err == global.ErrSyntax {
|
||||
data, err = global.FindFile(f, d["cache"], global.VOICE_PATH_OLD)
|
||||
data, err = global.FindFile(f, d["cache"], global.VoicePathOld)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -797,7 +797,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m interf
|
||||
}
|
||||
var data []byte
|
||||
if cover, ok := d["cover"]; ok {
|
||||
data, _ = global.FindFile(cover, cache, global.IMAGE_PATH)
|
||||
data, _ = global.FindFile(cover, cache, global.ImagePath)
|
||||
} else {
|
||||
_ = global.ExtractCover(v.File, v.File+".jpg")
|
||||
data, _ = ioutil.ReadFile(v.File + ".jpg")
|
||||
@ -817,7 +817,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m interf
|
||||
if !bytes.Equal(header, []byte{0x66, 0x74, 0x79, 0x70}) { // check file header ftyp
|
||||
_, _ = video.Seek(0, io.SeekStart)
|
||||
hash, _ := utils.ComputeMd5AndLength(video)
|
||||
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".mp4")
|
||||
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash[:])+".mp4")
|
||||
if global.PathExists(cacheFile) && cache == "1" {
|
||||
goto ok
|
||||
}
|
||||
@ -879,7 +879,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
||||
cache = "1"
|
||||
}
|
||||
hash := md5.Sum([]byte(f))
|
||||
cacheFile := path.Join(global.CACHE_PATH, hex.EncodeToString(hash[:])+".cache")
|
||||
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
var maxSize = func() int64 {
|
||||
if video {
|
||||
return maxVideoSize
|
||||
@ -928,9 +928,9 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
||||
}
|
||||
return &LocalImageElement{File: fu.Path}, nil
|
||||
}
|
||||
rawPath := path.Join(global.IMAGE_PATH, f)
|
||||
rawPath := path.Join(global.ImagePath, f)
|
||||
if video {
|
||||
rawPath = path.Join(global.VIDEO_PATH, f)
|
||||
rawPath = path.Join(global.VideoPath, f)
|
||||
if !global.PathExists(rawPath) {
|
||||
return nil, errors.New("invalid video")
|
||||
}
|
||||
@ -956,8 +956,8 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
||||
}
|
||||
return &LocalImageElement{Stream: bytes.NewReader(b)}, nil
|
||||
}
|
||||
if !global.PathExists(rawPath) && global.PathExists(path.Join(global.IMAGE_PATH_OLD, f)) {
|
||||
rawPath = path.Join(global.IMAGE_PATH_OLD, f)
|
||||
if !global.PathExists(rawPath) && global.PathExists(path.Join(global.ImagePathOld, f)) {
|
||||
rawPath = path.Join(global.ImagePathOld, f)
|
||||
}
|
||||
if !global.PathExists(rawPath) && global.PathExists(rawPath+".cqimg") {
|
||||
rawPath += ".cqimg"
|
||||
|
@ -17,15 +17,17 @@ import (
|
||||
|
||||
var format = "string"
|
||||
|
||||
//SetMessageFormat 设置消息上报格式,默认为string
|
||||
func SetMessageFormat(f string) {
|
||||
format = f
|
||||
}
|
||||
|
||||
func ToFormattedMessage(e []message.IMessageElement, code int64, raw ...bool) (r interface{}) {
|
||||
//ToFormattedMessage 将给定[]message.IMessageElement转换为通过coolq.SetMessageFormat所定义的消息上报格式
|
||||
func ToFormattedMessage(e []message.IMessageElement, id int64, raw ...bool) (r interface{}) {
|
||||
if format == "string" {
|
||||
r = ToStringMessage(e, code, raw...)
|
||||
r = ToStringMessage(e, id, raw...)
|
||||
} else if format == "array" {
|
||||
r = ToArrayMessage(e, code, raw...)
|
||||
r = ToArrayMessage(e, id, raw...)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -499,8 +501,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
switch i := elem.(type) {
|
||||
case *message.ImageElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(i.Filename)
|
||||
@ -510,8 +512,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
i.Filename = filename
|
||||
case *message.GroupImageElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(filename)
|
||||
@ -520,8 +522,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
}
|
||||
case *message.FriendImageElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(0)) // 发送时会调用url, 大概没事
|
||||
w.WriteString(filename)
|
||||
@ -530,8 +532,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
}
|
||||
case *message.GroupFlashImgElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(i.Filename)
|
||||
@ -541,8 +543,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
i.Filename = filename
|
||||
case *message.FriendFlashImgElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(i.Filename)
|
||||
@ -553,18 +555,18 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
case *message.VoiceElement:
|
||||
i.Name = strings.ReplaceAll(i.Name, "{", "")
|
||||
i.Name = strings.ReplaceAll(i.Name, "}", "")
|
||||
if !global.PathExists(path.Join(global.VOICE_PATH, i.Name)) {
|
||||
if !global.PathExists(path.Join(global.VoicePath, i.Name)) {
|
||||
b, err := global.GetBytes(i.Url)
|
||||
if err != nil {
|
||||
log.Warnf("语音文件 %v 下载失败: %v", i.Name, err)
|
||||
continue
|
||||
}
|
||||
_ = ioutil.WriteFile(path.Join(global.VOICE_PATH, i.Name), b, 0644)
|
||||
_ = ioutil.WriteFile(path.Join(global.VoicePath, i.Name), b, 0644)
|
||||
}
|
||||
case *message.ShortVideoElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".video"
|
||||
if !global.PathExists(path.Join(global.VIDEO_PATH, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.VIDEO_PATH, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
if !global.PathExists(path.Join(global.VideoPath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.VideoPath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.Write(i.ThumbMd5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
|
@ -4,15 +4,17 @@ import (
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Mrs4s/go-cqhttp/global/codec"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"os/exec"
|
||||
"path"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/global/codec"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var useSilkCodec = true
|
||||
|
||||
//InitCodec 初始化Silk编码器
|
||||
func InitCodec() {
|
||||
log.Info("正在加载silk编码器...")
|
||||
err := codec.Init()
|
||||
@ -22,6 +24,7 @@ func InitCodec() {
|
||||
}
|
||||
}
|
||||
|
||||
//EncoderSilk 将音频编码为Silk
|
||||
func EncoderSilk(data []byte) ([]byte, error) {
|
||||
if !useSilkCodec {
|
||||
return nil, errors.New("no silk encoder")
|
||||
@ -42,12 +45,14 @@ func EncoderSilk(data []byte) ([]byte, error) {
|
||||
return slk, nil
|
||||
}
|
||||
|
||||
func EncodeMP4(src string, dst string) error { // -y 覆盖文件
|
||||
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-c", "copy", "-map", "0", dst)
|
||||
//EncodeMP4 将给定视频文件编码为MP4
|
||||
func EncodeMP4(src string, target string) error { // -y 覆盖文件
|
||||
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-c", "copy", "-map", "0", target)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func ExtractCover(src string, dst string) error {
|
||||
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-r", "1", "-f", "image2", dst)
|
||||
//ExtractCover 获取给定视频文件的Cover
|
||||
func ExtractCover(src string, target string) error {
|
||||
cmd := exec.Command("ffmpeg", "-i", src, "-y", "-r", "1", "-f", "image2", target)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
@ -40,6 +40,7 @@ func getEncoderFilePath() string {
|
||||
return encoderFile
|
||||
}
|
||||
|
||||
//Init 下载Silk编码器
|
||||
func Init() error {
|
||||
if !fileExist(silkCachePath) {
|
||||
_ = os.MkdirAll(silkCachePath, os.ModePerm)
|
||||
@ -56,6 +57,7 @@ func Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//EncodeToSilk 将音频编码为Silk
|
||||
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
|
||||
// 1. 写入缓存文件
|
||||
rawPath := path.Join(silkCachePath, tempName+".wav")
|
||||
|
@ -4,10 +4,12 @@ package codec
|
||||
|
||||
import "errors"
|
||||
|
||||
//Init 下载silk编码器
|
||||
func Init() error {
|
||||
return errors.New("Unsupport arch now")
|
||||
}
|
||||
|
||||
//EncodeToSilk 将音频编码为Silk
|
||||
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
|
||||
return nil, errors.New("Unsupport arch now")
|
||||
}
|
||||
|
@ -4,10 +4,12 @@ package codec
|
||||
|
||||
import "errors"
|
||||
|
||||
//Init 下载silk编码器
|
||||
func Init() error {
|
||||
return errors.New("not support now")
|
||||
}
|
||||
|
||||
//EncodeToSilk 将音频编码为Silk
|
||||
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
|
||||
return nil, errors.New("not support now")
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
//DefaultConfigWithComments 为go-cqhttp的默认配置文件
|
||||
var DefaultConfigWithComments = `
|
||||
/*
|
||||
go-cqhttp 默认配置文件
|
||||
@ -134,7 +135,8 @@ var DefaultConfigWithComments = `
|
||||
}
|
||||
`
|
||||
|
||||
type JsonConfig struct {
|
||||
//JSONConfig Config对应的结构体
|
||||
type JSONConfig struct {
|
||||
Uin int64 `json:"uin"`
|
||||
Password string `json:"password"`
|
||||
EncryptPassword bool `json:"encrypt_password"`
|
||||
@ -153,39 +155,41 @@ type JsonConfig struct {
|
||||
} `json:"_rate_limit"`
|
||||
IgnoreInvalidCQCode bool `json:"ignore_invalid_cqcode"`
|
||||
ForceFragmented bool `json:"force_fragmented"`
|
||||
FixUrl bool `json:"fix_url"`
|
||||
FixURL bool `json:"fix_url"`
|
||||
ProxyRewrite string `json:"proxy_rewrite"`
|
||||
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
|
||||
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
||||
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
||||
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
|
||||
HTTPConfig *GoCQHTTPConfig `json:"http_config"`
|
||||
WSConfig *GoCQWebSocketConfig `json:"ws_config"`
|
||||
ReverseServers []*GoCQReverseWebSocketConfig `json:"ws_reverse_servers"`
|
||||
PostMessageFormat string `json:"post_message_format"`
|
||||
UseSSOAddress bool `json:"use_sso_address"`
|
||||
Debug bool `json:"debug"`
|
||||
LogLevel string `json:"log_level"`
|
||||
WebUi *GoCqWebUi `json:"web_ui"`
|
||||
WebUI *GoCQWebUI `json:"web_ui"`
|
||||
}
|
||||
|
||||
type CQHttpApiConfig struct {
|
||||
//CQHTTPAPIConfig HTTPAPI对应的Config结构体
|
||||
type CQHTTPAPIConfig struct {
|
||||
Host string `json:"host"`
|
||||
Port uint16 `json:"port"`
|
||||
UseHttp bool `json:"use_http"`
|
||||
UseHTTP bool `json:"use_http"`
|
||||
WSHost string `json:"ws_host"`
|
||||
WSPort uint16 `json:"ws_port"`
|
||||
UseWS bool `json:"use_ws"`
|
||||
WSReverseUrl string `json:"ws_reverse_url"`
|
||||
WSReverseApiUrl string `json:"ws_reverse_api_url"`
|
||||
WSReverseEventUrl string `json:"ws_reverse_event_url"`
|
||||
WSReverseURL string `json:"ws_reverse_url"`
|
||||
WSReverseAPIURL string `json:"ws_reverse_api_url"`
|
||||
WSReverseEventURL string `json:"ws_reverse_event_url"`
|
||||
WSReverseReconnectInterval uint16 `json:"ws_reverse_reconnect_interval"`
|
||||
WSReverseReconnectOnCode1000 bool `json:"ws_reverse_reconnect_on_code_1000"`
|
||||
UseWsReverse bool `json:"use_ws_reverse"`
|
||||
PostUrl string `json:"post_url"`
|
||||
PostURL string `json:"post_url"`
|
||||
AccessToken string `json:"access_token"`
|
||||
Secret string `json:"secret"`
|
||||
PostMessageFormat string `json:"post_message_format"`
|
||||
}
|
||||
|
||||
type GoCQHttpConfig struct {
|
||||
//GoCQHTTPConfig 正向HTTP对应config结构体
|
||||
type GoCQHTTPConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Host string `json:"host"`
|
||||
Port uint16 `json:"port"`
|
||||
@ -193,29 +197,33 @@ type GoCQHttpConfig struct {
|
||||
PostUrls map[string]string `json:"post_urls"`
|
||||
}
|
||||
|
||||
type GoCQWebsocketConfig struct {
|
||||
//GoCQWebSocketConfig 正向WebSocket对应Config结构体
|
||||
type GoCQWebSocketConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Host string `json:"host"`
|
||||
Port uint16 `json:"port"`
|
||||
}
|
||||
|
||||
type GoCQReverseWebsocketConfig struct {
|
||||
//GoCQReverseWebSocketConfig 反向WebSocket对应Config结构体
|
||||
type GoCQReverseWebSocketConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
ReverseUrl string `json:"reverse_url"`
|
||||
ReverseApiUrl string `json:"reverse_api_url"`
|
||||
ReverseEventUrl string `json:"reverse_event_url"`
|
||||
ReverseURL string `json:"reverse_url"`
|
||||
ReverseAPIURL string `json:"reverse_api_url"`
|
||||
ReverseEventURL string `json:"reverse_event_url"`
|
||||
ReverseReconnectInterval uint16 `json:"reverse_reconnect_interval"`
|
||||
}
|
||||
|
||||
type GoCqWebUi struct {
|
||||
//GoCQWebUI WebUI对应Config结构体
|
||||
type GoCQWebUI struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Host string `json:"host"`
|
||||
WebUiPort uint64 `json:"web_ui_port"`
|
||||
WebUIPort uint64 `json:"web_ui_port"`
|
||||
WebInput bool `json:"web_input"`
|
||||
}
|
||||
|
||||
func DefaultConfig() *JsonConfig {
|
||||
return &JsonConfig{
|
||||
//DefaultConfig 返回一份默认配置对应结构体
|
||||
func DefaultConfig() *JSONConfig {
|
||||
return &JSONConfig{
|
||||
EnableDB: true,
|
||||
ReLogin: struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
@ -237,42 +245,43 @@ func DefaultConfig() *JsonConfig {
|
||||
},
|
||||
PostMessageFormat: "string",
|
||||
ForceFragmented: false,
|
||||
HttpConfig: &GoCQHttpConfig{
|
||||
HTTPConfig: &GoCQHTTPConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
},
|
||||
WSConfig: &GoCQWebsocketConfig{
|
||||
WSConfig: &GoCQWebSocketConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 6700,
|
||||
},
|
||||
ReverseServers: []*GoCQReverseWebsocketConfig{
|
||||
ReverseServers: []*GoCQReverseWebSocketConfig{
|
||||
{
|
||||
Enabled: false,
|
||||
ReverseUrl: "ws://you_websocket_universal.server",
|
||||
ReverseApiUrl: "ws://you_websocket_api.server",
|
||||
ReverseEventUrl: "ws://you_websocket_event.server",
|
||||
ReverseURL: "ws://you_websocket_universal.server",
|
||||
ReverseAPIURL: "ws://you_websocket_api.server",
|
||||
ReverseEventURL: "ws://you_websocket_event.server",
|
||||
ReverseReconnectInterval: 3000,
|
||||
},
|
||||
},
|
||||
WebUi: &GoCqWebUi{
|
||||
WebUI: &GoCQWebUI{
|
||||
Enabled: true,
|
||||
Host: "127.0.0.1",
|
||||
WebInput: false,
|
||||
WebUiPort: 9999,
|
||||
WebUIPort: 9999,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Load(p string) *JsonConfig {
|
||||
//Load 加载配置文件
|
||||
func Load(p string) *JSONConfig {
|
||||
if !PathExists(p) {
|
||||
log.Warnf("尝试加载配置文件 %v 失败: 文件不存在", p)
|
||||
return nil
|
||||
}
|
||||
var dat map[string]interface{}
|
||||
var c = JsonConfig{}
|
||||
var c = JSONConfig{}
|
||||
err := hjson.Unmarshal([]byte(ReadAllText(p)), &dat)
|
||||
if err == nil {
|
||||
b, _ := json.Marshal(dat)
|
||||
@ -287,7 +296,8 @@ func Load(p string) *JsonConfig {
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c *JsonConfig) Save(p string) error {
|
||||
//Save 写入配置文件至path
|
||||
func (c *JSONConfig) Save(path string) error {
|
||||
data, err := hjson.MarshalWithOptions(c, hjson.EncoderOptions{
|
||||
Eol: "\n",
|
||||
BracesSameLine: true,
|
||||
@ -296,5 +306,5 @@ func (c *JsonConfig) Save(p string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return WriteAllText(p, string(data))
|
||||
return WriteAllText(path, string(data))
|
||||
}
|
||||
|
@ -10,8 +10,14 @@ import (
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
//MSG 消息Map
|
||||
type MSG map[string]interface{}
|
||||
|
||||
//Get 尝试从消息Map中取出key为s的值,若不存在则返回MSG{}
|
||||
//
|
||||
//若所给key对应的值的类型是global.MSG,则返回此值
|
||||
//
|
||||
//若所给key对应值的类型不是global.MSG,则返回MSG{"__str__": Val}
|
||||
func (m MSG) Get(s string) MSG {
|
||||
if v, ok := m[s]; ok {
|
||||
if msg, ok := v.(MSG); ok {
|
||||
@ -22,6 +28,7 @@ func (m MSG) Get(s string) MSG {
|
||||
return MSG{}
|
||||
}
|
||||
|
||||
//String 将消息Map转化为String。若Map存在key "__str__",则返回此key对应的值,否则将输出整张消息Map对应的JSON字符串
|
||||
func (m MSG) String() string {
|
||||
if str, ok := m["__str__"]; ok {
|
||||
return fmt.Sprint(str)
|
||||
@ -30,6 +37,7 @@ func (m MSG) String() string {
|
||||
return str
|
||||
}
|
||||
|
||||
//Filter 定义了一个消息上报过滤接口
|
||||
type Filter interface {
|
||||
Eval(payload MSG) bool
|
||||
}
|
||||
@ -39,6 +47,7 @@ type operationNode struct {
|
||||
filter Filter
|
||||
}
|
||||
|
||||
//NotOperator 定义了过滤器中Not操作符
|
||||
type NotOperator struct {
|
||||
operand Filter
|
||||
}
|
||||
@ -52,10 +61,12 @@ func notOperatorConstruct(argument gjson.Result) *NotOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行Not过滤
|
||||
func (op *NotOperator) Eval(payload MSG) bool {
|
||||
return !op.operand.Eval(payload)
|
||||
}
|
||||
|
||||
//AndOperator 定义了过滤器中And操作符
|
||||
type AndOperator struct {
|
||||
operands []operationNode
|
||||
}
|
||||
@ -91,6 +102,7 @@ func andOperatorConstruct(argument gjson.Result) *AndOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行And过滤
|
||||
func (andOperator *AndOperator) Eval(payload MSG) bool {
|
||||
res := true
|
||||
for _, operand := range andOperator.operands {
|
||||
@ -111,6 +123,7 @@ func (andOperator *AndOperator) Eval(payload MSG) bool {
|
||||
return res
|
||||
}
|
||||
|
||||
//OrOperator 定义了过滤器中Or操作符
|
||||
type OrOperator struct {
|
||||
operands []Filter
|
||||
}
|
||||
@ -127,6 +140,7 @@ func orOperatorConstruct(argument gjson.Result) *OrOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行Or过滤
|
||||
func (op *OrOperator) Eval(payload MSG) bool {
|
||||
res := false
|
||||
for _, operand := range op.operands {
|
||||
@ -138,6 +152,7 @@ func (op *OrOperator) Eval(payload MSG) bool {
|
||||
return res
|
||||
}
|
||||
|
||||
//EqualOperator 定义了过滤器中Equal操作符
|
||||
type EqualOperator struct {
|
||||
operand string
|
||||
}
|
||||
@ -148,10 +163,12 @@ func equalOperatorConstruct(argument gjson.Result) *EqualOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行Equal过滤
|
||||
func (op *EqualOperator) Eval(payload MSG) bool {
|
||||
return payload.String() == op.operand
|
||||
}
|
||||
|
||||
//NotEqualOperator 定义了过滤器中NotEqual操作符
|
||||
type NotEqualOperator struct {
|
||||
operand string
|
||||
}
|
||||
@ -162,10 +179,12 @@ func notEqualOperatorConstruct(argument gjson.Result) *NotEqualOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行NotEqual过滤
|
||||
func (op *NotEqualOperator) Eval(payload MSG) bool {
|
||||
return !(payload.String() == op.operand)
|
||||
}
|
||||
|
||||
//InOperator 定义了过滤器中In操作符
|
||||
type InOperator struct {
|
||||
operandString string
|
||||
operandArray []string
|
||||
@ -188,6 +207,7 @@ func inOperatorConstruct(argument gjson.Result) *InOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行In过滤
|
||||
func (op *InOperator) Eval(payload MSG) bool {
|
||||
payloadStr := payload.String()
|
||||
if op.operandArray != nil {
|
||||
@ -201,6 +221,7 @@ func (op *InOperator) Eval(payload MSG) bool {
|
||||
return strings.Contains(op.operandString, payloadStr)
|
||||
}
|
||||
|
||||
//ContainsOperator 定义了过滤器中Contains操作符
|
||||
type ContainsOperator struct {
|
||||
operand string
|
||||
}
|
||||
@ -214,10 +235,12 @@ func containsOperatorConstruct(argument gjson.Result) *ContainsOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行Contains过滤
|
||||
func (op *ContainsOperator) Eval(payload MSG) bool {
|
||||
return strings.Contains(payload.String(), op.operand)
|
||||
}
|
||||
|
||||
//RegexOperator 定义了过滤器中Regex操作符
|
||||
type RegexOperator struct {
|
||||
regex *regexp.Regexp
|
||||
}
|
||||
@ -231,11 +254,13 @@ func regexOperatorConstruct(argument gjson.Result) *RegexOperator {
|
||||
return op
|
||||
}
|
||||
|
||||
//Eval 对payload执行RegexO过滤
|
||||
func (op *RegexOperator) Eval(payload MSG) bool {
|
||||
matched := op.regex.MatchString(payload.String())
|
||||
return matched
|
||||
}
|
||||
|
||||
//Generate 根据给定操作符名opName及操作符参数argument创建一个过滤器实例
|
||||
func Generate(opName string, argument gjson.Result) Filter {
|
||||
switch opName {
|
||||
case "not":
|
||||
@ -259,8 +284,10 @@ func Generate(opName string, argument gjson.Result) Filter {
|
||||
}
|
||||
}
|
||||
|
||||
//EventFilter 初始化一个nil过滤器
|
||||
var EventFilter Filter = nil
|
||||
|
||||
//BootFilter 启动事件过滤器
|
||||
func BootFilter() {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
|
85
global/fs.go
85
global/fs.go
@ -9,7 +9,6 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/kardianos/osext"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
@ -21,75 +20,95 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kardianos/osext"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
IMAGE_PATH = path.Join("data", "images")
|
||||
IMAGE_PATH_OLD = path.Join("data", "image")
|
||||
VOICE_PATH = path.Join("data", "voices")
|
||||
VOICE_PATH_OLD = path.Join("data", "record")
|
||||
VIDEO_PATH = path.Join("data", "videos")
|
||||
CACHE_PATH = path.Join("data", "cache")
|
||||
|
||||
HEADER_AMR = []byte("#!AMR")
|
||||
HEADER_SILK = []byte("\x02#!SILK_V3")
|
||||
|
||||
ErrSyntax = errors.New("syntax error")
|
||||
const (
|
||||
//ImagePath go-cqhttp使用的图片缓存目录
|
||||
ImagePath = "data/images"
|
||||
//ImagePathOld 兼容旧版go-cqhtto使用的图片缓存目录
|
||||
ImagePathOld = "data/image"
|
||||
//VoicePath go-cqhttp使用的语音缓存目录
|
||||
VoicePath = "data/voices"
|
||||
//VoicePathOld 兼容旧版go-cqhtto使用的语音缓存目录
|
||||
VoicePathOld = "data/record"
|
||||
//VideoPath go-cqhttp使用的视频缓存目录
|
||||
VideoPath = "data/videos"
|
||||
//CachePath go-cqhttp使用的缓存目录
|
||||
CachePath = "data/cache"
|
||||
)
|
||||
|
||||
var (
|
||||
//ErrSyntax Path语法错误时返回的错误
|
||||
ErrSyntax = errors.New("syntax error")
|
||||
//HeaderAmr AMR文件头
|
||||
HeaderAmr = []byte("#!AMR")
|
||||
//HeaderSilk Silkv3文件头
|
||||
HeaderSilk = []byte("\x02#!SILK_V3")
|
||||
)
|
||||
|
||||
//PathExists 判断给定path是否存在
|
||||
func PathExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil || os.IsExist(err)
|
||||
}
|
||||
|
||||
//ReadAllText 读取给定path对应文件,无法读取时返回空值
|
||||
func ReadAllText(path string) string {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
//WriteAllText 将给定text写入给定path
|
||||
func WriteAllText(path, text string) error {
|
||||
return ioutil.WriteFile(path, []byte(text), 0644)
|
||||
}
|
||||
|
||||
//Check 检测err是否为nil
|
||||
func Check(err error) {
|
||||
if err != nil {
|
||||
log.Fatalf("遇到错误: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
//IsAMRorSILK 判断给定文件是否为Amr或Silk格式
|
||||
func IsAMRorSILK(b []byte) bool {
|
||||
return bytes.HasPrefix(b, HEADER_AMR) || bytes.HasPrefix(b, HEADER_SILK)
|
||||
return bytes.HasPrefix(b, HeaderAmr) || bytes.HasPrefix(b, HeaderSilk)
|
||||
}
|
||||
|
||||
func FindFile(f, cache, PATH string) (data []byte, err error) {
|
||||
//FindFile 从给定的File寻找文件,并返回文件byte数组。File是一个合法的URL。Path为文件寻找位置。
|
||||
//对于HTTP/HTTPS形式的URL,Cache为"1"或空时表示启用缓存
|
||||
func FindFile(file, cache, PATH string) (data []byte, err error) {
|
||||
data, err = nil, ErrSyntax
|
||||
if strings.HasPrefix(f, "http") || strings.HasPrefix(f, "https") {
|
||||
if strings.HasPrefix(file, "http") || strings.HasPrefix(file, "https") {
|
||||
if cache == "" {
|
||||
cache = "1"
|
||||
}
|
||||
hash := md5.Sum([]byte(f))
|
||||
cacheFile := path.Join(CACHE_PATH, hex.EncodeToString(hash[:])+".cache")
|
||||
hash := md5.Sum([]byte(file))
|
||||
cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
if PathExists(cacheFile) && cache == "1" {
|
||||
return ioutil.ReadFile(cacheFile)
|
||||
}
|
||||
data, err = GetBytes(f)
|
||||
data, err = GetBytes(file)
|
||||
_ = ioutil.WriteFile(cacheFile, data, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if strings.HasPrefix(f, "base64") {
|
||||
data, err = base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", ""))
|
||||
} else if strings.HasPrefix(file, "base64") {
|
||||
data, err = base64.StdEncoding.DecodeString(strings.ReplaceAll(file, "base64://", ""))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if strings.HasPrefix(f, "file") {
|
||||
} else if strings.HasPrefix(file, "file") {
|
||||
var fu *url.URL
|
||||
fu, err = url.Parse(f)
|
||||
fu, err = url.Parse(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -100,8 +119,8 @@ func FindFile(f, cache, PATH string) (data []byte, err error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if PathExists(path.Join(PATH, f)) {
|
||||
data, err = ioutil.ReadFile(path.Join(PATH, f))
|
||||
} else if PathExists(path.Join(PATH, file)) {
|
||||
data, err = ioutil.ReadFile(path.Join(PATH, file))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -109,19 +128,20 @@ func FindFile(f, cache, PATH string) (data []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
//DelFile 删除一个给定path,并返回删除结果
|
||||
func DelFile(path string) bool {
|
||||
err := os.Remove(path)
|
||||
if err != nil {
|
||||
// 删除失败
|
||||
log.Error(err)
|
||||
return false
|
||||
} else {
|
||||
// 删除成功
|
||||
log.Info(path + "删除成功")
|
||||
return true
|
||||
}
|
||||
// 删除成功
|
||||
log.Info(path + "删除成功")
|
||||
return true
|
||||
}
|
||||
|
||||
//ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔
|
||||
func ReadAddrFile(path string) []*net.TCPAddr {
|
||||
d, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
@ -140,10 +160,12 @@ func ReadAddrFile(path string) []*net.TCPAddr {
|
||||
return ret
|
||||
}
|
||||
|
||||
//WriteCounter 写入量计算实例
|
||||
type WriteCounter struct {
|
||||
Total uint64
|
||||
}
|
||||
|
||||
//Write 方法将写入的byte长度追加至写入的总长度Total中
|
||||
func (wc *WriteCounter) Write(p []byte) (int, error) {
|
||||
n := len(p)
|
||||
wc.Total += uint64(n)
|
||||
@ -151,12 +173,13 @@ func (wc *WriteCounter) Write(p []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (wc WriteCounter) PrintProgress() {
|
||||
//PrintProgress 方法将打印当前的总写入量
|
||||
func (wc *WriteCounter) PrintProgress() {
|
||||
fmt.Printf("\r%s", strings.Repeat(" ", 35))
|
||||
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
|
||||
}
|
||||
|
||||
// UpdateFromStream copy form getlantern/go-update
|
||||
//UpdateFromStream copy form getlantern/go-update
|
||||
func UpdateFromStream(updateWith io.Reader) (err error, errRecover error) {
|
||||
updatePath, err := osext.Executable()
|
||||
if err != nil {
|
||||
|
@ -5,8 +5,6 @@ import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"github.com/guonaihong/gout"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
@ -18,6 +16,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/guonaihong/gout"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
@ -44,13 +45,18 @@ var (
|
||||
MaxIdleConnsPerHost: 999,
|
||||
},
|
||||
}
|
||||
|
||||
//Proxy 存储Config.proxy_rewrite,用于设置代理
|
||||
Proxy string
|
||||
|
||||
//ErrOverSize 响应主体过大时返回此错误
|
||||
ErrOverSize = errors.New("oversize")
|
||||
|
||||
//UserAgent HTTP请求时使用的UA
|
||||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66"
|
||||
)
|
||||
|
||||
//GetBytes 对给定URL发送Get请求,返回响应主体
|
||||
func GetBytes(url string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
@ -76,6 +82,7 @@ func GetBytes(url string) ([]byte, error) {
|
||||
return body, nil
|
||||
}
|
||||
|
||||
//DownloadFile 将给定URL对应的文件下载至给定Path
|
||||
func DownloadFile(url, path string, limit int64, headers map[string]string) error {
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
@ -109,6 +116,7 @@ func DownloadFile(url, path string, limit int64, headers map[string]string) erro
|
||||
return nil
|
||||
}
|
||||
|
||||
//DownloadFileMultiThreading 使用threadCount个线程将给定URL对应的文件下载至给定Path
|
||||
func DownloadFileMultiThreading(url, path string, limit int64, threadCount int, headers map[string]string) error {
|
||||
if threadCount < 2 {
|
||||
return DownloadFile(url, path, limit, headers)
|
||||
@ -168,9 +176,9 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int,
|
||||
blockSize := func() int64 {
|
||||
if contentLength > 1024*1024 {
|
||||
return (contentLength / int64(threadCount)) - 10
|
||||
} else {
|
||||
return contentLength
|
||||
}
|
||||
return contentLength
|
||||
|
||||
}()
|
||||
if blockSize == contentLength {
|
||||
return copyStream(resp.Body)
|
||||
@ -189,7 +197,7 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
return errors.New("unknown status code.")
|
||||
return errors.New("unknown status code")
|
||||
}
|
||||
// 下载分块
|
||||
downloadBlock := func(block *BlockMetaData) error {
|
||||
@ -266,6 +274,7 @@ func DownloadFileMultiThreading(url, path string, limit int64, threadCount int,
|
||||
return lastErr
|
||||
}
|
||||
|
||||
//GetSliderTicket 通过给定的验证链接raw和id,获取验证结果Ticket
|
||||
func GetSliderTicket(raw, id string) (string, error) {
|
||||
var rsp string
|
||||
if err := gout.POST("https://api.shkong.com/gocqhttpapi/task").SetJSON(gout.H{
|
||||
@ -281,6 +290,7 @@ func GetSliderTicket(raw, id string) (string, error) {
|
||||
return g.Get("ticket").Str, nil
|
||||
}
|
||||
|
||||
//QQMusicSongInfo 通过给定id在QQ音乐上查找曲目信息
|
||||
func QQMusicSongInfo(id string) (gjson.Result, error) {
|
||||
d, err := GetBytes(`https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=0&data={%22comm%22:{%22ct%22:24,%22cv%22:0},%22songinfo%22:{%22method%22:%22get_song_detail_yqq%22,%22param%22:{%22song_type%22:0,%22song_mid%22:%22%22,%22song_id%22:` + id + `},%22module%22:%22music.pf_song_detail_svr%22}}`)
|
||||
if err != nil {
|
||||
@ -289,6 +299,7 @@ func QQMusicSongInfo(id string) (gjson.Result, error) {
|
||||
return gjson.ParseBytes(d).Get("songinfo.data"), nil
|
||||
}
|
||||
|
||||
//NeteaseMusicSongInfo 通过给定id在wdd音乐上查找曲目信息
|
||||
func NeteaseMusicSongInfo(id string) (gjson.Result, error) {
|
||||
d, err := GetBytes(fmt.Sprintf("http://music.163.com/api/song/detail/?id=%s&ids=%%5B%s%%5D", id, id))
|
||||
if err != nil {
|
||||
|
@ -21,6 +21,15 @@ var falseSet = map[string]struct{}{
|
||||
"0": {},
|
||||
}
|
||||
|
||||
//EnsureBool 判断给定的p是否可表示为合法Bool类型,否则返回defaultVal
|
||||
//
|
||||
//支持的合法类型有
|
||||
//
|
||||
//type bool
|
||||
//
|
||||
//type gjson.True or gjson.False
|
||||
//
|
||||
//type string "true","yes","1" or "false","no","0" (case insensitive)
|
||||
func EnsureBool(p interface{}, defaultVal bool) bool {
|
||||
var str string
|
||||
if b, ok := p.(bool); ok {
|
||||
@ -54,9 +63,13 @@ func EnsureBool(p interface{}, defaultVal bool) bool {
|
||||
}
|
||||
|
||||
// VersionNameCompare 检查版本名是否需要更新, 仅适用于 go-cqhttp 的版本命名规则
|
||||
//
|
||||
// 例: v0.9.29-fix2 == v0.9.29-fix2 -> false
|
||||
//
|
||||
// v0.9.29-fix1 < v0.9.29-fix2 -> true
|
||||
//
|
||||
// v0.9.29-fix2 > v0.9.29-fix1 -> false
|
||||
//
|
||||
// v0.9.29-fix2 < v0.9.30 -> true
|
||||
func VersionNameCompare(current, remote string) bool {
|
||||
sp := regexp.MustCompile(`[0-9]\d*`)
|
||||
@ -72,8 +85,9 @@ func VersionNameCompare(current, remote string) bool {
|
||||
return len(cur) < len(re)
|
||||
}
|
||||
|
||||
func SplitUrl(s string) []string {
|
||||
reg := regexp.MustCompile(`[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?`)
|
||||
//SplitURL 将给定URL字符串分割为两部分,用于URL预处理防止风控
|
||||
func SplitURL(s string) []string {
|
||||
reg := regexp.MustCompile(`(?i)[a-z\d][-a-z\d]{0,62}(\.[a-z\d][-a-z\d]{0,62})+\.?`)
|
||||
idx := reg.FindAllStringIndex(s, -1)
|
||||
if len(idx) == 0 {
|
||||
return []string{s}
|
||||
|
@ -9,13 +9,15 @@ import (
|
||||
var limiter *rate.Limiter
|
||||
var limitEnable = false
|
||||
|
||||
//RateLimit 执行API调用速率限制
|
||||
func RateLimit(ctx context.Context) {
|
||||
if limitEnable {
|
||||
_ = limiter.Wait(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func InitLimiter(r float64, b int) {
|
||||
//InitLimiter 初始化速率限制器
|
||||
func InitLimiter(frequency float64, bucketSize int) {
|
||||
limitEnable = true
|
||||
limiter = rate.NewLimiter(rate.Limit(r), b)
|
||||
limiter = rate.NewLimiter(rate.Limit(frequency), bucketSize)
|
||||
}
|
||||
|
68
main.go
68
main.go
@ -46,23 +46,23 @@ func init() {
|
||||
if err == nil {
|
||||
log.SetOutput(io.MultiWriter(os.Stderr, w))
|
||||
}
|
||||
if !global.PathExists(global.IMAGE_PATH) {
|
||||
if err := os.MkdirAll(global.IMAGE_PATH, 0755); err != nil {
|
||||
if !global.PathExists(global.ImagePath) {
|
||||
if err := os.MkdirAll(global.ImagePath, 0755); err != nil {
|
||||
log.Fatalf("创建图片缓存文件夹失败: %v", err)
|
||||
}
|
||||
}
|
||||
if !global.PathExists(global.VOICE_PATH) {
|
||||
if err := os.MkdirAll(global.VOICE_PATH, 0755); err != nil {
|
||||
if !global.PathExists(global.VoicePath) {
|
||||
if err := os.MkdirAll(global.VoicePath, 0755); err != nil {
|
||||
log.Fatalf("创建语音缓存文件夹失败: %v", err)
|
||||
}
|
||||
}
|
||||
if !global.PathExists(global.VIDEO_PATH) {
|
||||
if err := os.MkdirAll(global.VIDEO_PATH, 0755); err != nil {
|
||||
if !global.PathExists(global.VideoPath) {
|
||||
if err := os.MkdirAll(global.VideoPath, 0755); err != nil {
|
||||
log.Fatalf("创建视频缓存文件夹失败: %v", err)
|
||||
}
|
||||
}
|
||||
if !global.PathExists(global.CACHE_PATH) {
|
||||
if err := os.MkdirAll(global.CACHE_PATH, 0755); err != nil {
|
||||
if !global.PathExists(global.CachePath) {
|
||||
if err := os.MkdirAll(global.CachePath, 0755); err != nil {
|
||||
log.Fatalf("创建发送图片缓存文件夹失败: %v", err)
|
||||
}
|
||||
}
|
||||
@ -70,24 +70,24 @@ func init() {
|
||||
log.Info("发现 cqhttp.json 将在五秒后尝试导入配置,按 Ctrl+C 取消.")
|
||||
log.Warn("警告: 该操作会删除 cqhttp.json 并覆盖 config.hjson 文件.")
|
||||
time.Sleep(time.Second * 5)
|
||||
conf := global.CQHttpApiConfig{}
|
||||
conf := global.CQHTTPAPIConfig{}
|
||||
if err := json.Unmarshal([]byte(global.ReadAllText("cqhttp.json")), &conf); err != nil {
|
||||
log.Fatalf("读取文件 cqhttp.json 失败: %v", err)
|
||||
}
|
||||
goConf := global.DefaultConfig()
|
||||
goConf.AccessToken = conf.AccessToken
|
||||
goConf.HttpConfig.Host = conf.Host
|
||||
goConf.HttpConfig.Port = conf.Port
|
||||
goConf.HTTPConfig.Host = conf.Host
|
||||
goConf.HTTPConfig.Port = conf.Port
|
||||
goConf.WSConfig.Host = conf.WSHost
|
||||
goConf.WSConfig.Port = conf.WSPort
|
||||
if conf.PostUrl != "" {
|
||||
goConf.HttpConfig.PostUrls[conf.PostUrl] = conf.Secret
|
||||
if conf.PostURL != "" {
|
||||
goConf.HTTPConfig.PostUrls[conf.PostURL] = conf.Secret
|
||||
}
|
||||
if conf.UseWsReverse {
|
||||
goConf.ReverseServers[0].Enabled = true
|
||||
goConf.ReverseServers[0].ReverseUrl = conf.WSReverseUrl
|
||||
goConf.ReverseServers[0].ReverseApiUrl = conf.WSReverseApiUrl
|
||||
goConf.ReverseServers[0].ReverseEventUrl = conf.WSReverseEventUrl
|
||||
goConf.ReverseServers[0].ReverseURL = conf.WSReverseURL
|
||||
goConf.ReverseServers[0].ReverseAPIURL = conf.WSReverseAPIURL
|
||||
goConf.ReverseServers[0].ReverseEventURL = conf.WSReverseEventURL
|
||||
goConf.ReverseServers[0].ReverseReconnectInterval = conf.WSReverseReconnectInterval
|
||||
}
|
||||
if err := goConf.Save("config.hjson"); err != nil {
|
||||
@ -122,7 +122,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
var conf *global.JsonConfig
|
||||
var conf *global.JSONConfig
|
||||
if global.PathExists("config.json") {
|
||||
conf = global.Load("config.json")
|
||||
_ = conf.Save("config.hjson")
|
||||
@ -132,16 +132,16 @@ func main() {
|
||||
uin, _ := strconv.ParseInt(os.Getenv("UIN"), 10, 64)
|
||||
pwd := os.Getenv("PASS")
|
||||
post := os.Getenv("HTTP_POST")
|
||||
conf = &global.JsonConfig{
|
||||
conf = &global.JSONConfig{
|
||||
Uin: uin,
|
||||
Password: pwd,
|
||||
HttpConfig: &global.GoCQHttpConfig{
|
||||
HTTPConfig: &global.GoCQHTTPConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
},
|
||||
WSConfig: &global.GoCQWebsocketConfig{
|
||||
WSConfig: &global.GoCQWebSocketConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 6700,
|
||||
@ -150,7 +150,7 @@ func main() {
|
||||
Debug: os.Getenv("DEBUG") == "true",
|
||||
}
|
||||
if post != "" {
|
||||
conf.HttpConfig.PostUrls[post] = os.Getenv("HTTP_SECRET")
|
||||
conf.HTTPConfig.PostUrls[post] = os.Getenv("HTTP_SECRET")
|
||||
}
|
||||
} else {
|
||||
conf = global.Load("config.hjson")
|
||||
@ -215,7 +215,7 @@ func main() {
|
||||
log.Warnf("已开启Debug模式.")
|
||||
log.Debugf("开发交流群: 192548878")
|
||||
server.Debug = true
|
||||
if conf.WebUi == nil || !conf.WebUi.Enabled {
|
||||
if conf.WebUI == nil || !conf.WebUI.Enabled {
|
||||
log.Warnf("警告: 在Debug模式下未启用WebUi服务, 将无法进行性能分析.")
|
||||
}
|
||||
}
|
||||
@ -310,22 +310,22 @@ func main() {
|
||||
log.Infof("收到服务器地址更新通知, 将在下一次重连时应用. ")
|
||||
return true
|
||||
})
|
||||
if conf.WebUi == nil {
|
||||
conf.WebUi = &global.GoCqWebUi{
|
||||
if conf.WebUI == nil {
|
||||
conf.WebUI = &global.GoCQWebUI{
|
||||
Enabled: true,
|
||||
WebInput: false,
|
||||
Host: "0.0.0.0",
|
||||
WebUiPort: 9999,
|
||||
WebUIPort: 9999,
|
||||
}
|
||||
}
|
||||
if conf.WebUi.WebUiPort <= 0 {
|
||||
conf.WebUi.WebUiPort = 9999
|
||||
if conf.WebUI.WebUIPort <= 0 {
|
||||
conf.WebUI.WebUIPort = 9999
|
||||
}
|
||||
if conf.WebUi.Host == "" {
|
||||
conf.WebUi.Host = "127.0.0.1"
|
||||
if conf.WebUI.Host == "" {
|
||||
conf.WebUI.Host = "127.0.0.1"
|
||||
}
|
||||
global.Proxy = conf.ProxyRewrite
|
||||
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
|
||||
r := server.Restart
|
||||
go checkUpdate()
|
||||
@ -340,6 +340,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
//EncryptPwd 通过给定key加密给定pwd
|
||||
func EncryptPwd(pwd string, key []byte) string {
|
||||
tea := binary.NewTeaCipher(key)
|
||||
if tea == nil {
|
||||
@ -348,6 +349,7 @@ func EncryptPwd(pwd string, key []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(tea.Encrypt([]byte(pwd)))
|
||||
}
|
||||
|
||||
//DecryptPwd 通过给定key解密给定ePwd
|
||||
func DecryptPwd(ePwd string, key []byte) string {
|
||||
defer func() {
|
||||
if pan := recover(); pan != nil {
|
||||
@ -389,7 +391,7 @@ func checkUpdate() {
|
||||
log.Infof("检查更新完成. 当前已运行最新版本.")
|
||||
}
|
||||
|
||||
func selfUpdate(imageUrl string) {
|
||||
func selfUpdate(imageURL string) {
|
||||
console := bufio.NewReader(os.Stdin)
|
||||
readLine := func() (str string) {
|
||||
str, _ = console.ReadString('\n')
|
||||
@ -417,8 +419,8 @@ func selfUpdate(imageUrl string) {
|
||||
url := fmt.Sprintf(
|
||||
"%v/Mrs4s/go-cqhttp/releases/download/%v/go-cqhttp-%v-%v-%v",
|
||||
func() string {
|
||||
if imageUrl != "" {
|
||||
return imageUrl
|
||||
if imageURL != "" {
|
||||
return imageURL
|
||||
}
|
||||
return "https://github.com"
|
||||
}(),
|
||||
|
@ -36,13 +36,13 @@ var Console = make(chan os.Signal, 1)
|
||||
|
||||
var Restart = make(chan struct{}, 1)
|
||||
|
||||
var JsonConfig *global.JsonConfig
|
||||
var JSONConfig *global.JSONConfig
|
||||
|
||||
type webServer struct {
|
||||
engine *gin.Engine
|
||||
bot *coolq.CQBot
|
||||
Cli *client.QQClient
|
||||
Conf *global.JsonConfig //old config
|
||||
Conf *global.JSONConfig //old config
|
||||
Console *bufio.Reader
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func Failed(code int, msg string) coolq.MSG {
|
||||
func (s *webServer) Run(addr string, cli *client.QQClient) *coolq.CQBot {
|
||||
s.Cli = cli
|
||||
s.Conf = GetConf()
|
||||
JsonConfig = s.Conf
|
||||
JSONConfig = s.Conf
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
s.engine = gin.New()
|
||||
|
||||
@ -81,7 +81,7 @@ func (s *webServer) Run(addr string, cli *client.QQClient) *coolq.CQBot {
|
||||
|
||||
go func() {
|
||||
//开启端口监听
|
||||
if s.Conf.WebUi != nil && s.Conf.WebUi.Enabled {
|
||||
if s.Conf.WebUI != nil && s.Conf.WebUI.Enabled {
|
||||
if Debug {
|
||||
pprof.Register(s.engine)
|
||||
log.Debugf("pprof 性能分析服务已启动在 http://%v/debug/pprof, 如果有任何性能问题请下载报告并提交给开发者", addr)
|
||||
@ -166,8 +166,8 @@ func (s *webServer) Dologin() {
|
||||
_ = ioutil.WriteFile("captcha.jpg", rsp.CaptchaImage, 0644)
|
||||
img, _, _ := image.Decode(bytes.NewReader(rsp.CaptchaImage))
|
||||
fmt.Println(asciiart.New("image", img).Art)
|
||||
if conf.WebUi != nil && conf.WebUi.WebInput {
|
||||
log.Warnf("请输入验证码 (captcha.jpg): (http://%s:%d/admin/do_web_write 输入)", conf.WebUi.Host, conf.WebUi.WebUiPort)
|
||||
if conf.WebUI != nil && conf.WebUI.WebInput {
|
||||
log.Warnf("请输入验证码 (captcha.jpg): (http://%s:%d/admin/do_web_write 输入)", conf.WebUI.Host, conf.WebUI.WebUIPort)
|
||||
text = <-WebInput
|
||||
} else {
|
||||
log.Warn("请输入验证码 (captcha.jpg): (Enter 提交)")
|
||||
@ -212,8 +212,8 @@ func (s *webServer) Dologin() {
|
||||
return
|
||||
case client.UnsafeDeviceError:
|
||||
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证并重启Bot.", rsp.VerifyUrl)
|
||||
if conf.WebUi != nil && conf.WebUi.WebInput {
|
||||
log.Infof(" (http://%s:%d/admin/do_web_write 确认后继续)....", conf.WebUi.Host, conf.WebUi.WebUiPort)
|
||||
if conf.WebUI != nil && conf.WebUI.WebInput {
|
||||
log.Infof(" (http://%s:%d/admin/do_web_write 确认后继续)....", conf.WebUI.Host, conf.WebUI.WebUIPort)
|
||||
text = <-WebInput
|
||||
} else {
|
||||
log.Infof("按 Enter 继续....")
|
||||
@ -266,7 +266,7 @@ func (s *webServer) Dologin() {
|
||||
global.BootFilter()
|
||||
global.InitCodec()
|
||||
coolq.IgnoreInvalidCQCode = conf.IgnoreInvalidCQCode
|
||||
coolq.SplitUrl = conf.FixUrl
|
||||
coolq.SplitUrl = conf.FixURL
|
||||
coolq.ForceFragmented = conf.ForceFragmented
|
||||
log.Info("资源初始化完成, 开始处理信息.")
|
||||
log.Info("アトリは、高性能ですから!")
|
||||
@ -329,9 +329,9 @@ func (s *webServer) admin(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 获取当前配置文件信息
|
||||
func GetConf() *global.JsonConfig {
|
||||
if JsonConfig != nil {
|
||||
return JsonConfig
|
||||
func GetConf() *global.JSONConfig {
|
||||
if JSONConfig != nil {
|
||||
return JSONConfig
|
||||
}
|
||||
conf := global.Load("config.hjson")
|
||||
return conf
|
||||
@ -391,7 +391,7 @@ func AuthMiddleWare() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
func (s *webServer) DoReLogin() { // TODO: 协议层的 ReLogin
|
||||
JsonConfig = nil
|
||||
JSONConfig = nil
|
||||
conf := GetConf()
|
||||
OldConf := s.Conf
|
||||
cli := client.NewClient(conf.Uin, conf.Password)
|
||||
@ -430,7 +430,7 @@ func (s *webServer) DoReLogin() { // TODO: 协议层的 ReLogin
|
||||
s.Cli = cli
|
||||
s.Dologin()
|
||||
//关闭之前的 server
|
||||
if OldConf.HttpConfig != nil && OldConf.HttpConfig.Enabled {
|
||||
if OldConf.HTTPConfig != nil && OldConf.HTTPConfig.Enabled {
|
||||
HttpServer.ShutDown()
|
||||
}
|
||||
//if OldConf.WSConfig != nil && OldConf.WSConfig.Enabled {
|
||||
@ -443,31 +443,31 @@ func (s *webServer) DoReLogin() { // TODO: 协议层的 ReLogin
|
||||
|
||||
func (s *webServer) UpServer() {
|
||||
conf := GetConf()
|
||||
if conf.HttpConfig != nil && conf.HttpConfig.Enabled {
|
||||
go HttpServer.Run(fmt.Sprintf("%s:%d", conf.HttpConfig.Host, conf.HttpConfig.Port), conf.AccessToken, s.bot)
|
||||
for k, v := range conf.HttpConfig.PostUrls {
|
||||
NewHttpClient().Run(k, v, conf.HttpConfig.Timeout, s.bot)
|
||||
if conf.HTTPConfig != nil && conf.HTTPConfig.Enabled {
|
||||
go HttpServer.Run(fmt.Sprintf("%s:%d", conf.HTTPConfig.Host, conf.HTTPConfig.Port), conf.AccessToken, s.bot)
|
||||
for k, v := range conf.HTTPConfig.PostUrls {
|
||||
NewHttpClient().Run(k, v, conf.HTTPConfig.Timeout, s.bot)
|
||||
}
|
||||
}
|
||||
if conf.WSConfig != nil && conf.WSConfig.Enabled {
|
||||
go WebsocketServer.Run(fmt.Sprintf("%s:%d", conf.WSConfig.Host, conf.WSConfig.Port), conf.AccessToken, s.bot)
|
||||
go WebSocketServer.Run(fmt.Sprintf("%s:%d", conf.WSConfig.Host, conf.WSConfig.Port), conf.AccessToken, s.bot)
|
||||
}
|
||||
for _, rc := range conf.ReverseServers {
|
||||
go NewWebsocketClient(rc, conf.AccessToken, s.bot).Run()
|
||||
go NewWebSocketClient(rc, conf.AccessToken, s.bot).Run()
|
||||
}
|
||||
}
|
||||
|
||||
// 暂不支持ws服务的重启
|
||||
func (s *webServer) ReloadServer() {
|
||||
conf := GetConf()
|
||||
if conf.HttpConfig != nil && conf.HttpConfig.Enabled {
|
||||
go HttpServer.Run(fmt.Sprintf("%s:%d", conf.HttpConfig.Host, conf.HttpConfig.Port), conf.AccessToken, s.bot)
|
||||
for k, v := range conf.HttpConfig.PostUrls {
|
||||
NewHttpClient().Run(k, v, conf.HttpConfig.Timeout, s.bot)
|
||||
if conf.HTTPConfig != nil && conf.HTTPConfig.Enabled {
|
||||
go HttpServer.Run(fmt.Sprintf("%s:%d", conf.HTTPConfig.Host, conf.HTTPConfig.Port), conf.AccessToken, s.bot)
|
||||
for k, v := range conf.HTTPConfig.PostUrls {
|
||||
NewHttpClient().Run(k, v, conf.HTTPConfig.Timeout, s.bot)
|
||||
}
|
||||
}
|
||||
for _, rc := range conf.ReverseServers {
|
||||
go NewWebsocketClient(rc, conf.AccessToken, s.bot).Run()
|
||||
go NewWebSocketClient(rc, conf.AccessToken, s.bot).Run()
|
||||
}
|
||||
}
|
||||
|
||||
@ -531,7 +531,7 @@ func AdminDoConfigBase(s *webServer, c *gin.Context) {
|
||||
log.Fatalf("保存 config.hjson 时出现错误: %v", err)
|
||||
c.JSON(200, Failed(502, "保存 config.hjson 时出现错误:"+fmt.Sprintf("%v", err)))
|
||||
} else {
|
||||
JsonConfig = nil
|
||||
JSONConfig = nil
|
||||
c.JSON(200, coolq.OK(coolq.MSG{}))
|
||||
}
|
||||
}
|
||||
@ -540,23 +540,23 @@ func AdminDoConfigBase(s *webServer, c *gin.Context) {
|
||||
func AdminDoConfigHttp(s *webServer, c *gin.Context) {
|
||||
conf := GetConf()
|
||||
p, _ := strconv.ParseUint(c.PostForm("port"), 10, 16)
|
||||
conf.HttpConfig.Port = uint16(p)
|
||||
conf.HttpConfig.Host = c.PostForm("host")
|
||||
conf.HTTPConfig.Port = uint16(p)
|
||||
conf.HTTPConfig.Host = c.PostForm("host")
|
||||
if c.PostForm("enable") == "true" {
|
||||
conf.HttpConfig.Enabled = true
|
||||
conf.HTTPConfig.Enabled = true
|
||||
} else {
|
||||
conf.HttpConfig.Enabled = false
|
||||
conf.HTTPConfig.Enabled = false
|
||||
}
|
||||
t, _ := strconv.ParseInt(c.PostForm("timeout"), 10, 32)
|
||||
conf.HttpConfig.Timeout = int32(t)
|
||||
conf.HTTPConfig.Timeout = int32(t)
|
||||
if c.PostForm("post_url") != "" {
|
||||
conf.HttpConfig.PostUrls[c.PostForm("post_url")] = c.PostForm("post_secret")
|
||||
conf.HTTPConfig.PostUrls[c.PostForm("post_url")] = c.PostForm("post_secret")
|
||||
}
|
||||
if err := conf.Save("config.hjson"); err != nil {
|
||||
log.Fatalf("保存 config.hjson 时出现错误: %v", err)
|
||||
c.JSON(200, Failed(502, "保存 config.hjson 时出现错误:"+fmt.Sprintf("%v", err)))
|
||||
} else {
|
||||
JsonConfig = nil
|
||||
JSONConfig = nil
|
||||
c.JSON(200, coolq.OK(coolq.MSG{}))
|
||||
}
|
||||
}
|
||||
@ -576,7 +576,7 @@ func AdminDoConfigWs(s *webServer, c *gin.Context) {
|
||||
log.Fatalf("保存 config.hjson 时出现错误: %v", err)
|
||||
c.JSON(200, Failed(502, "保存 config.hjson 时出现错误:"+fmt.Sprintf("%v", err)))
|
||||
} else {
|
||||
JsonConfig = nil
|
||||
JSONConfig = nil
|
||||
c.JSON(200, coolq.OK(coolq.MSG{}))
|
||||
}
|
||||
}
|
||||
@ -584,9 +584,9 @@ func AdminDoConfigWs(s *webServer, c *gin.Context) {
|
||||
// 反向ws配置修改
|
||||
func AdminDoConfigReverse(s *webServer, c *gin.Context) {
|
||||
conf := GetConf()
|
||||
conf.ReverseServers[0].ReverseApiUrl = c.PostForm("reverse_api_url")
|
||||
conf.ReverseServers[0].ReverseUrl = c.PostForm("reverse_url")
|
||||
conf.ReverseServers[0].ReverseEventUrl = c.PostForm("reverse_event_url")
|
||||
conf.ReverseServers[0].ReverseAPIURL = c.PostForm("reverse_api_url")
|
||||
conf.ReverseServers[0].ReverseURL = c.PostForm("reverse_url")
|
||||
conf.ReverseServers[0].ReverseEventURL = c.PostForm("reverse_event_url")
|
||||
t, _ := strconv.ParseUint(c.PostForm("reverse_reconnect_interval"), 10, 16)
|
||||
conf.ReverseServers[0].ReverseReconnectInterval = uint16(t)
|
||||
if c.PostForm("enable") == "true" {
|
||||
@ -598,7 +598,7 @@ func AdminDoConfigReverse(s *webServer, c *gin.Context) {
|
||||
log.Fatalf("保存 config.hjson 时出现错误: %v", err)
|
||||
c.JSON(200, Failed(502, "保存 config.hjson 时出现错误:"+fmt.Sprintf("%v", err)))
|
||||
} else {
|
||||
JsonConfig = nil
|
||||
JSONConfig = nil
|
||||
c.JSON(200, coolq.OK(coolq.MSG{}))
|
||||
}
|
||||
}
|
||||
@ -617,7 +617,7 @@ func AdminDoConfigJson(s *webServer, c *gin.Context) {
|
||||
log.Fatalf("保存 config.hjson 时出现错误: %v", err)
|
||||
c.JSON(200, Failed(502, "保存 config.hjson 时出现错误:"+fmt.Sprintf("%v", err)))
|
||||
} else {
|
||||
JsonConfig = nil
|
||||
JSONConfig = nil
|
||||
c.JSON(200, coolq.OK(coolq.MSG{}))
|
||||
}
|
||||
}
|
||||
|
@ -17,36 +17,38 @@ import (
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type websocketServer struct {
|
||||
type webSocketServer struct {
|
||||
bot *coolq.CQBot
|
||||
token string
|
||||
eventConn []*websocketConn
|
||||
eventConn []*webSocketConn
|
||||
eventConnMutex sync.Mutex
|
||||
handshake string
|
||||
}
|
||||
|
||||
type websocketClient struct {
|
||||
conf *global.GoCQReverseWebsocketConfig
|
||||
//WebSocketClient Websocket客户端实例
|
||||
type WebSocketClient struct {
|
||||
conf *global.GoCQReverseWebSocketConfig
|
||||
token string
|
||||
bot *coolq.CQBot
|
||||
|
||||
universalConn *websocketConn
|
||||
eventConn *websocketConn
|
||||
universalConn *webSocketConn
|
||||
eventConn *webSocketConn
|
||||
}
|
||||
|
||||
type websocketConn struct {
|
||||
type webSocketConn struct {
|
||||
*websocket.Conn
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
var WebsocketServer = &websocketServer{}
|
||||
//WebSocketServer 初始化一个WebSocketServer实例
|
||||
var WebSocketServer = &webSocketServer{}
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
func (s *websocketServer) Run(addr, authToken string, b *coolq.CQBot) {
|
||||
func (s *webSocketServer) Run(addr, authToken string, b *coolq.CQBot) {
|
||||
s.token = authToken
|
||||
s.bot = b
|
||||
s.handshake = fmt.Sprintf(`{"_post_method":2,"meta_event_type":"lifecycle","post_type":"meta_event","self_id":%d,"sub_type":"connect","time":%d}`,
|
||||
@ -61,29 +63,31 @@ func (s *websocketServer) Run(addr, authToken string, b *coolq.CQBot) {
|
||||
}()
|
||||
}
|
||||
|
||||
func NewWebsocketClient(conf *global.GoCQReverseWebsocketConfig, authToken string, b *coolq.CQBot) *websocketClient {
|
||||
return &websocketClient{conf: conf, token: authToken, bot: b}
|
||||
//NewWebSocketClient 初始化一个NWebSocket客户端
|
||||
func NewWebSocketClient(conf *global.GoCQReverseWebSocketConfig, authToken string, b *coolq.CQBot) *WebSocketClient {
|
||||
return &WebSocketClient{conf: conf, token: authToken, bot: b}
|
||||
}
|
||||
|
||||
func (c *websocketClient) Run() {
|
||||
//Run 运行实例
|
||||
func (c *WebSocketClient) Run() {
|
||||
if !c.conf.Enabled {
|
||||
return
|
||||
}
|
||||
if c.conf.ReverseUrl != "" {
|
||||
if c.conf.ReverseURL != "" {
|
||||
c.connectUniversal()
|
||||
} else {
|
||||
if c.conf.ReverseApiUrl != "" {
|
||||
c.connectApi()
|
||||
if c.conf.ReverseAPIURL != "" {
|
||||
c.connectAPI()
|
||||
}
|
||||
if c.conf.ReverseEventUrl != "" {
|
||||
if c.conf.ReverseEventURL != "" {
|
||||
c.connectEvent()
|
||||
}
|
||||
}
|
||||
c.bot.OnEventPush(c.onBotPushEvent)
|
||||
}
|
||||
|
||||
func (c *websocketClient) connectApi() {
|
||||
log.Infof("开始尝试连接到反向Websocket API服务器: %v", c.conf.ReverseApiUrl)
|
||||
func (c *WebSocketClient) connectAPI() {
|
||||
log.Infof("开始尝试连接到反向Websocket API服务器: %v", c.conf.ReverseAPIURL)
|
||||
header := http.Header{
|
||||
"X-Client-Role": []string{"API"},
|
||||
"X-Self-ID": []string{strconv.FormatInt(c.bot.Client.Uin, 10)},
|
||||
@ -92,22 +96,22 @@ func (c *websocketClient) connectApi() {
|
||||
if c.token != "" {
|
||||
header["Authorization"] = []string{"Token " + c.token}
|
||||
}
|
||||
conn, _, err := websocket.DefaultDialer.Dial(c.conf.ReverseApiUrl, header)
|
||||
conn, _, err := websocket.DefaultDialer.Dial(c.conf.ReverseAPIURL, header)
|
||||
if err != nil {
|
||||
log.Warnf("连接到反向Websocket API服务器 %v 时出现错误: %v", c.conf.ReverseApiUrl, err)
|
||||
log.Warnf("连接到反向Websocket API服务器 %v 时出现错误: %v", c.conf.ReverseAPIURL, err)
|
||||
if c.conf.ReverseReconnectInterval != 0 {
|
||||
time.Sleep(time.Millisecond * time.Duration(c.conf.ReverseReconnectInterval))
|
||||
c.connectApi()
|
||||
c.connectAPI()
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Infof("已连接到反向Websocket API服务器 %v", c.conf.ReverseApiUrl)
|
||||
wrappedConn := &websocketConn{Conn: conn}
|
||||
go c.listenApi(wrappedConn, false)
|
||||
log.Infof("已连接到反向Websocket API服务器 %v", c.conf.ReverseAPIURL)
|
||||
wrappedConn := &webSocketConn{Conn: conn}
|
||||
go c.listenAPI(wrappedConn, false)
|
||||
}
|
||||
|
||||
func (c *websocketClient) connectEvent() {
|
||||
log.Infof("开始尝试连接到反向Websocket Event服务器: %v", c.conf.ReverseEventUrl)
|
||||
func (c *WebSocketClient) connectEvent() {
|
||||
log.Infof("开始尝试连接到反向Websocket Event服务器: %v", c.conf.ReverseEventURL)
|
||||
header := http.Header{
|
||||
"X-Client-Role": []string{"Event"},
|
||||
"X-Self-ID": []string{strconv.FormatInt(c.bot.Client.Uin, 10)},
|
||||
@ -116,9 +120,9 @@ func (c *websocketClient) connectEvent() {
|
||||
if c.token != "" {
|
||||
header["Authorization"] = []string{"Token " + c.token}
|
||||
}
|
||||
conn, _, err := websocket.DefaultDialer.Dial(c.conf.ReverseEventUrl, header)
|
||||
conn, _, err := websocket.DefaultDialer.Dial(c.conf.ReverseEventURL, header)
|
||||
if err != nil {
|
||||
log.Warnf("连接到反向Websocket Event服务器 %v 时出现错误: %v", c.conf.ReverseEventUrl, err)
|
||||
log.Warnf("连接到反向Websocket Event服务器 %v 时出现错误: %v", c.conf.ReverseEventURL, err)
|
||||
if c.conf.ReverseReconnectInterval != 0 {
|
||||
time.Sleep(time.Millisecond * time.Duration(c.conf.ReverseReconnectInterval))
|
||||
c.connectEvent()
|
||||
@ -133,12 +137,12 @@ func (c *websocketClient) connectEvent() {
|
||||
log.Warnf("反向Websocket 握手时出现错误: %v", err)
|
||||
}
|
||||
|
||||
log.Infof("已连接到反向Websocket Event服务器 %v", c.conf.ReverseEventUrl)
|
||||
c.eventConn = &websocketConn{Conn: conn}
|
||||
log.Infof("已连接到反向Websocket Event服务器 %v", c.conf.ReverseEventURL)
|
||||
c.eventConn = &webSocketConn{Conn: conn}
|
||||
}
|
||||
|
||||
func (c *websocketClient) connectUniversal() {
|
||||
log.Infof("开始尝试连接到反向Websocket Universal服务器: %v", c.conf.ReverseUrl)
|
||||
func (c *WebSocketClient) connectUniversal() {
|
||||
log.Infof("开始尝试连接到反向Websocket Universal服务器: %v", c.conf.ReverseURL)
|
||||
header := http.Header{
|
||||
"X-Client-Role": []string{"Universal"},
|
||||
"X-Self-ID": []string{strconv.FormatInt(c.bot.Client.Uin, 10)},
|
||||
@ -147,9 +151,9 @@ func (c *websocketClient) connectUniversal() {
|
||||
if c.token != "" {
|
||||
header["Authorization"] = []string{"Token " + c.token}
|
||||
}
|
||||
conn, _, err := websocket.DefaultDialer.Dial(c.conf.ReverseUrl, header)
|
||||
conn, _, err := websocket.DefaultDialer.Dial(c.conf.ReverseURL, header)
|
||||
if err != nil {
|
||||
log.Warnf("连接到反向Websocket Universal服务器 %v 时出现错误: %v", c.conf.ReverseUrl, err)
|
||||
log.Warnf("连接到反向Websocket Universal服务器 %v 时出现错误: %v", c.conf.ReverseURL, err)
|
||||
if c.conf.ReverseReconnectInterval != 0 {
|
||||
time.Sleep(time.Millisecond * time.Duration(c.conf.ReverseReconnectInterval))
|
||||
c.connectUniversal()
|
||||
@ -164,12 +168,12 @@ func (c *websocketClient) connectUniversal() {
|
||||
log.Warnf("反向Websocket 握手时出现错误: %v", err)
|
||||
}
|
||||
|
||||
wrappedConn := &websocketConn{Conn: conn}
|
||||
go c.listenApi(wrappedConn, true)
|
||||
wrappedConn := &webSocketConn{Conn: conn}
|
||||
go c.listenAPI(wrappedConn, true)
|
||||
c.universalConn = wrappedConn
|
||||
}
|
||||
|
||||
func (c *websocketClient) listenApi(conn *websocketConn, u bool) {
|
||||
func (c *WebSocketClient) listenAPI(conn *webSocketConn, u bool) {
|
||||
defer conn.Close()
|
||||
for {
|
||||
_, buf, err := conn.ReadMessage()
|
||||
@ -184,12 +188,12 @@ func (c *websocketClient) listenApi(conn *websocketConn, u bool) {
|
||||
if c.conf.ReverseReconnectInterval != 0 {
|
||||
time.Sleep(time.Millisecond * time.Duration(c.conf.ReverseReconnectInterval))
|
||||
if !u {
|
||||
go c.connectApi()
|
||||
go c.connectAPI()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
|
||||
func (c *WebSocketClient) onBotPushEvent(m coolq.MSG) {
|
||||
if c.eventConn != nil {
|
||||
log.Debugf("向WS服务器 %v 推送Event: %v", c.eventConn.RemoteAddr().String(), m.ToJson())
|
||||
conn := c.eventConn
|
||||
@ -222,7 +226,7 @@ func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *websocketServer) event(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *webSocketServer) event(w http.ResponseWriter, r *http.Request) {
|
||||
if s.token != "" {
|
||||
if auth := r.URL.Query().Get("access_token"); auth != s.token {
|
||||
if auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2); len(auth) != 2 || auth[1] != s.token {
|
||||
@ -246,14 +250,14 @@ func (s *websocketServer) event(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Infof("接受 Websocket 连接: %v (/event)", r.RemoteAddr)
|
||||
|
||||
conn := &websocketConn{Conn: c}
|
||||
conn := &webSocketConn{Conn: c}
|
||||
|
||||
s.eventConnMutex.Lock()
|
||||
s.eventConn = append(s.eventConn, conn)
|
||||
s.eventConnMutex.Unlock()
|
||||
}
|
||||
|
||||
func (s *websocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||
if s.token != "" {
|
||||
if auth := r.URL.Query().Get("access_token"); auth != s.token {
|
||||
if auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2); len(auth) != 2 || auth[1] != s.token {
|
||||
@ -269,11 +273,11 @@ func (s *websocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
log.Infof("接受 Websocket 连接: %v (/api)", r.RemoteAddr)
|
||||
conn := &websocketConn{Conn: c}
|
||||
go s.listenApi(conn)
|
||||
conn := &webSocketConn{Conn: c}
|
||||
go s.listenAPI(conn)
|
||||
}
|
||||
|
||||
func (s *websocketServer) any(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *webSocketServer) any(w http.ResponseWriter, r *http.Request) {
|
||||
if s.token != "" {
|
||||
if auth := r.URL.Query().Get("access_token"); auth != s.token {
|
||||
if auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2); len(auth) != 2 || auth[1] != s.token {
|
||||
@ -295,12 +299,12 @@ func (s *websocketServer) any(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
log.Infof("接受 Websocket 连接: %v (/)", r.RemoteAddr)
|
||||
conn := &websocketConn{Conn: c}
|
||||
conn := &webSocketConn{Conn: c}
|
||||
s.eventConn = append(s.eventConn, conn)
|
||||
s.listenApi(conn)
|
||||
s.listenAPI(conn)
|
||||
}
|
||||
|
||||
func (s *websocketServer) listenApi(c *websocketConn) {
|
||||
func (s *webSocketServer) listenAPI(c *webSocketConn) {
|
||||
defer c.Close()
|
||||
for {
|
||||
t, payload, err := c.ReadMessage()
|
||||
@ -314,7 +318,7 @@ func (s *websocketServer) listenApi(c *websocketConn) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *websocketConn) handleRequest(bot *coolq.CQBot, payload []byte) {
|
||||
func (c *webSocketConn) handleRequest(bot *coolq.CQBot, payload []byte) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Printf("处置WS命令时发生无法恢复的异常:%v\n%s", err, debug.Stack())
|
||||
@ -325,7 +329,7 @@ func (c *websocketConn) handleRequest(bot *coolq.CQBot, payload []byte) {
|
||||
j := gjson.ParseBytes(payload)
|
||||
t := strings.ReplaceAll(j.Get("action").Str, "_async", "")
|
||||
log.Debugf("WS接收到API调用: %v 参数: %v", t, j.Get("params").Raw)
|
||||
if f, ok := wsApi[t]; ok {
|
||||
if f, ok := wsAPI[t]; ok {
|
||||
ret := f(bot, j.Get("params"))
|
||||
if j.Get("echo").Exists() {
|
||||
ret["echo"] = j.Get("echo").Value()
|
||||
@ -344,7 +348,7 @@ func (c *websocketConn) handleRequest(bot *coolq.CQBot, payload []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *websocketServer) onBotPushEvent(m coolq.MSG) {
|
||||
func (s *webSocketServer) onBotPushEvent(m coolq.MSG) {
|
||||
s.eventConnMutex.Lock()
|
||||
defer s.eventConnMutex.Unlock()
|
||||
for i, l := 0, len(s.eventConn); i < l; i++ {
|
||||
@ -368,7 +372,7 @@ func (s *websocketServer) onBotPushEvent(m coolq.MSG) {
|
||||
}
|
||||
}
|
||||
|
||||
var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
||||
var wsAPI = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
||||
"get_login_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQGetLoginInfo()
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user