package com.browserproxy;
//CHECKSTYLE:OFF checkstyle:magicnumber
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.proxy.CaptureType;
import org.junit.Assert;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* Browser Mob based class
*
*
*/
@Slf4j
public class BProxy {
@Getter
private static BrowserMobProxy proxy = null;
private int proxyPort = 9876;
/***********************************************************
* public BrowserMobProxy getProxyServer()
* Sets up and starts the Browser Mob Proxy
*
*
* @return Proxy instance
***********************************************************/
public void getProxyServer() throws UnknownHostException {
getProxyServer(CaptureType.REQUEST_CONTENT,
CaptureType.REQUEST_HEADERS,
CaptureType.RESPONSE_CONTENT,
CaptureType.RESPONSE_HEADERS);
}
/**
*
* @param captureTypes Comma Separated list
* @throws UnknownHostException
*
* - Sets up and starts the Browser Mob Proxy
* * - Allows the ability to set capture types for the HAR capture
* *
* * EX:
* * getProxyServer(CaptureType.REQUEST_CONTENT,
* * CaptureType.REQUEST_HEADERS,
* * CaptureType.RESPONSE_CONTENT,
* * CaptureType.RESPONSE_HEADERS);
*
*/
public void getProxyServer(CaptureType... captureTypes) throws UnknownHostException {
getProxyServer(proxyPort, captureTypes);
}
/**
* public void getProxyServer(int port, CaptureType... captureTypes)
* @param port - Port to start Proxy server
* @param captureTypes - Comma separated list of capture Types
* @throws UnknownHostException
*
* - Ability to set the port
* - Sets up and starts the Browser Mob Proxy
* - Allows the ability to set capture types for the HAR capture
*
*/
public void getProxyServer(int port, CaptureType... captureTypes) throws UnknownHostException {
if(proxy == null) {
proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
InetAddress connectableAddress = ClientUtil.getConnectableAddress(); // Default behaviour
log.info("Connectable Address=" + connectableAddress);
// above line is needed for application with invalid certificates
log.info("Local Host Address=" + InetAddress.getLocalHost());
proxy.start(port, InetAddress.getByName("localhost"));
log.info("Local Host Address=" + proxy.getClientBindAddress());
log.info("Starting Browser Mob Proxy");
proxy.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
proxy.enableHarCaptureTypes(captureTypes);
}
}
/***********************************************************
* private InetSocketAddress getNetProxy()
* @return address of proxy
*
* Returns the address of the network proxy being used
***********************************************************/
protected InetSocketAddress getNetProxy() {
System.setProperty("java.net.useSystemProxies", "true");
List<java.net.Proxy> l = null;
try {
l = ProxySelector.getDefault().select(
new URI("http://www.google.com/"));
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
InetSocketAddress addr = null;
for (Iterator<java.net.Proxy> iter = l.iterator(); iter.hasNext(); ) {
java.net.Proxy myProxy = iter.next();
System.out.println("proxy hostname : " + myProxy.type());
addr = (InetSocketAddress) myProxy.address();
if (addr == null) {
log.info("No Proxy");
} else {
log.info("proxy hostname : " + addr.getHostName());
log.info("proxy port : " + addr.getPort());
}
}
return addr;
}
/***********************************************************
* public Proxy setSeleniumProxy(BrowserMobProxy proxyServer)
* @param proxyServer
* @return seleniumProxy
*
* Sets up the Selenium Proxy
***********************************************************/
public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
try {
String hostIp = "localhost";
seleniumProxy.setHttpProxy(hostIp + ":" + proxyServer.getPort());
seleniumProxy.setSslProxy(hostIp + ":" + proxyServer.getPort());
} catch (Exception e) {
e.printStackTrace();
Assert.fail("invalid Host Address");
}
return seleniumProxy;
}
/***********************************************************
* private ChromeOptions getChromeOptions()
* Sets the Chrome Options for Browser Mob Proxy
* @return options
*
* This is for any apps using this outside of
* the selenium framework.
***********************************************************/
protected ChromeOptions getChromeOptions() {
ChromeOptions options = new ChromeOptions();
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
options.setCapability(CapabilityType.PROXY, getSeleniumProxy(proxy));
return options;
}
/***********************************************************
* public void quit()
* Gives the ability to close (quit) the Browser Mob Proxy
***********************************************************/
public void quit() {
proxy.stop();
}
/***********************************************************
* private void writeHar(Har har, String filePath)
* @param har
* @param filePath
* Handles writing the Har file based upon path
*
***********************************************************/
protected void writeHar(Har har, String filePath) {
try {
proxy.getHar().writeTo(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* protected void writeHar()
*/
protected void writeHar(){
writeHar(getHar(), "target/test.har");
}
/**
* public void blacklistRequests(String blackListCollection, int RespCode)
* @param blackListCollection
* @param RespCode
*
* Allows user to add items to blacklist from results
* Ex: blacklistRequests("https?://.*\\.google-analytics\\.com/.*", 410);
* 410 is essentially "Gone"
*/
public void blacklistRequests(String blackListCollection, int RespCode){
proxy.blacklistRequests(blackListCollection, RespCode);
}
/**
* public void whitelistRequests(Collection<String> collection, int StatusCode)
* @param collection
* @param StatusCode
*
* Ex: whitelistRequests("https?://*.*.yoursite.com/.*. https://*.*.someOtherYourSite.*".split(","), 200);
*
* Allows user to add URL patterns to allow
*/
public void whitelistRequests(Collection<String> collection, int StatusCode){
proxy.whitelistRequests(collection, StatusCode);
}
/***********************************************************
* public void newHar(String name)
* @param name
*
* Create a new HAR with the label specified by the parameter 'name'
***********************************************************/
public void newHar(String name) {
proxy.newHar(name);
}
/***********************************************************
* public void newHar()
*
* Create new Har
***********************************************************/
public void newHar() {
proxy.newHar();
}
/***********************************************************
* public void newPage()
*
* Creates a new page entry
***********************************************************/
public void newPage() {
proxy.newPage();
}
/***********************************************************
* public void newPage(String pageName)
* @param pageName
* Designates a new page in the HAR
***********************************************************/
public void newPage(String pageName) {
proxy.newPage(pageName);
}
/***********************************************************
* public Har getHar()
* @return
*
* Collect the performance data from the BrowserMob proxy server.
* Get the HAR data.
***********************************************************/
public Har getHar() {
return proxy.getHar();
}
/***********************************************************
* public void endHar()
*
* Ends the HAR file
***********************************************************/
public void endHar() {
proxy.endHar();
}
}