1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-06 20:13:50 +08:00

feather: add set_restart api support

This commit is contained in:
Ink-33 2020-11-16 22:36:47 +08:00
parent 0f0f74ece8
commit 4a3288cb8d
No known key found for this signature in database
GPG Key ID: 5D8B1D036EFB0D2E
12 changed files with 93 additions and 37 deletions

View File

@ -5,12 +5,13 @@ import (
"encoding/gob"
"encoding/json"
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"hash/crc32"
"path"
"sync"
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"

View File

@ -8,11 +8,6 @@ import (
xml2 "encoding/xml"
"errors"
"fmt"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"io/ioutil"
"net/url"
"path"
@ -20,6 +15,12 @@ import (
"runtime"
"strconv"
"strings"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
var matchReg = regexp.MustCompile(`\[CQ:\w+?.*?]`)

View File

@ -2,16 +2,17 @@ package coolq
import (
"encoding/hex"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
"io/ioutil"
"path"
"strconv"
"strings"
"time"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
)
var format = "string"

View File

@ -4,11 +4,12 @@ import (
"crypto/md5"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wdvxdr1123/go-silk/silk"
"io/ioutil"
"path"
"sync"
log "github.com/sirupsen/logrus"
"github.com/wdvxdr1123/go-silk/silk"
)
var codec silk.Encoder

View File

@ -1,11 +1,12 @@
package global
import (
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"io/ioutil"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
type Filter interface {

View File

@ -7,8 +7,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net"
"net/url"
@ -17,6 +15,9 @@ import (
"runtime"
"strconv"
"strings"
"github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus"
)
var (

View File

@ -4,11 +4,12 @@ import (
"bytes"
"compress/gzip"
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/tidwall/gjson"
)
var client = &http.Client{

View File

@ -1,11 +1,12 @@
package global
import (
"github.com/tidwall/gjson"
"math"
"regexp"
"strconv"
"strings"
"github.com/tidwall/gjson"
)
var trueSet = map[string]struct{}{

View File

@ -2,6 +2,7 @@ package global
import (
"context"
"golang.org/x/time/rate"
)

View File

@ -6,13 +6,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/yinghau76/go-ascii-art"
"image"
"io/ioutil"
"net/http"
@ -21,12 +14,22 @@ import (
"strconv"
"strings"
"time"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
asciiart "github.com/yinghau76/go-ascii-art"
)
var WebInput = make(chan string, 1) //长度1用于阻塞
var Console = make(chan os.Signal, 1)
var Restart = make(chan struct{}, 1)
var JsonConfig *global.JsonConfig
type webServer struct {
@ -41,16 +44,17 @@ var WebServer = &webServer{}
// admin 子站的 路由映射
var HttpuriAdmin = map[string]func(s *webServer, c *gin.Context){
"do_restart": AdminDoRestart, //热重启
"get_web_write": AdminWebWrite, //获取是否验证码输入
"do_web_write": AdminDoWebWrite, //web上进行输入操作
"do_restart_docker": AdminDoRestartDocker, //直接停止依赖supervisord/docker重新拉起
"do_config_base": AdminDoConfigBase, //修改config.json中的基础部分
"do_config_http": AdminDoConfigHttp, //修改config.json的http部分
"do_config_ws": AdminDoConfigWs, //修改config.json的正向ws部分
"do_config_reverse": AdminDoConfigReverse, //修改config.json 中的反向ws部分
"do_config_json": AdminDoConfigJson, //直接修改 config.json配置
"get_config_json": AdminGetConfigJson, //拉取 当前的config.json配置
"do_restart": AdminDoRestart, //热重启
"do_process_restart": AdminProcessRestart, //进程重启
"get_web_write": AdminWebWrite, //获取是否验证码输入
"do_web_write": AdminDoWebWrite, //web上进行输入操作
"do_restart_docker": AdminDoRestartDocker, //直接停止依赖supervisord/docker重新拉起
"do_config_base": AdminDoConfigBase, //修改config.json中的基础部分
"do_config_http": AdminDoConfigHttp, //修改config.json的http部分
"do_config_ws": AdminDoConfigWs, //修改config.json的正向ws部分
"do_config_reverse": AdminDoConfigReverse, //修改config.json 中的反向ws部分
"do_config_json": AdminDoConfigJson, //直接修改 config.json配置
"get_config_json": AdminGetConfigJson, //拉取 当前的config.json配置
}
func Failed(code int, msg string) coolq.MSG {
@ -423,6 +427,12 @@ func AdminDoRestart(s *webServer, c *gin.Context) {
return
}
// 进程重启
func AdminProcessRestart(s *webServer, c *gin.Context) {
Restart <- struct{}{}
c.JSON(200, coolq.OK(coolq.MSG{}))
}
// 冷重启
func AdminDoRestartDocker(s *webServer, c *gin.Context) {
Console <- os.Kill

View File

@ -5,12 +5,14 @@ import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"github.com/guonaihong/gout/dataflow"
"net/http"
"os"
"strconv"
"strings"
"time"
"unsafe"
"github.com/guonaihong/gout/dataflow"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
@ -343,6 +345,25 @@ func SetGroupLeave(s *httpServer, c *gin.Context) {
c.JSON(200, s.bot.CQSetGroupLeave(gid))
}
func SetRestart(s *httpServer, c *gin.Context) {
d, t := getParamWithType(c, "delay")
if t == gjson.Null {
d = "0"
}
delay, err := strconv.ParseInt(d, 10, 64)
if err != nil || delay < 0 {
c.JSON(200, Failed(100, "Invalid delay"))
return
}
c.JSON(200, coolq.MSG{"data": nil, "retcode": 0, "status": "async"})
go func(delay int64) {
var del *time.Duration = (*time.Duration)(unsafe.Pointer(&delay))
time.Sleep(*del * time.Millisecond)
Restart <- struct{}{}
}(delay * time.Hour.Milliseconds())
}
func GetForwardMessage(s *httpServer, c *gin.Context) {
resId := getParam(c, "message_id")
c.JSON(200, s.bot.CQGetForwardMessage(resId))
@ -488,6 +509,7 @@ var httpApi = map[string]func(s *httpServer, c *gin.Context){
"set_group_whole_ban": SetWholeBan,
"set_group_name": SetGroupName,
"set_group_admin": SetGroupAdmin,
"set_restart": SetRestart,
"_send_group_notice": SendGroupNotice,
"set_group_leave": SetGroupLeave,
"get_image": GetImage,

View File

@ -8,6 +8,7 @@ import (
"strings"
"sync"
"time"
"unsafe"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
@ -480,6 +481,20 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
"get_group_honor_info": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
return bot.CQGetGroupHonorInfo(p.Get("group_id").Int(), p.Get("type").Str)
},
"set_restart": func(c *coolq.CQBot, p gjson.Result) coolq.MSG {
var delay int64 = 0
delay = p.Get("delay").Int()
if delay < 0 {
return Failed(100, "Invalid delay")
}
defer func(delay int64) {
var del *time.Duration = (*time.Duration)(unsafe.Pointer(&delay))
time.Sleep(*del * time.Millisecond)
Restart <- struct{}{}
}(delay * time.Hour.Milliseconds())
return coolq.MSG{"data": nil, "retcode": 0, "status": "async"}
},
"can_send_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
return bot.CQCanSendImage()
},