diff --git a/coolq/bot.go b/coolq/bot.go index 1262b07..6ea59f6 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -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" diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 658ee05..3f864bb 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -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+?.*?]`) diff --git a/coolq/event.go b/coolq/event.go index 9d2e300..bbfa09f 100644 --- a/coolq/event.go +++ b/coolq/event.go @@ -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" diff --git a/global/codec.go b/global/codec.go index b519b68..bc9b9e0 100644 --- a/global/codec.go +++ b/global/codec.go @@ -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 diff --git a/global/filter.go b/global/filter.go index b2ac4cb..f47f62e 100644 --- a/global/filter.go +++ b/global/filter.go @@ -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 { diff --git a/global/fs.go b/global/fs.go index d8bccaf..10dc7a2 100644 --- a/global/fs.go +++ b/global/fs.go @@ -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 ( diff --git a/global/net.go b/global/net.go index d30d796..1495fb1 100644 --- a/global/net.go +++ b/global/net.go @@ -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{ diff --git a/global/param.go b/global/param.go index 5f01bea..7acd5eb 100644 --- a/global/param.go +++ b/global/param.go @@ -1,11 +1,12 @@ package global import ( - "github.com/tidwall/gjson" "math" "regexp" "strconv" "strings" + + "github.com/tidwall/gjson" ) var trueSet = map[string]struct{}{ diff --git a/global/ratelimit.go b/global/ratelimit.go index 79594be..b8bfb2a 100644 --- a/global/ratelimit.go +++ b/global/ratelimit.go @@ -2,6 +2,7 @@ package global import ( "context" + "golang.org/x/time/rate" ) diff --git a/server/apiAdmin.go b/server/apiAdmin.go index 3454273..6c14120 100644 --- a/server/apiAdmin.go +++ b/server/apiAdmin.go @@ -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 diff --git a/server/http.go b/server/http.go index 8c12e43..5b58c7e 100644 --- a/server/http.go +++ b/server/http.go @@ -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, diff --git a/server/websocket.go b/server/websocket.go index 668ea00..b01fd21 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -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() },