nps/web/controllers/client.go

135 lines
3.1 KiB
Go
Raw Normal View History

package controllers
import (
2019-02-09 01:07:47 -08:00
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/rate"
2019-02-05 08:35:23 -08:00
"github.com/cnlh/nps/server"
)
type ClientController struct {
BaseController
}
2019-01-28 19:20:39 -08:00
func (s *ClientController) List() {
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-09 01:07:47 -08:00
t := &file.Client{
VerifyKey: crypt.GetRandomString(16),
Id: file.GetCsvDb().GetClientId(),
Status: true,
2019-01-28 19:20:39 -08:00
Remark: s.GetString("remark"),
2019-02-09 01:07:47 -08:00
Cnf: &file.Config{
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-09 01:07:47 -08:00
Flow: &file.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-09 01:07:47 -08:00
t.Rate = rate.NewRate(int64(t.RateLimit * 1024))
2019-01-27 22:45:55 -08:00
t.Rate.Start()
}
2019-02-09 01:07:47 -08:00
file.GetCsvDb().NewClient(t)
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-09 01:07:47 -08:00
if c, err := file.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()
}
}
//修改客户端
func (s *ClientController) Edit() {
2019-01-28 19:20:39 -08:00
id := s.GetIntNoErr("id")
if s.Ctx.Request.Method == "GET" {
s.Data["menu"] = "client"
2019-02-09 01:07:47 -08:00
if c, err := file.GetCsvDb().GetClient(id); err != nil {
s.error()
} else {
s.Data["c"] = c
}
s.SetInfo("修改")
s.display()
} else {
2019-02-09 01:07:47 -08:00
if c, err := file.GetCsvDb().GetClient(id); err != nil {
s.error()
} else {
2019-01-28 19:20:39 -08:00
c.Remark = s.GetString("remark")
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-09 01:07:47 -08:00
c.Rate = rate.NewRate(int64(c.RateLimit * 1024))
2019-01-27 22:45:55 -08:00
c.Rate.Start()
} else {
c.Rate = nil
}
2019-02-15 06:59:28 -08:00
file.GetCsvDb().StoreClientsToCsv()
}
s.AjaxOk("修改成功")
}
}
//更改状态
func (s *ClientController) ChangeStatus() {
id := s.GetIntNoErr("id")
2019-02-09 01:07:47 -08:00
if client, err := file.GetCsvDb().GetClient(id); err == nil {
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-09 01:07:47 -08:00
if err := file.GetCsvDb().DelClient(id); err != nil {
s.AjaxErr("删除失败")
}
server.DelTunnelAndHostByClientId(id)
server.DelClientConnect(id)
s.AjaxOk("删除成功")
}