base_auth.go 4.9 KB

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