index.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package controllers
  2. import (
  3. "github.com/cnlh/easyProxy/server"
  4. "github.com/cnlh/easyProxy/utils"
  5. )
  6. type IndexController struct {
  7. BaseController
  8. }
  9. func (s *IndexController) Index() {
  10. s.Data["data"] = server.GetDashboardData()
  11. s.SetInfo("dashboard")
  12. s.display("index/index")
  13. }
  14. func (s *IndexController) Help() {
  15. s.SetInfo("使用说明")
  16. s.display("index/help")
  17. }
  18. func (s *IndexController) Tcp() {
  19. s.SetInfo("tcp隧道管理")
  20. s.SetType("tunnelServer")
  21. s.display("index/list")
  22. }
  23. func (s *IndexController) Udp() {
  24. s.SetInfo("udp隧道管理")
  25. s.SetType("udpServer")
  26. s.display("index/list")
  27. }
  28. func (s *IndexController) Socks5() {
  29. s.SetInfo("socks5管理")
  30. s.SetType("socks5Server")
  31. s.display("index/list")
  32. }
  33. func (s *IndexController) Http() {
  34. s.SetInfo("http代理管理")
  35. s.SetType("httpProxyServer")
  36. s.display("index/list")
  37. }
  38. func (s *IndexController) Host() {
  39. s.SetInfo("host模式管理")
  40. s.SetType("hostServer")
  41. s.display("index/list")
  42. }
  43. func (s *IndexController) All() {
  44. s.Data["menu"] = "client"
  45. clientId := s.GetString("client_id")
  46. s.Data["client_id"] = clientId
  47. s.SetInfo("客户端" + clientId + "的所有隧道")
  48. s.display("index/list")
  49. }
  50. func (s *IndexController) GetTunnel() {
  51. start, length := s.GetAjaxParams()
  52. taskType := s.GetString("type")
  53. clientId := s.GetIntNoErr("client_id")
  54. list, cnt := server.GetTunnel(start, length, taskType, clientId)
  55. s.AjaxTable(list, cnt, cnt)
  56. }
  57. func (s *IndexController) Add() {
  58. if s.Ctx.Request.Method == "GET" {
  59. s.Data["type"] = s.GetString("type")
  60. s.Data["client_id"] = s.GetString("client_id")
  61. s.SetInfo("新增")
  62. s.display()
  63. } else {
  64. t := &utils.Tunnel{
  65. TcpPort: s.GetIntNoErr("port"),
  66. Mode: s.GetString("type"),
  67. Target: s.GetString("target"),
  68. Config: &utils.Config{
  69. U: s.GetString("u"),
  70. P: s.GetString("p"),
  71. Compress: s.GetString("compress"),
  72. Crypt: s.GetBoolNoErr("crypt"),
  73. Mux: s.GetBoolNoErr("mux"),
  74. },
  75. Id: server.CsvDb.GetTaskId(),
  76. UseClientCnf: s.GetBoolNoErr("use_client"),
  77. Status: true,
  78. Remark: s.GetString("remark"),
  79. Flow: &utils.Flow{},
  80. }
  81. var err error
  82. if t.Client, err = server.CsvDb.GetClient(s.GetIntNoErr("client_id")); err != nil {
  83. s.AjaxErr(err.Error())
  84. }
  85. server.CsvDb.NewTask(t)
  86. if err := server.AddTask(t); err != nil {
  87. s.AjaxErr(err.Error())
  88. } else {
  89. s.AjaxOk("添加成功")
  90. }
  91. }
  92. }
  93. func (s *IndexController) Edit() {
  94. id := s.GetIntNoErr("id")
  95. if s.Ctx.Request.Method == "GET" {
  96. if t, err := server.CsvDb.GetTask(id); err != nil {
  97. s.error()
  98. } else {
  99. s.Data["t"] = t
  100. }
  101. s.SetInfo("修改")
  102. s.display()
  103. } else {
  104. if t, err := server.CsvDb.GetTask(id); err != nil {
  105. s.error()
  106. } else {
  107. t.TcpPort = s.GetIntNoErr("port")
  108. t.Mode = s.GetString("type")
  109. t.Target = s.GetString("target")
  110. t.Id = id
  111. t.Client.Id = s.GetIntNoErr("client_id")
  112. t.Config.U = s.GetString("u")
  113. t.Config.P = s.GetString("p")
  114. t.Config.Compress = s.GetString("compress")
  115. t.Config.Crypt = s.GetBoolNoErr("crypt")
  116. t.Config.Mux = s.GetBoolNoErr("mux")
  117. t.UseClientCnf = s.GetBoolNoErr("use_client")
  118. t.Remark = s.GetString("remark")
  119. if t.Client, err = server.CsvDb.GetClient(s.GetIntNoErr("client_id")); err != nil {
  120. s.AjaxErr("修改失败")
  121. }
  122. server.CsvDb.UpdateTask(t)
  123. }
  124. s.AjaxOk("修改成功")
  125. }
  126. }
  127. func (s *IndexController) Stop() {
  128. id := s.GetIntNoErr("id")
  129. if err := server.StopServer(id); err != nil {
  130. s.AjaxErr("停止失败")
  131. }
  132. s.AjaxOk("停止成功")
  133. }
  134. func (s *IndexController) Del() {
  135. id := s.GetIntNoErr("id")
  136. if err := server.DelTask(id); err != nil {
  137. s.AjaxErr("删除失败")
  138. }
  139. s.AjaxOk("删除成功")
  140. }
  141. func (s *IndexController) Start() {
  142. id := s.GetIntNoErr("id")
  143. if err := server.StartTask(id); err != nil {
  144. s.AjaxErr("开启失败")
  145. }
  146. s.AjaxOk("开启成功")
  147. }
  148. func (s *IndexController) HostList() {
  149. if s.Ctx.Request.Method == "GET" {
  150. s.Data["client_id"] = s.GetString("client_id")
  151. s.Data["menu"] = "host"
  152. s.SetInfo("域名列表")
  153. s.display("index/hlist")
  154. } else {
  155. start, length := s.GetAjaxParams()
  156. clientId := s.GetIntNoErr("client_id")
  157. list, cnt := server.CsvDb.GetHost(start, length, clientId)
  158. s.AjaxTable(list, cnt, cnt)
  159. }
  160. }
  161. func (s *IndexController) DelHost() {
  162. host := s.GetString("host")
  163. if err := server.CsvDb.DelHost(host); err != nil {
  164. s.AjaxErr("删除失败")
  165. }
  166. s.AjaxOk("删除成功")
  167. }
  168. func (s *IndexController) AddHost() {
  169. if s.Ctx.Request.Method == "GET" {
  170. s.Data["client_id"] = s.GetString("client_id")
  171. s.Data["menu"] = "host"
  172. s.SetInfo("新增")
  173. s.display("index/hadd")
  174. } else {
  175. h := &utils.Host{
  176. Host: s.GetString("host"),
  177. Target: s.GetString("target"),
  178. HeaderChange: s.GetString("header"),
  179. HostChange: s.GetString("hostchange"),
  180. Remark: s.GetString("remark"),
  181. Flow: &utils.Flow{},
  182. }
  183. var err error
  184. if h.Client, err = server.CsvDb.GetClient(s.GetIntNoErr("client_id")); err != nil {
  185. s.AjaxErr("添加失败")
  186. }
  187. server.CsvDb.NewHost(h)
  188. s.AjaxOk("添加成功")
  189. }
  190. }
  191. func (s *IndexController) EditHost() {
  192. host := s.GetString("host")
  193. if s.Ctx.Request.Method == "GET" {
  194. s.Data["menu"] = "host"
  195. if h, err := server.GetInfoByHost(host); err != nil {
  196. s.error()
  197. } else {
  198. s.Data["h"] = h
  199. }
  200. s.SetInfo("修改")
  201. s.display("index/hedit")
  202. } else {
  203. if h, err := server.GetInfoByHost(host); err != nil {
  204. s.error()
  205. } else {
  206. h.Host = s.GetString("nhost")
  207. h.Target = s.GetString("target")
  208. h.HeaderChange = s.GetString("header")
  209. h.HostChange = s.GetString("hostchange")
  210. h.Remark = s.GetString("remark")
  211. server.CsvDb.UpdateHost(h)
  212. var err error
  213. if h.Client, err = server.CsvDb.GetClient(s.GetIntNoErr("client_id")); err != nil {
  214. s.AjaxErr("修改失败")
  215. }
  216. }
  217. s.AjaxOk("修改成功")
  218. }
  219. }