1
0

base.go 4.2 KB

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