ForwardCtrl.go 11 KB

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