Search in sources :

Example 1 with ProxyConfiguration

use of com.predic8.membrane.core.transport.http.client.ProxyConfiguration in project service-proxy by membrane.

the class ProxyTest method setUp.

@Before
public void setUp() throws Exception {
    proxy = new ProxyConfiguration();
    proxy.setUsername("predic8");
    proxy.setPassword("secret");
}
Also used : ProxyConfiguration(com.predic8.membrane.core.transport.http.client.ProxyConfiguration) Before(org.junit.Before)

Example 2 with ProxyConfiguration

use of com.predic8.membrane.core.transport.http.client.ProxyConfiguration in project service-proxy by membrane.

the class HttpRouter method createTransport.

/**
 * Same as the default config from monitor-beans.xml
 */
private Transport createTransport(ProxyConfiguration proxyConfiguration) {
    Transport transport = new HttpTransport();
    List<Interceptor> interceptors = new ArrayList<Interceptor>();
    interceptors.add(new RuleMatchingInterceptor());
    interceptors.add(new DispatchingInterceptor());
    interceptors.add(new UserFeatureInterceptor());
    HTTPClientInterceptor httpClientInterceptor = new HTTPClientInterceptor();
    interceptors.add(httpClientInterceptor);
    transport.setInterceptors(interceptors);
    return transport;
}
Also used : HttpTransport(com.predic8.membrane.core.transport.http.HttpTransport) HTTPClientInterceptor(com.predic8.membrane.core.interceptor.HTTPClientInterceptor) ArrayList(java.util.ArrayList) HttpTransport(com.predic8.membrane.core.transport.http.HttpTransport) Transport(com.predic8.membrane.core.transport.Transport) HTTPClientInterceptor(com.predic8.membrane.core.interceptor.HTTPClientInterceptor) RuleMatchingInterceptor(com.predic8.membrane.core.interceptor.RuleMatchingInterceptor) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) UserFeatureInterceptor(com.predic8.membrane.core.interceptor.UserFeatureInterceptor) DispatchingInterceptor(com.predic8.membrane.core.interceptor.DispatchingInterceptor) UserFeatureInterceptor(com.predic8.membrane.core.interceptor.UserFeatureInterceptor) RuleMatchingInterceptor(com.predic8.membrane.core.interceptor.RuleMatchingInterceptor) DispatchingInterceptor(com.predic8.membrane.core.interceptor.DispatchingInterceptor)

Example 3 with ProxyConfiguration

use of com.predic8.membrane.core.transport.http.client.ProxyConfiguration in project service-proxy by membrane.

the class Connection method open.

public static Connection open(String host, int port, String localHost, SSLProvider sslProvider, ConnectionManager mgr, int connectTimeout, @Nullable String sniServername, @Nullable ProxyConfiguration proxy, @Nullable SSLProvider proxySSLProvider) throws UnknownHostException, IOException {
    Connection con = new Connection(mgr, host, sslProvider, sniServername, proxy);
    String origHost = host;
    int origPort = port;
    SSLProvider origSSLProvider = sslProvider;
    String origSniServername = sniServername;
    if (proxy != null) {
        sslProvider = proxySSLProvider;
        host = proxy.getHost();
        port = proxy.getPort();
        sniServername = null;
    }
    if (sslProvider != null) {
        if (isNullOrEmpty(localHost))
            con.socket = sslProvider.createSocket(host, port, connectTimeout, sniServername);
        else
            con.socket = sslProvider.createSocket(host, port, InetAddress.getByName(localHost), 0, connectTimeout, sniServername);
    } else {
        if (isNullOrEmpty(localHost)) {
            con.socket = new Socket();
        } else {
            con.socket = new Socket();
            con.socket.bind(new InetSocketAddress(InetAddress.getByName(localHost), 0));
        }
        con.socket.connect(new InetSocketAddress(host, port), connectTimeout);
    }
    if (proxy != null && origSSLProvider != null) {
        con.doTunnelHandshake(proxy, con.socket, origHost, origPort);
        con.socket = origSSLProvider.createSocket(con.socket, origHost, origPort, connectTimeout, origSniServername);
    }
    log.debug("Opened connection on localPort: " + con.socket.getLocalPort());
    // Creating output stream before input stream is suggested.
    con.out = new BufferedOutputStream(con.socket.getOutputStream(), 2048);
    con.in = new BufferedInputStream(con.socket.getInputStream(), 2048);
    return con;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SSLProvider(com.predic8.membrane.core.transport.ssl.SSLProvider) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 4 with ProxyConfiguration

use of com.predic8.membrane.core.transport.http.client.ProxyConfiguration in project service-proxy by membrane.

the class ProxySSLTest method test.

@Test
public void test() throws Exception {
    // Step 1: create the backend
    Router backend = new Router();
    backend.setHotDeploy(false);
    ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(backendPort), null, 0);
    if (backendUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setKeyStore(new KeyStore());
        ssl.getKeyStore().setLocation("classpath:/ssl-rsa.keystore");
        ssl.getKeyStore().setKeyPassword("secret");
        sp.setSslInboundParser(ssl);
    }
    sp.getInterceptors().add(new CountInterceptor());
    backend.getRuleManager().addProxy(sp, RuleManager.RuleDefinitionSource.MANUAL);
    backend.start();
    // Step 2: put a proxy in front of it
    AtomicInteger proxyCounter = new AtomicInteger();
    Router proxyRouter = new Router();
    proxyRouter.setHotDeploy(false);
    ProxyRule proxy = new ProxyRule(new ProxyRuleKey(proxyPort));
    proxy.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            proxyCounter.incrementAndGet();
            return super.handleRequest(exc);
        }
    });
    if (proxyUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setKeyStore(new KeyStore());
        ssl.getKeyStore().setLocation("classpath:/ssl-rsa2.keystore");
        ssl.getKeyStore().setKeyPassword("secret");
        proxy.setSslInboundParser(ssl);
    }
    proxyRouter.getRuleManager().addProxy(proxy, RuleManager.RuleDefinitionSource.MANUAL);
    proxyRouter.start();
    // Step 3: configure the client to access the backend through the proxy
    HttpClientConfiguration httpClientConfiguration = new HttpClientConfiguration();
    ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
    proxyConfiguration.setHost("localhost");
    proxyConfiguration.setPort(proxyPort);
    if (proxyUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setTrustStore(new TrustStore());
        ssl.getTrustStore().setLocation("classpath:/ssl-rsa-pub2.keystore");
        ssl.getTrustStore().setPassword("secret");
        // workarond the fact that the certificate was not issued for 'localhost'
        ssl.setEndpointIdentificationAlgorithm("");
        proxyConfiguration.setSslParser(ssl);
    }
    httpClientConfiguration.setProxy(proxyConfiguration);
    HttpClient hc = new HttpClient(httpClientConfiguration);
    // Step 4: Test client
    Exchange exc = new Request.Builder().get("http" + (backendUsesSSL ? "s" : "") + "://localhost:" + backendPort + "/foo").buildExchange();
    if (backendUsesSSL) {
        SSLParser ssl = new SSLParser();
        ssl.setTrustStore(new TrustStore());
        ssl.getTrustStore().setLocation("classpath:/ssl-rsa-pub.keystore");
        ssl.getTrustStore().setPassword("secret");
        // workarond the fact that the certificate was not issued for 'localhost'
        ssl.setEndpointIdentificationAlgorithm("");
        exc.setProperty(Exchange.SSL_CONTEXT, new StaticSSLContext(ssl, new ResolverMap(), null));
    }
    hc.call(exc);
    Assert.assertEquals(200, exc.getResponse().getStatusCode());
    Assert.assertEquals("Did the request go through the proxy?", 1, proxyCounter.get());
    proxyRouter.shutdown();
    backend.shutdown();
}
Also used : CountInterceptor(com.predic8.membrane.core.interceptor.CountInterceptor) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) StaticSSLContext(com.predic8.membrane.core.transport.ssl.StaticSSLContext) Router(com.predic8.membrane.core.Router) ResolverMap(com.predic8.membrane.core.resolver.ResolverMap) TrustStore(com.predic8.membrane.core.config.security.TrustStore) KeyStore(com.predic8.membrane.core.config.security.KeyStore) SSLParser(com.predic8.membrane.core.config.security.SSLParser) Exchange(com.predic8.membrane.core.exchange.Exchange) ProxyConfiguration(com.predic8.membrane.core.transport.http.client.ProxyConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Outcome(com.predic8.membrane.core.interceptor.Outcome) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) HttpClientConfiguration(com.predic8.membrane.core.transport.http.client.HttpClientConfiguration) Test(org.junit.Test)

Example 5 with ProxyConfiguration

use of com.predic8.membrane.core.transport.http.client.ProxyConfiguration in project service-proxy by membrane.

the class ViaProxyTest method setUp.

@Before
public void setUp() throws Exception {
    ProxyConfiguration proxy = new ProxyConfiguration();
    proxy.setHost("localhost");
    proxy.setPort(3128);
    proxyRouter = new HttpRouter(proxy);
    proxyRouter.getRuleManager().addProxy(new ProxyRule(new ProxyRuleKey(3128)), RuleDefinitionSource.MANUAL);
    proxyRouter.init();
    router = new HttpRouter();
    router.getRuleManager().addProxyAndOpenPortIfNew(new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 4000), "thomas-bayer.com", 80));
    router.init();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ProxyConfiguration(com.predic8.membrane.core.transport.http.client.ProxyConfiguration) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) ProxyRule(com.predic8.membrane.core.rules.ProxyRule) ProxyRuleKey(com.predic8.membrane.core.rules.ProxyRuleKey) HttpRouter(com.predic8.membrane.core.HttpRouter) Before(org.junit.Before)

Aggregations

ProxyConfiguration (com.predic8.membrane.core.transport.http.client.ProxyConfiguration)3 Before (org.junit.Before)2 HttpRouter (com.predic8.membrane.core.HttpRouter)1 Router (com.predic8.membrane.core.Router)1 KeyStore (com.predic8.membrane.core.config.security.KeyStore)1 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 TrustStore (com.predic8.membrane.core.config.security.TrustStore)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)1 CountInterceptor (com.predic8.membrane.core.interceptor.CountInterceptor)1 DispatchingInterceptor (com.predic8.membrane.core.interceptor.DispatchingInterceptor)1 HTTPClientInterceptor (com.predic8.membrane.core.interceptor.HTTPClientInterceptor)1 Interceptor (com.predic8.membrane.core.interceptor.Interceptor)1 Outcome (com.predic8.membrane.core.interceptor.Outcome)1 RuleMatchingInterceptor (com.predic8.membrane.core.interceptor.RuleMatchingInterceptor)1 UserFeatureInterceptor (com.predic8.membrane.core.interceptor.UserFeatureInterceptor)1 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)1 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)1 ProxyRuleKey (com.predic8.membrane.core.rules.ProxyRuleKey)1 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)1