From f0c6eff23a2c73eb78618d6e2f30274affa9151c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B2=B3?= Date: Tue, 29 Jan 2019 11:20:39 +0800 Subject: [PATCH] Web api --- conf/app.conf | 3 +++ web/controllers/base.go | 7 +++++-- web/controllers/client.go | 23 ++++++++++++++++++----- web/controllers/index.go | 27 ++++++++++++++++++++++++++- web/views/client/add.html | 2 +- web/views/client/edit.html | 2 +- web/views/client/list.html | 2 +- web/views/public/layout.html | 2 +- 8 files changed, 56 insertions(+), 12 deletions(-) diff --git a/conf/app.conf b/conf/app.conf index 1f53dd5..1066958 100755 --- a/conf/app.conf +++ b/conf/app.conf @@ -14,3 +14,6 @@ hostPort=80 ##客户端与服务端通信端口 tcpport=8284 + +#web api免验证IP地址 +authip=127.0.0.1 diff --git a/web/controllers/base.go b/web/controllers/base.go index 7140f20..f4c5097 100755 --- a/web/controllers/base.go +++ b/web/controllers/base.go @@ -19,8 +19,11 @@ func (s *BaseController) Prepare() { controllerName, actionName := s.GetControllerAndAction() s.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10]) s.actionName = strings.ToLower(actionName) - if s.GetSession("auth") != true { - s.Redirect("/login/index", 302) + arr := strings.Split(s.Ctx.Request.RemoteAddr, ":") + if len(arr) > 0 && arr[0] != beego.AppConfig.String("authip") { + if s.GetSession("auth") != true { + s.Redirect("/login/index", 302) + } } } diff --git a/web/controllers/client.go b/web/controllers/client.go index ec8c381..935e126 100644 --- a/web/controllers/client.go +++ b/web/controllers/client.go @@ -9,7 +9,7 @@ type ClientController struct { BaseController } -func (s *ClientController) Client() { +func (s *ClientController) List() { if s.Ctx.Request.Method == "GET" { s.Data["menu"] = "client" s.SetInfo("客户端管理") @@ -32,7 +32,7 @@ func (s *ClientController) Add() { VerifyKey: utils.GetRandomString(16), Id: server.CsvDb.GetClientId(), Status: true, - Remark: s.GetString("Remark"), + Remark: s.GetString("remark"), Cnf: &utils.Config{ U: s.GetString("u"), P: s.GetString("p"), @@ -55,12 +55,26 @@ func (s *ClientController) Add() { s.AjaxOk("添加成功") } } +func (s *ClientController) GetClient() { + if s.Ctx.Request.Method == "POST" { + id := s.GetIntNoErr("id") + data := make(map[string]interface{}) + if c, err := server.CsvDb.GetClient(id); err != nil { + data["code"] = 0 + } else { + data["code"] = 1 + data["data"] = c + } + s.Data["json"] = data + s.ServeJSON() + } +} //修改客户端 func (s *ClientController) Edit() { + id := s.GetIntNoErr("id") if s.Ctx.Request.Method == "GET" { s.Data["menu"] = "client" - id := s.GetIntNoErr("id") if c, err := server.CsvDb.GetClient(id); err != nil { s.error() } else { @@ -69,11 +83,10 @@ func (s *ClientController) Edit() { s.SetInfo("修改") s.display() } else { - id := s.GetIntNoErr("Id") if c, err := server.CsvDb.GetClient(id); err != nil { s.error() } else { - c.Remark = s.GetString("Remark") + c.Remark = s.GetString("remark") c.Cnf.U = s.GetString("u") c.Cnf.P = s.GetString("p") c.Cnf.Compress = s.GetString("compress") diff --git a/web/controllers/index.go b/web/controllers/index.go index d1babe1..ec52003 100755 --- a/web/controllers/index.go +++ b/web/controllers/index.go @@ -101,7 +101,18 @@ func (s *IndexController) Add() { } } } - +func (s *IndexController) GetOneTunnel() { + id := s.GetIntNoErr("id") + data := make(map[string]interface{}) + if t, err := server.CsvDb.GetTask(id); err != nil { + data["code"] = 0 + } else { + data["code"] = 1 + data["data"] = t + } + s.Data["json"] = data + s.ServeJSON() +} func (s *IndexController) Edit() { id := s.GetIntNoErr("id") if s.Ctx.Request.Method == "GET" { @@ -175,6 +186,20 @@ func (s *IndexController) HostList() { } } +func (s *IndexController) GetHost() { + if s.Ctx.Request.Method == "POST" { + data := make(map[string]interface{}) + if h, err := server.GetInfoByHost(s.GetString("host")); err != nil { + data["code"] = 0 + } else { + data["data"] = h + data["code"] = 1 + } + s.Data["json"] = data + s.ServeJSON() + } +} + func (s *IndexController) DelHost() { host := s.GetString("host") if err := server.CsvDb.DelHost(host); err != nil { diff --git a/web/views/client/add.html b/web/views/client/add.html index 0871ff9..f567d93 100755 --- a/web/views/client/add.html +++ b/web/views/client/add.html @@ -6,7 +6,7 @@
- +
diff --git a/web/views/client/edit.html b/web/views/client/edit.html index a3ca7f4..87a7008 100755 --- a/web/views/client/edit.html +++ b/web/views/client/edit.html @@ -7,7 +7,7 @@
- +
diff --git a/web/views/client/list.html b/web/views/client/list.html index 5face39..f1964b3 100755 --- a/web/views/client/list.html +++ b/web/views/client/list.html @@ -114,7 +114,7 @@ autoWidth: false, ordering: false, ajax: { - url: '/client/client', + url: '/client/list', type: 'POST' }, dom: '<"top"fl><"toolbar">rt<"bottom"ip><"clear">', diff --git a/web/views/public/layout.html b/web/views/public/layout.html index b45c1c6..e6fc123 100755 --- a/web/views/public/layout.html +++ b/web/views/public/layout.html @@ -42,7 +42,7 @@