From 5e7fe3e46d69ce831ca2e6bd51a8590584275c39 Mon Sep 17 00:00:00 2001 From: sqrrm Date: Sun, 1 Sep 2019 18:49:43 +0200 Subject: [PATCH] Optional allowed host --- .../com/neemre/btcdcli4j/core/NodeProperties.java | 5 +++-- .../com/neemre/btcdcli4j/daemon/BtcdDaemonImpl.java | 10 +++++++--- .../daemon/notification/NotificationMonitor.java | 12 ++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/neemre/btcdcli4j/core/NodeProperties.java b/core/src/main/java/com/neemre/btcdcli4j/core/NodeProperties.java index 62173a7..de0b393 100644 --- a/core/src/main/java/com/neemre/btcdcli4j/core/NodeProperties.java +++ b/core/src/main/java/com/neemre/btcdcli4j/core/NodeProperties.java @@ -21,8 +21,9 @@ public enum NodeProperties { HTTP_AUTH_SCHEME("node.bitcoind.http.auth_scheme", "Basic"), ALERT_PORT("node.bitcoind.notification.alert.port", "5158"), BLOCK_PORT("node.bitcoind.notification.block.port", "5159"), - WALLET_PORT("node.bitcoind.notification.wallet.port", "5160"); + WALLET_PORT("node.bitcoind.notification.wallet.port", "5160"), + ALLOWED_HOST("node.bitcoind.notification.block.host", ""); private final String key; private final String defaultValue; -} \ No newline at end of file +} diff --git a/daemon/src/main/java/com/neemre/btcdcli4j/daemon/BtcdDaemonImpl.java b/daemon/src/main/java/com/neemre/btcdcli4j/daemon/BtcdDaemonImpl.java index 3f0eb59..95db672 100644 --- a/daemon/src/main/java/com/neemre/btcdcli4j/daemon/BtcdDaemonImpl.java +++ b/daemon/src/main/java/com/neemre/btcdcli4j/daemon/BtcdDaemonImpl.java @@ -178,8 +178,10 @@ private void initialize() { } private void buildMonitors(Properties nodeConfig) { + String allowedHost = nodeConfig.getProperty(NodeProperties.ALLOWED_HOST.getKey()); int alertPort = Integer.parseInt(nodeConfig.getProperty(NodeProperties.ALERT_PORT.getKey())); - NotificationMonitor alertNotificationMonitor = new NotificationMonitor(Notifications.ALERT, alertPort, null, + NotificationMonitor alertNotificationMonitor = new NotificationMonitor(Notifications.ALERT, alertPort, + allowedHost, null, throwable -> { if (errorHandler != null) errorHandler.accept(throwable); @@ -187,7 +189,8 @@ private void buildMonitors(Properties nodeConfig) { monitors.put(Notifications.ALERT, alertNotificationMonitor); int blockPort = Integer.parseInt(nodeConfig.getProperty(NodeProperties.BLOCK_PORT.getKey())); - NotificationMonitor blockNotificationMonitor = new NotificationMonitor(Notifications.BLOCK, blockPort, client, + NotificationMonitor blockNotificationMonitor = new NotificationMonitor(Notifications.BLOCK, blockPort, + allowedHost, client, throwable -> { if (errorHandler != null) errorHandler.accept(throwable); @@ -195,7 +198,8 @@ private void buildMonitors(Properties nodeConfig) { monitors.put(Notifications.BLOCK, blockNotificationMonitor); int walletPort = Integer.parseInt(nodeConfig.getProperty(NodeProperties.WALLET_PORT.getKey())); - NotificationMonitor walletNotificationMonitor = new NotificationMonitor(Notifications.WALLET, walletPort, client, + NotificationMonitor walletNotificationMonitor = new NotificationMonitor(Notifications.WALLET, walletPort, + allowedHost, client, throwable -> { if (errorHandler != null) errorHandler.accept(throwable); diff --git a/daemon/src/main/java/com/neemre/btcdcli4j/daemon/notification/NotificationMonitor.java b/daemon/src/main/java/com/neemre/btcdcli4j/daemon/notification/NotificationMonitor.java index e9dfb6a..c1dfc30 100644 --- a/daemon/src/main/java/com/neemre/btcdcli4j/daemon/notification/NotificationMonitor.java +++ b/daemon/src/main/java/com/neemre/btcdcli4j/daemon/notification/NotificationMonitor.java @@ -39,6 +39,7 @@ public class NotificationMonitor extends Observable implements Observer, Callabl private Notifications type; private int serverPort; + private String allowedHost; private ServerSocket serverSocket; private volatile boolean isActive; @@ -51,10 +52,10 @@ public class NotificationMonitor extends Observable implements Observer, Callabl private ListeningExecutorService workerPool; public NotificationMonitor(Notifications type, int serverPort, @Nullable BtcdClient client) { - this(type, serverPort, client, null); + this(type, serverPort, "", client, null); } - public NotificationMonitor(Notifications type, int serverPort, @Nullable BtcdClient client, + public NotificationMonitor(Notifications type, int serverPort, String allowedHost, @Nullable BtcdClient client, @Nullable Consumer errorHandler) { LOG.info("** NotificationMonitor(): launching new '{}' notification monitor (port: '{}', " + "RPC-capable: '{}')", type.name(), serverPort, ((client == null) ? "no" : "yes")); @@ -62,6 +63,7 @@ public NotificationMonitor(Notifications type, int serverPort, @Nullable BtcdCli this.type = type; this.serverPort = serverPort; this.client = client; + this.allowedHost = allowedHost; } @Override @@ -125,13 +127,15 @@ public boolean isActive() { private void activate() throws NotificationHandlerException { Thread.currentThread().setName(getUniqueName()); + InetAddress host = InetAddress.getLoopbackAddress(); isActive = true; try { - serverSocket = new ServerSocket(serverPort, 5, InetAddress.getLoopbackAddress()); + host = allowedHost.isEmpty() ? host : InetAddress.getByName(allowedHost); + serverSocket = new ServerSocket(serverPort, 5, host); serverSocket.setSoTimeout(IDLE_SOCKET_TIMEOUT); } catch (IOException e) { try { - serverSocket = new ServerSocket(0, 5, InetAddress.getLoopbackAddress()); + serverSocket = new ServerSocket(0, 5, host); serverSocket.setSoTimeout(IDLE_SOCKET_TIMEOUT); LOG.warn("-- activate(..): failed to create server socket (monitor: '{}', port: " + "'{}'), reverting to unused port '{}'", type.name(), serverPort,