base.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. s.Data["https_just_proxy"], _ = beego.AppConfig.Bool("https_just_proxy")
  44. s.Data["allow_user_login"], _ = beego.AppConfig.Bool("allow_user_login")
  45. }
  46. //加载模板
  47. func (s *BaseController) display(tpl ...string) {
  48. var tplname string
  49. if s.Data["menu"] == nil {
  50. s.Data["menu"] = s.actionName
  51. }
  52. if len(tpl) > 0 {
  53. tplname = strings.Join([]string{tpl[0], "html"}, ".")
  54. } else {
  55. tplname = s.controllerName + "/" + s.actionName + ".html"
  56. }
  57. ip := s.Ctx.Request.Host
  58. s.Data["ip"] = common.GetIpByAddr(ip)
  59. s.Data["bridgeType"] = beego.AppConfig.String("bridge_type")
  60. if common.IsWindows() {
  61. s.Data["win"] = ".exe"
  62. }
  63. s.Data["p"] = server.Bridge.TunnelPort
  64. s.Data["proxyPort"] = beego.AppConfig.String("hostPort")
  65. s.Layout = "public/layout.html"
  66. s.TplName = tplname
  67. }
  68. //错误
  69. func (s *BaseController) error() {
  70. s.Layout = "public/layout.html"
  71. s.TplName = "public/error.html"
  72. }
  73. //去掉没有err返回值的int
  74. func (s *BaseController) GetIntNoErr(key string, def ...int) int {
  75. strv := s.Ctx.Input.Query(key)
  76. if len(strv) == 0 && len(def) > 0 {
  77. return def[0]
  78. }
  79. val, _ := strconv.Atoi(strv)
  80. return val
  81. }
  82. //获取去掉错误的bool值
  83. func (s *BaseController) GetBoolNoErr(key string, def ...bool) bool {
  84. strv := s.Ctx.Input.Query(key)
  85. if len(strv) == 0 && len(def) > 0 {
  86. return def[0]
  87. }
  88. val, _ := strconv.ParseBool(strv)
  89. return val
  90. }
  91. //ajax正确返回
  92. func (s *BaseController) AjaxOk(str string) {
  93. s.Data["json"] = ajax(str, 1)
  94. s.ServeJSON()
  95. s.StopRun()
  96. }
  97. //ajax错误返回
  98. func (s *BaseController) AjaxErr(str string) {
  99. s.Data["json"] = ajax(str, 0)
  100. s.ServeJSON()
  101. s.StopRun()
  102. }
  103. //组装ajax
  104. func ajax(str string, status int) map[string]interface{} {
  105. json := make(map[string]interface{})
  106. json["status"] = status
  107. json["msg"] = str
  108. return json
  109. }
  110. //ajax table返回
  111. func (s *BaseController) AjaxTable(list interface{}, cnt int, recordsTotal int) {
  112. json := make(map[string]interface{})
  113. json["rows"] = list
  114. json["total"] = recordsTotal
  115. s.Data["json"] = json
  116. s.ServeJSON()
  117. s.StopRun()
  118. }
  119. //ajax table参数
  120. func (s *BaseController) GetAjaxParams() (start, limit int) {
  121. return s.GetIntNoErr("offset"), s.GetIntNoErr("limit")
  122. }
  123. func (s *BaseController) SetInfo(name string) {
  124. s.Data["name"] = name
  125. }
  126. func (s *BaseController) SetType(name string) {
  127. s.Data["type"] = name
  128. }
  129. func (s *BaseController) CheckUserAuth() {
  130. if s.controllerName == "client" {
  131. if s.actionName == "add" {
  132. s.StopRun()
  133. return
  134. }
  135. if id := s.GetIntNoErr("id"); id != 0 {
  136. if id != s.GetSession("clientId").(int) {
  137. s.StopRun()
  138. return
  139. }
  140. }
  141. }
  142. if s.controllerName == "index" {
  143. if id := s.GetIntNoErr("id"); id != 0 {
  144. belong := false
  145. if strings.Contains(s.actionName, "h") {
  146. if v, ok := file.GetDb().JsonDb.Hosts.Load(id); ok {
  147. if v.(*file.Host).Client.Id == s.GetSession("clientId").(int) {
  148. belong = true
  149. }
  150. }
  151. } else {
  152. if v, ok := file.GetDb().JsonDb.Tasks.Load(id); ok {
  153. if v.(*file.Tunnel).Client.Id == s.GetSession("clientId").(int) {
  154. belong = true
  155. }
  156. }
  157. }
  158. if !belong {
  159. s.StopRun()
  160. }
  161. }
  162. }
  163. }