From e3f0dbc4acd0f0fc00675042dd51d687e9a8a3c6 Mon Sep 17 00:00:00 2001 From: scjtqs Date: Sat, 3 Oct 2020 17:28:21 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=85=B3=E9=97=AD=20web=20admin=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/apiAdmin.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/server/apiAdmin.go b/server/apiAdmin.go index ad83d97..7ede328 100644 --- a/server/apiAdmin.go +++ b/server/apiAdmin.go @@ -17,6 +17,7 @@ import ( "io/ioutil" "net/http" "os" + "os/signal" "strconv" "strings" "time" @@ -69,12 +70,21 @@ func (s *webServer) Run(addr string, cli *client.QQClient) *coolq.CQBot { s.engine.Any("/admin/:action", s.admin) go func() { - log.Infof("Admin API 服务器已启动: %v", addr) - err := s.engine.Run(addr) - if err != nil { - log.Error(err) - log.Infof("请检查端口是否被占用.") - time.Sleep(time.Second * 5) + //开启端口监听 + if s.Conf.WebUi.Enabled{ + log.Infof("Admin API 服务器已启动: %v", addr) + err := s.engine.Run(addr) + if err != nil { + log.Error(err) + log.Infof("请检查端口是否被占用.") + time.Sleep(time.Second * 5) + os.Exit(1) + } + }else{ + //关闭端口监听 + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, os.Kill) + <-c os.Exit(1) } }() From 95c399a00338e07a8be666d2209fa1a9ef5963b5 Mon Sep 17 00:00:00 2001 From: scjtqs Date: Sat, 3 Oct 2020 17:43:03 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=20admin=20api?= =?UTF-8?q?=20=E7=9A=84=E7=9B=91=E5=90=AC=20host=E6=94=AF=E6=8C=81?= =?UTF-8?q?=EF=BC=8C=E9=AA=8C=E8=AF=81=E7=A0=81=E6=8F=90=E7=A4=BA=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20host+port?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/config.go | 2 ++ main.go | 6 +++++- server/apiAdmin.go | 8 ++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/global/config.go b/global/config.go index 71ecaaf..e380e99 100644 --- a/global/config.go +++ b/global/config.go @@ -81,6 +81,7 @@ type GoCQReverseWebsocketConfig struct { type GoCqWebUi struct { Enabled bool `json:"enabled"` + Host string `json:"host"` WebUiPort uint64 `json:"web_ui_port"` WebInput bool `json:"web_input"` } @@ -130,6 +131,7 @@ func DefaultConfig() *JsonConfig { }, WebUi: &GoCqWebUi{ Enabled: true, + Host: "0.0.0.0", WebInput: false, WebUiPort: 9999, }, diff --git a/main.go b/main.go index 15f10ab..46ce32d 100644 --- a/main.go +++ b/main.go @@ -237,17 +237,21 @@ func main() { conf.WebUi = &global.GoCqWebUi{ Enabled: true, WebInput: false, + Host: "0.0.0.0", WebUiPort: 9999, } } if conf.WebUi.WebUiPort <= 0 { conf.WebUi.WebUiPort = 9999 } + if conf.WebUi.Host == "" { + conf.WebUi.Host = "0.0.0.0" + } confErr := conf.Save("config.json") if confErr != nil { log.Error("保存配置文件失败") } - b := server.WebServer.Run(fmt.Sprintf("%s:%d", "0.0.0.0", conf.WebUi.WebUiPort), cli) + b := server.WebServer.Run(fmt.Sprintf("%s:%d", conf.WebUi.Host, conf.WebUi.WebUiPort), cli) c := server.Console signal.Notify(c, os.Interrupt, os.Kill) <-c diff --git a/server/apiAdmin.go b/server/apiAdmin.go index 7ede328..c136391 100644 --- a/server/apiAdmin.go +++ b/server/apiAdmin.go @@ -71,7 +71,7 @@ func (s *webServer) Run(addr string, cli *client.QQClient) *coolq.CQBot { go func() { //开启端口监听 - if s.Conf.WebUi.Enabled{ + if s.Conf.WebUi.Enabled { log.Infof("Admin API 服务器已启动: %v", addr) err := s.engine.Run(addr) if err != nil { @@ -80,7 +80,7 @@ func (s *webServer) Run(addr string, cli *client.QQClient) *coolq.CQBot { time.Sleep(time.Second * 5) os.Exit(1) } - }else{ + } else { //关闭端口监听 c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) @@ -109,7 +109,7 @@ func (s *webServer) Dologin() { img, _, _ := image.Decode(bytes.NewReader(rsp.CaptchaImage)) fmt.Println(asciiart.New("image", img).Art) if conf.WebUi.WebInput { - log.Warn("请输入验证码 (captcha.jpg): (http://127.0.0.1/admin/web_write 输入)") + log.Warnf("请输入验证码 (captcha.jpg): (http://%s:%d/admin/web_write 输入)", conf.WebUi.Host, conf.WebUi.WebUiPort) text = <-WebInput } else { log.Warn("请输入验证码 (captcha.jpg): (Enter 提交)") @@ -121,7 +121,7 @@ func (s *webServer) Dologin() { case client.UnsafeDeviceError: log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证并重启Bot.", rsp.VerifyUrl) if conf.WebUi.WebInput { - log.Infof(" (http://127.0.0.1/admin/web_write 确认后继续)....") + log.Infof(" (http://%s:%d/admin/web_write 确认后继续)....", conf.WebUi.Host, conf.WebUi.WebUiPort) text = <-WebInput } else { log.Infof(" 按 Enter 继续....") From b19b114d3a6d3dfb211c07e1fc568dd2030c7d31 Mon Sep 17 00:00:00 2001 From: scjtqs Date: Sat, 3 Oct 2020 17:54:15 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix=20web=E8=BE=93=E5=85=A5api=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/apiAdmin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/apiAdmin.go b/server/apiAdmin.go index c136391..6d44638 100644 --- a/server/apiAdmin.go +++ b/server/apiAdmin.go @@ -109,7 +109,7 @@ func (s *webServer) Dologin() { img, _, _ := image.Decode(bytes.NewReader(rsp.CaptchaImage)) fmt.Println(asciiart.New("image", img).Art) if conf.WebUi.WebInput { - log.Warnf("请输入验证码 (captcha.jpg): (http://%s:%d/admin/web_write 输入)", conf.WebUi.Host, conf.WebUi.WebUiPort) + 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 提交)") @@ -121,7 +121,7 @@ func (s *webServer) Dologin() { case client.UnsafeDeviceError: log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证并重启Bot.", rsp.VerifyUrl) if conf.WebUi.WebInput { - log.Infof(" (http://%s:%d/admin/web_write 确认后继续)....", conf.WebUi.Host, conf.WebUi.WebUiPort) + log.Infof(" (http://%s:%d/admin/do_web_write 确认后继续)....", conf.WebUi.Host, conf.WebUi.WebUiPort) text = <-WebInput } else { log.Infof(" 按 Enter 继续....") From 4d404eacd9f4a3fc38dc68ddb2fd483ed279a60b Mon Sep 17 00:00:00 2001 From: scjtqs Date: Sun, 4 Oct 2020 20:01:59 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E8=8E=B7=E5=8F=96=20?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E5=8F=82=E6=95=B0=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=90=8D=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=BD=93=E5=89=8Dadmin=20api=E7=9A=84=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/adminApi.md | 232 +++++++++++++++++++++++++++++++++++++++++++++ server/apiAdmin.go | 2 +- 2 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 docs/adminApi.md diff --git a/docs/adminApi.md b/docs/adminApi.md new file mode 100644 index 0000000..68dde26 --- /dev/null +++ b/docs/adminApi.md @@ -0,0 +1,232 @@ +# 管理 API + +> 支持跨域 + +## 公共参数 + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ----------- | +| access_token | string | 校验口令,config.json中配置 | + + + +## admin/do_restart + +### 热重启 + +> 热重启 + +> ps: 目前不支持ws部分的修改生效 + +method:`POST/GET` + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ---- | ------------------------------------- | +| 无||| + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + + +### admin/get_web_write + +> 拉取验证码/设备锁 + +method: `GET` + + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ----------- | +| 无||| + +返回: + +```json +{"data": {"ispic": true,"picbase64":"xxxxx"}, "retcode": 0, "status": "ok"} +``` +| 参数名 | 类型 | 说明 | +| ------ | ------ | ----------- | +| ispic| bool| 是否是验证码类型 true是,false为不是(比如设备锁| +|picbas64| string| 验证码的base64编码内容,加上头,放入img标签即可显示| + +### admin/do_web_write + +> web输入验证码/设备锁确认 + +method: `POST` formdata + + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ----------- | +| input | string | 输入的类容 | + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + + +### admin/do_restart_docker + +> 冷重启 + +> 注意:此api 会直接结束掉进程,需要依赖docker/supervisor等进程管理工具来自动拉起 + +method: `POST` + + +参数: + +| 参数名 |类型 | 说明 | +| ------ | ------ | -----------| +| 无 | | | + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + +### admin/do_config_base + +> 基础配置 + +method: `POST` formdata + + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| uin | string | qq号 | +| password | string | qq密码 | +| enable_db | string | 是否启动数据库,填 'true' 或者 'false' | +| access_token | string | 授权 token | + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + + +### admin/do_config_http + +> http服务配置 + +method: `POST` formdata + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| port | string | 服务端口 | +| host | string | 服务监听地址 | +| enable | string | 是否启用 ,填 'true' 或者 'false' | +| timeout | string | http请求超时时间 | +| post_url | string | post上报地址 不需要就填空字符串,或者不填| +| post_secret | string | post上报的secret 不需要就填空字符串,或者不填 | + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + + +### admin/do_config_ws + +> 正向ws设置 + +method: `POST` formdata + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| port | string | 服务端口 | +| host | string | 服务监听地址 | +| enable | string | 是否启用 ,填 'true' 或者 'false' | + + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + +### admin/do_config_reverse + +> 反向ws配置 + +method: `POST` formdata + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| port | string | 服务端口 | +| host | string | 服务监听地址 | +| enable | string | 是否启用 ,填 'true' 或者 'false' | + + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + +### admin/do_config_json + +> 直接修改 config.json配置 + +method: `POST` formdata + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| json | string | 完整的config.json的配合,json字符串 | + + +返回: + +```json +{"data": {}, "retcode": 0, "status": "ok"} +``` + +### admin/get_config_json + +> 获取当前 config.json配置 + +method: `GET` + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| 无 | | | + + +返回: + +```json +{"data": {"config":"xxxx"}, "retcode": 0, "status": "ok"} +``` + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| config | string | 完整的config.json的配合,json字符串 | + diff --git a/server/apiAdmin.go b/server/apiAdmin.go index 6d44638..f80ae8f 100644 --- a/server/apiAdmin.go +++ b/server/apiAdmin.go @@ -50,7 +50,7 @@ var HttpuriAdmin = map[string]func(s *webServer, c *gin.Context){ "do_config_ws": AdminDoConfigWs, //修改config.json的正向ws部分 "do_config_reverse": AdminDoConfigReverse, //修改config.json 中的反向ws部分 "do_config_json": AdminDoConfigJson, //直接修改 config.json配置 - "get_config_json": AdminDoConfigJson, //拉取 当前的config.json配置 + "get_config_json": AdminGetConfigJson, //拉取 当前的config.json配置 } func Failed(code int, msg string) coolq.MSG {