httputil.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package httputil
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. )
  9. var (
  10. cfRayPattern = regexp.MustCompile(`(?i)cf-ray[:\s=]+([a-z0-9-]+)`)
  11. cRayPattern = regexp.MustCompile(`(?i)cRay:\s*'([a-z0-9-]+)'`)
  12. htmlChallenge = []string{
  13. "window._cf_chl_opt",
  14. "just a moment",
  15. "enable javascript and cookies to continue",
  16. "__cf_chl_",
  17. "challenge-platform",
  18. }
  19. )
  20. // IsCloudflareChallengeResponse reports whether the upstream response matches Cloudflare challenge behavior.
  21. func IsCloudflareChallengeResponse(statusCode int, headers http.Header, body []byte) bool {
  22. if statusCode != http.StatusForbidden && statusCode != http.StatusTooManyRequests {
  23. return false
  24. }
  25. if headers != nil && strings.EqualFold(strings.TrimSpace(headers.Get("cf-mitigated")), "challenge") {
  26. return true
  27. }
  28. preview := strings.ToLower(TruncateBody(body, 4096))
  29. for _, marker := range htmlChallenge {
  30. if strings.Contains(preview, marker) {
  31. return true
  32. }
  33. }
  34. contentType := ""
  35. if headers != nil {
  36. contentType = strings.ToLower(strings.TrimSpace(headers.Get("content-type")))
  37. }
  38. if strings.Contains(contentType, "text/html") &&
  39. (strings.Contains(preview, "<html") || strings.Contains(preview, "<!doctype html")) &&
  40. (strings.Contains(preview, "cloudflare") || strings.Contains(preview, "challenge")) {
  41. return true
  42. }
  43. return false
  44. }
  45. // ExtractCloudflareRayID extracts cf-ray from headers or response body.
  46. func ExtractCloudflareRayID(headers http.Header, body []byte) string {
  47. if headers != nil {
  48. rayID := strings.TrimSpace(headers.Get("cf-ray"))
  49. if rayID != "" {
  50. return rayID
  51. }
  52. rayID = strings.TrimSpace(headers.Get("Cf-Ray"))
  53. if rayID != "" {
  54. return rayID
  55. }
  56. }
  57. preview := TruncateBody(body, 8192)
  58. if matches := cfRayPattern.FindStringSubmatch(preview); len(matches) >= 2 {
  59. return strings.TrimSpace(matches[1])
  60. }
  61. if matches := cRayPattern.FindStringSubmatch(preview); len(matches) >= 2 {
  62. return strings.TrimSpace(matches[1])
  63. }
  64. return ""
  65. }
  66. // FormatCloudflareChallengeMessage appends cf-ray info when available.
  67. func FormatCloudflareChallengeMessage(base string, headers http.Header, body []byte) string {
  68. rayID := ExtractCloudflareRayID(headers, body)
  69. if rayID == "" {
  70. return base
  71. }
  72. return fmt.Sprintf("%s (cf-ray: %s)", base, rayID)
  73. }
  74. // ExtractUpstreamErrorCodeAndMessage extracts structured error code/message from common JSON layouts.
  75. func ExtractUpstreamErrorCodeAndMessage(body []byte) (string, string) {
  76. trimmed := strings.TrimSpace(string(body))
  77. if trimmed == "" {
  78. return "", ""
  79. }
  80. if !json.Valid([]byte(trimmed)) {
  81. return "", truncateMessage(trimmed, 256)
  82. }
  83. var payload map[string]any
  84. if err := json.Unmarshal([]byte(trimmed), &payload); err != nil {
  85. return "", truncateMessage(trimmed, 256)
  86. }
  87. code := firstNonEmpty(
  88. extractNestedString(payload, "error", "code"),
  89. extractRootString(payload, "code"),
  90. )
  91. message := firstNonEmpty(
  92. extractNestedString(payload, "error", "message"),
  93. extractRootString(payload, "message"),
  94. extractNestedString(payload, "error", "detail"),
  95. extractRootString(payload, "detail"),
  96. )
  97. return strings.TrimSpace(code), truncateMessage(strings.TrimSpace(message), 512)
  98. }
  99. // TruncateBody truncates body text for logging/inspection.
  100. func TruncateBody(body []byte, max int) string {
  101. if max <= 0 {
  102. max = 512
  103. }
  104. raw := strings.TrimSpace(string(body))
  105. if len(raw) <= max {
  106. return raw
  107. }
  108. return raw[:max] + "...(truncated)"
  109. }
  110. func truncateMessage(s string, max int) string {
  111. if max <= 0 {
  112. return ""
  113. }
  114. if len(s) <= max {
  115. return s
  116. }
  117. return s[:max] + "...(truncated)"
  118. }
  119. func firstNonEmpty(values ...string) string {
  120. for _, v := range values {
  121. if strings.TrimSpace(v) != "" {
  122. return v
  123. }
  124. }
  125. return ""
  126. }
  127. func extractRootString(m map[string]any, key string) string {
  128. if m == nil {
  129. return ""
  130. }
  131. v, ok := m[key]
  132. if !ok {
  133. return ""
  134. }
  135. s, _ := v.(string)
  136. return s
  137. }
  138. func extractNestedString(m map[string]any, parent, key string) string {
  139. if m == nil {
  140. return ""
  141. }
  142. node, ok := m[parent]
  143. if !ok {
  144. return ""
  145. }
  146. child, ok := node.(map[string]any)
  147. if !ok {
  148. return ""
  149. }
  150. s, _ := child[key].(string)
  151. return s
  152. }