-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProxyHelper.java
More file actions
54 lines (44 loc) · 2.23 KB
/
ProxyHelper.java
File metadata and controls
54 lines (44 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.proxy.auth.AuthType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.net.InetSocketAddress;
public class ProxyHelper {
static WebDriver getDriver(BrowserMobProxyServer proxy, boolean headless) {
ChromeOptions options = new ChromeOptions();
// Required to work with https URLS
options.addArguments("--ignore-certificate-errors");
// Visibility of the browser is controlled by the parameter headless
options.setHeadless(headless);
// Enable proxy for Selenium
options.setProxy(ClientUtil.createSeleniumProxy(proxy));
WebDriver driver = new ChromeDriver(options);
return driver;
}
static BrowserMobProxyServer getProxy(String endpoint, String username, String password, String countryCode) {
// The endpoint contains host and port. Split it for further use.
String[] endpointParts = endpoint.split(":");
if (endpointParts.length != 2)
throw new IllegalArgumentException("Endpoint should include host and port. For example, pr.oxylabs.io:7777");
String updatedUser = updateUserForCountry(username, countryCode);
// BrowserMobProxyServer makes it easy to communicate with Chrome.
BrowserMobProxyServer proxy = new BrowserMobProxyServer();
// Handling http and https URLs
proxy.setTrustAllServers(true);
// Oxylabs proxy as added to the chain of locally running proxy server
proxy.setChainedProxy(new InetSocketAddress(endpointParts[0], Integer.parseInt(endpointParts[1])));
proxy.chainedProxyAuthorization(updatedUser, password, AuthType.BASIC);
// This is local proxy in JVM. Port is assinged automatically.
// It must be stopped using stop() method before exiting.
proxy.start(0);
return proxy;
}
private static String updateUserForCountry(String userName, String countryCode) {
if (countryCode == null) {
return userName;
}
return String.format("customer-%s-cc-%s", userName, countryCode);
}
}