io.vertx.core.net.NetServerOptions Java Examples
The following examples show how to use
io.vertx.core.net.NetServerOptions.
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: SecureScuttlebuttVertxServer.java From incubator-tuweni with Apache License 2.0 | 6 votes |
/** * Starts the server. * * @return a handle to the completion of the operation */ public AsyncCompletion start() { server = vertx .createNetServer( new NetServerOptions().setTcpKeepAlive(true).setHost(addr.getHostString()).setPort(addr.getPort())); CompletableAsyncCompletion completion = AsyncCompletion.incomplete(); server.connectHandler(new NetSocketHandler()::handle); server.listen(res -> { if (res.failed()) { completion.completeExceptionally(res.cause()); } else { completion.complete(); } }); return completion; }
Example #2
Source File: AbstractServiceBaseTest.java From hono with Eclipse Public License 2.0 | 6 votes |
/** * Verifies that only TLSv1.2 is enabled by default. * */ @Test public void testAddTlsKeyCertOptionsDisablesAllProtocolVersionsButTls12() { // GIVEN a default configuration for TLS final ServiceConfigProperties config = new ServiceConfigProperties(); config.setKeyStorePath(PREFIX_KEY_PATH + "/authServerKeyStore.p12"); // WHEN configuring a service using the configuration final AbstractServiceBase<ServiceConfigProperties> service = createService(config); final NetServerOptions options = new NetServerOptions(); service.addTlsKeyCertOptions(options); // THEN SSL is enabled and only TLSv1.2 is enabled assertTrue(options.isSsl()); assertTrue(options.getEnabledSecureTransportProtocols().contains("TLSv1.2")); assertTrue(options.getEnabledSecureTransportProtocols().size() == 1); }
Example #3
Source File: AbstractServiceBaseTest.java From hono with Eclipse Public License 2.0 | 6 votes |
/** * Verifies that only the configured TLS protocols are enabled. * */ @Test public void testAddTlsKeyCertOptionsDisablesTlsProtocolVersions() { // GIVEN a configuration with only TLS 1 and TLS 1.1 enabled final ServiceConfigProperties config = new ServiceConfigProperties(); config.setKeyStorePath(PREFIX_KEY_PATH + "/authServerKeyStore.p12"); config.setSecureProtocols(Arrays.asList("TLSv1", "TLSv1.1")); // WHEN configuring a service using the configuration final AbstractServiceBase<ServiceConfigProperties> service = createService(config); final NetServerOptions options = new NetServerOptions(); service.addTlsKeyCertOptions(options); // THEN SSL is enabled and only TLSv1 and TLSv1.1 are supported assertTrue(options.isSsl()); assertTrue(options.getEnabledSecureTransportProtocols().size() == 2); assertTrue(options.getEnabledSecureTransportProtocols().contains("TLSv1")); assertTrue(options.getEnabledSecureTransportProtocols().contains("TLSv1.1")); }
Example #4
Source File: AbstractServiceBaseTest.java From hono with Eclipse Public License 2.0 | 6 votes |
/** * Verifies that SNI TLS is enabled. * */ @Test public void testSNIEnable() { // GIVEN a default TLS configuration final ServiceConfigProperties config = new ServiceConfigProperties(); config.setKeyStorePath(PREFIX_KEY_PATH + "/authServerKeyStore.p12"); config.setSecureProtocols(Arrays.asList("TLSv1.1")); config.setSni(true); // WHEN configuring a service using the configuration final AbstractServiceBase<ServiceConfigProperties> service = createService(config); final NetServerOptions options = new NetServerOptions(); service.addTlsKeyCertOptions(options); // THEN SSL is enabled and also SNI is enabled assertTrue(options.isSsl()); assertTrue(options.isSni()); }
Example #5
Source File: TestTcpServer.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testTcpServerSSL(@Mocked Vertx vertx, @Mocked AsyncResultCallback<InetSocketAddress> callback, @Mocked NetServer netServer) { new Expectations() { { vertx.createNetServer((NetServerOptions) any); result = netServer; netServer.connectHandler((Handler) any); netServer.listen(anyInt, anyString, (Handler) any); } }; URIEndpointObject endpointObject = new URIEndpointObject("highway://127.0.0.1:6663?sslEnabled=true"); TcpServer server = new TcpServerForTest(endpointObject); // assert done in Expectations server.init(vertx, "", callback); }
Example #6
Source File: VertxNetUtils.java From Lealone-Plugins with Apache License 2.0 | 6 votes |
public static NetServerOptions getNetServerOptions(EncryptionOptions eo) { if (eo == null) { return new NetServerOptions(); } NetServerOptions options = new NetServerOptions().setSsl(true); options.setKeyStoreOptions(new JksOptions().setPath(eo.keystore).setPassword(eo.keystore_password)); if (eo.truststore != null) { if (eo.require_client_auth) { options.setClientAuth(ClientAuth.REQUIRED); } options.setTrustStoreOptions(new JksOptions().setPath(eo.truststore).setPassword(eo.truststore_password)); } if (eo.cipher_suites != null) { for (String cipherSuitee : eo.cipher_suites) options.addEnabledCipherSuite(cipherSuitee); } return options; }
Example #7
Source File: MQTTBroker.java From vertx-mqtt-broker with Apache License 2.0 | 6 votes |
private void startTcpServer(ConfigParser c) { int port = c.getPort(); String keyPath = c.getTlsKeyPath(); String certPath = c.getTlsCertPath(); boolean tlsEnabled = c.isTlsEnabled(); int idleTimeout = c.getSocketIdleTimeout(); // MQTT over TCP NetServerOptions opt = new NetServerOptions() .setTcpKeepAlive(true) .setIdleTimeout(idleTimeout) // in seconds; 0 means "don't timeout". .setPort(port); if(tlsEnabled) { opt.setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions() .setKeyPath(keyPath) .setCertPath(certPath) ); } NetServer netServer = vertx.createNetServer(opt); Map<String, MQTTSession> sessions = new MonitoredMap<>(); netServer.connectHandler(netSocket -> { MQTTNetSocket mqttNetSocket = new MQTTNetSocket(vertx, c, netSocket, sessions); mqttNetSocket.start(); }).listen(); }
Example #8
Source File: VertxRLPxService.java From cava with Apache License 2.0 | 6 votes |
@Override public AsyncCompletion start() { if (started.compareAndSet(false, true)) { handlers = new LinkedHashMap<SubProtocol, SubProtocolHandler>(); for (SubProtocol subProtocol : subProtocols) { handlers.put(subProtocol, subProtocol.createHandler(this)); } client = vertx.createNetClient(new NetClientOptions()); server = vertx .createNetServer(new NetServerOptions().setPort(listenPort).setHost(networkInterface).setTcpKeepAlive(true)) .connectHandler(this::receiveMessage); CompletableAsyncCompletion complete = AsyncCompletion.incomplete(); server.listen(res -> { if (res.succeeded()) { complete.complete(); } else { complete.completeExceptionally(res.cause()); } }); return complete; } else { return AsyncCompletion.completed(); } }
Example #9
Source File: VertxRLPxService.java From incubator-tuweni with Apache License 2.0 | 6 votes |
@Override public AsyncCompletion start() { if (started.compareAndSet(false, true)) { handlers = new LinkedHashMap<SubProtocol, SubProtocolHandler>(); for (SubProtocol subProtocol : subProtocols) { handlers.put(subProtocol, subProtocol.createHandler(this)); } client = vertx.createNetClient(new NetClientOptions()); server = vertx .createNetServer(new NetServerOptions().setPort(listenPort).setHost(networkInterface).setTcpKeepAlive(true)) .connectHandler(this::receiveMessage); CompletableAsyncCompletion complete = AsyncCompletion.incomplete(); server.listen(res -> { if (res.succeeded()) { complete.complete(); } else { complete.completeExceptionally(res.cause()); } }); return complete; } else { return AsyncCompletion.completed(); } }
Example #10
Source File: TcpServerProvider.java From jetlinks-community with Apache License 2.0 | 6 votes |
@Nonnull @Override public Mono<TcpServerProperties> createConfig(@Nonnull NetworkProperties properties) { return Mono.defer(() -> { TcpServerProperties config = FastBeanCopier.copy(properties.getConfigurations(), new TcpServerProperties()); config.setId(properties.getId()); if (config.getOptions() == null) { config.setOptions(new NetServerOptions()); } if (config.isSsl()) { config.getOptions().setSsl(true); return certificateManager.getCertificate(config.getCertId()) .map(VertxKeyCertTrustOptions::new) .doOnNext(config.getOptions()::setKeyCertOptions) .doOnNext(config.getOptions()::setTrustOptions) .thenReturn(config); } return Mono.just(config); }); }
Example #11
Source File: StratumServer.java From besu with Apache License 2.0 | 6 votes |
public CompletableFuture<?> start() { if (started.compareAndSet(false, true)) { logger.info("Starting stratum server on {}:{}", networkInterface, port); server = vertx.createNetServer( new NetServerOptions().setPort(port).setHost(networkInterface).setTcpKeepAlive(true)); CompletableFuture<?> result = new CompletableFuture<>(); server.connectHandler(this::handle); server.listen( res -> { if (res.failed()) { result.completeExceptionally( new StratumServerException( String.format( "Failed to bind Stratum Server listener to %s:%s: %s", networkInterface, port, res.cause().getMessage()))); } else { result.complete(null); } }); return result; } return CompletableFuture.completedFuture(null); }
Example #12
Source File: MetricsTestBase.java From vertx-dropwizard-metrics with Apache License 2.0 | 5 votes |
protected NetServer createNetServer(NetServerOptions options) { NetServer server = vertx.createNetServer(options); toClose.add(() -> { CountDownLatch latch = new CountDownLatch(1); server.close(ar -> { latch.countDown(); }); awaitLatch(latch); return null; }); return server; }
Example #13
Source File: VertxNetServer.java From Lealone-Plugins with Apache License 2.0 | 5 votes |
private NetServer getNetServer() { NetServerOptions nso; if (ssl) { ServerEncryptionOptions options = getServerEncryptionOptions(); nso = VertxNetUtils.getNetServerOptions(options); } else { nso = VertxNetUtils.getNetServerOptions(null); } nso.setHost(host); nso.setPort(port); NetServer server = vertx.createNetServer(nso); return server; }
Example #14
Source File: TcpEventBusBridgeImpl.java From vertx-tcp-eventbus-bridge with Apache License 2.0 | 5 votes |
public TcpEventBusBridgeImpl(Vertx vertx, BridgeOptions options, NetServerOptions netServerOptions, Handler<BridgeEvent> eventHandler) { this.eb = vertx.eventBus(); this.options = options != null ? options : new BridgeOptions(); this.bridgeEventHandler = eventHandler; server = vertx.createNetServer(netServerOptions == null ? new NetServerOptions() : netServerOptions); server.connectHandler(this::handler); }
Example #15
Source File: ZookeeperLeaderFinderTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
FakeZk(int id, Function<Integer, Boolean> isLeader) { this.id = id; this.isLeader = isLeader; NetServerOptions nso = new NetServerOptions() .setSsl(true) .setKeyCertOptions(zkCertificate.keyCertOptions()) .setTrustOptions(coCertificate.trustOptions()); netServer = vertx.createNetServer(nso); }
Example #16
Source File: AuthorizationTests.java From vertx-mqtt-broker with Apache License 2.0 | 5 votes |
private NetServer startTcpBroker(JsonObject conf) { ConfigParser c = new ConfigParser(conf); NetServerOptions opt = new NetServerOptions().setTcpKeepAlive(true) .setIdleTimeout(conf.getInteger("socket_idle_timeout")).setPort(conf.getInteger("tcp_port")); NetServer netServer = vertx.createNetServer(opt); netServer.connectHandler(netSocket -> { Map<String, MQTTSession> sessions = new HashMap<>(); MQTTNetSocket mqttNetSocket = new MQTTNetSocket(vertx, c, netSocket, sessions); mqttNetSocket.start(); }).listen(); return netServer; }
Example #17
Source File: AbstractServiceBase.java From hono with Eclipse Public License 2.0 | 5 votes |
/** * Adds TLS key & certificate configuration to a given set of server options. * <p> * If <em>config</em> contains key & certificate configuration it is added to * the given server options and the <em>ssl</em> flag is set to {@code true}. * <p> * If the server option's ssl flag is set, then the protocols from the <em>disabledTlsVersions</em> * configuration property are removed from the options (and thus disabled). * <p> * Finally, if a working instance of Netty's <em>tcnative</em> library is found, then * it is used instead of the JDK's default SSL engine. * * @param serverOptions The options to add configuration to. */ protected final void addTlsKeyCertOptions(final NetServerOptions serverOptions) { final KeyCertOptions keyCertOptions = getConfig().getKeyCertOptions(); if (keyCertOptions != null) { serverOptions.setSsl(true).setKeyCertOptions(keyCertOptions); } if (serverOptions.isSsl()) { final boolean isOpenSslAvailable = OpenSsl.isAvailable(); final boolean supportsKeyManagerFactory = OpenSsl.supportsKeyManagerFactory(); final boolean useOpenSsl = getConfig().isNativeTlsRequired() || (isOpenSslAvailable && supportsKeyManagerFactory); log.debug("OpenSSL [available: {}, supports KeyManagerFactory: {}]", isOpenSslAvailable, supportsKeyManagerFactory); if (useOpenSsl) { log.info("using OpenSSL [version: {}] instead of JDK's default SSL engine", OpenSsl.versionString()); serverOptions.setSslEngineOptions(new OpenSSLEngineOptions()); } else { log.info("using JDK's default SSL engine"); } serverOptions.getEnabledSecureTransportProtocols() .forEach(protocol -> serverOptions.removeEnabledSecureTransportProtocol(protocol)); getConfig().getSecureProtocols().forEach(protocol -> { log.info("enabling secure protocol [{}]", protocol); serverOptions.addEnabledSecureTransportProtocol(protocol); }); serverOptions.setSni(getConfig().isSni()); log.info("Service supports TLS ServerNameIndication: {}", getConfig().isSni()); } }
Example #18
Source File: TcpServerProviderTest.java From jetlinks-community with Apache License 2.0 | 5 votes |
@BeforeAll static void init() { TcpServerProperties properties = TcpServerProperties.builder() .id("test") .port(8080) .options(new NetServerOptions()) .parserType(PayloadParserType.FIXED_LENGTH) .parserConfiguration(Collections.singletonMap("size", 5)) .build(); TcpServerProvider provider = new TcpServerProvider((id) -> Mono.empty(), Vertx.vertx(), new DefaultPayloadParserBuilder()); tcpServer = provider.createNetwork(properties); }
Example #19
Source File: Server.java From redpipe with Apache License 2.0 | 5 votes |
private Completable startVertx(VertxResteasyDeployment deployment) { return Completable.defer(() -> { Router router = Router.router(vertx); AppGlobals globals = AppGlobals.get(); globals.setRouter(router); VertxPluginRequestHandler resteasyHandler = new VertxPluginRequestHandler(vertx, deployment, plugins); return doOnPlugins(plugin -> plugin.preRoute()) .doOnComplete(() -> { setupRoutes(router); router.route().handler(routingContext -> { ResteasyProviderFactory.pushContext(RoutingContext.class, routingContext); ResteasyProviderFactory.pushContext(io.vertx.rxjava.ext.web.RoutingContext.class, io.vertx.rxjava.ext.web.RoutingContext.newInstance(routingContext.getDelegate())); resteasyHandler.handle(routingContext.request()); }); }).concatWith(doOnPlugins(plugin -> plugin.postRoute())) .concatWith(Completable.defer(() -> { // Start the front end server using the Jax-RS controller int port = globals.getConfig().getInteger("http_port", 9000); String host = globals.getConfig().getString("http_host", NetServerOptions.DEFAULT_HOST); return vertx.createHttpServer() .requestHandler(router::accept) .rxListen(port, host) .doOnSuccess(server -> System.out.println("Server started on port " + server.actualPort())) .doOnError(t -> t.printStackTrace()) .ignoreElement(); })); }); }
Example #20
Source File: SecureScuttlebuttVertxServer.java From cava with Apache License 2.0 | 5 votes |
/** * Starts the server. * * @return a handle to the completion of the operation */ public AsyncCompletion start() { server = vertx.createNetServer( new NetServerOptions().setTcpKeepAlive(true).setHost(addr.getHostString()).setPort(addr.getPort())); CompletableAsyncCompletion completion = AsyncCompletion.incomplete(); server.connectHandler(new NetSocketHandler()::handle); server.listen(res -> { if (res.failed()) { completion.completeExceptionally(res.cause()); } else { completion.complete(); } }); return completion; }
Example #21
Source File: VertxTLSBuilder.java From servicecomb-java-chassis with Apache License 2.0 | 5 votes |
public static NetServerOptions buildNetServerOptions(SSLOption sslOption, SSLCustom sslCustom, NetServerOptions netServerOptions) { buildTCPSSLOptions(sslOption, sslCustom, netServerOptions); if (sslOption.isAuthPeer()) { netServerOptions.setClientAuth(ClientAuth.REQUIRED); } else { netServerOptions.setClientAuth(ClientAuth.REQUEST); } return netServerOptions; }
Example #22
Source File: VertxMetricsImpl.java From vertx-micrometer-metrics with Apache License 2.0 | 5 votes |
@Override public TCPMetrics<?> createNetServerMetrics(NetServerOptions netServerOptions, SocketAddress socketAddress) { if (netServerMetrics != null) { return netServerMetrics.forAddress(socketAddress); } return DummyVertxMetrics.DummyTCPMetrics.INSTANCE; }
Example #23
Source File: AbstractServiceBase.java From hono with Eclipse Public License 2.0 | 5 votes |
/** * Adds TLS trust anchor configuration to a given set of server options. * <p> * The options for configuring the server side trust anchor are * determined by invoking the {@link #getServerTrustOptions()} method. * However, the trust anchor options returned by that method will only be added to the * given server options if its <em>ssl</em> flag is set to {@code true} and if its * <em>trustOptions</em> property is {@code null}. * * @param serverOptions The options to add configuration to. */ protected final void addTlsTrustOptions(final NetServerOptions serverOptions) { if (serverOptions.isSsl() && serverOptions.getTrustOptions() == null) { final TrustOptions trustOptions = getServerTrustOptions(); if (trustOptions != null) { serverOptions.setTrustOptions(trustOptions).setClientAuth(ClientAuth.REQUEST); log.info("enabling client authentication using certificates [{}]", trustOptions.getClass().getName()); } } }
Example #24
Source File: TcpEventBusBridgeImpl.java From vertx-tcp-eventbus-bridge with Apache License 2.0 | 4 votes |
public TcpEventBusBridgeImpl(Vertx vertx, BridgeOptions options, NetServerOptions netServerOptions) { this(vertx, options, netServerOptions, null); }
Example #25
Source File: EventBusBridgeServerVerticle.java From vertx-mqtt-broker with Apache License 2.0 | 4 votes |
@Override public void start() throws Exception { address = MQTTSession.ADDRESS; JsonObject conf = config(); localBridgePort = conf.getInteger("local_bridge_port", 7007); idleTimeout = conf.getInteger("socket_idle_timeout", 120); ssl_cert_key = conf.getString("ssl_cert_key"); ssl_cert = conf.getString("ssl_cert"); ssl_trust = conf.getString("ssl_trust"); // [TCP -> BUS] listen TCP publish to BUS NetServerOptions opt = new NetServerOptions() .setTcpKeepAlive(true) .setIdleTimeout(idleTimeout) .setPort(localBridgePort) ; if(ssl_cert_key != null && ssl_cert != null && ssl_trust != null) { opt.setSsl(true).setClientAuth(ClientAuth.REQUIRED) .setPemKeyCertOptions(new PemKeyCertOptions() .setKeyPath(ssl_cert_key) .setCertPath(ssl_cert) ) .setPemTrustOptions(new PemTrustOptions() .addCertPath(ssl_trust) ) ; } netServer = vertx.createNetServer(opt); netServer.connectHandler(sock -> { final EventBusNetBridge ebnb = new EventBusNetBridge(sock, vertx.eventBus(), address); sock.closeHandler(aVoid -> { logger.info("Bridge Server - closed connection from client ip: " + sock.remoteAddress()); ebnb.stop(); }); sock.exceptionHandler(throwable -> { logger.error("Bridge Server - Exception: " + throwable.getMessage(), throwable); ebnb.stop(); }); logger.info("Bridge Server - new connection from client ip: " + sock.remoteAddress()); RecordParser parser = ebnb.initialHandhakeProtocolParser(); sock.handler(parser::handle); }).listen(); }
Example #26
Source File: VertxMetricsImpl.java From vertx-dropwizard-metrics with Apache License 2.0 | 4 votes |
@Override public TCPMetrics<?> createNetServerMetrics(NetServerOptions options, SocketAddress localAddress) { String baseName = MetricRegistry.name(nameOf("net.servers"), TCPMetricsImpl.addressName(localAddress)); return new TCPMetricsImpl(registry, baseName); }
Example #27
Source File: VertxSubstitutions.java From quarkus with Apache License 2.0 | 4 votes |
@Substitute private NetServerOptions getServerOptions() { throw new RuntimeException("Not Implemented"); }
Example #28
Source File: VertxNetUtils.java From Lealone-Plugins with Apache License 2.0 | 4 votes |
public static NetServer createNetServer(Vertx vertx, EncryptionOptions eo) { NetServerOptions netServerOptions = VertxNetUtils.getNetServerOptions(eo); NetServer server = vertx.createNetServer(netServerOptions); return server; }
Example #29
Source File: DefaultVertxMetrics.java From servicecomb-java-chassis with Apache License 2.0 | 4 votes |
@Override public TCPMetrics<?> createNetServerMetrics(NetServerOptions options, SocketAddress localAddress) { DefaultServerEndpointMetric endpointMetric = serverEndpointMetricMap .computeIfAbsent(localAddress.toString(), DefaultServerEndpointMetric::new); return new DefaultTcpServerMetrics(endpointMetric); }
Example #30
Source File: TcpServer.java From servicecomb-java-chassis with Apache License 2.0 | 4 votes |
public void init(Vertx vertx, String sslKey, AsyncResultCallback<InetSocketAddress> callback) { NetServer netServer; if (endpointObject.isSslEnabled()) { SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(sslKey, null); SSLOption sslOption; if (factory == null) { sslOption = SSLOption.buildFromYaml(sslKey); } else { sslOption = factory.createSSLOption(); } SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass()); NetServerOptions serverOptions = new NetServerOptions(); VertxTLSBuilder.buildNetServerOptions(sslOption, sslCustom, serverOptions); netServer = vertx.createNetServer(serverOptions); } else { netServer = vertx.createNetServer(); } netServer.connectHandler(netSocket -> { DefaultTcpServerMetrics serverMetrics = (DefaultTcpServerMetrics) ((NetSocketImpl) netSocket).metrics(); DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric(); long connectedCount = endpointMetric.getCurrentConnectionCount(); int connectionLimit = getConnectionLimit(); if (connectedCount > connectionLimit) { netSocket.close(); endpointMetric.onRejectByConnectionLimit(); return; } TcpServerConnection connection = createTcpServerConnection(); connection.init(netSocket); }); netServer.exceptionHandler(e -> { LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e)); }); InetSocketAddress socketAddress = endpointObject.getSocketAddress(); netServer.listen(socketAddress.getPort(), socketAddress.getHostString(), ar -> { if (ar.succeeded()) { callback.success(socketAddress); return; } // 监听失败 String msg = String.format("listen failed, address=%s", socketAddress.toString()); callback.fail(new Exception(msg, ar.cause())); }); }