Skip to content

Commit

Permalink
Merge pull request #7 from sqrrm/optional-allowed-host
Browse files Browse the repository at this point in the history
Optional allowed host
  • Loading branch information
sqrrm authored Sep 2, 2019
2 parents b8be37b + 5e7fe3e commit 27b9433
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,28 @@ 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);
});
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);
});
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -51,17 +52,18 @@ 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<Throwable> errorHandler) {
LOG.info("** NotificationMonitor(): launching new '{}' notification monitor (port: '{}', "
+ "RPC-capable: '{}')", type.name(), serverPort, ((client == null) ? "no" : "yes"));
this.errorHandler = errorHandler;
this.type = type;
this.serverPort = serverPort;
this.client = client;
this.allowedHost = allowedHost;
}

@Override
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 27b9433

Please sign in to comment.