org.littleshoot.proxy.impl.DefaultHttpProxyServer Java Examples

The following examples show how to use org.littleshoot.proxy.impl.DefaultHttpProxyServer. 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: LittleProxyDefaultConfigExample.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // initialize an MitmManager with default settings
    ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder().build();

    // to save the generated CA certificate for installation in a browser, see SaveGeneratedCAExample.java

    // tell the HttpProxyServerBootstrap to use the new MitmManager
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap()
            .withManInTheMiddle(mitmManager)
            .start();

    // make your requests to the proxy server
    //...

    proxyServer.abort();
}
 
Example #2
Source File: TestHttpClient.java    From nifi with Apache License 2.0 6 votes vote down vote up
private static void startProxyServerWithAuth() throws IOException {
    int proxyServerPort;
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        proxyServerPort = serverSocket.getLocalPort();
    }
    proxyServerWithAuth = DefaultHttpProxyServer.bootstrap()
            .withPort(proxyServerPort)
            .withAllowLocalOnly(true)
            .withProxyAuthenticator(new ProxyAuthenticator() {
                @Override
                public boolean authenticate(String userName, String password) {
                    return PROXY_USER.equals(userName) && PROXY_PASSWORD.equals(password);
                }

                @Override
                public String getRealm() {
                    return "NiFi Unit Test";
                }
            })
            // Use less threads to mitigate Gateway Timeout (504) with proxy test
            .withThreadPoolConfiguration(new ThreadPoolConfiguration()
                    .withAcceptorThreads(2)
                    .withClientToProxyWorkerThreads(4)
                    .withProxyToServerWorkerThreads(4))
            .start();
}
 
Example #3
Source File: HTTPProxyAuthConduitTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startProxy() {
    proxy = DefaultHttpProxyServer.bootstrap()
        .withPort(PROXY_PORT)
        .withProxyAuthenticator(new ProxyAuthenticator() {
            @Override
            public boolean authenticate(String userName, String password) {
                return "password".equals(password) && "CXF".equals(userName);
            }
            @Override
            public String getRealm() {
                return null;
            }
        })
        .plusActivityTracker(requestFilter)
        .start();
}
 
Example #4
Source File: HTTPSProxyAuthConduitTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startProxy() {
    System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
    proxy = DefaultHttpProxyServer.bootstrap()
        .withPort(PROXY_PORT)
        .withProxyAuthenticator(new ProxyAuthenticator() {
            @Override
            public boolean authenticate(String userName, String password) {
                return "password".equals(password) && "CXF".equals(userName);
            }
            @Override
            public String getRealm() {
                return null;
            }
        })
        .plusActivityTracker(requestFilter)
        .start();
}
 
Example #5
Source File: Launcher.java    From LittleProxy-mitm with Apache License 2.0 6 votes vote down vote up
public static void main(final String... args) {
    File log4jConfigurationFile = new File(
            "src/test/resources/log4j.xml");
    if (log4jConfigurationFile.exists()) {
        DOMConfigurator.configureAndWatch(
                log4jConfigurationFile.getAbsolutePath(), 15);
    }
    try {
        final int port = 9090;

        System.out.println("About to start server on port: " + port);
        HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer
                .bootstrapFromFile("./littleproxy.properties")
                .withPort(port).withAllowLocalOnly(false);

        bootstrap.withManInTheMiddle(new CertificateSniffingMitmManager());

        System.out.println("About to start...");
        bootstrap.start();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        System.exit(1);
    }
}
 
Example #6
Source File: SalesforceProxyTestIT.java    From components with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setupProxy() {
    ProxyAuthenticator auth = new ProxyAuthenticator() {

        @Override
        public boolean authenticate(String username, String password) {
            return proxyUsername.equals(username) && proxyPassword.equals(password);
        }

        @Override
        public String getRealm() {
            return null;
        }

    };
    server = DefaultHttpProxyServer.bootstrap().withPort(proxyPort).withProxyAuthenticator(auth).start();

    setProxySettingForClient();
}
 
Example #7
Source File: Launcher.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
public static void main(final String... args) {
    File log4jConfigurationFile = new File(
            "src/test/resources/log4j.xml");
    if (log4jConfigurationFile.exists()) {
        DOMConfigurator.configureAndWatch(
                log4jConfigurationFile.getAbsolutePath(), 15);
    }
    try {
        final int port = 9090;

        System.out.println("About to start server on port: " + port);
        HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer
                .bootstrapFromFile("./littleproxy.properties")
                .withPort(port).withAllowLocalOnly(false);

        bootstrap.withManInTheMiddle(new CertificateSniffingMitmManager());

        System.out.println("About to start...");
        bootstrap.start();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        System.exit(1);
    }
}
 
Example #8
Source File: TestHttpClient.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private static void startProxyServerWithAuth() throws IOException {
    int proxyServerPort;
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        proxyServerPort = serverSocket.getLocalPort();
    }
    proxyServerWithAuth = DefaultHttpProxyServer.bootstrap()
            .withPort(proxyServerPort)
            .withAllowLocalOnly(true)
            .withProxyAuthenticator(new ProxyAuthenticator() {
                @Override
                public boolean authenticate(String userName, String password) {
                    return PROXY_USER.equals(userName) && PROXY_PASSWORD.equals(password);
                }

                @Override
                public String getRealm() {
                    return "NiFi Unit Test";
                }
            })
            // Use less threads to mitigate Gateway Timeout (504) with proxy test
            .withThreadPoolConfiguration(new ThreadPoolConfiguration()
                    .withAcceptorThreads(2)
                    .withClientToProxyWorkerThreads(4)
                    .withProxyToServerWorkerThreads(4))
            .start();
}
 
Example #9
Source File: Launcher.java    From CapturePacket with MIT License 6 votes vote down vote up
public static void main(final String... args) {
    File log4jConfigurationFile = new File(
            "src/test/resources/log4j.xml");
    if (log4jConfigurationFile.exists()) {
        DOMConfigurator.configureAndWatch(
                log4jConfigurationFile.getAbsolutePath(), 15);
    }
    try {
        final int port = 9090;

        System.out.println("About to start server on port: " + port);
        HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer
                .bootstrapFromFile("./littleproxy.properties")
                .withPort(port).withAllowLocalOnly(false);

        bootstrap.withManInTheMiddle(new CertificateSniffingMitmManager());

        System.out.println("About to start...");
        bootstrap.start();

    } catch (Exception e) {
        log.error(e.getMessage(), e);
        System.exit(1);
    }
}
 
Example #10
Source File: CustomCAPemFileExample.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // load the root certificate and private key from existing PEM-encoded certificate and private key files
    PemFileCertificateSource fileCertificateSource = new PemFileCertificateSource(
            new File("/path/to/my/certificate.cer"),    // the PEM-encoded certificate file
            new File("/path/to/my/private-key.pem"),    // the PEM-encoded private key file
            "privateKeyPassword");                      // the password for the private key -- can be null if the private key is not encrypted


    // tell the MitmManager to use the custom certificate and private key
    ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder()
            .rootCertificateSource(fileCertificateSource)
            .build();

    // tell the HttpProxyServerBootstrap to use the new MitmManager
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap()
            .withManInTheMiddle(mitmManager)
            .start();

    // make your requests to the proxy server
    //...

    proxyServer.abort();
}
 
Example #11
Source File: CustomCAKeyStoreExample.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // load the root certificate and private key from an existing KeyStore
    KeyStoreFileCertificateSource fileCertificateSource = new KeyStoreFileCertificateSource(
            "PKCS12",                               // KeyStore type. for .jks files (Java KeyStore), use "JKS"
            new File("/path/to/my/keystore.p12"),
            "keyAlias",                             // alias of the private key in the KeyStore; if you did not specify an alias when creating it, use "1"
            "keystorePassword");


    // tell the MitmManager to use the custom certificate and private key
    ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder()
            .rootCertificateSource(fileCertificateSource)
            .build();

    // tell the HttpProxyServerBootstrap to use the new MitmManager
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap()
            .withManInTheMiddle(mitmManager)
            .start();

    // make your requests to the proxy server
    //...

    proxyServer.abort();
}
 
Example #12
Source File: SaveGeneratedCAExample.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // create a dynamic CA root certificate generator using default settings (2048-bit RSA keys)
    RootCertificateGenerator rootCertificateGenerator = RootCertificateGenerator.builder().build();

    // save the dynamically-generated CA root certificate for installation in a browser
    rootCertificateGenerator.saveRootCertificateAsPemFile(new File("/tmp/my-dynamic-ca.cer"));

    // tell the MitmManager to use the root certificate we just generated
    ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder()
            .rootCertificateSource(rootCertificateGenerator)
            .build();

    // tell the HttpProxyServerBootstrap to use the new MitmManager
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap()
            .withManInTheMiddle(mitmManager)
            .start();

    // make your requests to the proxy server
    //...

    proxyServer.abort();
}
 
Example #13
Source File: ProxyServerUtil.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static HttpProxyServer start() {
	log.info("*** STARTED TEST PROXY SERVER ***");
	HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(8089).withProxyAuthenticator(new ProxyAuthenticator() {
		@Override
		public boolean authenticate(String username, String password) {
			return username.equals("test") && password.equals("works");
		}

		@Override
		public String getRealm() {
			return null;
		}
	}).start();
	return proxyServer;
}
 
Example #14
Source File: ProxyTest.java    From gradle-download-task with Apache License 2.0 5 votes vote down vote up
/**
 * Runs a proxy server counting requests
 * @param authenticating true if the proxy should require authentication
 * @throws Exception if an error occurred
 */
private void startProxy(boolean authenticating) throws Exception {
    proxyPort = findPort();
    
    HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer.bootstrap()
            .withPort(proxyPort)
            .withFiltersSource(new HttpFiltersSourceAdapter() {
                public HttpFilters filterRequest(HttpRequest originalRequest,
                        ChannelHandlerContext ctx) {
                   return new HttpFiltersAdapter(originalRequest) {
                      @Override
                      public void proxyToServerRequestSent() {
                          proxyCounter++;
                      }
                   };
                }
            });
    
    if (authenticating) {
        bootstrap = bootstrap.withProxyAuthenticator(new ProxyAuthenticator() {
            @Override
            public boolean authenticate(String userName, String password) {
                return PROXY_USERNAME.equals(userName) &&
                        PROXY_PASSWORD.equals(password);
            }

            @Override
            public String getRealm() {
                return "gradle-download-task";
            }
        });
    }
    
    proxy = bootstrap.start();
}
 
Example #15
Source File: EllipticCurveCAandServerExample.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // create a dyamic CA root certificate generator using Elliptic Curve keys
    RootCertificateGenerator ecRootCertificateGenerator = RootCertificateGenerator.builder()
            .keyGenerator(new ECKeyGenerator())     // use EC keys, instead of the default RSA
            .build();

    // save the dynamically-generated CA root certificate for installation in a browser
    ecRootCertificateGenerator.saveRootCertificateAsPemFile(new File("/tmp/my-dynamic-ca.cer"));

    // save the dynamically-generated CA private key for use in future LittleProxy executions
    // (see CustomCAPemFileExample.java for an example loading a previously-generated CA cert + key from a PEM file)
    ecRootCertificateGenerator.savePrivateKeyAsPemFile(new File("/tmp/my-ec-private-key.pem"), "secretPassword");

    // tell the MitmManager to use the root certificate we just generated, and to use EC keys when
    // creating impersonated server certs
    ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder()
            .rootCertificateSource(ecRootCertificateGenerator)
            .serverKeyGenerator(new ECKeyGenerator())
            .build();

    // tell the HttpProxyServerBootstrap to use the new MitmManager
    HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap()
            .withManInTheMiddle(mitmManager)
            .start();

    // make your requests to the proxy server
    //...

    proxyServer.abort();
}
 
Example #16
Source File: TestHttpClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
private static void startProxyServer() throws IOException {
    int proxyServerPort;
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        proxyServerPort = serverSocket.getLocalPort();
    }
    proxyServer = DefaultHttpProxyServer.bootstrap()
            .withPort(proxyServerPort)
            .withAllowLocalOnly(true)
            // Use less threads to mitigate Gateway Timeout (504) with proxy test
            .withThreadPoolConfiguration(new ThreadPoolConfiguration()
                .withAcceptorThreads(2)
                .withClientToProxyWorkerThreads(4)
                .withProxyToServerWorkerThreads(4))
            .start();
}
 
Example #17
Source File: HTTPProxyConduitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startProxy() {
    proxy = DefaultHttpProxyServer.bootstrap()
        .withPort(PROXY_PORT)
        .plusActivityTracker(requestFilter)
        .start();
}
 
Example #18
Source File: HTTPSProxyConduitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startProxy() {
    proxy = DefaultHttpProxyServer.bootstrap()
        .withPort(PROXY_PORT)
        .plusActivityTracker(requestFilter)
        .start();
}
 
Example #19
Source File: TestHttpClient.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private static void startProxyServer() throws IOException {
    int proxyServerPort;
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        proxyServerPort = serverSocket.getLocalPort();
    }
    proxyServer = DefaultHttpProxyServer.bootstrap()
            .withPort(proxyServerPort)
            .withAllowLocalOnly(true)
            // Use less threads to mitigate Gateway Timeout (504) with proxy test
            .withThreadPoolConfiguration(new ThreadPoolConfiguration()
                .withAcceptorThreads(2)
                .withClientToProxyWorkerThreads(4)
                .withProxyToServerWorkerThreads(4))
            .start();
}
 
Example #20
Source File: CliProxyEnvVarIT.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp()
        throws Exception
{
    httpMockServer = new MockWebServer();
    httpMockServer.start();

    httpsMockServer = new MockWebServer();
    HandshakeCertificates handshakeCertificates = localhost();
    SSLSocketFactory socketFactory = handshakeCertificates.sslSocketFactory();


    httpsMockServer.useHttps(socketFactory, false);
    httpsMockServer.start();

    httpProxy = DefaultHttpProxyServer
            .bootstrap()
            .withPort(0)
            .plusActivityTracker(httpProxyRequestTracker)
            .start();
    httpProxyUrl = "http://" + httpProxy.getListenAddress().getHostString() + ":" + httpProxy.getListenAddress().getPort();

    httpsProxy = DefaultHttpProxyServer
            .bootstrap()
            .withPort(0)
            .plusActivityTracker(httpsProxyRequestTracker)
            .withSslEngineSource(new SelfSignedSslEngineSource())
            .withAuthenticateSslClients(false)
            .start();
    httpsProxyUrl = "https://" + httpsProxy.getListenAddress().getHostString() + ":" + httpsProxy.getListenAddress().getPort();
}
 
Example #21
Source File: ProxyServerUtil.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static HttpProxyServer start() {
	log.info("*** STARTED TEST PROXY SERVER ***");
	HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(8089).withProxyAuthenticator(new ProxyAuthenticator() {
		@Override
		public boolean authenticate(String username, String password) {
			return username.equals("test") && password.equals("works");
		}

		@Override
		public String getRealm() {
			return null;
		}
	}).start();
	return proxyServer;
}
 
Example #22
Source File: Launcher.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the proxy from the command line.
 *
 * @param port Any command line arguments.
 */
public static void startHttpProxyService(int port) {
    //  pollLog4JConfigurationFileIfAvailable();

    System.out.println("About to start server on port: " + port);
    HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer
            .bootstrapFromFile("./littleproxy.properties")
            .withPort(port)
            //.withTransportProtocol(TransportProtocol.UDT)
            .withAllowLocalOnly(false);


    System.out.println("About to start...");
    bootstrap.start();
}
 
Example #23
Source File: CXF6655Test.java    From cxf with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void startServers() throws Exception {
    assertTrue("server did not launch correctly", launchServer(Server.class, true));
    proxy = DefaultHttpProxyServer.bootstrap().withPort(PROXY_PORT).start();
}
 
Example #24
Source File: HttpProxyTest.java    From java-cloudant with Apache License 2.0 4 votes vote down vote up
/**
 * Starts a littleproxy instance that will proxy the requests. Applies appropriate configuration
 * options to the proxy based on the test parameters.
 *
 * @throws Exception
 */
@BeforeEach
public void setupAndStartProxy(final boolean okUsable,
                               final boolean useSecureProxy,
                               final boolean useHttpsServer,
                               final boolean useProxyAuth) throws Exception {

    HttpProxyServerBootstrap proxyBoostrap = DefaultHttpProxyServer.bootstrap()
            .withAllowLocalOnly(true) // only run on localhost
            .withAuthenticateSslClients(false); // we aren't checking client certs

    if (useProxyAuth) {
        // check the proxy user and password
        ProxyAuthenticator pa = new ProxyAuthenticator() {
            @Override
            public boolean authenticate(String userName, String password) {
                return (mockProxyUser.equals(userName) && mockProxyPass.equals(password));
            }

            @Override
            public String getRealm() {
                return null;
            }
        };
        proxyBoostrap.withProxyAuthenticator(pa);
    }

    if (useSecureProxy) {
        proxyBoostrap.withSslEngineSource(new SslEngineSource() {
            @Override
            public SSLEngine newSslEngine() {
                return MockWebServerResources.getSSLContext().createSSLEngine();
            }

            @Override
            public SSLEngine newSslEngine(String peerHost, int peerPort) {
                return MockWebServerResources.getSSLContext().createSSLEngine(peerHost,
                        peerPort);
            }
        });
    }

    // Start the proxy server
    proxy = proxyBoostrap.start();
}
 
Example #25
Source File: Proxy.java    From LittleProxy-mitm with Apache License 2.0 4 votes vote down vote up
protected HttpProxyServerBootstrap bootstrap() {
    return DefaultHttpProxyServer.bootstrap().withPort(proxyPort);
}