2019-01-24 20:10:12 -08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2019-08-09 20:15:25 -07:00
|
|
|
"github.com/astaxie/beego"
|
2019-03-01 01:23:14 -08:00
|
|
|
"github.com/cnlh/nps/lib/common"
|
2019-02-09 01:07:47 -08:00
|
|
|
"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"
|
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"
|
2019-03-01 01:23:14 -08:00
|
|
|
s.SetInfo("client")
|
2019-01-24 20:10:12 -08:00
|
|
|
s.display("client/list")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
start, length := s.GetAjaxParams()
|
2019-03-26 08:34:55 -07:00
|
|
|
clientIdSession := s.GetSession("clientId")
|
|
|
|
var clientId int
|
|
|
|
if clientIdSession == nil {
|
|
|
|
clientId = 0
|
|
|
|
} else {
|
|
|
|
clientId = clientIdSession.(int)
|
|
|
|
}
|
2019-04-10 05:54:51 -07:00
|
|
|
list, cnt := server.GetClientList(start, length, s.getEscapeString("search"), s.getEscapeString("sort"), s.getEscapeString("order"), clientId)
|
2019-01-24 20:10:12 -08:00
|
|
|
s.AjaxTable(list, cnt, cnt)
|
|
|
|
}
|
|
|
|
|
|
|
|
//添加客户端
|
|
|
|
func (s *ClientController) Add() {
|
|
|
|
if s.Ctx.Request.Method == "GET" {
|
|
|
|
s.Data["menu"] = "client"
|
2019-03-01 01:23:14 -08:00
|
|
|
s.SetInfo("add client")
|
2019-01-24 20:10:12 -08:00
|
|
|
s.display()
|
|
|
|
} else {
|
2019-02-09 01:07:47 -08:00
|
|
|
t := &file.Client{
|
2019-04-10 05:54:51 -07:00
|
|
|
VerifyKey: s.getEscapeString("vkey"),
|
2019-03-29 00:21:30 -07:00
|
|
|
Id: int(file.GetDb().JsonDb.GetClientId()),
|
2019-01-24 20:10:12 -08:00
|
|
|
Status: true,
|
2019-04-10 05:54:51 -07:00
|
|
|
Remark: s.getEscapeString("remark"),
|
2019-02-09 01:07:47 -08:00
|
|
|
Cnf: &file.Config{
|
2019-04-10 05:54:51 -07:00
|
|
|
U: s.getEscapeString("u"),
|
|
|
|
P: s.getEscapeString("p"),
|
|
|
|
Compress: common.GetBoolByStr(s.getEscapeString("compress")),
|
2019-01-24 20:10:12 -08:00
|
|
|
Crypt: s.GetBoolNoErr("crypt"),
|
|
|
|
},
|
2019-03-23 07:19:59 -07:00
|
|
|
ConfigConnAllow: s.GetBoolNoErr("config_conn_allow"),
|
|
|
|
RateLimit: s.GetIntNoErr("rate_limit"),
|
|
|
|
MaxConn: s.GetIntNoErr("max_conn"),
|
2019-04-10 05:54:51 -07:00
|
|
|
WebUserName: s.getEscapeString("web_username"),
|
|
|
|
WebPassword: s.getEscapeString("web_password"),
|
2019-04-08 02:01:08 -07:00
|
|
|
MaxTunnelNum: s.GetIntNoErr("max_tunnel"),
|
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-01-24 20:10:12 -08:00
|
|
|
}
|
2019-03-29 00:21:30 -07:00
|
|
|
if err := file.GetDb().NewClient(t); err != nil {
|
2019-02-23 21:17:43 -08:00
|
|
|
s.AjaxErr(err.Error())
|
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
s.AjaxOk("add success")
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
}
|
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-03-29 00:21:30 -07:00
|
|
|
if c, err := file.GetDb().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-03-29 00:21:30 -07:00
|
|
|
if c, err := file.GetDb().GetClient(id); err != nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
s.error()
|
|
|
|
} else {
|
|
|
|
s.Data["c"] = c
|
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
s.SetInfo("edit client")
|
2019-01-24 20:10:12 -08:00
|
|
|
s.display()
|
|
|
|
} else {
|
2019-03-29 00:21:30 -07:00
|
|
|
if c, err := file.GetDb().GetClient(id); err != nil {
|
2019-01-24 20:10:12 -08:00
|
|
|
s.error()
|
|
|
|
} else {
|
2019-04-10 05:54:51 -07:00
|
|
|
if s.getEscapeString("web_username") != "" {
|
|
|
|
if s.getEscapeString("web_username") == beego.AppConfig.String("web_username") || !file.GetDb().VerifyUserName(s.getEscapeString("web_username"), c.Id) {
|
2019-03-28 19:41:57 -07:00
|
|
|
s.AjaxErr("web login username duplicate, please reset")
|
|
|
|
return
|
|
|
|
}
|
2019-02-23 21:17:43 -08:00
|
|
|
}
|
2019-03-26 19:06:10 -07:00
|
|
|
if s.GetSession("isAdmin").(bool) {
|
2019-04-10 05:54:51 -07:00
|
|
|
if !file.GetDb().VerifyVkey(s.getEscapeString("vkey"), c.Id) {
|
2019-03-28 19:41:57 -07:00
|
|
|
s.AjaxErr("Vkey duplicate, please reset")
|
|
|
|
return
|
|
|
|
}
|
2019-04-10 05:54:51 -07:00
|
|
|
c.VerifyKey = s.getEscapeString("vkey")
|
2019-03-26 19:06:10 -07:00
|
|
|
c.Flow.FlowLimit = int64(s.GetIntNoErr("flow_limit"))
|
|
|
|
c.RateLimit = s.GetIntNoErr("rate_limit")
|
|
|
|
c.MaxConn = s.GetIntNoErr("max_conn")
|
2019-04-08 02:01:08 -07:00
|
|
|
c.MaxTunnelNum = s.GetIntNoErr("max_tunnel")
|
2019-03-26 19:06:10 -07:00
|
|
|
}
|
2019-04-10 05:54:51 -07:00
|
|
|
c.Remark = s.getEscapeString("remark")
|
|
|
|
c.Cnf.U = s.getEscapeString("u")
|
|
|
|
c.Cnf.P = s.getEscapeString("p")
|
|
|
|
c.Cnf.Compress = common.GetBoolByStr(s.getEscapeString("compress"))
|
2019-03-28 19:41:57 -07:00
|
|
|
c.Cnf.Crypt = s.GetBoolNoErr("crypt")
|
2019-04-08 02:01:08 -07:00
|
|
|
b, err := beego.AppConfig.Bool("allow_user_change_username")
|
|
|
|
if s.GetSession("isAdmin").(bool) || (err == nil && b) {
|
2019-04-10 05:54:51 -07:00
|
|
|
c.WebUserName = s.getEscapeString("web_username")
|
2019-04-08 02:01:08 -07:00
|
|
|
}
|
2019-04-10 05:54:51 -07:00
|
|
|
c.WebPassword = s.getEscapeString("web_password")
|
2019-03-23 07:19:59 -07:00
|
|
|
c.ConfigConnAllow = s.GetBoolNoErr("config_conn_allow")
|
2019-01-27 22:45:55 -08:00
|
|
|
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 {
|
2019-03-25 20:13:07 -07:00
|
|
|
c.Rate = rate.NewRate(int64(2 << 23))
|
|
|
|
c.Rate.Start()
|
2019-01-27 22:45:55 -08:00
|
|
|
}
|
2019-03-29 00:21:30 -07:00
|
|
|
file.GetDb().JsonDb.StoreClientsToJsonFile()
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
s.AjaxOk("save success")
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//更改状态
|
|
|
|
func (s *ClientController) ChangeStatus() {
|
|
|
|
id := s.GetIntNoErr("id")
|
2019-03-29 00:21:30 -07:00
|
|
|
if client, err := file.GetDb().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)
|
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
s.AjaxOk("modified success")
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
s.AjaxErr("modified fail")
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//删除客户端
|
|
|
|
func (s *ClientController) Del() {
|
|
|
|
id := s.GetIntNoErr("id")
|
2019-03-29 00:21:30 -07:00
|
|
|
if err := file.GetDb().DelClient(id); err != nil {
|
2019-03-01 01:23:14 -08:00
|
|
|
s.AjaxErr("delete error")
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|
2019-03-28 19:41:57 -07:00
|
|
|
server.DelTunnelAndHostByClientId(id, false)
|
2019-01-24 20:10:12 -08:00
|
|
|
server.DelClientConnect(id)
|
2019-03-01 01:23:14 -08:00
|
|
|
s.AjaxOk("delete success")
|
2019-01-24 20:10:12 -08:00
|
|
|
}
|