From c1e558a12340656b32cd0b0ff321ae5981ee676d Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Thu, 7 Feb 2019 10:10:07 -0700 Subject: [PATCH] fix a pairing issue make sure we bind the HTTP server to the same address that we told Bonjour to advertise. otherwise we might end up listening on some rando interface that may not actually be able to accept connections --- src/main/java/com/beowulfe/hap/HomekitServer.java | 2 +- .../hap/impl/http/impl/HomekitHttpServer.java | 7 +++++-- .../hap/impl/http/impl/NettyHomekitHttpService.java | 11 +++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/beowulfe/hap/HomekitServer.java b/src/main/java/com/beowulfe/hap/HomekitServer.java index 47e7afb2a..5c70c8e02 100644 --- a/src/main/java/com/beowulfe/hap/HomekitServer.java +++ b/src/main/java/com/beowulfe/hap/HomekitServer.java @@ -40,7 +40,7 @@ public class HomekitServer { */ public HomekitServer(InetAddress localAddress, int port, int nThreads) throws IOException { this.localAddress = localAddress; - http = new HomekitHttpServer(port, nThreads); + http = new HomekitHttpServer(localAddress, port, nThreads); } /** diff --git a/src/main/java/com/beowulfe/hap/impl/http/impl/HomekitHttpServer.java b/src/main/java/com/beowulfe/hap/impl/http/impl/HomekitHttpServer.java index 189b82e46..fb0ba3610 100644 --- a/src/main/java/com/beowulfe/hap/impl/http/impl/HomekitHttpServer.java +++ b/src/main/java/com/beowulfe/hap/impl/http/impl/HomekitHttpServer.java @@ -2,11 +2,13 @@ import com.beowulfe.hap.impl.HomekitWebHandler; import com.beowulfe.hap.impl.http.HomekitClientConnectionFactory; +import java.net.InetAddress; import java.util.concurrent.CompletableFuture; public class HomekitHttpServer implements HomekitWebHandler { private NettyHomekitHttpService service = null; + private final InetAddress localAddress; private final int port; private final int nThreads; @@ -17,7 +19,8 @@ public void stop() { } } - public HomekitHttpServer(int port, int nThreads) { + public HomekitHttpServer(InetAddress localAddress, int port, int nThreads) { + this.localAddress = localAddress; this.port = port; this.nThreads = nThreads; } @@ -25,7 +28,7 @@ public HomekitHttpServer(int port, int nThreads) { @Override public CompletableFuture start(HomekitClientConnectionFactory clientConnectionFactory) { if (service == null) { - this.service = NettyHomekitHttpService.create(port, nThreads); + this.service = NettyHomekitHttpService.create(localAddress, port, nThreads); return this.service.create(clientConnectionFactory); } else { throw new RuntimeException("HomekitHttpServer can only be started once"); diff --git a/src/main/java/com/beowulfe/hap/impl/http/impl/NettyHomekitHttpService.java b/src/main/java/com/beowulfe/hap/impl/http/impl/NettyHomekitHttpService.java index 1247c3936..72a2707e1 100644 --- a/src/main/java/com/beowulfe/hap/impl/http/impl/NettyHomekitHttpService.java +++ b/src/main/java/com/beowulfe/hap/impl/http/impl/NettyHomekitHttpService.java @@ -10,6 +10,7 @@ import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.util.concurrent.*; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.concurrent.CompletableFuture; @@ -23,16 +24,18 @@ class NettyHomekitHttpService { private static final Logger logger = LoggerFactory.getLogger(NettyHomekitHttpService.class); private final ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); + private final InetAddress localAddress; private final int port; private final int nThreads; - public static NettyHomekitHttpService create(int port, int nThreads) { - return new NettyHomekitHttpService(port, nThreads); + public static NettyHomekitHttpService create(InetAddress localAddress, int port, int nThreads) { + return new NettyHomekitHttpService(localAddress, port, nThreads); } - private NettyHomekitHttpService(int port, int nThreads) { + private NettyHomekitHttpService(InetAddress localAddress, int port, int nThreads) { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); + this.localAddress = localAddress; this.port = port; this.nThreads = nThreads; } @@ -46,7 +49,7 @@ public CompletableFuture create(HomekitClientConnectionFactory connecti .childHandler(new ServerInitializer(connectionFactory, allChannels, nThreads)) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); - final ChannelFuture bindFuture = b.bind(port); + final ChannelFuture bindFuture = b.bind(localAddress, port); bindFuture.addListener( new GenericFutureListener>() {