Beispiel #1
0
func DefaultConfig() *Config {
	return &Config{
		Validator: func(p ProxyInfo) (*ProxyQuality, error) {
			urlStr := fmt.Sprintf("http://%s:%d", p.Host, p.Port)
			url, err := url.Parse(urlStr)
			if err != nil {
				return nil, err
			}
			proxy := http.ProxyURL(url)

			client := &http.Client{
				Transport: &http.Transport{
					Proxy: proxy,
				},
				Timeout: time.Duration(10 * time.Second),
			}

			startTime := time.Now()
			resp, err := client.Get("http://search.yahoo.co.jp/search?p=%E6%97%A5%E6%9C%AC")

			if err != nil {
				return nil, err
			}

			if resp.StatusCode != 200 {
				return nil, errors.New("Error response code " + string(resp.StatusCode))
			}

			return &ProxyQuality{
				ConnectTime: int(time.Now().Sub(startTime).Seconds()),
			}, nil

		},
		ValidateInterval:  600,
		maxValidateThread: 50,

		Filter: &AcceptAll{},

		Provider: provider.CreateAllLoader(),

		ProxyStrategy: func(alive map[ProxyInfo]ProxyState, request *http.Request) *ProxyInfo {
			for info, _ := range alive {
				if request.URL.Scheme == "https" && info.Type&HTTPS == 0 {
					continue
				}
				return &info
			}
			return nil
		},
	}
}
Beispiel #2
0
func handle_cached_json(w http.ResponseWriter, r *http.Request) {
	if !checkPermission(w, r) {
		return
	}

	paramCache := r.URL.Query().Get(QUERY_CACHE) != "false"
	providers := r.URL.Query().Get(QUERY_PROVIDERS)
	if providers == "" {
		providers = "all"
	}

	ctx := appengine.NewContext(r)
	client := urlfetch.Client(ctx)
	key := datastore.NewKey(ctx, QUERY_PROVIDERS, providers, 0, nil)

	cache := &ProxyList{}

	if !paramCache {
		var p provider.ProxyProvider
		if providers == "all" {
			p = provider.CreateAllLoader()
		} else {
			p = provider.CreateProvider(providers)
		}

		if p == nil {
			errJson(w, NO_PROVIDER)
			return
		}

		p.SetClient(client)
		items, err := p.Load()

		if err != nil {
			formatError(w, err)
			return
		}

		cache = &ProxyList{
			Timestamp: time.Now(),
			Proxies:   make([]provider.ProxyItem, len(items)),
		}
		for i, item := range items {
			cache.Proxies[i] = *item
		}
		datastore.Put(ctx, key, cache)
	} else {
		datastore.Get(ctx, key, cache)
	}

	if strings.HasSuffix(r.URL.Path, "xml") {
		ret, _ := xml.Marshal(cache.Proxies)
		w.Header().Set("Content-Type", "application/xml")
		fmt.Fprint(w, "<proxylist>")
		fmt.Fprint(w, string(ret))
		fmt.Fprint(w, "</proxylist>")
	} else {
		ret, _ := json.Marshal(cache.Proxies)
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprint(w, string(ret))
	}
}