How to use http proxy with auth
pgshow opened this issue · comments
ChromeCaps = chrome.Capabilities{ Args: []string{ "--proxy-server=http://username:password@127.0.0.1:19004", }, }
I used this code, but it's not working!!!
I also used the "--proxy-server="
method and it doesn't seem to be working for me..
I've also tried the following:
caps.AddProxy(selenium.Proxy{ Type: selenium.Manual, HTTP: fmt.Sprintf("%s:%s@%s:%d", pUser, pPass, pHost, pPort), FTP: fmt.Sprintf("%s:%s@%s:%d", pUser, pPass, pHost, pPort), SSL: fmt.Sprintf("%s:%s@%s:%d", pUser, pPass, pHost, pPort), NoProxy: nil, })
With no signs of connecting to the proxy. I wonder if there is some way to check whether the proxy connection succeeded?
I did some research and the expert said there is no way for selenium to use proxy with Auth. But they've given a solution. https://stackoverflow.com/questions/9888323/how-to-override-basic-authentication-in-selenium2-with-java-using-chrome-driver
I did some research and the expert said there is no way for selenium to use proxy with Auth. But they've given a solution. https://stackoverflow.com/questions/9888323/how-to-override-basic-authentication-in-selenium2-with-java-using-chrome-driver
You seem to be right. I dug a little deeper as I'm using a 3rd party service for rotating proxies. I removed the credentials and added IP Authentication via the proxy provider and now I'm seeing requests being made. This is the first time I've had a successful proxy request, my guess is the API/Selenium doesn't support HTTP Auth as you suggested.
func main() {
var (
proxyHost string = "26.8.157.214"
proxyPort string = "1060"
proxyUser string = "userName"
proxyPass string = "password"
chromedriverPath string = "/usr/local/bin/chromedriver"
)
var products []Product
// initialize a Chrome browser instance on port 4444
service, err := selenium.NewChromeDriverService(chromedriverPath, 4444)
if err != nil {
log.Fatal("Error:", err)
}
defer service.Stop()
selOptions := selenium.Capabilities{}
chromeOptions := chrome.Capabilities{Args: []string{
"--headless-new",
"--no-sandbox",
}}
chromeProxyExtFileName := "proxy_auth_plugin.zip"
if err := BuildProxyExtension(chromeProxyExtFileName, proxyHost, proxyPort, proxyUser, proxyPass); err != nil {
log.Fatal("BuildProxyExtension Error:", err)
}
chromeOptions.AddExtension(chromeProxyExtFileName)
selOptions.AddChrome(chromeOptions)
// create a new remote client with the specified options
driver, err := selenium.NewRemote(selOptions, "")
if err != nil {
log.Fatal("Error:", err)
}
// other code ...
}
// BuildProxyExtension creates chrome extension
func BuildProxyExtension(zipFName, host, port, userName, password string) error {
const (
manifestFName = "manifest.json"
backFName = "background.js"
)
manifest_json := `{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}`
background_js := fmt.Sprintf(`var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%s",
port: parseInt(%s)
},
bypassList: ["localhost"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%s",
password: "%s"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);`, host, port, userName, password)
fos, err := os.Create(zipFName)
if err != nil {
return fmt.Errorf("os.Create Error: %w", err)
}
defer fos.Close()
zipWriter := zip.NewWriter(fos)
preManifestFile, err := os.Create(manifestFName)
if err != nil {
return fmt.Errorf("os.Create manifestFile Error: %w", err)
}
if _, err = preManifestFile.Write([]byte(manifest_json)); err != nil {
return fmt.Errorf("preManifestFile.Write Error: %w", err)
}
preManifestFile.Close()
manifestFile, err := os.Open(manifestFName)
if err != nil {
return fmt.Errorf("os.Open manifestFile Error: %w", err)
}
wmf, err := zipWriter.Create(manifestFName)
if err != nil {
return fmt.Errorf("zipWriter.Create Error: %w", err)
}
if _, err := io.Copy(wmf, manifestFile); err != nil {
return fmt.Errorf("io.Copy Error: %w", err)
}
manifestFile.Close()
preBackFile, err := os.Create(backFName)
if err != nil {
return fmt.Errorf("os.Create preBackFile Error: %w", err)
}
if _, err = preBackFile.Write([]byte(background_js)); err != nil {
return fmt.Errorf("preBackFile.Write Error: %w", err)
}
preBackFile.Close()
backFile, err := os.Open(backFName)
if err != nil {
return fmt.Errorf("os.Open backFile Error: %w", err)
}
wbf, err := zipWriter.Create(backFName)
if err != nil {
return fmt.Errorf("zipWriter.Create(backFName) Error: %w", err)
}
if _, err := io.Copy(wbf, backFile); err != nil {
return fmt.Errorf("io.Copy Error: %w", err)
}
backFile.Close()
if err := os.Remove(manifestFName); err != nil {
return fmt.Errorf("os.Remove(manifestFName) Error: %w", err)
}
if err := os.Remove(backFName); err != nil {
return fmt.Errorf("os.Remove(backFName) Error: %w", err)
}
return zipWriter.Close()
}