com.browserup.bup.BrowserUpProxyServer Java Examples

The following examples show how to use com.browserup.bup.BrowserUpProxyServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyServerFactoryTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest({BrowserUpProxyServer.class, ThreadPoolConfiguration.class, ProxyServerFactory.class})
public void testCreateProxyServerConfigDisableMitm() throws Exception
{
    MitmManagerOptions mitmManagerOptions = mock(MitmManagerOptions.class);
    IMitmManagerFactory mitmManagerFactory = mock(IMitmManagerFactory.class);
    MitmManager mitmManager = mock(MitmManager.class);
    when(mitmManagerFactory.createMitmManager(mitmManagerOptions)).thenReturn(mitmManager);
    BrowserUpProxyServer mockedServer = mock(BrowserUpProxyServer.class);
    PowerMockito.whenNew(BrowserUpProxyServer.class).withNoArguments().thenReturn(mockedServer);

    proxyServerFactory.setMitmManagerOptions(mitmManagerOptions);
    proxyServerFactory.setMitmManagerFactory(mitmManagerFactory);
    proxyServerFactory.setMitmEnabled(true);
    proxyServerFactory.createProxyServer();

    verify(mockedServer).setMitmManager(mitmManager);
}
 
Example #2
Source File: ProxyTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
void testClearRequestFilters()
{
    configureProxy();
    BrowserUpProxyServer browserMobProxyServer = mock(BrowserUpProxyServer.class);
    ResponseFilter responseFilter = mock(ResponseFilter.class);
    RequestFilter requestFilter = mock(RequestFilter.class);
    ResponseFilterAdapter.FilterSource fsResponse = new ResponseFilterAdapter.FilterSource(responseFilter);
    RequestFilterAdapter.FilterSource fsRequest = new RequestFilterAdapter.FilterSource(requestFilter);
    when(proxyServerFactory.createProxyServer()).thenReturn(browserMobProxyServer);
    List<HttpFiltersSource> toRemove = new ArrayList<>();
    toRemove.add(fsResponse);
    toRemove.add(fsRequest);
    when(browserMobProxyServer.getFilterFactories()).thenReturn(toRemove).thenReturn(toRemove);
    proxy.start();
    proxy.clearRequestFilters();
    assertTrue(toRemove.size() == 1 && toRemove.contains(fsResponse));
}
 
Example #3
Source File: ProxyServerFactory.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Override
public BrowserUpProxy createProxyServer()
{
    BrowserUpProxyServer proxyServer = new BrowserUpProxyServer();
    proxyServer.setHostNameResolver(advancedHostResolver);
    proxyServer.setTrustAllServers(trustAllServers);
    proxyServer.enableHarCaptureTypes(captureTypes);
    if (mitmEnabled)
    {
        proxyServer.setMitmManager(mitmManagerFactory.createMitmManager(mitmManagerOptions));
    }
    ThreadPoolConfiguration config = new ThreadPoolConfiguration();
    config.withClientToProxyWorkerThreads(PROXY_WORKER_THREADS);
    config.withProxyToServerWorkerThreads(PROXY_WORKER_THREADS);
    proxyServer.setThreadPoolConfiguration(config);
    return proxyServer;
}
 
Example #4
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    Cache<Integer, BrowserUpProxyServer> cache = proxyCache.get();
    if (cache != null) {
        try {
            cache.cleanUp();
        } catch (RuntimeException e) {
            LOG.warn("Error occurred while attempting to clean up expired proxies", e);
        }
    } else {
        // the cache instance was garbage collected, so it no longer needs to be cleaned up. throw an exception
        // to prevent the scheduled executor from re-scheduling this cleanup
        LOG.info("Proxy Cache was garbage collected. No longer cleaning up expired proxies for unused ProxyManager.");

        throw new RuntimeException("Exiting ProxyCleanupTask");
    }
}
 
Example #5
Source File: ExpiringProxyTest.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpiredProxyStops() throws InterruptedException {
    int minPort = new Random().nextInt(50000) + 10000;

    ProxyManager proxyManager = new ProxyManager(
        minPort,
        minPort + 100,
        2);

    BrowserUpProxyServer proxy = proxyManager.create();
    int port = proxy.getPort();

    BrowserUpProxyServer retrievedProxy = proxyManager.get(port);

    assertEquals("ProxyManager did not return the expected proxy instance", proxy, retrievedProxy);

    Thread.sleep(2500);

    // explicitly create a new proxy to cause a write to the cache. cleanups happen on "every" write and "occasional" reads, so force a cleanup by writing.
    int newPort = proxyManager.create().getPort();
    proxyManager.delete(newPort);

    BrowserUpProxyServer expiredProxy = proxyManager.get(port);

    assertNull("ProxyManager did not expire proxy as expected", expiredProxy);
}
 
Example #6
Source File: ExpiringProxyTest.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroTtlProxyDoesNotExpire() throws InterruptedException {
    int minPort = new Random().nextInt(50000) + 10000;

    ProxyManager proxyManager = new ProxyManager(
        minPort,
        minPort + 100,
        0);

    BrowserUpProxyServer proxy = proxyManager.create();
    int port = proxy.getPort();

    BrowserUpProxyServer retrievedProxy = proxyManager.get(port);

    assertEquals("ProxyManager did not return the expected proxy instance", proxy, retrievedProxy);

    Thread.sleep(2500);

    // explicitly create a new proxy to cause a write to the cache. cleanups happen on "every" write and "occasional" reads, so force a cleanup by writing.
    int newPort = proxyManager.create().getPort();
    proxyManager.delete(newPort);

    BrowserUpProxyServer nonExpiredProxy = proxyManager.get(port);

    assertEquals("ProxyManager did not return the expected proxy instance", proxy, nonExpiredProxy);
}
 
Example #7
Source File: BrowserUpHttpFilterChain.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
public BrowserUpHttpFilterChain(BrowserUpProxyServer proxyServer, HttpRequest originalRequest, ChannelHandlerContext ctx) {
    super(originalRequest, ctx);

    this.proxyServer = proxyServer;

    if (proxyServer.getFilterFactories() != null) {
        filters = new ArrayList<>(proxyServer.getFilterFactories().size());

        // instantiate all HttpFilters using the proxy's filter factories
        // allow filter factories to avoid adding a filter on a per-request basis by returning a null
        // HttpFilters instance
        proxyServer.getFilterFactories().stream()
                .map(filterFactory -> filterFactory.filterRequest(originalRequest, ctx))
                .filter(Objects::nonNull).forEach(filters::add);
    } else {
        filters = Collections.emptyList();
    }
}
 
Example #8
Source File: Proxy.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public void clearRequestFilters()
{
    List<HttpFiltersSource> toRemove = new ArrayList<>();
    for (HttpFiltersSource source : ((BrowserUpProxyServer) proxyServer).getFilterFactories())
    {
        if (source instanceof FilterSource)
        {
            toRemove.add(source);
        }
    }
    ((BrowserUpProxyServer) proxyServer).getFilterFactories().removeAll(toRemove);
}
 
Example #9
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
public void delete(int port) {
    BrowserUpProxyServer proxy = proxies.remove(port);
    if (proxy == null) {
        return;
    }

    // temporary: to avoid stopping an already-stopped BrowserUpProxyServer instance, see if it's stopped before re-stopping it
    if (!(proxy).isStopped()) {
        proxy.stop();
    }
}
 
Example #10
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
private BrowserUpProxyServer startProxy(BrowserUpProxyServer proxy, int port, InetAddress clientBindAddr, InetAddress serverBindAddr) {
    if (port != 0) {
        BrowserUpProxyServer old = proxies.putIfAbsent(port, proxy);
        if (old != null) {
            LOG.info("Proxy already exists at port {}", port);
            throw new ProxyExistsException(port);
        }
    }

    try {
        proxy.start(port, clientBindAddr, serverBindAddr);

        if (port == 0) {
            int realPort = proxy.getPort();
            proxies.put(realPort, proxy);
        }

        return proxy;
    } catch (Exception ex) {
        if (port != 0) {
            proxies.remove(port);
        }
        try {
            proxy.stop();
        } catch (Exception ex2) {
            ex.addSuppressed(ex2);
        }
        throw ex;
    }
}
 
Example #11
Source File: InterceptorTest.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanModifyRequest() throws IOException {
    String url = "/modifyrequest";
    stubFor(
            get(urlEqualTo(url)).
                    willReturn(ok().
                            withBody("success").
                            withHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=utf-8")));

    proxy = new BrowserUpProxyServer();
    proxy.start();

    proxy.addFirstHttpFilterFactory(new HttpFiltersSourceAdapter() {
        @Override
        public HttpFilters filterRequest(HttpRequest originalRequest) {
            return new HttpFiltersAdapter(originalRequest) {
                @Override
                public HttpResponse clientToProxyRequest(HttpObject httpObject) {
                    if (httpObject instanceof HttpRequest) {
                        HttpRequest httpRequest = (HttpRequest) httpObject;
                        httpRequest.setUri(httpRequest.uri().replace("/originalrequest", "/modifyrequest"));
                    }

                    return super.clientToProxyRequest(httpObject);
                }
            };
        }
    });

    try (CloseableHttpClient httpClient = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) {
        CloseableHttpResponse response = httpClient.execute(new HttpGet("http://localhost:" + mockServerPort + "/originalrequest"));
        String responseBody = NewProxyServerTestUtil.toStringAndClose(response.getEntity().getContent());

        assertEquals("Expected server to return a 200", 200, response.getStatusLine().getStatusCode());
        assertEquals("Did not receive expected response from mock server", "success", responseBody);

        verify(1, getRequestedFor(urlEqualTo(url)));
    }
}
 
Example #12
Source File: InterceptorTest.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testMitmDisabledHttpsRequestFilterNotAvailable() throws IOException {
    String url = "/mitmdisabled";
    stubFor(get(urlMatching(url)).willReturn(ok().withBody("success")));

    proxy = new BrowserUpProxyServer();
    proxy.setMitmDisabled(true);

    proxy.start();

    final AtomicBoolean connectRequestFilterFired = new AtomicBoolean(false);
    final AtomicBoolean getRequestFilterFired = new AtomicBoolean(false);

    proxy.addRequestFilter(new RequestFilter() {
        @Override
        public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents, HttpMessageInfo messageInfo) {
            if (request.getMethod().equals(HttpMethod.CONNECT)) {
                connectRequestFilterFired.set(true);
            } else if (request.getMethod().equals(HttpMethod.GET)) {
                getRequestFilterFired.set(true);
            }
            return null;
        }
    });

    try (CloseableHttpClient httpClient = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) {
        CloseableHttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + mockServerHttpsPort + "/mitmdisabled"));

        assertEquals("Expected server to return a 200", 200, response.getStatusLine().getStatusCode());

        assertTrue("Expected request filter to fire on CONNECT", connectRequestFilterFired.get());
        assertFalse("Expected request filter to fail to fire on GET because MITM is disabled", getRequestFilterFired.get());

        verify(1, getRequestedFor(urlEqualTo(url)));
    }
}
 
Example #13
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
@Inject
public ProxyManager(@Named("minPort") Integer minPort, @Named("maxPort") Integer maxPort, final @Named("ttl") Integer ttl) {
    this.minPort = minPort;
    this.maxPort = maxPort;
    this.lastPort = maxPort;
    if (ttl > 0) {
        // proxies should be evicted after the specified ttl, so set up an evicting cache and a listener to stop the proxies when they're evicted
        RemovalListener<Integer, BrowserUpProxyServer> removalListener = new RemovalListener<Integer, BrowserUpProxyServer>() {
            public void onRemoval(RemovalNotification<Integer, BrowserUpProxyServer> removal) {
                try {
                    BrowserUpProxyServer proxy = removal.getValue();
                    if (proxy != null) {
                        LOG.info("Expiring ProxyServer on port {} after {} seconds without activity", proxy.getPort(), ttl);
                        proxy.stop();
                    }
                } catch (Exception ex) {
                    LOG.warn("Error while stopping an expired proxy on port " + removal.getKey(), ex);
                }
            }
        };

        this.proxyCache = CacheBuilder.newBuilder()
                .expireAfterAccess(ttl, TimeUnit.SECONDS)
                .removalListener(removalListener)
                .build();

        this.proxies = proxyCache.asMap();

        // schedule the asynchronous proxy cleanup task
        ScheduledExecutorHolder.expiredProxyCleanupExecutor.scheduleWithFixedDelay(new ProxyCleanupTask(proxyCache),
                EXPIRED_PROXY_CLEANUP_INTERVAL_SECONDS, EXPIRED_PROXY_CLEANUP_INTERVAL_SECONDS, TimeUnit.SECONDS);
    } else {
        this.proxies = new ConcurrentHashMap<Integer, BrowserUpProxyServer>();
        // nothing to timeout, so no Cache
        this.proxyCache = null;
    }
}
 
Example #14
Source File: ProxyServerFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest({BrowserUpProxyServer.class, ThreadPoolConfiguration.class, ProxyServerFactory.class})
public void testCreateProxyServerConfig() throws Exception
{
    MitmManagerOptions mitmManagerOptions = mock(MitmManagerOptions.class);
    IMitmManagerFactory mitmManagerFactory = mock(IMitmManagerFactory.class);
    MitmManager mitmManager = mock(MitmManager.class);
    when(mitmManagerFactory.createMitmManager(mitmManagerOptions)).thenReturn(mitmManager);
    BrowserUpProxyServer mockedServer = mock(BrowserUpProxyServer.class);
    PowerMockito.whenNew(BrowserUpProxyServer.class).withNoArguments().thenReturn(mockedServer);
    ThreadPoolConfiguration mockedConfig = mock(ThreadPoolConfiguration.class);
    PowerMockito.whenNew(ThreadPoolConfiguration.class).withNoArguments().thenReturn(mockedConfig);
    AdvancedHostResolver hostNameResolver = mock(AdvancedHostResolver.class);

    boolean trustAllServers = true;
    proxyServerFactory.setMitmManagerOptions(mitmManagerOptions);
    proxyServerFactory.setMitmManagerFactory(mitmManagerFactory);
    proxyServerFactory.setTrustAllServers(trustAllServers);
    proxyServerFactory.setMitmEnabled(true);
    proxyServerFactory.setAdvancedHostResolver(hostNameResolver);
    proxyServerFactory.setCaptureTypes(CaptureType.getAllContentCaptureTypes());
    proxyServerFactory.createProxyServer();
    int expectedThreadsCount = 16;
    verify(mockedConfig).withClientToProxyWorkerThreads(expectedThreadsCount);
    verify(mockedConfig).withProxyToServerWorkerThreads(expectedThreadsCount);
    verify(mockedServer).setTrustAllServers(trustAllServers);
    verify(mockedServer).setMitmManager(mitmManager);
    verify(mockedServer).setThreadPoolConfiguration(mockedConfig);
    verify(mockedServer).setHostNameResolver(hostNameResolver);
    verify(mockedServer).enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
}
 
Example #15
Source File: InterceptorTest.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for executing response modification tests.
 */
private void testModifiedResponse(final String originalText, final String newText) throws IOException {
    String url = "/modifyresponse";
    stubFor(
            get(urlMatching(url)).
                    willReturn(ok().
                            withBody(originalText).
                            withHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=utf-8")));

    proxy = new BrowserUpProxyServer();
    proxy.start();

    proxy.addFirstHttpFilterFactory(new HttpFiltersSourceAdapter() {
        @Override
        public HttpFilters filterRequest(HttpRequest originalRequest) {
            return new HttpFiltersAdapter(originalRequest) {
                @Override
                public HttpObject proxyToClientResponse(HttpObject httpObject) {
                    if (httpObject instanceof FullHttpResponse) {
                        FullHttpResponse httpResponseAndContent = (FullHttpResponse) httpObject;

                        String bodyContent = HttpObjectUtil.extractHttpEntityBody(httpResponseAndContent);

                        if (bodyContent.equals(originalText)) {
                            HttpObjectUtil.replaceTextHttpEntityBody(httpResponseAndContent, newText);
                        }
                    }

                    return super.proxyToClientResponse(httpObject);
                }
            };
        }

        @Override
        public int getMaximumResponseBufferSizeInBytes() {
            return 10000;
        }
    });

    try (CloseableHttpClient httpClient = NewProxyServerTestUtil.getNewHttpClient(proxy.getPort())) {
        CloseableHttpResponse response = httpClient.execute(new HttpGet("http://localhost:" + mockServerPort + "/modifyresponse"));
        String responseBody = NewProxyServerTestUtil.toStringAndClose(response.getEntity().getContent());

        assertEquals("Expected server to return a 200", 200, response.getStatusLine().getStatusCode());
        assertEquals("Did not receive expected response from mock server", newText, responseBody);

        verify(1, getRequestedFor(urlEqualTo(url)));
    }
}
 
Example #16
Source File: NewProxyServerTest.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
@Before
public void setUpProxyServer() {
    proxy = new BrowserUpProxyServer();
    proxy.start();
}
 
Example #17
Source File: NeodymiumWebDriverTest.java    From neodymium-library with MIT License 4 votes vote down vote up
public static void assertProxyStopped(BrowserUpProxy proxy)
{
    Assert.assertNotNull(proxy);
    Assert.assertTrue(proxy.isStarted());
    Assert.assertTrue(((BrowserUpProxyServer) proxy).isStopped());
}
 
Example #18
Source File: NeodymiumWebDriverTest.java    From neodymium-library with MIT License 4 votes vote down vote up
public static void assertProxyAlive(BrowserUpProxy proxy)
{
    Assert.assertNotNull(proxy);
    Assert.assertTrue(proxy.isStarted());
    Assert.assertFalse(((BrowserUpProxyServer) proxy).isStopped());
}
 
Example #19
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public Collection<BrowserUpProxyServer> get() {
    return proxies.values();
}
 
Example #20
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer get(int port) {
    return proxies.get(port);
}
 
Example #21
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create(int port) {
    return create(null, false, null, null, null, port, null, null, false, false);
}
 
Example #22
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create() {
    return create(null, false, null, null, null, null, null, null, false, false);
}
 
Example #23
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword) {
    return create(upstreamHttpProxy, false, null, proxyUsername, proxyPassword, null, null, null, false, false);
}
 
Example #24
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword, Integer port) {
    return create(upstreamHttpProxy, false, null, proxyUsername, proxyPassword, port, null, null, false, false);
}
 
Example #25
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword, Integer port, String bindAddr, boolean useEcc, boolean trustAllServers) {
    return create(upstreamHttpProxy, false, null, proxyUsername, proxyPassword, port, null, null, false, false);
}
 
Example #26
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create(String upstreamHttpProxy, boolean upstreamProxyHttps, String proxyUsername, String proxyPassword, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers) {
    return create(upstreamHttpProxy, upstreamProxyHttps, null, proxyUsername, proxyPassword, port, bindAddr, serverBindAddr, useEcc, trustAllServers);
}
 
Example #27
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers) {
    return create(upstreamHttpProxy, false, null, proxyUsername, proxyPassword, port, bindAddr, serverBindAddr, useEcc, trustAllServers);
}
 
Example #28
Source File: ProxyManager.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
public ProxyCleanupTask(Cache<Integer, BrowserUpProxyServer> cache) {
    this.proxyCache = new WeakReference<Cache<Integer, BrowserUpProxyServer>>(cache);
}