proxy.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package service
  2. import (
  3. "net"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. )
  8. const (
  9. FallbackModeNone = "none"
  10. FallbackModeProxy = "proxy"
  11. FallbackModeDirect = "direct"
  12. )
  13. type Proxy struct {
  14. ID int64
  15. Name string
  16. Protocol string
  17. Host string
  18. Port int
  19. Username string
  20. Password string
  21. Status string
  22. CreatedAt time.Time
  23. UpdatedAt time.Time
  24. ExpiresAt *time.Time
  25. FallbackMode string
  26. BackupProxyID *int64
  27. ExpiryWarnDays int
  28. }
  29. func (p *Proxy) IsActive() bool {
  30. return p.Status == StatusActive
  31. }
  32. // IsExpired 报告代理是否已过期(基于 expires_at,与 status 无关)。
  33. func (p *Proxy) IsExpired(now time.Time) bool {
  34. return p.ExpiresAt != nil && !p.ExpiresAt.After(now)
  35. }
  36. func (p *Proxy) URL() string {
  37. u := &url.URL{
  38. Scheme: p.Protocol,
  39. Host: net.JoinHostPort(p.Host, strconv.Itoa(p.Port)),
  40. }
  41. switch {
  42. case p.Username != "" && p.Password != "":
  43. u.User = url.UserPassword(p.Username, p.Password)
  44. case p.Username != "":
  45. // Preserve credentials when a proxy only requires a username.
  46. u.User = url.User(p.Username)
  47. case p.Password != "":
  48. // A few proxy providers use an empty username with a password.
  49. u.User = url.UserPassword("", p.Password)
  50. }
  51. return u.String()
  52. }
  53. type ProxyWithAccountCount struct {
  54. Proxy
  55. AccountCount int64
  56. LatencyMs *int64
  57. LatencyStatus string
  58. LatencyMessage string
  59. IPAddress string
  60. Country string
  61. CountryCode string
  62. Region string
  63. City string
  64. QualityStatus string
  65. QualityScore *int
  66. QualityGrade string
  67. QualitySummary string
  68. QualityChecked *int64
  69. }
  70. type ProxyAccountSummary struct {
  71. ID int64
  72. Name string
  73. Platform string
  74. Type string
  75. Notes *string
  76. }