ForwardCtrl.go 8.6 KB

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