index.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package controllers
  2. import (
  3. "github.com/cnlh/nps/lib/file"
  4. "github.com/cnlh/nps/server"
  5. "github.com/cnlh/nps/server/tool"
  6. )
  7. type IndexController struct {
  8. BaseController
  9. }
  10. func (s *IndexController) Index() {
  11. s.Data["data"] = server.GetDashboardData()
  12. s.SetInfo("dashboard")
  13. s.display("index/index")
  14. }
  15. func (s *IndexController) Help() {
  16. s.SetInfo("about")
  17. s.display("index/help")
  18. }
  19. func (s *IndexController) Tcp() {
  20. s.SetInfo("tcp")
  21. s.SetType("tcp")
  22. s.display("index/list")
  23. }
  24. func (s *IndexController) Udp() {
  25. s.SetInfo("udp")
  26. s.SetType("udp")
  27. s.display("index/list")
  28. }
  29. func (s *IndexController) Socks5() {
  30. s.SetInfo("socks5")
  31. s.SetType("socks5")
  32. s.display("index/list")
  33. }
  34. func (s *IndexController) Http() {
  35. s.SetInfo("http proxy")
  36. s.SetType("httpProxy")
  37. s.display("index/list")
  38. }
  39. func (s *IndexController) File() {
  40. s.SetInfo("file server")
  41. s.SetType("file")
  42. s.display("index/list")
  43. }
  44. func (s *IndexController) Secret() {
  45. s.SetInfo("secret")
  46. s.SetType("secret")
  47. s.display("index/list")
  48. }
  49. func (s *IndexController) P2p() {
  50. s.SetInfo("p2p")
  51. s.SetType("p2p")
  52. s.display("index/list")
  53. }
  54. func (s *IndexController) Host() {
  55. s.SetInfo("host")
  56. s.SetType("hostServer")
  57. s.display("index/list")
  58. }
  59. func (s *IndexController) All() {
  60. s.Data["menu"] = "client"
  61. clientId := s.getEscapeString("client_id")
  62. s.Data["client_id"] = clientId
  63. s.SetInfo("client id:" + clientId)
  64. s.display("index/list")
  65. }
  66. func (s *IndexController) GetTunnel() {
  67. start, length := s.GetAjaxParams()
  68. taskType := s.getEscapeString("type")
  69. clientId := s.GetIntNoErr("client_id")
  70. list, cnt := server.GetTunnel(start, length, taskType, clientId, s.getEscapeString("search"))
  71. s.AjaxTable(list, cnt, cnt)
  72. }
  73. func (s *IndexController) Add() {
  74. if s.Ctx.Request.Method == "GET" {
  75. s.Data["type"] = s.getEscapeString("type")
  76. s.Data["client_id"] = s.getEscapeString("client_id")
  77. s.SetInfo("add tunnel")
  78. s.display()
  79. } else {
  80. t := &file.Tunnel{
  81. Port: s.GetIntNoErr("port"),
  82. ServerIp: s.getEscapeString("server_ip"),
  83. Mode: s.getEscapeString("type"),
  84. Target: &file.Target{TargetStr: s.getEscapeString("target"), LocalProxy: s.GetBoolNoErr("local_proxy")},
  85. Id: int(file.GetDb().JsonDb.GetTaskId()),
  86. Status: true,
  87. Remark: s.getEscapeString("remark"),
  88. Password: s.getEscapeString("password"),
  89. LocalPath: s.getEscapeString("local_path"),
  90. StripPre: s.getEscapeString("strip_pre"),
  91. Flow: &file.Flow{},
  92. }
  93. if !tool.TestServerPort(t.Port, t.Mode) {
  94. s.AjaxErr("The port cannot be opened because it may has been occupied or is no longer allowed.")
  95. }
  96. var err error
  97. if t.Client, err = file.GetDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
  98. s.AjaxErr(err.Error())
  99. }
  100. if t.Client.MaxTunnelNum != 0 && t.Client.GetTunnelNum() >= t.Client.MaxTunnelNum {
  101. s.AjaxErr("The number of tunnels exceeds the limit")
  102. }
  103. if err := file.GetDb().NewTask(t); err != nil {
  104. s.AjaxErr(err.Error())
  105. }
  106. if err := server.AddTask(t); err != nil {
  107. s.AjaxErr(err.Error())
  108. } else {
  109. s.AjaxOk("add success")
  110. }
  111. }
  112. }
  113. func (s *IndexController) GetOneTunnel() {
  114. id := s.GetIntNoErr("id")
  115. data := make(map[string]interface{})
  116. if t, err := file.GetDb().GetTask(id); err != nil {
  117. data["code"] = 0
  118. } else {
  119. data["code"] = 1
  120. data["data"] = t
  121. }
  122. s.Data["json"] = data
  123. s.ServeJSON()
  124. }
  125. func (s *IndexController) Edit() {
  126. id := s.GetIntNoErr("id")
  127. if s.Ctx.Request.Method == "GET" {
  128. if t, err := file.GetDb().GetTask(id); err != nil {
  129. s.error()
  130. } else {
  131. s.Data["t"] = t
  132. }
  133. s.SetInfo("edit tunnel")
  134. s.display()
  135. } else {
  136. if t, err := file.GetDb().GetTask(id); err != nil {
  137. s.error()
  138. } else {
  139. if client, err := file.GetDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
  140. s.AjaxErr("modified error,the client is not exist")
  141. return
  142. } else {
  143. t.Client = client
  144. }
  145. if s.GetIntNoErr("port") != t.Port {
  146. if !tool.TestServerPort(s.GetIntNoErr("port"), t.Mode) {
  147. s.AjaxErr("The port cannot be opened because it may has been occupied or is no longer allowed.")
  148. return
  149. }
  150. t.Port = s.GetIntNoErr("port")
  151. }
  152. t.ServerIp = s.getEscapeString("server_ip")
  153. t.Mode = s.getEscapeString("type")
  154. t.Target = &file.Target{TargetStr: s.getEscapeString("target")}
  155. t.Password = s.getEscapeString("password")
  156. t.Id = id
  157. t.LocalPath = s.getEscapeString("local_path")
  158. t.StripPre = s.getEscapeString("strip_pre")
  159. t.Remark = s.getEscapeString("remark")
  160. t.Target.LocalProxy = s.GetBoolNoErr("local_proxy")
  161. file.GetDb().UpdateTask(t)
  162. server.StopServer(t.Id)
  163. server.StartTask(t.Id)
  164. }
  165. s.AjaxOk("modified success")
  166. }
  167. }
  168. func (s *IndexController) Stop() {
  169. id := s.GetIntNoErr("id")
  170. if err := server.StopServer(id); err != nil {
  171. s.AjaxErr("stop error")
  172. }
  173. s.AjaxOk("stop success")
  174. }
  175. func (s *IndexController) Del() {
  176. id := s.GetIntNoErr("id")
  177. if err := server.DelTask(id); err != nil {
  178. s.AjaxErr("delete error")
  179. }
  180. s.AjaxOk("delete success")
  181. }
  182. func (s *IndexController) Start() {
  183. id := s.GetIntNoErr("id")
  184. if err := server.StartTask(id); err != nil {
  185. s.AjaxErr("start error")
  186. }
  187. s.AjaxOk("start success")
  188. }
  189. func (s *IndexController) HostList() {
  190. if s.Ctx.Request.Method == "GET" {
  191. s.Data["client_id"] = s.getEscapeString("client_id")
  192. s.Data["menu"] = "host"
  193. s.SetInfo("host list")
  194. s.display("index/hlist")
  195. } else {
  196. start, length := s.GetAjaxParams()
  197. clientId := s.GetIntNoErr("client_id")
  198. list, cnt := file.GetDb().GetHost(start, length, clientId, s.getEscapeString("search"))
  199. s.AjaxTable(list, cnt, cnt)
  200. }
  201. }
  202. func (s *IndexController) GetHost() {
  203. if s.Ctx.Request.Method == "POST" {
  204. data := make(map[string]interface{})
  205. if h, err := file.GetDb().GetHostById(s.GetIntNoErr("id")); err != nil {
  206. data["code"] = 0
  207. } else {
  208. data["data"] = h
  209. data["code"] = 1
  210. }
  211. s.Data["json"] = data
  212. s.ServeJSON()
  213. }
  214. }
  215. func (s *IndexController) DelHost() {
  216. id := s.GetIntNoErr("id")
  217. if err := file.GetDb().DelHost(id); err != nil {
  218. s.AjaxErr("delete error")
  219. }
  220. s.AjaxOk("delete success")
  221. }
  222. func (s *IndexController) AddHost() {
  223. if s.Ctx.Request.Method == "GET" {
  224. s.Data["client_id"] = s.getEscapeString("client_id")
  225. s.Data["menu"] = "host"
  226. s.SetInfo("add host")
  227. s.display("index/hadd")
  228. } else {
  229. h := &file.Host{
  230. Id: int(file.GetDb().JsonDb.GetHostId()),
  231. Host: s.getEscapeString("host"),
  232. Target: &file.Target{TargetStr: s.getEscapeString("target"), LocalProxy: s.GetBoolNoErr("local_proxy")},
  233. HeaderChange: s.getEscapeString("header"),
  234. HostChange: s.getEscapeString("hostchange"),
  235. Remark: s.getEscapeString("remark"),
  236. Location: s.getEscapeString("location"),
  237. Flow: &file.Flow{},
  238. Scheme: s.getEscapeString("scheme"),
  239. KeyFilePath: s.getEscapeString("key_file_path"),
  240. CertFilePath: s.getEscapeString("cert_file_path"),
  241. }
  242. var err error
  243. if h.Client, err = file.GetDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
  244. s.AjaxErr("add error the client can not be found")
  245. }
  246. if err := file.GetDb().NewHost(h); err != nil {
  247. s.AjaxErr("add fail" + err.Error())
  248. }
  249. s.AjaxOk("add success")
  250. }
  251. }
  252. func (s *IndexController) EditHost() {
  253. id := s.GetIntNoErr("id")
  254. if s.Ctx.Request.Method == "GET" {
  255. s.Data["menu"] = "host"
  256. if h, err := file.GetDb().GetHostById(id); err != nil {
  257. s.error()
  258. } else {
  259. s.Data["h"] = h
  260. }
  261. s.SetInfo("edit")
  262. s.display("index/hedit")
  263. } else {
  264. if h, err := file.GetDb().GetHostById(id); err != nil {
  265. s.error()
  266. } else {
  267. if h.Host != s.getEscapeString("host") {
  268. tmpHost := new(file.Host)
  269. tmpHost.Host = s.getEscapeString("host")
  270. tmpHost.Location = s.getEscapeString("location")
  271. tmpHost.Scheme = s.getEscapeString("scheme")
  272. if file.GetDb().IsHostExist(tmpHost) {
  273. s.AjaxErr("host has exist")
  274. return
  275. }
  276. }
  277. if client, err := file.GetDb().GetClient(s.GetIntNoErr("client_id")); err != nil {
  278. s.AjaxErr("modified error,the client is not exist")
  279. } else {
  280. h.Client = client
  281. }
  282. h.Host = s.getEscapeString("host")
  283. h.Target = &file.Target{TargetStr: s.getEscapeString("target")}
  284. h.HeaderChange = s.getEscapeString("header")
  285. h.HostChange = s.getEscapeString("hostchange")
  286. h.Remark = s.getEscapeString("remark")
  287. h.Location = s.getEscapeString("location")
  288. h.Scheme = s.getEscapeString("scheme")
  289. h.KeyFilePath = s.getEscapeString("key_file_path")
  290. h.CertFilePath = s.getEscapeString("cert_file_path")
  291. h.Target.LocalProxy = s.GetBoolNoErr("local_proxy")
  292. file.GetDb().JsonDb.StoreHostToJsonFile()
  293. }
  294. s.AjaxOk("modified success")
  295. }
  296. }