base_auth.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_bridge/models"
  5. "eta/eta_mini_bridge/utils"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/beego/beego/v2/server/web"
  11. )
  12. type BaseAuthController struct {
  13. web.Controller
  14. Token string
  15. }
  16. func (this *BaseAuthController) Prepare() {
  17. method := this.Ctx.Input.Method()
  18. uri := this.Ctx.Input.URI()
  19. fmt.Println("Url:", uri)
  20. header := this.Ctx.Request.Header
  21. headerBody, err := json.Marshal(header)
  22. fmt.Println(err)
  23. fmt.Println("header:" + string(headerBody))
  24. if method != "HEAD" {
  25. //校验签名
  26. nonce := this.Ctx.Input.Header("Nonce")
  27. timestamp := this.Ctx.Input.Header("Timestamp")
  28. appid := this.Ctx.Input.Header("Appid")
  29. signature := this.Ctx.Input.Header("Signature")
  30. if nonce == "" {
  31. errMsg := "随机字符串不能为空"
  32. this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  33. this.StopRun()
  34. return
  35. }
  36. if timestamp == "" {
  37. errMsg := "时间戳不能为空"
  38. this.JSON(models.BaseResponse{Ret: 400, Msg: "", ErrMsg: errMsg}, false, false)
  39. this.StopRun()
  40. return
  41. }
  42. checkSign := utils.GetSign(nonce, timestamp, appid, utils.ETA_MINI_API_SECRET)
  43. if signature != checkSign {
  44. errMsg := "签名错误"
  45. this.JSON(models.BaseResponse{Ret: 401, Msg: "", ErrMsg: errMsg}, false, false)
  46. this.StopRun()
  47. return
  48. }
  49. if method != "GET" && method != "POST" {
  50. errMsg := "无效的请求方式"
  51. this.JSON(models.BaseResponse{Ret: 501, Msg: "", ErrMsg: errMsg}, false, false)
  52. this.StopRun()
  53. return
  54. }
  55. } else {
  56. this.JSON(models.BaseResponse{Ret: 500, Msg: "系统异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  57. this.StopRun()
  58. return
  59. }
  60. }
  61. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  62. //所有请求都做这么个处理吧,目前这边都是做编辑、刷新逻辑处理(新增的话,并没有指标id,不会有影响)
  63. var (
  64. hasIndent = false
  65. hasEncoding = false
  66. )
  67. if web.BConfig.RunMode == web.PROD {
  68. hasIndent = false
  69. }
  70. if len(encoding) > 0 && encoding[0] == true {
  71. hasEncoding = true
  72. }
  73. if c.Data["json"] == nil {
  74. utils.ApiLog.Info("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值")
  75. return
  76. }
  77. baseRes := c.Data["json"].(*models.BaseResponse)
  78. if baseRes != nil && baseRes.Ret != 408 {
  79. //body, _ := json.Marshal(baseRes)
  80. //var requestBody string
  81. //method := c.Ctx.Input.Method()
  82. //if method == "GET" {
  83. // requestBody = c.Ctx.Request.RequestURI
  84. //} else {
  85. // requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  86. //}
  87. //if baseRes.Ret != 200 && baseRes.IsSendEmail {
  88. // go utils.SendEmail(utils.APP_NAME_CN+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> "+"Params"+requestBody+" <br/>"+"ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body)+"<br/>", utils.EmailSendToUsers)
  89. //}
  90. // 记录错误日志, 并清掉错误信息避免暴露给外部
  91. if baseRes.ErrMsg != "" {
  92. utils.ApiLog.Info(baseRes.ErrMsg)
  93. baseRes.ErrMsg = ""
  94. }
  95. }
  96. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  97. }
  98. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  99. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  100. var content []byte
  101. var err error
  102. if hasIndent {
  103. content, err = json.MarshalIndent(data, "", " ")
  104. } else {
  105. content, err = json.Marshal(data)
  106. }
  107. if err != nil {
  108. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  109. return err
  110. }
  111. ip := c.Ctx.Input.IP()
  112. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  113. if err != nil {
  114. requestBody = string(c.Ctx.Input.RequestBody)
  115. }
  116. if requestBody == "" {
  117. requestBody = c.Ctx.Input.URI()
  118. }
  119. c.logUri(content, requestBody, ip)
  120. if coding {
  121. content = []byte(utils.StringsToJSON(string(content)))
  122. }
  123. return c.Ctx.Output.Body(content)
  124. }
  125. func (c *BaseAuthController) logUri(respContent []byte, requestBody, ip string) {
  126. authorization := ""
  127. method := c.Ctx.Input.Method()
  128. uri := c.Ctx.Input.URI()
  129. if method != "HEAD" {
  130. if method == "POST" || method == "GET" {
  131. authorization = c.Ctx.Input.Header("authorization")
  132. if authorization == "" {
  133. authorization = c.Ctx.Input.Header("Authorization")
  134. }
  135. if authorization == "" {
  136. newAuthorization := c.GetString("authorization")
  137. if newAuthorization != `` {
  138. authorization = "authorization=" + newAuthorization
  139. } else {
  140. newAuthorization = c.GetString("Authorization")
  141. authorization = "authorization=" + newAuthorization
  142. }
  143. } else {
  144. if strings.Contains(authorization, ";") {
  145. authorization = strings.Replace(authorization, ";", "$", 1)
  146. }
  147. }
  148. if authorization == "" {
  149. strArr := strings.Split(uri, "?")
  150. for k, v := range strArr {
  151. fmt.Println(k, v)
  152. }
  153. if len(strArr) > 1 {
  154. authorization = strArr[1]
  155. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  156. fmt.Println(authorization)
  157. }
  158. }
  159. }
  160. }
  161. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  162. return
  163. }