ip.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. //
  3. //import (
  4. // "encoding/base64"
  5. // "encoding/json"
  6. // "fmt"
  7. // "io/ioutil"
  8. // "net"
  9. // "net/http"
  10. // "sync"
  11. // "time"
  12. //)
  13. //
  14. //const (
  15. // ip_pool_size = 200
  16. // base64Table = "ABNOPqrceQRSTklmUDEFGXYZabnopfghHVWdijstuvwCIJKLMxyz0123456789+/"
  17. //)
  18. //
  19. //type (
  20. // //IpItem
  21. // IpItem struct {
  22. // Ip string `json:"ip"`
  23. // LifeTime int64 `json:"lifetime"`
  24. // Ports []string `json:"ports"`
  25. // Raw string
  26. // }
  27. // //IpPool
  28. // IpPool struct {
  29. // c chan *IpItem
  30. // refreshThreshold int
  31. // limitRemain int64
  32. // currentIp *IpItem
  33. // lock *sync.RWMutex
  34. // }
  35. //)
  36. //
  37. //var (
  38. // coder = base64.NewEncoding(base64Table)
  39. //)
  40. //
  41. //// NewIpPool
  42. //func NewIpPool(refreshThreshold, limitRemain int) *IpPool {
  43. // ipp := &IpPool{make(chan *IpItem, ip_pool_size),
  44. // refreshThreshold, int64(limitRemain),
  45. // nil, new(sync.RWMutex),
  46. // }
  47. // go ipp.watch()
  48. // return ipp
  49. //}
  50. //
  51. //// watch
  52. //func (ip *IpPool) watch() {
  53. // for {
  54. // if len(ip.c) < ip.refreshThreshold { //IP池满足最小限制,否则更新
  55. // ip.refere()
  56. // }
  57. // time.Sleep(5 * time.Second)
  58. // }
  59. //}
  60. //
  61. //// Refere 更新
  62. //func (ip *IpPool) refere() {
  63. // defer ip.lock.Unlock()
  64. // ip.lock.Lock()
  65. // if len(ip.c) > ip.refreshThreshold {
  66. // return
  67. // }
  68. // resp, err := http.Get("http://proxy.spdata.jianyu360.com/proxy/getallip")
  69. // if err != nil {
  70. // fmt.Print(err.Error())
  71. // return
  72. // }
  73. // bs, err := ioutil.ReadAll(resp.Body)
  74. // if err != nil {
  75. // fmt.Print(err.Error())
  76. // return
  77. // }
  78. // resp.Body.Close()
  79. // data := make([]*IpItem, 0, 0)
  80. // err = json.Unmarshal(bs, &data) //解析获取的IP信息
  81. // if err != nil {
  82. // fmt.Print(err.Error())
  83. // return
  84. // }
  85. // for _, v := range data {
  86. // fmt.Print(">")
  87. // rawIp, err := coder.DecodeString(v.Ip)
  88. // if err != nil {
  89. // continue
  90. // }
  91. // ipStr := string(rawIp)
  92. // v.Ip = ipStr
  93. // for _, p := range v.Ports { //一条连接携带的所有端口
  94. // addr := ipStr + ":" + p
  95. // if ip.checkDial(addr) { //检测地址是否可用
  96. // fmt.Print("+")
  97. // v.Raw = addr
  98. // break
  99. // } else {
  100. // fmt.Print("?")
  101. // }
  102. // }
  103. // if v.Raw != "" { //某个IP+端口可用即可
  104. // ip.c <- v
  105. //
  106. // }
  107. // }
  108. //
  109. // fmt.Printf("--{proxy size %d}--", len(ip.c))
  110. //}
  111. //
  112. //// checkDial
  113. //func (ip *IpPool) checkDial(addr string) bool {
  114. // conn, err := net.DialTimeout("tcp", addr, 1*time.Second)
  115. // if err == nil {
  116. // conn.Close()
  117. // }
  118. // return err == nil
  119. //}
  120. //
  121. //// Size
  122. //func (ip *IpPool) Size() int {
  123. // return len(ip.c)
  124. //}
  125. //
  126. //// Get 获取IP
  127. //func (ip *IpPool) Get() string {
  128. // var addr *IpItem
  129. // for i := 0; i < 3; i++ {
  130. // addr = nil
  131. // now := time.Now().Unix()
  132. // for len(ip.c) > 0 {
  133. // v := <-ip.c
  134. // if v.LifeTime-now > ip.limitRemain { //找到可用
  135. // addr = v
  136. // break
  137. // }
  138. // }
  139. //
  140. // if addr == nil { //没有可用IP,刷新IP池
  141. // fmt.Print("{no proxy}")
  142. // ip.refere()
  143. // continue
  144. // } else if !(ip.checkDial(addr.Raw)) {
  145. // continue
  146. // } else {
  147. // ip.c <- addr
  148. // break
  149. // }
  150. // }
  151. // if addr != nil {
  152. // fmt.Printf("{%s %d}", addr.Raw, ip.Size())
  153. // return addr.Raw
  154. // } else {
  155. // fmt.Print("{??}")
  156. // return "127.0.0.1:9090"
  157. // }
  158. //}
  159. //
  160. //// Remove
  161. //func (ip *IpPool) RemoveCurrentItem() {
  162. //
  163. //}