proxies.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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) Less(i, j int) bool {
  11. return ps[i].BaseInfo().Name < ps[j].BaseInfo().Name
  12. }
  13. func (ps ProxyList) Swap(i, j int) {
  14. ps[i], ps[j] = ps[j], ps[i]
  15. }
  16. func (ps ProxyList) Deduplication() ProxyList {
  17. result := make(ProxyList, 0, len(ps))
  18. temp := map[string]struct{}{}
  19. for _, item := range ps {
  20. if item != nil {
  21. if _, ok := temp[item.Identifier()]; !ok {
  22. temp[item.Identifier()] = struct{}{}
  23. result = append(result, item)
  24. }
  25. }
  26. }
  27. return result
  28. }
  29. func (ps ProxyList) Sort() ProxyList {
  30. sort.Sort(ps)
  31. return ps
  32. }
  33. func (ps ProxyList) NameAddCounrty() ProxyList {
  34. num := len(ps)
  35. for i := 0; i < num; i++ {
  36. country, err := geoIp.Find(ps[i].BaseInfo().Server)
  37. if err != nil || country == "" {
  38. country = "Earth"
  39. }
  40. ps[i].SetName(fmt.Sprintf("%s", country))
  41. }
  42. return ps
  43. }
  44. func (ps ProxyList) NameAddIndex() ProxyList {
  45. num := len(ps)
  46. for i := 0; i < num; i++ {
  47. ps[i].SetName(fmt.Sprintf("%s_%d", ps[i].BaseInfo().Name, i+1))
  48. }
  49. return ps
  50. }
  51. func Deduplication(src ProxyList) ProxyList {
  52. result := make(ProxyList, 0, len(src))
  53. temp := map[string]struct{}{}
  54. for _, item := range src {
  55. if item != nil {
  56. if _, ok := temp[item.Identifier()]; !ok {
  57. temp[item.Identifier()] = struct{}{}
  58. result = append(result, item)
  59. }
  60. }
  61. }
  62. return result
  63. }