metadata.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package blademaster
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "go-common/library/conf/env"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. // http head
  13. _httpHeaderUser = "x1-bilispy-user"
  14. _httpHeaderColor = "x1-bilispy-color"
  15. _httpHeaderTimeout = "x1-bilispy-timeout"
  16. _httpHeaderRemoteIP = "x-backend-bili-real-ip"
  17. _httpHeaderRemoteIPPort = "x-backend-bili-real-ipport"
  18. )
  19. // mirror return true if x1-bilispy-mirror in http header and its value is 1 or true.
  20. func mirror(req *http.Request) bool {
  21. mirrorStr := req.Header.Get("x1-bilispy-mirror")
  22. if mirrorStr == "" {
  23. return false
  24. }
  25. val, err := strconv.ParseBool(mirrorStr)
  26. if err != nil {
  27. log.Warn("blademaster: failed to parse mirror: %+v", errors.Wrap(err, mirrorStr))
  28. return false
  29. }
  30. if !val {
  31. log.Warn("blademaster: request mirrorStr value :%s is false", mirrorStr)
  32. }
  33. return val
  34. }
  35. // setCaller set caller into http request.
  36. func setCaller(req *http.Request) {
  37. req.Header.Set(_httpHeaderUser, env.AppID)
  38. }
  39. // caller get caller from http request.
  40. func caller(req *http.Request) string {
  41. return req.Header.Get(_httpHeaderUser)
  42. }
  43. // setColor set color into http request.
  44. func setColor(req *http.Request, color string) {
  45. req.Header.Set(_httpHeaderColor, color)
  46. }
  47. // color get color from http request.
  48. func color(req *http.Request) string {
  49. c := req.Header.Get(_httpHeaderColor)
  50. if c == "" {
  51. c = env.Color
  52. }
  53. return c
  54. }
  55. // setTimeout set timeout into http request.
  56. func setTimeout(req *http.Request, timeout time.Duration) {
  57. td := int64(timeout / time.Millisecond)
  58. req.Header.Set(_httpHeaderTimeout, strconv.FormatInt(td, 10))
  59. }
  60. // timeout get timeout from http request.
  61. func timeout(req *http.Request) time.Duration {
  62. to := req.Header.Get(_httpHeaderTimeout)
  63. timeout, err := strconv.ParseInt(to, 10, 64)
  64. if err == nil && timeout > 20 {
  65. timeout -= 20 // reduce 20ms every time.
  66. }
  67. return time.Duration(timeout) * time.Millisecond
  68. }
  69. // remoteIP implements a best effort algorithm to return the real client IP, it parses
  70. // X-BACKEND-BILI-REAL-IP or X-Real-IP or X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
  71. // Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
  72. func remoteIP(req *http.Request) (remote string) {
  73. if remote = req.Header.Get(_httpHeaderRemoteIP); remote != "" && remote != "null" {
  74. return
  75. }
  76. var xff = req.Header.Get("X-Forwarded-For")
  77. if idx := strings.IndexByte(xff, ','); idx > -1 {
  78. if remote = strings.TrimSpace(xff[:idx]); remote != "" {
  79. return
  80. }
  81. }
  82. if remote = req.Header.Get("X-Real-IP"); remote != "" {
  83. return
  84. }
  85. remote = req.RemoteAddr[:strings.Index(req.RemoteAddr, ":")]
  86. return
  87. }
  88. func remotePort(req *http.Request) (port string) {
  89. if port = req.Header.Get(_httpHeaderRemoteIPPort); port != "" && port != "null" {
  90. return
  91. }
  92. return
  93. }