base.go 5.0 KB

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