Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a AuthenticatedRoundRobinProxyHTTP method to authenticate proxies #578

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package proxy

import (
"context"
"encoding/base64"
"net/http"
"net/url"
"sync/atomic"
Expand All @@ -28,6 +29,12 @@ type roundRobinSwitcher struct {
index uint32
}

type authenticatedRoundRobinSwitcher struct {
roundRobinSwitcher colly.ProxyFunc
username string
password string
}

func (r *roundRobinSwitcher) GetProxy(pr *http.Request) (*url.URL, error) {
index := atomic.AddUint32(&r.index, 1) - 1
u := r.proxyURLs[index%uint32(len(r.proxyURLs))]
Expand All @@ -37,6 +44,18 @@ func (r *roundRobinSwitcher) GetProxy(pr *http.Request) (*url.URL, error) {
return u, nil
}

func (r *authenticatedRoundRobinSwitcher) GetAuthenticatedProxy(request *http.Request) (*url.URL, error) {
auth := r.username + ":" + r.password
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
request.Header.Add("Proxy-Authorization", basicAuth)
request.Header.Add("Proxy-Connection", "Keep-Alive")
url, err := r.roundRobinSwitcher(request)
if err != nil {
return nil, err
}
return url, nil
}

// RoundRobinProxySwitcher creates a proxy switcher function which rotates
// ProxyURLs on every request.
// The proxy type is determined by the URL scheme. "http", "https"
Expand All @@ -56,3 +75,16 @@ func RoundRobinProxySwitcher(ProxyURLs ...string) (colly.ProxyFunc, error) {
}
return (&roundRobinSwitcher{urls, 0}).GetProxy, nil
}

// AuthenticatedRoundRobinProxySwitcher decorates RoundRobinProxySwitcher and sets proxy credentials.
// See RoundRobinProxySwitcher for more details on rotating proxies.
// This method sets correct "Proxy-Connection" and "Proxy-Authorization" headers.
// "Proxy-Authorization" contains the credentials (username, password) to authenticate
// a user agent to a proxy server.
func AuthenticatedRoundRobinProxySwitcher(username string, password string, ProxyURLs ...string) (func(*http.Request) (*url.URL, error), error) {
roundRobinSwitcher, err := RoundRobinProxySwitcher(ProxyURLs...)
if err != nil {
return nil, err
}
return (&authenticatedRoundRobinSwitcher{roundRobinSwitcher, username, password}).GetAuthenticatedProxy, nil
}