client.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package controllers
  2. import (
  3. "github.com/cnlh/nps/lib/common"
  4. "github.com/cnlh/nps/lib/file"
  5. "github.com/cnlh/nps/lib/rate"
  6. "github.com/cnlh/nps/server"
  7. )
  8. type ClientController struct {
  9. BaseController
  10. }
  11. func (s *ClientController) List() {
  12. if s.Ctx.Request.Method == "GET" {
  13. s.Data["menu"] = "client"
  14. s.SetInfo("client")
  15. s.display("client/list")
  16. return
  17. }
  18. start, length := s.GetAjaxParams()
  19. list, cnt := server.GetClientList(start, length, s.GetString("search"))
  20. s.AjaxTable(list, cnt, cnt)
  21. }
  22. //添加客户端
  23. func (s *ClientController) Add() {
  24. if s.Ctx.Request.Method == "GET" {
  25. s.Data["menu"] = "client"
  26. s.SetInfo("add client")
  27. s.display()
  28. } else {
  29. t := &file.Client{
  30. VerifyKey: s.GetString("vkey"),
  31. Id: int(file.GetCsvDb().GetClientId()),
  32. Status: true,
  33. Remark: s.GetString("remark"),
  34. Cnf: &file.Config{
  35. U: s.GetString("u"),
  36. P: s.GetString("p"),
  37. Compress: common.GetBoolByStr(s.GetString("compress")),
  38. Crypt: s.GetBoolNoErr("crypt"),
  39. },
  40. ConfigConnAllow: s.GetBoolNoErr("config_conn_allow"),
  41. RateLimit: s.GetIntNoErr("rate_limit"),
  42. MaxConn: s.GetIntNoErr("max_conn"),
  43. Flow: &file.Flow{
  44. ExportFlow: 0,
  45. InletFlow: 0,
  46. FlowLimit: int64(s.GetIntNoErr("flow_limit")),
  47. },
  48. }
  49. if t.RateLimit > 0 {
  50. t.Rate = rate.NewRate(int64(t.RateLimit * 1024))
  51. t.Rate.Start()
  52. }
  53. if err := file.GetCsvDb().NewClient(t); err != nil {
  54. s.AjaxErr(err.Error())
  55. }
  56. s.AjaxOk("add success")
  57. }
  58. }
  59. func (s *ClientController) GetClient() {
  60. if s.Ctx.Request.Method == "POST" {
  61. id := s.GetIntNoErr("id")
  62. data := make(map[string]interface{})
  63. if c, err := file.GetCsvDb().GetClient(id); err != nil {
  64. data["code"] = 0
  65. } else {
  66. data["code"] = 1
  67. data["data"] = c
  68. }
  69. s.Data["json"] = data
  70. s.ServeJSON()
  71. }
  72. }
  73. //修改客户端
  74. func (s *ClientController) Edit() {
  75. id := s.GetIntNoErr("id")
  76. if s.Ctx.Request.Method == "GET" {
  77. s.Data["menu"] = "client"
  78. if c, err := file.GetCsvDb().GetClient(id); err != nil {
  79. s.error()
  80. } else {
  81. s.Data["c"] = c
  82. }
  83. s.SetInfo("edit client")
  84. s.display()
  85. } else {
  86. if c, err := file.GetCsvDb().GetClient(id); err != nil {
  87. s.error()
  88. } else {
  89. if !file.GetCsvDb().VerifyVkey(s.GetString("vkey"), c.Id) {
  90. s.AjaxErr("Vkey duplicate, please reset")
  91. }
  92. c.VerifyKey = s.GetString("vkey")
  93. c.Remark = s.GetString("remark")
  94. c.Cnf.U = s.GetString("u")
  95. c.Cnf.P = s.GetString("p")
  96. c.Cnf.Compress = common.GetBoolByStr(s.GetString("compress"))
  97. c.Cnf.Crypt = s.GetBoolNoErr("crypt")
  98. c.Flow.FlowLimit = int64(s.GetIntNoErr("flow_limit"))
  99. c.RateLimit = s.GetIntNoErr("rate_limit")
  100. c.MaxConn = s.GetIntNoErr("max_conn")
  101. c.ConfigConnAllow = s.GetBoolNoErr("config_conn_allow")
  102. if c.Rate != nil {
  103. c.Rate.Stop()
  104. }
  105. if c.RateLimit > 0 {
  106. c.Rate = rate.NewRate(int64(c.RateLimit * 1024))
  107. c.Rate.Start()
  108. } else {
  109. c.Rate = nil
  110. }
  111. file.GetCsvDb().StoreClientsToCsv()
  112. }
  113. s.AjaxOk("save success")
  114. }
  115. }
  116. //更改状态
  117. func (s *ClientController) ChangeStatus() {
  118. id := s.GetIntNoErr("id")
  119. if client, err := file.GetCsvDb().GetClient(id); err == nil {
  120. client.Status = s.GetBoolNoErr("status")
  121. if client.Status == false {
  122. server.DelClientConnect(client.Id)
  123. }
  124. s.AjaxOk("modified success")
  125. }
  126. s.AjaxErr("modified fail")
  127. }
  128. //删除客户端
  129. func (s *ClientController) Del() {
  130. id := s.GetIntNoErr("id")
  131. if err := file.GetCsvDb().DelClient(id); err != nil {
  132. s.AjaxErr("delete error")
  133. }
  134. server.DelTunnelAndHostByClientId(id)
  135. server.DelClientConnect(id)
  136. s.AjaxOk("delete success")
  137. }