ForwardCtrl.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. forwardJob := Service.SysDataS.GetForwardJob(entity)
  193. if forwardJob != nil && forwardJob.Status == Constant.RunStatus_Running {
  194. //正在转发中
  195. continue
  196. }
  197. config := Service.SysDataS.ToForwardConfig(entity)
  198. go Service.ForWardServ.OpenForward(config, resultChan)
  199. fmt.Println(<-resultChan)
  200. }
  201. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  202. c.ServeJSON()
  203. }
  204. // @router /u/CloseAllForward [get,post]
  205. func (c *ForwardCtrl) CloseAllForward() {
  206. //forwards := Service.SysDataS.GetAllPortForwardList(1)
  207. //for _, entity := range forwards {
  208. // config := Service.SysDataS.ToForwardConfig(entity)
  209. // Service.ForWardServ.CloseForward(config)
  210. //}
  211. Service.ForWardServ.CloseAllForward()
  212. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  213. c.ServeJSON()
  214. }
  215. // @router /u/ApiDoc [get]
  216. func (c *ForwardCtrl) ApiDoc() {
  217. c.TplName = "ucenter/apiDoc.html"
  218. }
  219. // @router /u/NetAgent [get]
  220. func (c *ForwardCtrl) NetAgent() {
  221. magicAddr := beego.AppConfig.DefaultString("magic.service", ":7000")
  222. c.Data["magicAddr"] = magicAddr
  223. agentForward := Service.MagicServ.ForwardInfo
  224. if agentForward == nil {
  225. agentForward = new(Models.PortForward)
  226. agentForward.Addr = ""
  227. agentForward.Port = 3307
  228. agentForward.Protocol = "TCP"
  229. agentForward.TargetAddr = "127.0.0.1"
  230. agentForward.TargetPort = 3306
  231. agentForward.FType = 2
  232. }
  233. c.Data["agentForward"] = agentForward
  234. c.TplName = "ucenter/netAgent.html"
  235. }
  236. // @router /u/OpenMagicService [post]
  237. func (c *ForwardCtrl) OpenMagicService() {
  238. addr := beego.AppConfig.DefaultString("magic.service", ":7000")
  239. resultChan := make(chan Models.FuncResult)
  240. go Service.MagicServ.StartMagicService(addr, resultChan)
  241. c.Data["json"] = <-resultChan
  242. //c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  243. c.ServeJSON()
  244. }
  245. // @router /u/CloseMagicService [post]
  246. func (c *ForwardCtrl) CloseMagicService() {
  247. resultChan := make(chan Models.FuncResult)
  248. go Service.MagicServ.StopMagicService(resultChan)
  249. c.Data["json"] = <-resultChan
  250. c.ServeJSON()
  251. }
  252. // @router /u/GetMagicStatus [post]
  253. func (c *ForwardCtrl) GetMagicStatus() {
  254. magicListener := Service.MagicServ.GetMagicListener()
  255. if magicListener == nil {
  256. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "未运行"}
  257. } else {
  258. c.Data["json"] = Models.FuncResult{Code: 0, Msg: "正在运行中..."}
  259. }
  260. c.ServeJSON()
  261. }
  262. // @router /u/GetNetAgentStatus [post]
  263. func (c *ForwardCtrl) GetNetAgentStatus() {
  264. agentMap := Service.MagicServ.GetMagicClient()
  265. if len(agentMap) > 0 {
  266. count := len(agentMap)
  267. for k, _ := range agentMap {
  268. c.Data["json"] = Models.FuncResult{Code: 0, Msg: k, Data: count}
  269. //只取1个先
  270. break
  271. }
  272. } else {
  273. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "未检测到Agent连接"}
  274. }
  275. c.ServeJSON()
  276. }
  277. // @router /u/ClearNetAgentStatus [post]
  278. func (c *ForwardCtrl) ClearNetAgentStatus() {
  279. agentMap := Service.MagicServ.GetMagicClient()
  280. if len(agentMap) > 0 {
  281. for k, v := range agentMap {
  282. if v != nil {
  283. v.Close()
  284. Service.MagicServ.UnRegistryMagicClient(k)
  285. logs.Debug("关闭Agent:", k)
  286. }
  287. }
  288. }
  289. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  290. c.ServeJSON()
  291. }
  292. // @router /u/StartAgentJob [post]
  293. func (c *ForwardCtrl) StartAgentJob() {
  294. lAddr := c.GetString("lAddr", "")
  295. protocol := c.GetString("protocol", "TCP")
  296. targetAddr := c.GetString("targetAddr", "")
  297. fType, _ := c.GetInt("fType")
  298. portForward := new(Models.PortForward)
  299. portForward.Addr = Utils.Split(lAddr, ":")[0]
  300. portForward.Port = Utils.ToInt(Utils.Split(lAddr, ":")[1])
  301. portForward.Protocol = protocol
  302. portForward.TargetAddr = Utils.Split(targetAddr, ":")[0]
  303. portForward.TargetPort = Utils.ToInt(Utils.Split(targetAddr, ":")[1])
  304. portForward.FType = fType
  305. resultChan := make(chan Models.FuncResult)
  306. go Service.MagicServ.StartMagicForward(portForward, resultChan)
  307. c.Data["json"] = <-resultChan
  308. c.ServeJSON()
  309. }
  310. // @router /u/StopAgentJob [post]
  311. func (c *ForwardCtrl) StopAgentJob() {
  312. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  313. c.ServeJSON()
  314. }
  315. // @router /u/ChangeForwardDebug [get,post]
  316. func (c *ForwardCtrl) ChangeForwardDebug() {
  317. id, _ := c.GetInt("status")
  318. Service.ForWardDebug = Utils.If(id == 1, true, false).(bool)
  319. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  320. c.ServeJSON()
  321. }
  322. // @router /u/AddBatchForward [get]
  323. func (c *ForwardCtrl) AddBatchForward() {
  324. c.TplName = "ucenter/addBatchForward.html"
  325. }
  326. // @router /u/SaveBatchForward [post]
  327. func (c *ForwardCtrl) SaveBatchForward() {
  328. rows, _ := c.GetInt("rows")
  329. var entities []*Models.PortForward
  330. for i := 0; i < rows; i++ {
  331. name := c.GetString(fmt.Sprint("name[", i, "]"), "-")
  332. port, _ := c.GetInt(fmt.Sprint("port[", i, "]"))
  333. protocol := c.GetString(fmt.Sprint("protocol[", i, "]"), "TCP")
  334. targetAddr := c.GetString(fmt.Sprint("targetAddr[", i, "]"), "")
  335. targetPort, _ := c.GetInt(fmt.Sprint("targetPort[", i, "]"))
  336. if port < 0 || port > 65535 {
  337. //
  338. c.Data["json"] = Models.FuncResult{Code: 1, Msg: fmt.Sprint("监听端口 不在允许的范围 ", port)}
  339. c.ServeJSON()
  340. return
  341. }
  342. if targetPort < 0 || targetPort > 65535 {
  343. //
  344. c.Data["json"] = Models.FuncResult{Code: 1, Msg: fmt.Sprint("目标端口 不在允许的范围 ", targetPort)}
  345. c.ServeJSON()
  346. return
  347. }
  348. if Utils.IsEmpty(targetAddr) {
  349. //
  350. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "目标地址 不能为空"}
  351. c.ServeJSON()
  352. return
  353. }
  354. name = Utils.FilterHtml(name)
  355. entity := &Models.PortForward{}
  356. entity.Id = 0
  357. entity.Name = name
  358. entity.Addr = ""
  359. entity.Port = port
  360. entity.Protocol = protocol
  361. entity.TargetAddr = targetAddr
  362. entity.TargetPort = targetPort
  363. entity.Others = ""
  364. entity.FType = 0
  365. entity.Status = 1
  366. entities = append(entities, entity)
  367. }
  368. for _, entity := range entities {
  369. err := Service.SysDataS.SavePortForward(entity)
  370. if err != nil {
  371. logs.Error("SaveForward ", err.Error())
  372. }
  373. }
  374. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  375. c.ServeJSON()
  376. }
  377. // @router /u/ImportForward [get]
  378. func (c *ForwardCtrl) ImportForward() {
  379. c.TplName = "ucenter/importForward.html"
  380. }
  381. // @router /u/SaveImportForward [post]
  382. func (c *ForwardCtrl) SaveImportForward() {
  383. splitChar := c.GetString("splitChar", ",")
  384. inputDatas := c.GetString("inputDatas", "")
  385. if Utils.IsEmpty(inputDatas) {
  386. //
  387. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "导入的数据不能为空"}
  388. c.ServeJSON()
  389. return
  390. }
  391. dataRows := Utils.Split(inputDatas, "\n")
  392. var entities []*Models.PortForward
  393. for _, rowContent := range dataRows {
  394. if Utils.IsEmpty(rowContent) {
  395. continue
  396. }
  397. //名称,本地监听地址,本地监听端口,协议类型,目标地址,目标端口
  398. rowDatas := Utils.Split(rowContent, splitChar)
  399. if len(rowDatas) < 6 {
  400. continue
  401. }
  402. name := rowDatas[0]
  403. name = Utils.FilterHtml(name)
  404. if Utils.IsEmpty(name) {
  405. name = "-"
  406. }
  407. addr := rowDatas[1]
  408. port := Utils.ToInt(rowDatas[2])
  409. protocol := Utils.If(rowDatas[3] == "TCP", "TCP", "UDP").(string)
  410. targetAddr := rowDatas[4]
  411. targetPort := Utils.ToInt(rowDatas[5])
  412. if Utils.IsEmpty(targetAddr) {
  413. //
  414. c.Data["json"] = Models.FuncResult{Code: 1, Msg: "目标地址 不能为空"}
  415. c.ServeJSON()
  416. return
  417. }
  418. if port < 0 || port > 65535 {
  419. //
  420. c.Data["json"] = Models.FuncResult{Code: 1, Msg: fmt.Sprint("监听端口 不在允许的范围 ", port)}
  421. c.ServeJSON()
  422. return
  423. }
  424. if targetPort < 0 || targetPort > 65535 {
  425. //
  426. c.Data["json"] = Models.FuncResult{Code: 1, Msg: fmt.Sprint("目标端口 不在允许的范围 ", targetPort)}
  427. c.ServeJSON()
  428. return
  429. }
  430. entity := &Models.PortForward{}
  431. entity.Id = 0
  432. entity.Name = name
  433. entity.Addr = addr
  434. entity.Port = port
  435. entity.Protocol = protocol
  436. entity.TargetAddr = targetAddr
  437. entity.TargetPort = targetPort
  438. entity.Others = ""
  439. entity.FType = 0
  440. entity.Status = 1
  441. entities = append(entities, entity)
  442. }
  443. for _, entity := range entities {
  444. err := Service.SysDataS.SavePortForward(entity)
  445. if err != nil {
  446. logs.Error("SaveForward ", err.Error())
  447. }
  448. }
  449. c.Data["json"] = Models.FuncResult{Code: 0, Msg: ""}
  450. c.ServeJSON()
  451. }
粤ICP备19079148号