proxy.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package model
  2. import "time"
  3. // Proxy stores network proxy configurations for task execution.
  4. type Proxy struct {
  5. ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
  6. Name string `gorm:"size:100;not null" json:"name"`
  7. Protocol string `gorm:"size:20;not null;default:'http'" json:"protocol"` // http / https / socks5
  8. Host string `gorm:"size:255;not null" json:"host"`
  9. Port int `gorm:"not null" json:"port"`
  10. Username string `gorm:"size:100" json:"username"`
  11. Password string `gorm:"size:100" json:"password"`
  12. Region string `gorm:"size:50" json:"region"` // 地区标签,如 US / HK / JP
  13. Enabled bool `gorm:"default:true" json:"enabled"`
  14. Status string `gorm:"size:20;default:'unknown'" json:"status"` // unknown / ok / fail
  15. LastCheckedAt *time.Time `json:"last_checked_at"`
  16. Remark string `gorm:"size:500" json:"remark"`
  17. CreatedAt time.Time `json:"created_at"`
  18. UpdatedAt time.Time `json:"updated_at"`
  19. }
  20. func (Proxy) TableName() string { return "proxies" }
  21. // ProxyURL returns the full proxy URL string for HTTP clients.
  22. func (p Proxy) ProxyURL() string {
  23. auth := ""
  24. if p.Username != "" {
  25. auth = p.Username
  26. if p.Password != "" {
  27. auth += ":" + p.Password
  28. }
  29. auth += "@"
  30. }
  31. return p.Protocol + "://" + auth + p.Host + ":" + itoa(p.Port)
  32. }
  33. func itoa(n int) string {
  34. if n == 0 {
  35. return "0"
  36. }
  37. s := ""
  38. for n > 0 {
  39. s = string(rune('0'+n%10)) + s
  40. n /= 10
  41. }
  42. return s
  43. }