/** * test socks5 proxy for accessing arbitrary server port with authentication. */ @Test public void testWithSocks5ProxyAuth() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setPort(11080) .setUsername("username").setPassword("username")); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new SocksProxy("username"); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { assertTrue(ar2.succeeded()); testComplete(); }); }); await(); }
@Test public void testProxyConnectError() { testConnectErrorNotifiesOnEventLoop(new NetClientOptions() .setProxyOptions(new ProxyOptions() .setPort(1234) .setType(ProxyType.SOCKS5) .setHost("localhost"))); }
@Test public void testCopyProxyOptions() { ProxyOptions options = new ProxyOptions(); options.setType(randType); options.setHost(randHost); options.setPort(randPort); options.setUsername(randUsername); options.setPassword(randPassword); ProxyOptions copy = new ProxyOptions(options); assertEquals(randType, copy.getType()); assertEquals(randPort, copy.getPort()); assertEquals(randHost, copy.getHost()); assertEquals(randUsername, copy.getUsername()); assertEquals(randPassword, copy.getPassword()); }
/** * Copy constructor. * * @param other the options to copy */ public ProxyOptions(ProxyOptions other) { host = other.getHost(); port = other.getPort(); username = other.getUsername(); password = other.getPassword(); type = other.getType(); }
case "host": if (member.getValue() instanceof String) { obj.setHost((String)member.getValue()); obj.setPassword((String)member.getValue()); obj.setPort(((Number)member.getValue()).intValue()); obj.setType(io.vertx.core.net.ProxyType.valueOf((String)member.getValue())); obj.setUsername((String)member.getValue());
/** * test http connect proxy for accessing a arbitrary server port * note that this may not work with a "real" proxy since there are usually access rules defined * that limit the target host and ports (e.g. connecting to localhost or to port 25 may not be allowed) */ @Test public void testWithHttpConnectProxy() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setPort(13128)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new HttpProxy(null); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("localhost:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
private WebClientOptions initWebClientOptions(Request request) { WebClientOptions options = new WebClientOptions(); options.setKeepAlive(true).setReuseAddress(true).setFollowRedirects(true); if (Preconditions.isNotBlank(request.getUserAgent())) { options.setUserAgent(request.getUserAgent()); } if (Preconditions.isNotBlank(request.getProxy())) { ProxyOptions proxyOptions = new ProxyOptions(); proxyOptions.setHost(request.getProxy().getIp()); proxyOptions.setPort(request.getProxy().getPort()); options.setProxyOptions(proxyOptions); } if (Preconditions.isNotBlank(request.getHeader())) { header = request.getHeader(); } return options; }
@Test public void testHttpProxyFtpRequest() throws Exception { startProxy(null, ProxyType.HTTP); proxy.setForceUri("http://" + DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT); server.requestHandler(req -> req.response().setStatusCode(200).end()); startServer(); WebClientOptions options = new WebClientOptions(); options.setProxyOptions(new ProxyOptions().setPort(proxy.getPort())); WebClient client = WebClient.create(vertx, options); client .getAbs("ftp://ftp.gnu.org/gnu/") .send(ar -> { if (ar.succeeded()) { // Obtain response HttpResponse<Buffer> response = ar.result(); assertEquals(200, response.statusCode()); assertEquals("ftp://ftp.gnu.org/gnu/", proxy.getLastUri()); testComplete(); } else { fail(ar.cause()); } }); await(); }
/** * Copy constructor * * @param other the options to copy */ public ClientOptionsBase(ClientOptionsBase other) { super(other); this.connectTimeout = other.getConnectTimeout(); this.trustAll = other.isTrustAll(); this.metricsName = other.metricsName; this.proxyOptions = other.proxyOptions != null ? new ProxyOptions(other.proxyOptions) : null; this.localAddress = other.localAddress; }
if (options != null && !ssl && options.getType()== ProxyType.HTTP) {
static void toJson(ProxyOptions obj, java.util.Map<String, Object> json) { if (obj.getHost() != null) { json.put("host", obj.getHost()); } if (obj.getPassword() != null) { json.put("password", obj.getPassword()); } json.put("port", obj.getPort()); if (obj.getType() != null) { json.put("type", obj.getType().name()); } if (obj.getUsername() != null) { json.put("username", obj.getUsername()); } } }
/** * test socks4a proxy for accessing arbitrary server port. */ @Test public void testWithSocks4aProxy() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new Socks4Proxy(null); proxy.start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("localhost:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
case "proxyOptions": if (member.getValue() instanceof JsonObject) { obj.setProxyOptions(new io.vertx.core.net.ProxyOptions((JsonObject)member.getValue()));
proxyType = options.getProxyOptions() != null ? options.getProxyOptions().getType() : null; httpCM.start(); websocketCM.start();
private void proxyTest(int error, String username, String url, Handler<AsyncResult<HttpClientResponse>> assertResponse) throws Exception { startProxy(error, username); final HttpClientOptions options = new HttpClientOptions() .setSsl(url.startsWith("https")) .setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost("localhost") .setPort(proxy.getPort())); HttpClient client = vertx.createHttpClient(options); client.getAbs(url, assertResponse).end(); await(); }
@Test public void testProxyOptions() { ProxyOptions options = new ProxyOptions(); assertEquals(ProxyOptions.DEFAULT_TYPE, options.getType()); assertEquals(options, options.setType(randType)); assertEquals(randType, options.getType()); assertNullPointerException(() -> options.setType(null)); assertEquals(ProxyOptions.DEFAULT_HOST, options.getHost()); assertEquals(options, options.setHost(randHost)); assertEquals(randHost, options.getHost()); assertNullPointerException(() -> options.setHost(null)); assertEquals(ProxyOptions.DEFAULT_PORT, options.getPort()); assertEquals(options, options.setPort(randPort)); assertEquals(randPort, options.getPort()); assertIllegalArgumentException(() -> options.setPort(-1)); assertIllegalArgumentException(() -> options.setPort(65536)); assertEquals(null, options.getUsername()); assertEquals(options, options.setUsername(randUsername)); assertEquals(randUsername, options.getUsername()); assertEquals(null, options.getPassword()); assertEquals(options, options.setPassword(randPassword)); assertEquals(randPassword, options.getPassword()); }
@Test public void testDefaultOptionsJson() { ProxyOptions def = new ProxyOptions(); ProxyOptions options = new ProxyOptions(new JsonObject()); assertEquals(def.getType(), options.getType()); assertEquals(def.getPort(), options.getPort()); assertEquals(def.getHost(), options.getHost()); assertEquals(def.getUsername(), options.getUsername()); assertEquals(def.getPassword(), options.getPassword()); }
/** * test socks4a proxy for accessing arbitrary server port using an already resolved address. */ @Test public void testWithSocks4LocalResolver() throws Exception { NetClientOptions clientOptions = new NetClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS4).setPort(11080)); NetClient client = vertx.createNetClient(clientOptions); server.connectHandler(sock -> { }); proxy = new Socks4Proxy(null).start(vertx); server.listen(1234, "localhost", ar -> { assertTrue(ar.succeeded()); client.connect(1234, "127.0.0.1", ar2 -> { if (ar2.failed()) { log.warn("failed", ar2.cause()); } assertTrue(ar2.succeeded()); // make sure we have gone through the proxy assertEquals("127.0.0.1:1234", proxy.getLastUri()); testComplete(); }); }); await(); }
/** * Copy constructor * * @param other the options to copy */ public ClientOptionsBase(ClientOptionsBase other) { super(other); this.connectTimeout = other.getConnectTimeout(); this.trustAll = other.isTrustAll(); this.metricsName = other.metricsName; this.proxyOptions = other.proxyOptions != null ? new ProxyOptions(other.proxyOptions) : null; this.localAddress = other.localAddress; }
if (options != null && !ssl && options.getType()== ProxyType.HTTP) {