1
0

base.go 5.4 KB

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