ForwardCtrl.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package Controllers
  2. import (
  3. "fmt"
  4. "forward-core/Constant"
  5. "forward-core/Models"
  6. "forward-core/Utils"
  7. "forward-server/Controllers/BaseCtrl"
  8. "forward-server/Service"
  9. "github.com/astaxie/beego"
  10. "github.com/astaxie/beego/logs"
  11. )
  12. type ForwardCtrl struct {
  13. BaseCtrl.ConsoleCtrl
  14. }
  15. // @router /u/ForwardList [get]
  16. func (c *ForwardCtrl) ForwardList() {
  17. c.TplName = "ucenter/forwardList.html"
  18. }
  19. // @router /u/ForwardList/json [post]
  20. func (c *ForwardCtrl) ForwardListJson() {
  21. pageParam := new(Models.PageParam)
  22. pageParam.PIndex, _ = c.GetInt64("pIndex")
  23. pageParam.PSize, _ = c.GetInt64("pSize")
  24. port, _ := c.GetInt("port")
  25. targetAddr := c.GetString("targetAddr", "")
  26. targetPort, _ := c.GetInt("targetPort")
  27. query := &Models.PortForward{}
  28. query.Port = port
  29. query.TargetAddr = targetAddr
  30. query.TargetPort = targetPort
  31. query.FType = -1
  32. pageData := Service.SysDataS.GetPortForwardList(query, pageParam.PIndex, pageParam.PSize)
  33. for _, entity := range pageData.Data.([]*Models.PortForward) {
  34. forwardJob := Service.SysDataS.GetForwardJob(entity)
  35. //entity.Status = Utils.If(forwardJob!=nil, int(forwardJob.Status), 0).(int)
  36. if forwardJob != nil {
  37. entity.Status = int(forwardJob.Status)
  38. } else {
  39. entity.Status = 0
  40. }
  41. }
  42. c.Data["json"] = Models.FuncResult{Code: 0, Msg: "success", Data: pageData}
  43. c.ServeJSON()
  44. }
  45. // @router /u/AddForward [get,post]
  46. func (c *ForwardCtrl) AddForward() {
  47. entity := Models.PortForward{}
  48. entity.Status = 1
  49. c.Data["entity"] = entity
  50. c.TplName = "ucenter/forwardForm.html"
  51. }
  52. // @router /u/EditForward [get,post]
  53. func (c *ForwardCtrl) EditForward() {
  54. id, _ := c.GetInt("id")
  55. entity := Service.SysDataS.GetPortForwardById(id)
  56. c.Data["entity"] = entity
  57. c.TplName = "ucenter/forwardForm.html"
  58. }
  59. // @router /u/DelForward [post]
  60. func (c *ForwardCtrl) DelForward() {
  61. ids := c.GetString("ids")
  62. var idArray []int
  63. for _, id := range Utils.Split(ids, ",") {
  64. _id := Utils.ToInt(id)
  65. //检查是否正在转发中
  66. entity := Service.SysDataS.GetPortForwardById(_id)
  67. forwardJob := Service.SysDataS.GetForwardJob(entity)
  68. if forwardJob != nil && forwardJob.Status == Constant.RunStatus_Running {
  69. c.Data["json"] = Models.FuncResult{Code: 1, Msg: fmt.Sprint("[", entity.Name, "] 正在转发中,不能删除")}
  70. c.ServeJSON()
  71. return
  72. } else {
  73. idArray = append(idArray, _id)
  74. }
  75. }
  76. err := Service.SysDataS.DelPortForwards(idArray)
  77. if err == nil {
  78. //
  79. c.Data["json"] = Models.FuncResult{Code: 0, Msg: "success"}
  80. } else {
  81. c.Data["json"] = Models.FuncResult{Code: 1, Msg: err.Error()}
  82. logs.Error("DelForward err:", err)
  83. }
  84. c.ServeJSON()
  85. }
  86. // @router /u/SaveForward [post]
  87. func (c *ForwardCtrl) SaveForward() {
  88. id, _ := c.GetInt("id")
  89. name := c.GetString("name", "")
  90. addr := c.GetString("addr", "")
  91. port, _ := c.GetInt("port")
  92. protocol := c.GetString("protocol", "TCP")
  93. targetAddr := c.GetString("targetAddr", "")
  94. targetPort, _ := c.GetInt("targetPort")
  95. others := c.GetString("others", "")
  96. fType, _ := c.GetInt("fType")
  97. status, _ := c.GetInt("status")
  98. if Utils.IsEmpty(name) {
  99. //
  100. //c.Data["json"] = Models.FuncResult{Code: 1, Msg: "名称 不能为空"}
  101. //c.ServeJSON()
  102. //return
  103. name = "-"
  104. }
  105. if port < 0 || port > 65535 {
  106. //
  107. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "监听端口 不在允许的范围"}
  108. c.ServeJSON()
  109. return
  110. }
  111. if Utils.IsEmpty(targetAddr) {
  112. //
  113. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "目标地址 不能为空"}
  114. c.ServeJSON()
  115. return
  116. }
  117. if targetPort < 0 || targetPort > 65535 {
  118. //
  119. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "目标端口 不在允许的范围"}
  120. c.ServeJSON()
  121. return
  122. }
  123. if status != 0 && status != 1 {
  124. //
  125. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "输入的 启用/禁用 值不正确"}
  126. c.ServeJSON()
  127. return
  128. }
  129. // if Utils.IsNotEmpty(others) {
  130. // //如果有others信息,则检查
  131. // }
  132. if fType > 0 {
  133. //内网穿透模式,暂不支持多端口分发
  134. others = ""
  135. }
  136. if id > 0 {
  137. entity := Service.SysDataS.GetPortForwardById(id)
  138. forwardJob := Service.SysDataS.GetForwardJob(entity)
  139. if forwardJob != nil && forwardJob.Status == Constant.RunStatus_Running {
  140. //正在转发中,修改前先关闭
  141. c.Data["json"] = Models.FuncResult{Code: 1, Msg: fmt.Sprint("[", entity.Name, "] 正在转发中,不能修改")}
  142. c.ServeJSON()
  143. return
  144. }
  145. }
  146. name = Utils.FilterHtml(name)
  147. entity := &Models.PortForward{}
  148. entity.Id = id
  149. entity.Name = name
  150. entity.Addr = addr
  151. entity.Port = port
  152. entity.Protocol = protocol
  153. entity.TargetAddr = targetAddr
  154. entity.TargetPort = targetPort
  155. entity.Others = others
  156. entity.FType = fType
  157. entity.Status = status
  158. err := Service.SysDataS.SavePortForward(entity)
  159. if err == nil {
  160. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  161. } else {
  162. logs.Error("SaveForward ", err.Error())
  163. c.Data["json"] = Models.FuncResult{Code: 1, Msg: err.Error()}
  164. }
  165. c.ServeJSON()
  166. }
  167. // @router /u/OpenForward [get,post]
  168. func (c *ForwardCtrl) OpenForward() {
  169. id, _ := c.GetInt("id")
  170. entity := Service.SysDataS.GetPortForwardById(id)
  171. resultChan := make(chan Models.FuncResult)
  172. config := Service.SysDataS.ToForwardConfig(entity)
  173. go Service.ForWardServ.OpenForward(config, resultChan)
  174. c.Data["json"] = <-resultChan
  175. c.ServeJSON()
  176. }
  177. // @router /u/CloseForward [get,post]
  178. func (c *ForwardCtrl) CloseForward() {
  179. id, _ := c.GetInt("id")
  180. entity := Service.SysDataS.GetPortForwardById(id)
  181. config := Service.SysDataS.ToForwardConfig(entity)
  182. Service.ForWardServ.CloseForward(config)
  183. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  184. c.ServeJSON()
  185. }
  186. // @router /u/OpenAllForward [get,post]
  187. func (c *ForwardCtrl) OpenAllForward() {
  188. forwards := Service.SysDataS.GetAllPortForwardList(1)
  189. for _, entity := range forwards {
  190. resultChan := make(chan Models.FuncResult)
  191. config := Service.SysDataS.ToForwardConfig(entity)
  192. go Service.ForWardServ.OpenForward(config, resultChan)
  193. fmt.Println(<-resultChan)
  194. }
  195. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  196. c.ServeJSON()
  197. }
  198. // @router /u/CloseAllForward [get,post]
  199. func (c *ForwardCtrl) CloseAllForward() {
  200. //forwards := Service.SysDataS.GetAllPortForwardList(1)
  201. //for _, entity := range forwards {
  202. // config := Service.SysDataS.ToForwardConfig(entity)
  203. // Service.ForWardServ.CloseForward(config)
  204. //}
  205. Service.ForWardServ.CloseAllForward()
  206. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  207. c.ServeJSON()
  208. }
  209. // @router /u/ApiDoc [get]
  210. func (c *ForwardCtrl) ApiDoc() {
  211. c.TplName = "ucenter/apiDoc.html"
  212. }
  213. // @router /u/NetAgent [get]
  214. func (c *ForwardCtrl) NetAgent() {
  215. magicAddr := beego.AppConfig.DefaultString("magic.service", ":7000")
  216. c.Data["magicAddr"] = magicAddr
  217. agentForward := Service.MagicServ.ForwardInfo
  218. if agentForward == nil {
  219. agentForward = new(Models.PortForward)
  220. agentForward.Addr = ""
  221. agentForward.Port = 3307
  222. agentForward.Protocol = "TCP"
  223. agentForward.TargetAddr = "127.0.0.1"
  224. agentForward.TargetPort = 3306
  225. agentForward.FType = 2
  226. }
  227. c.Data["agentForward"] = agentForward
  228. c.TplName = "ucenter/netAgent.html"
  229. }
  230. // @router /u/OpenMagicService [post]
  231. func (c *ForwardCtrl) OpenMagicService() {
  232. addr := beego.AppConfig.DefaultString("magic.service", ":7000")
  233. resultChan := make(chan Models.FuncResult)
  234. go Service.MagicServ.StartMagicService(addr, resultChan)
  235. c.Data["json"] = <-resultChan
  236. //c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  237. c.ServeJSON()
  238. }
  239. // @router /u/CloseMagicService [post]
  240. func (c *ForwardCtrl) CloseMagicService() {
  241. resultChan := make(chan Models.FuncResult)
  242. go Service.MagicServ.StopMagicService(resultChan)
  243. c.Data["json"] = <-resultChan
  244. c.ServeJSON()
  245. }
  246. // @router /u/GetMagicStatus [post]
  247. func (c *ForwardCtrl) GetMagicStatus() {
  248. magicListener := Service.MagicServ.GetMagicListener()
  249. if magicListener == nil {
  250. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "未运行"}
  251. } else {
  252. c.Data["json"] = Models.FuncResult{Code: 0, Msg: "正在运行中..."}
  253. }
  254. c.ServeJSON()
  255. }
  256. // @router /u/GetNetAgentStatus [post]
  257. func (c *ForwardCtrl) GetNetAgentStatus() {
  258. agentMap := Service.MagicServ.GetMagicClient()
  259. if len(agentMap) > 0 {
  260. count := len(agentMap)
  261. for k, _ := range agentMap {
  262. c.Data["json"] = Models.FuncResult{Code: 0, Msg: k, Data: count}
  263. //只取1个先
  264. break
  265. }
  266. } else {
  267. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "未检测到Agent连接"}
  268. }
  269. c.ServeJSON()
  270. }
  271. // @router /u/ClearNetAgentStatus [post]
  272. func (c *ForwardCtrl) ClearNetAgentStatus() {
  273. agentMap := Service.MagicServ.GetMagicClient()
  274. if len(agentMap) > 0 {
  275. for k, v := range agentMap {
  276. if v != nil {
  277. v.Close()
  278. Service.MagicServ.UnRegistryMagicClient(k)
  279. logs.Debug("关闭Agent:", k)
  280. }
  281. }
  282. }
  283. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  284. c.ServeJSON()
  285. }
  286. // @router /u/StartAgentJob [post]
  287. func (c *ForwardCtrl) StartAgentJob() {
  288. lAddr := c.GetString("lAddr", "")
  289. protocol := c.GetString("protocol", "TCP")
  290. targetAddr := c.GetString("targetAddr", "")
  291. fType, _ := c.GetInt("fType")
  292. portForward := new(Models.PortForward)
  293. portForward.Addr = Utils.Split(lAddr, ":")[0]
  294. portForward.Port = Utils.ToInt(Utils.Split(lAddr, ":")[1])
  295. portForward.Protocol = protocol
  296. portForward.TargetAddr = Utils.Split(targetAddr, ":")[0]
  297. portForward.TargetPort = Utils.ToInt(Utils.Split(targetAddr, ":")[1])
  298. portForward.FType = fType
  299. resultChan := make(chan Models.FuncResult)
  300. go Service.MagicServ.StartMagicForward(portForward, resultChan)
  301. c.Data["json"] = <-resultChan
  302. c.ServeJSON()
  303. }
  304. // @router /u/StopAgentJob [post]
  305. func (c *ForwardCtrl) StopAgentJob() {
  306. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  307. c.ServeJSON()
  308. }
粤ICP备19079148号