Use bindAddress from Settings for UI, API and P2P.

This commit is contained in:
catbref 2019-08-14 15:11:20 +01:00
parent 8a0d93f304
commit e631e69fa1
3 changed files with 141 additions and 127 deletions

View File

@ -2,6 +2,9 @@ package org.qora.api;
import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource; import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.eclipse.jetty.rewrite.handler.RedirectPatternRule; import org.eclipse.jetty.rewrite.handler.RedirectPatternRule;
import org.eclipse.jetty.rewrite.handler.RewriteHandler; import org.eclipse.jetty.rewrite.handler.RewriteHandler;
import org.eclipse.jetty.server.CustomRequestLog; import org.eclipse.jetty.server.CustomRequestLog;
@ -23,18 +26,37 @@ import org.qora.settings.Settings;
public class ApiService { public class ApiService {
private final Server server; private static ApiService instance;
private final ResourceConfig config; private final ResourceConfig config;
private Server server;
public ApiService() { private ApiService() {
config = new ResourceConfig(); this.config = new ResourceConfig();
config.packages("org.qora.api.resource"); this.config.packages("org.qora.api.resource");
config.register(OpenApiResource.class); this.config.register(OpenApiResource.class);
config.register(ApiDefinition.class); this.config.register(ApiDefinition.class);
config.register(AnnotationPostProcessor.class); this.config.register(AnnotationPostProcessor.class);
}
// Create RPC server public static ApiService getInstance() {
this.server = new Server(Settings.getInstance().getApiPort()); if (instance == null)
instance = new ApiService();
return instance;
}
public Iterable<Class<?>> getResources() {
// return resources;
return this.config.getClasses();
}
public void start() {
try {
// Create API server
InetAddress bindAddr = InetAddress.getByName(Settings.getInstance().getBindAddress());
InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getApiPort());
this.server = new Server(endpoint);
// Error handler // Error handler
ErrorHandler errorHandler = new ApiErrorHandler(); ErrorHandler errorHandler = new ApiErrorHandler();
@ -46,7 +68,7 @@ public class ApiService {
logWriter.setAppend(true); logWriter.setAppend(true);
logWriter.setTimeZone("UTC"); logWriter.setTimeZone("UTC");
RequestLog requestLog = new CustomRequestLog(logWriter, CustomRequestLog.EXTENDED_NCSA_FORMAT); RequestLog requestLog = new CustomRequestLog(logWriter, CustomRequestLog.EXTENDED_NCSA_FORMAT);
server.setRequestLog(requestLog); this.server.setRequestLog(requestLog);
} }
// IP address based access control // IP address based access control
@ -73,7 +95,7 @@ public class ApiService {
context.addFilter(corsFilterHolder, "/*", null); context.addFilter(corsFilterHolder, "/*", null);
// API servlet // API servlet
ServletContainer container = new ServletContainer(config); ServletContainer container = new ServletContainer(this.config);
ServletHolder apiServlet = new ServletHolder(container); ServletHolder apiServlet = new ServletHolder(container);
apiServlet.setInitOrder(1); apiServlet.setInitOrder(1);
context.addServlet(apiServlet, "/*"); context.addServlet(apiServlet, "/*");
@ -88,28 +110,9 @@ public class ApiService {
rewriteHandler.addRule(new RedirectPatternRule("", "/api-documentation/")); // redirect to Swagger UI start page rewriteHandler.addRule(new RedirectPatternRule("", "/api-documentation/")); // redirect to Swagger UI start page
rewriteHandler.addRule(new RedirectPatternRule("/api-documentation", "/api-documentation/")); // redirect to Swagger UI start page rewriteHandler.addRule(new RedirectPatternRule("/api-documentation", "/api-documentation/")); // redirect to Swagger UI start page
}
// XXX: replace singleton pattern by dependency injection?
private static ApiService instance;
public static ApiService getInstance() {
if (instance == null) {
instance = new ApiService();
}
return instance;
}
public Iterable<Class<?>> getResources() {
// return resources;
return config.getClasses();
}
public void start() {
try {
// Start server // Start server
server.start(); this.server.start();
} catch (Exception e) { } catch (Exception e) {
// Failed to start // Failed to start
throw new RuntimeException("Failed to start API", e); throw new RuntimeException("Failed to start API", e);
@ -119,9 +122,12 @@ public class ApiService {
public void stop() { public void stop() {
try { try {
// Stop server // Stop server
server.stop(); this.server.stop();
} catch (Exception e) { } catch (Exception e) {
// Failed to stop // Failed to stop
} }
this.server = null;
} }
} }

View File

@ -43,6 +43,9 @@ public class Settings {
// Settings, and other config files // Settings, and other config files
private String userPath; private String userPath;
// Common to all networking (UI/API/P2P)
private String bindAddress = "::"; // Use IPv6 wildcard to listen on all local addresses
// Node management UI // Node management UI
private boolean uiEnabled = true; private boolean uiEnabled = true;
private Integer uiPort; private Integer uiPort;
@ -71,7 +74,6 @@ public class Settings {
// Peer-to-peer related // Peer-to-peer related
private boolean isTestNet = false; private boolean isTestNet = false;
private Integer listenPort; private Integer listenPort;
private String bindAddress = "::"; // Use IPv6 wildcard to listen on all local addresses
/** Minimum number of peers to allow block generation / synchronization. */ /** Minimum number of peers to allow block generation / synchronization. */
private int minBlockchainPeers = 3; private int minBlockchainPeers = 3;
/** Target number of outbound connections to peers we should make. */ /** Target number of outbound connections to peers we should make. */

View File

@ -1,5 +1,8 @@
package org.qora.ui; package org.qora.ui;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.eclipse.jetty.rewrite.handler.RedirectPatternRule; import org.eclipse.jetty.rewrite.handler.RedirectPatternRule;
import org.eclipse.jetty.rewrite.handler.RewriteHandler; import org.eclipse.jetty.rewrite.handler.RewriteHandler;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
@ -14,12 +17,26 @@ import org.qora.settings.Settings;
public class UiService { public class UiService {
public static final String DOWNLOADS_RESOURCE_PATH = "node-ui-downloads"; public static final String DOWNLOADS_RESOURCE_PATH = "node-ui-downloads";
private static UiService instance;
private final Server server; private Server server;
public UiService() { private UiService() {
}
public static UiService getInstance() {
if (instance == null)
instance = new UiService();
return instance;
}
public void start() {
try {
// Create node management UI server // Create node management UI server
this.server = new Server(Settings.getInstance().getUiPort()); InetAddress bindAddr = InetAddress.getByName(Settings.getInstance().getBindAddress());
InetSocketAddress endpoint = new InetSocketAddress(bindAddr, Settings.getInstance().getUiPort());
this.server = new Server(endpoint);
// IP address based access control // IP address based access control
InetAccessHandler accessHandler = new InetAccessHandler(); InetAccessHandler accessHandler = new InetAccessHandler();
@ -61,22 +78,9 @@ public class UiService {
context.addServlet(uiServlet, "/*"); context.addServlet(uiServlet, "/*");
rewriteHandler.addRule(new RedirectPatternRule("", "/index.html")); // node management UI start page rewriteHandler.addRule(new RedirectPatternRule("", "/index.html")); // node management UI start page
}
private static UiService instance;
public static UiService getInstance() {
if (instance == null) {
instance = new UiService();
}
return instance;
}
public void start() {
try {
// Start server // Start server
server.start(); this.server.start();
} catch (Exception e) { } catch (Exception e) {
// Failed to start // Failed to start
throw new RuntimeException("Failed to start node management UI", e); throw new RuntimeException("Failed to start node management UI", e);
@ -86,10 +90,12 @@ public class UiService {
public void stop() { public void stop() {
try { try {
// Stop server // Stop server
server.stop(); this.server.stop();
} catch (Exception e) { } catch (Exception e) {
// Failed to stop // Failed to stop
} }
this.server = null;
} }
} }