2019-01-24 20:10:12 -08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2019-02-03 01:25:00 -08:00
|
|
|
"github.com/cnlh/nps/lib"
|
2019-02-05 08:35:23 -08:00
|
|
|
"github.com/cnlh/nps/server"
|
2019-01-24 20:10:12 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClientController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2019-01-28 19:20:39 -08:00
|
|
|
func (s *ClientController) List() {
|
2019-01-24 20:10:12 -08:00
|
|
|
if s.Ctx.Request.Method == "GET" {
|
|
|
|
s.Data["menu"] = "client"
|
|
|
|
s.SetInfo("客户端管理")
|
|
|
|
s.display("client/list")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
start, length := s.GetAjaxParams()
|
|
|
|
list, cnt := server.GetClientList(start, length)
|
|
|
|
s.AjaxTable(list, cnt, cnt)
|
|
|
|
}
|
|
|
|
|
|
|
|
//添加客户端
|
|
|
|
func (s *ClientController) Add() {
|
|
|
|
if s.Ctx.Request.Method == "GET" {
|
|
|
|
s.Data["menu"] = "client"
|
|
|
|
s.SetInfo("新增")
|
|
|
|
s.display()
|
|
|
|
} else {
|
2019-02-03 01:25:00 -08:00
|
|
|
t := &lib.Client{
|
|
|
|
VerifyKey: lib.GetRandomString(16),
|
2019-02-05 08:35:23 -08:00
|
|
|
Id: lib.GetCsvDb().GetClientId(),
|
2019-01-24 20:10:12 -08:00
|
|
|
Status: true,
|
2019-01-28 19:20:39 -08:00
|
|
|
Remark: s.GetString("remark"),
|
2019-02-03 01:25:00 -08:00
|
|
|
Cnf: &lib.Config{
|
2019-01-24 20:10:12 -08:00
|
|
|
U: s.GetString("u"),
|
|
|
|
P: s.GetString("p"),
|
|
|
|
Compress: s.GetString("compress"),
|
|
|
|
Crypt: s.GetBoolNoErr("crypt"),
|
|
|
|
},
|
2019-01-27 22:45:55 -08:00
|
|
|
RateLimit: s.GetIntNoErr("rate_limit"),
|
2019-02-03 01:25:00 -08:00
|
|
|
Flow: &lib.Flow{
|
2019-01-27 22:45:55 -08:00
|
|
|
ExportFlow: 0,
|
|
|
|
InletFlow: 0,
|
|
|
|
FlowLimit: int64(s.GetIntNoErr("flow_limit")),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if t.RateLimit > 0 {
|
2019-02-03 01:25:00 -08:00
|
|
|
t.Rate = lib.NewRate(int64(t.RateLimit * 1024))
|
2019-01-27 22:45:55 -08:00
|
|
|
t.Rate.Start()
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
2019-02-05 08:35:23 -08:00
|
|
|
lib.GetCsvDb().NewClient(t)
|
2019-01-24 20:10:12 -08:00
|
|
|
s.AjaxOk("添加成功")
|
|
|
|
}
|
|
|
|
}
|
2019-01-28 19:20:39 -08:00
|
|
|
func (s *ClientController) GetClient() {
|
|
|
|
if s.Ctx.Request.Method == "POST" {
|
|
|
|
id := s.GetIntNoErr("id")
|
|
|
|
data := make(map[string]interface{})
|
2019-02-05 08:35:23 -08:00
|
|
|
if c, err := lib.GetCsvDb().GetClient(id); err != nil {
|
2019-01-28 19:20:39 -08:00
|
|
|
data["code"] = 0
|
|
|
|
} else {
|
|
|
|
data["code"] = 1
|
|
|
|
data["data"] = c
|
|
|
|
}
|
|
|
|
s.Data["json"] = data
|
|
|
|
s.ServeJSON()
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 20:10:12 -08:00
|
|
|
|
|
|
|
//修改客户端
|
|
|
|
func (s *ClientController) Edit() {
|
2019-01-28 19:20:39 -08:00
|
|
|
id := s.GetIntNoErr("id")
|
2019-01-24 20:10:12 -08:00
|
|
|
if s.Ctx.Request.Method == "GET" {
|
|
|
|
s.Data["menu"] = "client"
|
2019-02-05 08:35:23 -08:00
|
|
|
if c, err := lib.GetCsvDb().GetClient(id); err != nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
s.error()
|
|
|
|
} else {
|
|
|
|
s.Data["c"] = c
|
|
|
|
}
|
|
|
|
s.SetInfo("修改")
|
|
|
|
s.display()
|
|
|
|
} else {
|
2019-02-05 08:35:23 -08:00
|
|
|
if c, err := lib.GetCsvDb().GetClient(id); err != nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
s.error()
|
|
|
|
} else {
|
2019-01-28 19:20:39 -08:00
|
|
|
c.Remark = s.GetString("remark")
|
2019-01-24 20:10:12 -08:00
|
|
|
c.Cnf.U = s.GetString("u")
|
|
|
|
c.Cnf.P = s.GetString("p")
|
|
|
|
c.Cnf.Compress = s.GetString("compress")
|
|
|
|
c.Cnf.Crypt = s.GetBoolNoErr("crypt")
|
2019-01-27 22:45:55 -08:00
|
|
|
c.Flow.FlowLimit = int64(s.GetIntNoErr("flow_limit"))
|
|
|
|
c.RateLimit = s.GetIntNoErr("rate_limit")
|
|
|
|
if c.Rate != nil {
|
|
|
|
c.Rate.Stop()
|
|
|
|
}
|
|
|
|
if c.RateLimit > 0 {
|
2019-02-03 01:25:00 -08:00
|
|
|
c.Rate = lib.NewRate(int64(c.RateLimit * 1024))
|
2019-01-27 22:45:55 -08:00
|
|
|
c.Rate.Start()
|
|
|
|
} else {
|
|
|
|
c.Rate = nil
|
|
|
|
}
|
2019-02-05 08:35:23 -08:00
|
|
|
lib.GetCsvDb().UpdateClient(c)
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
s.AjaxOk("修改成功")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//更改状态
|
|
|
|
func (s *ClientController) ChangeStatus() {
|
|
|
|
id := s.GetIntNoErr("id")
|
2019-02-05 08:35:23 -08:00
|
|
|
if client, err := lib.GetCsvDb().GetClient(id); err == nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
client.Status = s.GetBoolNoErr("status")
|
|
|
|
if client.Status == false {
|
|
|
|
server.DelClientConnect(client.Id)
|
|
|
|
}
|
|
|
|
s.AjaxOk("修改成功")
|
|
|
|
}
|
|
|
|
s.AjaxErr("修改失败")
|
|
|
|
}
|
|
|
|
|
|
|
|
//删除客户端
|
|
|
|
func (s *ClientController) Del() {
|
|
|
|
id := s.GetIntNoErr("id")
|
2019-02-05 08:35:23 -08:00
|
|
|
if err := lib.GetCsvDb().DelClient(id); err != nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
s.AjaxErr("删除失败")
|
|
|
|
}
|
|
|
|
server.DelTunnelAndHostByClientId(id)
|
|
|
|
server.DelClientConnect(id)
|
|
|
|
s.AjaxOk("删除成功")
|
|
|
|
}
|