use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.
the class NetClientImpl method connectInternal.
public void connectInternal(ProxyOptions proxyOptions, SocketAddress remoteAddress, SocketAddress peerAddress, String serverName, boolean ssl, boolean useAlpn, boolean registerWriteHandlers, Promise<NetSocket> connectHandler, ContextInternal context, int remainingAttempts) {
checkClosed();
EventLoop eventLoop = context.nettyEventLoop();
if (eventLoop.inEventLoop()) {
Objects.requireNonNull(connectHandler, "No null connectHandler accepted");
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoop);
bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
vertx.transport().configure(options, remoteAddress.isDomainSocket(), bootstrap);
ChannelProvider channelProvider = new ChannelProvider(bootstrap, sslHelper, context).proxyOptions(proxyOptions);
channelProvider.handler(ch -> connected(context, ch, connectHandler, remoteAddress, channelProvider.applicationProtocol(), registerWriteHandlers));
io.netty.util.concurrent.Future<Channel> fut = channelProvider.connect(remoteAddress, peerAddress, serverName, ssl, useAlpn);
fut.addListener((GenericFutureListener<io.netty.util.concurrent.Future<Channel>>) future -> {
if (!future.isSuccess()) {
Throwable cause = future.cause();
boolean connectError = cause instanceof ConnectException || cause instanceof FileNotFoundException;
if (connectError && (remainingAttempts > 0 || remainingAttempts == -1)) {
context.emit(v -> {
log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds");
vertx.setTimer(options.getReconnectInterval(), tid -> connectInternal(proxyOptions, remoteAddress, peerAddress, serverName, ssl, useAlpn, registerWriteHandlers, connectHandler, context, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1));
});
} else {
failed(context, null, cause, connectHandler);
}
}
});
} else {
eventLoop.execute(() -> connectInternal(proxyOptions, remoteAddress, peerAddress, serverName, ssl, useAlpn, registerWriteHandlers, connectHandler, context, remainingAttempts));
}
}
use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.
the class ProxyOptionsTest method testDefaultOptionsJson.
@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());
}
use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.
the class ProxyOptionsTest method testCopyProxyOptions.
@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());
}
use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.
the class HttpClientImpl method webSocket.
private void webSocket(WebSocketConnectOptions connectOptions, PromiseInternal<WebSocket> promise) {
ProxyOptions proxyOptions = getProxyOptions(connectOptions.getProxyOptions());
int port = getPort(connectOptions);
String host = getHost(connectOptions);
SocketAddress addr = SocketAddress.inetSocketAddress(port, host);
if (proxyFilter != null) {
if (!proxyFilter.test(addr)) {
proxyOptions = null;
}
}
EndpointKey key = new EndpointKey(connectOptions.isSsl() != null ? connectOptions.isSsl() : options.isSsl(), proxyOptions, addr, addr);
ContextInternal ctx = promise.context();
EventLoopContext eventLoopContext;
if (ctx instanceof EventLoopContext) {
eventLoopContext = (EventLoopContext) ctx;
} else {
eventLoopContext = vertx.createEventLoopContext(ctx.nettyEventLoop(), ctx.workerPool(), ctx.classLoader());
}
webSocketCM.getConnection(eventLoopContext, key, ar -> {
if (ar.succeeded()) {
Http1xClientConnection conn = (Http1xClientConnection) ar.result();
conn.toWebSocket(ctx, connectOptions.getURI(), connectOptions.getHeaders(), connectOptions.getAllowOriginHeader(), connectOptions.getVersion(), connectOptions.getSubProtocols(), HttpClientImpl.this.options.getMaxWebSocketFrameSize(), promise);
} else {
promise.fail(ar.cause());
}
});
}
use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.
the class HttpClientImpl method doRequest.
private void doRequest(RequestOptions request, PromiseInternal<HttpClientRequest> promise) {
String host = getHost(request);
int port = getPort(request);
SocketAddress server = request.getServer();
if (server == null) {
server = SocketAddress.inetSocketAddress(port, host);
}
ProxyOptions proxyOptions = getProxyOptions(request.getProxyOptions());
HttpMethod method = request.getMethod();
String requestURI = request.getURI();
Boolean ssl = request.isSsl();
MultiMap headers = request.getHeaders();
long timeout = request.getTimeout();
Boolean followRedirects = request.getFollowRedirects();
Objects.requireNonNull(method, "no null method accepted");
Objects.requireNonNull(host, "no null host accepted");
Objects.requireNonNull(requestURI, "no null requestURI accepted");
boolean useAlpn = this.options.isUseAlpn();
boolean useSSL = ssl != null ? ssl : this.options.isSsl();
if (!useAlpn && useSSL && this.options.getProtocolVersion() == HttpVersion.HTTP_2) {
throw new IllegalArgumentException("Must enable ALPN when using H2");
}
checkClosed();
if (proxyFilter != null) {
if (!proxyFilter.test(server)) {
proxyOptions = null;
}
}
if (proxyOptions != null) {
if (!useSSL && proxyOptions.getType() == ProxyType.HTTP) {
// If the requestURI is as not absolute URI then we do not recompute one for the proxy
if (!ABS_URI_START_PATTERN.matcher(requestURI).find()) {
int defaultPort = 80;
String addPort = (port != -1 && port != defaultPort) ? (":" + port) : "";
requestURI = (ssl == Boolean.TRUE ? "https://" : "http://") + host + addPort + requestURI;
}
if (proxyOptions.getUsername() != null && proxyOptions.getPassword() != null) {
if (headers == null) {
headers = HttpHeaders.headers();
}
headers.add("Proxy-Authorization", "Basic " + Base64.getEncoder().encodeToString((proxyOptions.getUsername() + ":" + proxyOptions.getPassword()).getBytes()));
}
server = SocketAddress.inetSocketAddress(proxyOptions.getPort(), proxyOptions.getHost());
proxyOptions = null;
}
}
String peerHost = host;
if (peerHost.endsWith(".")) {
peerHost = peerHost.substring(0, peerHost.length() - 1);
}
SocketAddress peerAddress = SocketAddress.inetSocketAddress(port, peerHost);
doRequest(method, peerAddress, server, host, port, useSSL, requestURI, headers, request.getTraceOperation(), timeout, followRedirects, proxyOptions, promise);
}
Aggregations