1
0

base.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package controllers
  2. import (
  3. "github.com/cnlh/nps/lib/common"
  4. "github.com/cnlh/nps/lib/crypt"
  5. "github.com/cnlh/nps/lib/file"
  6. "github.com/cnlh/nps/server"
  7. "github.com/cnlh/nps/vender/github.com/astaxie/beego"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type BaseController struct {
  13. beego.Controller
  14. controllerName string
  15. actionName string
  16. }
  17. //初始化参数
  18. func (s *BaseController) Prepare() {
  19. controllerName, actionName := s.GetControllerAndAction()
  20. s.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
  21. s.actionName = strings.ToLower(actionName)
  22. // web api verify
  23. // param 1 is md5(authKey+Current timestamp)
  24. // param 2 is timestamp (It's limited to 20 seconds.)
  25. md5Key := s.GetString("auth_key")
  26. timestamp := s.GetIntNoErr("timestamp")
  27. configKey := beego.AppConfig.String("auth_key")
  28. timeNowUnix := time.Now().Unix()
  29. if !(((timeNowUnix - int64(timestamp)) <= 20) && ((timeNowUnix - int64(timestamp)) >= -20) && (crypt.Md5(configKey+strconv.Itoa(timestamp)) == md5Key)) {
  30. if s.GetSession("auth") != true {
  31. s.Redirect("/login/index", 302)
  32. }
  33. }
  34. if s.GetSession("isAdmin") != nil && !s.GetSession("isAdmin").(bool) {
  35. s.Ctx.Input.SetData("client_id", s.GetSession("clientId").(int))
  36. s.Ctx.Input.SetParam("client_id", strconv.Itoa(s.GetSession("clientId").(int)))
  37. s.Data["isAdmin"] = false
  38. s.CheckUserAuth()
  39. } else {
  40. s.Data["isAdmin"] = true
  41. }
  42. }
  43. //加载模板
  44. func (s *BaseController) display(tpl ...string) {
  45. var tplname string
  46. if s.Data["menu"] == nil {
  47. s.Data["menu"] = s.actionName
  48. }
  49. if len(tpl) > 0 {
  50. tplname = strings.Join([]string{tpl[0], "html"}, ".")
  51. } else {
  52. tplname = s.controllerName + "/" + s.actionName + ".html"
  53. }
  54. ip := s.Ctx.Request.Host
  55. s.Data["ip"] = common.GetIpByAddr(ip)
  56. s.Data["bridgeType"] = beego.AppConfig.String("bridge_type")
  57. if common.IsWindows() {
  58. s.Data["win"] = ".exe"
  59. }
  60. s.Data["p"] = server.Bridge.TunnelPort
  61. s.Data["proxyPort"] = beego.AppConfig.String("hostPort")
  62. s.Layout = "public/layout.html"
  63. s.TplName = tplname
  64. }
  65. //错误
  66. func (s *BaseController) error() {
  67. s.Layout = "public/layout.html"
  68. s.TplName = "public/error.html"
  69. }
  70. //去掉没有err返回值的int
  71. func (s *BaseController) GetIntNoErr(key string, def ...int) int {
  72. strv := s.Ctx.Input.Query(key)
  73. if len(strv) == 0 && len(def) > 0 {
  74. return def[0]
  75. }
  76. val, _ := strconv.Atoi(strv)
  77. return val
  78. }
  79. //获取去掉错误的bool值
  80. func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
  81. strv := s.Ctx.Input.Query(key)
  82. if len(strv) == 0 && len(def) > 0 {
  83. return def[0]
  84. }
  85. val, _ := strconv.ParseBool(strv)
  86. return val
  87. }
  88. //ajax正确返回
  89. func (s *BaseController) AjaxOk(str string) {
  90. s.Data["json"] = ajax(str, 1)
  91. s.ServeJSON()
  92. s.StopRun()
  93. }
  94. //ajax错误返回
  95. func (s *BaseController) AjaxErr(str string) {
  96. s.Data["json"] = ajax(str, 0)
  97. s.ServeJSON()
  98. s.StopRun()
  99. }
  100. //组装ajax
  101. func ajax(str string, status int) map[string]interface{} {
  102. json := make(map[string]interface{})
  103. json["status"] = status
  104. json["msg"] = str
  105. return json
  106. }
  107. //ajax table返回
  108. func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int) {
  109. json := make(map[string]interface{})
  110. json["rows"] = list
  111. json["total"] = recordsTotal
  112. s.Data["json"] = json
  113. s.ServeJSON()
  114. s.StopRun()
  115. }
  116. //ajax table参数
  117. func (s *BaseController) GetAjaxParams() (start, limit int) {
  118. return s.GetIntNoErr("offset"), s.GetIntNoErr("limit")
  119. }
  120. func (s *BaseController) SetInfo(name string) {
  121. s.Data["name"] = name
  122. }
  123. func (s *BaseController) SetType(name string) {
  124. s.Data["type"] = name
  125. }
  126. func (s *BaseController) CheckUserAuth() {
  127. if s.controllerName == "client" {
  128. s.StopRun()
  129. }
  130. if s.controllerName == "index" {
  131. if id := s.GetIntNoErr("id"); id != 0 {
  132. belong := false
  133. if strings.Contains(s.actionName, "h") {
  134. if v, ok := file.GetCsvDb().Hosts.Load(id); ok {
  135. if v.(*file.Host).Client.Id == s.GetSession("clientId").(int) {
  136. belong = true
  137. }
  138. }
  139. } else {
  140. if v, ok := file.GetCsvDb().Tasks.Load(id); ok {
  141. if v.(*file.Tunnel).Client.Id == s.GetSession("clientId").(int) {
  142. belong = true
  143. }
  144. }
  145. }
  146. if !belong {
  147. s.StopRun()
  148. }
  149. }
  150. }
  151. }