base.go 5.6 KB

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