proxies.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package proxy
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. type ProxyList []Proxy
  7. func (ps ProxyList) Len() int {
  8. return len(ps)
  9. }
  10. func (ps ProxyList) TypeLen(t string) int {
  11. l := 0
  12. for _, p := range ps {
  13. if p.TypeName() == t {
  14. l++
  15. }
  16. }
  17. return l
  18. }
  19. func (ps ProxyList) Less(i, j int) bool {
  20. return ps[i].BaseInfo().Name < ps[j].BaseInfo().Name
  21. }
  22. func (ps ProxyList) Swap(i, j int) {
  23. ps[i], ps[j] = ps[j], ps[i]
  24. }
  25. func (ps ProxyList) Deduplication() ProxyList {
  26. result := make(ProxyList, 0, len(ps))
  27. temp := map[string]struct{}{}
  28. for _, item := range ps {
  29. if item != nil {
  30. if _, ok := temp[item.Identifier()]; !ok {
  31. temp[item.Identifier()] = struct{}{}
  32. result = append(result, item)
  33. }
  34. }
  35. }
  36. return result
  37. }
  38. func (ps ProxyList) Sort() ProxyList {
  39. sort.Sort(ps)
  40. return ps
  41. }
  42. func (ps ProxyList) NameAddCounrty() ProxyList {
  43. num := len(ps)
  44. for i := 0; i < num; i++ {
  45. country, err := geoIp.Find(ps[i].BaseInfo().Server)
  46. if err != nil || country == "" {
  47. country = "Earth"
  48. }
  49. ps[i].SetName(fmt.Sprintf("%s", country))
  50. }
  51. return ps
  52. }
  53. func (ps ProxyList) NameAddIndex() ProxyList {
  54. num := len(ps)
  55. for i := 0; i < num; i++ {
  56. ps[i].SetName(fmt.Sprintf("%s_%d", ps[i].BaseInfo().Name, i+1))
  57. }
  58. return ps
  59. }
  60. func Deduplication(src ProxyList) ProxyList {
  61. result := make(ProxyList, 0, len(src))
  62. temp := map[string]struct{}{}
  63. for _, item := range src {
  64. if item != nil {
  65. if _, ok := temp[item.Identifier()]; !ok {
  66. temp[item.Identifier()] = struct{}{}
  67. result = append(result, item)
  68. }
  69. }
  70. }
  71. return result
  72. }
  73. func (ps ProxyList) Clone() ProxyList {
  74. result := make(ProxyList, 0, len(ps))
  75. for _, pp := range ps {
  76. if pp != nil {
  77. result = append(result, pp.Clone())
  78. }
  79. }
  80. return result
  81. }