123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package main
- //
- //import (
- // "encoding/base64"
- // "encoding/json"
- // "fmt"
- // "io/ioutil"
- // "net"
- // "net/http"
- // "sync"
- // "time"
- //)
- //
- //const (
- // ip_pool_size = 200
- // base64Table = "ABNOPqrceQRSTklmUDEFGXYZabnopfghHVWdijstuvwCIJKLMxyz0123456789+/"
- //)
- //
- //type (
- // //IpItem
- // IpItem struct {
- // Ip string `json:"ip"`
- // LifeTime int64 `json:"lifetime"`
- // Ports []string `json:"ports"`
- // Raw string
- // }
- // //IpPool
- // IpPool struct {
- // c chan *IpItem
- // refreshThreshold int
- // limitRemain int64
- // currentIp *IpItem
- // lock *sync.RWMutex
- // }
- //)
- //
- //var (
- // coder = base64.NewEncoding(base64Table)
- //)
- //
- //// NewIpPool
- //func NewIpPool(refreshThreshold, limitRemain int) *IpPool {
- // ipp := &IpPool{make(chan *IpItem, ip_pool_size),
- // refreshThreshold, int64(limitRemain),
- // nil, new(sync.RWMutex),
- // }
- // go ipp.watch()
- // return ipp
- //}
- //
- //// watch
- //func (ip *IpPool) watch() {
- // for {
- // if len(ip.c) < ip.refreshThreshold { //IP池满足最小限制,否则更新
- // ip.refere()
- // }
- // time.Sleep(5 * time.Second)
- // }
- //}
- //
- //// Refere 更新
- //func (ip *IpPool) refere() {
- // defer ip.lock.Unlock()
- // ip.lock.Lock()
- // if len(ip.c) > ip.refreshThreshold {
- // return
- // }
- // resp, err := http.Get("http://proxy.spdata.jianyu360.com/proxy/getallip")
- // if err != nil {
- // fmt.Print(err.Error())
- // return
- // }
- // bs, err := ioutil.ReadAll(resp.Body)
- // if err != nil {
- // fmt.Print(err.Error())
- // return
- // }
- // resp.Body.Close()
- // data := make([]*IpItem, 0, 0)
- // err = json.Unmarshal(bs, &data) //解析获取的IP信息
- // if err != nil {
- // fmt.Print(err.Error())
- // return
- // }
- // for _, v := range data {
- // fmt.Print(">")
- // rawIp, err := coder.DecodeString(v.Ip)
- // if err != nil {
- // continue
- // }
- // ipStr := string(rawIp)
- // v.Ip = ipStr
- // for _, p := range v.Ports { //一条连接携带的所有端口
- // addr := ipStr + ":" + p
- // if ip.checkDial(addr) { //检测地址是否可用
- // fmt.Print("+")
- // v.Raw = addr
- // break
- // } else {
- // fmt.Print("?")
- // }
- // }
- // if v.Raw != "" { //某个IP+端口可用即可
- // ip.c <- v
- //
- // }
- // }
- //
- // fmt.Printf("--{proxy size %d}--", len(ip.c))
- //}
- //
- //// checkDial
- //func (ip *IpPool) checkDial(addr string) bool {
- // conn, err := net.DialTimeout("tcp", addr, 1*time.Second)
- // if err == nil {
- // conn.Close()
- // }
- // return err == nil
- //}
- //
- //// Size
- //func (ip *IpPool) Size() int {
- // return len(ip.c)
- //}
- //
- //// Get 获取IP
- //func (ip *IpPool) Get() string {
- // var addr *IpItem
- // for i := 0; i < 3; i++ {
- // addr = nil
- // now := time.Now().Unix()
- // for len(ip.c) > 0 {
- // v := <-ip.c
- // if v.LifeTime-now > ip.limitRemain { //找到可用
- // addr = v
- // break
- // }
- // }
- //
- // if addr == nil { //没有可用IP,刷新IP池
- // fmt.Print("{no proxy}")
- // ip.refere()
- // continue
- // } else if !(ip.checkDial(addr.Raw)) {
- // continue
- // } else {
- // ip.c <- addr
- // break
- // }
- // }
- // if addr != nil {
- // fmt.Printf("{%s %d}", addr.Raw, ip.Size())
- // return addr.Raw
- // } else {
- // fmt.Print("{??}")
- // return "127.0.0.1:9090"
- // }
- //}
- //
- //// Remove
- //func (ip *IpPool) RemoveCurrentItem() {
- //
- //}
|