proxy_info.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package common
  2. import (
  3. "encoding/xml"
  4. "git.aionnect.com/aionnect/go-common/utils"
  5. "git.aionnect.com/aionnect/go-common/utils/date"
  6. "git.aionnect.com/aionnect/go-common/utils/jsonutil"
  7. "net"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 类型
  13. const (
  14. HTTP = "HTTP"
  15. HTTPS = "HTTPS"
  16. SOCKS = "SOCKS"
  17. )
  18. // 匿名度
  19. const (
  20. High string = "高匿名"
  21. Normal string = "透明"
  22. )
  23. type ProxyInfo struct {
  24. IP string `json:"ip" xorm:"ip"`
  25. Port int `json:"port" xorm:"port"`
  26. Anonymity string `json:"anonymity" xorm:"anonymity"`
  27. Type string `json:"type" xorm:"type"`
  28. Location string `json:"location" xorm:"location"`
  29. Speed time.Duration `json:"speed" xorm:"speed"`
  30. UpdateTime date.Datetime `json:"updateTime" xorm:"update_time"`
  31. ID utils.Long `xorm:"'id' not null pk BIGINT(20)" json:"id"`
  32. UpdatedAt date.Datetime `xorm:"'update_at' updated not null TIMESTAMP" json:"updatedAt,omitempty"`
  33. CreatedAt date.Datetime `xorm:"'create_at' not null DATETIME" json:"createdAt,omitempty"`
  34. }
  35. func ConvertToIP(text string) string {
  36. ip := net.ParseIP(strings.TrimSpace(text))
  37. if ip.IsLoopback() || ip.IsUnspecified() {
  38. return ""
  39. }
  40. return ip.String()
  41. }
  42. func ConvertToSpeed(text string) time.Duration {
  43. text = strings.TrimSpace(text)
  44. if text == "" {
  45. return 0
  46. }
  47. text = strings.ReplaceAll(text, "秒", "")
  48. n, err := strconv.ParseFloat(text, 10)
  49. if nil != err {
  50. return 0
  51. }
  52. return time.Duration(n * float64(time.Second))
  53. }
  54. func (ProxyInfo) TableName() string {
  55. return "proxy_info"
  56. }
  57. func (that *ProxyInfo) UnmarshalJSON(value []byte) error { // 反序列化Json的时候,如果没有ID,自动给一个
  58. type Alias ProxyInfo
  59. alias := &struct {
  60. *Alias
  61. }{Alias: (*Alias)(that)}
  62. if err := jsonutil.Unmarshal(value, &alias); err != nil {
  63. return err
  64. }
  65. if nil != alias && alias.ID == 0 {
  66. alias.ID = utils.NextId()
  67. }
  68. return nil
  69. }
  70. func (that *ProxyInfo) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { // 反序列化XML的时候,如果没有ID,自动给一个
  71. type Alias ProxyInfo
  72. alias := &struct {
  73. *Alias
  74. }{Alias: (*Alias)(that)}
  75. if err := d.DecodeElement(&alias, &start); err != nil {
  76. return err
  77. }
  78. if nil != alias && alias.ID == 0 {
  79. alias.ID = utils.NextId()
  80. }
  81. return nil
  82. }
  83. // 分页参数
  84. type ProxyPagingParams struct {
  85. PagingNo int // 页码
  86. Limit int // 一次最多爬多少条(因为按分页爬,可能会稍微超出一点点)
  87. Count int // 本次已爬有效数据条数计数
  88. }