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/gob"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/syndtr/goleveldb/leveldb"
"hash/crc32" "hash/crc32"
"path" "path"
"sync" "sync"
"time" "time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client" "github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/message"

View File

@ -8,11 +8,6 @@ import (
xml2 "encoding/xml" xml2 "encoding/xml"
"errors" "errors"
"fmt" "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" "io/ioutil"
"net/url" "net/url"
"path" "path"
@ -20,6 +15,12 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "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+?.*?]`) var matchReg = regexp.MustCompile(`\[CQ:\w+?.*?]`)

View File

@ -2,16 +2,17 @@ package coolq
import ( import (
"encoding/hex" "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" "io/ioutil"
"path" "path"
"strconv" "strconv"
"strings" "strings"
"time" "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" var format = "string"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,13 +6,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "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" "image"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -21,12 +14,22 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "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 WebInput = make(chan string, 1) //长度1用于阻塞
var Console = make(chan os.Signal, 1) var Console = make(chan os.Signal, 1)
var Restart = make(chan struct{}, 1)
var JsonConfig *global.JsonConfig var JsonConfig *global.JsonConfig
type webServer struct { type webServer struct {
@ -42,6 +45,7 @@ var WebServer = &webServer{}
// admin 子站的 路由映射 // admin 子站的 路由映射
var HttpuriAdmin = map[string]func(s *webServer, c *gin.Context){ var HttpuriAdmin = map[string]func(s *webServer, c *gin.Context){
"do_restart": AdminDoRestart, //热重启 "do_restart": AdminDoRestart, //热重启
"do_process_restart": AdminProcessRestart, //进程重启
"get_web_write": AdminWebWrite, //获取是否验证码输入 "get_web_write": AdminWebWrite, //获取是否验证码输入
"do_web_write": AdminDoWebWrite, //web上进行输入操作 "do_web_write": AdminDoWebWrite, //web上进行输入操作
"do_restart_docker": AdminDoRestartDocker, //直接停止依赖supervisord/docker重新拉起 "do_restart_docker": AdminDoRestartDocker, //直接停止依赖supervisord/docker重新拉起
@ -423,6 +427,12 @@ func AdminDoRestart(s *webServer, c *gin.Context) {
return return
} }
// 进程重启
func AdminProcessRestart(s *webServer, c *gin.Context) {
Restart <- struct{}{}
c.JSON(200, coolq.OK(coolq.MSG{}))
}
// 冷重启 // 冷重启
func AdminDoRestartDocker(s *webServer, c *gin.Context) { func AdminDoRestartDocker(s *webServer, c *gin.Context) {
Console <- os.Kill Console <- os.Kill

View File

@ -5,12 +5,14 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"github.com/guonaihong/gout/dataflow"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unsafe"
"github.com/guonaihong/gout/dataflow"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
@ -343,6 +345,25 @@ func SetGroupLeave(s *httpServer, c *gin.Context) {
c.JSON(200, s.bot.CQSetGroupLeave(gid)) 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) { func GetForwardMessage(s *httpServer, c *gin.Context) {
resId := getParam(c, "message_id") resId := getParam(c, "message_id")
c.JSON(200, s.bot.CQGetForwardMessage(resId)) 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_whole_ban": SetWholeBan,
"set_group_name": SetGroupName, "set_group_name": SetGroupName,
"set_group_admin": SetGroupAdmin, "set_group_admin": SetGroupAdmin,
"set_restart": SetRestart,
"_send_group_notice": SendGroupNotice, "_send_group_notice": SendGroupNotice,
"set_group_leave": SetGroupLeave, "set_group_leave": SetGroupLeave,
"get_image": GetImage, "get_image": GetImage,

View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"unsafe"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "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 { "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) 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 { "can_send_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
return bot.CQCanSendImage() return bot.CQCanSendImage()
}, },