From e3f0dbc4acd0f0fc00675042dd51d687e9a8a3c6 Mon Sep 17 00:00:00 2001 From: scjtqs Date: Sat, 3 Oct 2020 17:28:21 +0800 Subject: [PATCH 1/3] =?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 ad83d97c..7ede3285 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/3] =?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 71ecaafb..e380e996 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 15f10ab3..46ce32dd 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 7ede3285..c136391f 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/3] =?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 c136391f..6d446382 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 继续....")