base.go 5.2 KB

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