base_auth.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_pub/models"
  5. "eta/eta_pub/services/alarm_msg"
  6. "eta/eta_pub/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/server/web"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. )
  13. type BaseAuthController struct {
  14. web.Controller
  15. }
  16. func (this *BaseAuthController) Prepare() {
  17. fmt.Println("enter prepare")
  18. method := this.Ctx.Input.Method()
  19. uri := this.Ctx.Input.URI()
  20. fmt.Println("Url:", uri)
  21. if method != "HEAD" {
  22. if method == "POST" || method == "GET" {
  23. authorization := this.Ctx.Input.Header("authorization")
  24. if authorization == "" {
  25. authorization = this.Ctx.Input.Header("Authorization")
  26. }
  27. if authorization == "" {
  28. this.JSON(models.BaseResponse{Ret: 408, Msg: "签名为空!", ErrMsg: "签名错误,请重新授权:Token is empty or account is empty"}, false, false)
  29. this.StopRun()
  30. return
  31. }
  32. if authorization != utils.Authorization {
  33. this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误,请重新授权!", ErrMsg: "签名错误,请重新授权:Token is empty or account is empty"}, false, false)
  34. this.StopRun()
  35. return
  36. }
  37. } else {
  38. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  39. this.StopRun()
  40. return
  41. }
  42. }
  43. }
  44. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  45. var (
  46. hasIndent = false
  47. hasEncoding = false
  48. )
  49. if web.BConfig.RunMode == web.PROD {
  50. hasIndent = false
  51. }
  52. if len(encoding) > 0 && encoding[0] == true {
  53. hasEncoding = true
  54. }
  55. if c.Data["json"] == nil {
  56. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  57. return
  58. }
  59. baseRes := c.Data["json"].(*models.BaseResponse)
  60. if baseRes != nil && baseRes.Ret != 408 {
  61. body, _ := json.Marshal(baseRes)
  62. var requestBody string
  63. method := c.Ctx.Input.Method()
  64. if method == "GET" {
  65. requestBody = c.Ctx.Request.RequestURI
  66. } else {
  67. //requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  68. requestBody = string(c.Ctx.Input.RequestBody)
  69. }
  70. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  71. msg := "URI:" + c.Ctx.Input.URI() + "<br/> " + "Params" + requestBody + " <br/>" + "ErrMsg:" + baseRes.ErrMsg + ";<br/>Msg:" + baseRes.Msg + ";<br/> Body:" + string(body) + "<br/>"
  72. go alarm_msg.SendAlarmMsg(msg, 3)
  73. }
  74. }
  75. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  76. }
  77. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  78. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  79. var content []byte
  80. var err error
  81. if hasIndent {
  82. content, err = json.MarshalIndent(data, "", " ")
  83. } else {
  84. content, err = json.Marshal(data)
  85. }
  86. if err != nil {
  87. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  88. return err
  89. }
  90. ip := c.Ctx.Input.IP()
  91. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  92. if err != nil {
  93. requestBody = string(c.Ctx.Input.RequestBody)
  94. }
  95. if requestBody == "" {
  96. requestBody = c.Ctx.Input.URI()
  97. }
  98. c.logUri(content, requestBody, ip)
  99. if coding {
  100. content = []byte(utils.StringsToJSON(string(content)))
  101. }
  102. return c.Ctx.Output.Body(content)
  103. }
  104. func (c *BaseAuthController) logUri(content []byte, requestBody, ip string) {
  105. authorization := ""
  106. method := c.Ctx.Input.Method()
  107. uri := c.Ctx.Input.URI()
  108. fmt.Println("Url:", uri)
  109. if method != "HEAD" {
  110. if method == "POST" || method == "GET" {
  111. authorization = c.Ctx.Input.Header("authorization")
  112. if authorization == "" {
  113. authorization = c.Ctx.Input.Header("Authorization")
  114. }
  115. if authorization == "" {
  116. newAuthorization := c.GetString("authorization")
  117. if newAuthorization != `` {
  118. authorization = "authorization=" + newAuthorization
  119. } else {
  120. newAuthorization = c.GetString("Authorization")
  121. authorization = "authorization=" + newAuthorization
  122. }
  123. } else {
  124. if strings.Contains(authorization, ";") {
  125. authorization = strings.Replace(authorization, ";", "$", 1)
  126. }
  127. }
  128. if authorization == "" {
  129. strArr := strings.Split(uri, "?")
  130. for k, v := range strArr {
  131. fmt.Println(k, v)
  132. }
  133. if len(strArr) > 1 {
  134. authorization = strArr[1]
  135. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  136. fmt.Println(authorization)
  137. }
  138. }
  139. }
  140. }
  141. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  142. return
  143. }