diff --git a/pom.xml b/pom.xml index 2a87677e..ef1ef89d 100644 --- a/pom.xml +++ b/pom.xml @@ -47,5 +47,15 @@ jersey-server 2.27 + + org.glassfish.jersey.containers + jersey-container-servlet + 2.27 + + + org.eclipse.jetty + jetty-maven-plugin + 9.4.11.v20180605 + \ No newline at end of file diff --git a/src/api/ApiService.java b/src/api/ApiService.java new file mode 100644 index 00000000..509f6913 --- /dev/null +++ b/src/api/ApiService.java @@ -0,0 +1,71 @@ +package api; + +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.InetAccessHandler; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.servlet.ServletContainer; + +import settings.Settings; + +public class ApiService { + + public Server server; + + public ApiService() + { + //CREATE CONFIG + Set> s = new HashSet>(); + s.add(BlocksResource.class); + + ResourceConfig config = new ResourceConfig(s); + + //CREATE CONTAINER + ServletContainer container = new ServletContainer(config); + + //CREATE CONTEXT + ServletContextHandler context = new ServletContextHandler(); + context.setContextPath("/"); + context.addServlet(new ServletHolder(container),"/*"); + + //CREATE WHITELIST + InetAccessHandler accessHandler = new InetAccessHandler(); + for(String pattern : Settings.getInstance().getRpcAllowed()) + accessHandler.include(pattern); + accessHandler.setHandler(context); + + //CREATE RPC SERVER + this.server = new Server(Settings.getInstance().getRpcPort()); + this.server.setHandler(accessHandler); + } + + public void start() + { + try + { + //START RPC + server.start(); + } + catch (Exception e) + { + //FAILED TO START RPC + } + } + + public void stop() + { + try + { + //STOP RPC + server.stop(); + } + catch (Exception e) + { + //FAILED TO STOP RPC + } + } +} diff --git a/src/settings/Settings.java b/src/settings/Settings.java index be62d2a9..4600f867 100644 --- a/src/settings/Settings.java +++ b/src/settings/Settings.java @@ -2,8 +2,12 @@ package settings; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Set; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -20,9 +24,15 @@ public class Settings { private int maxBytePerFee = 1024; private String userpath = ""; + //RPC + private int rpcPort = 9085; + private List rpcAllowed = new ArrayList(Arrays.asList("127.0.0.1")); + private boolean rpcEnabled = true; + // Constants private static final String SETTINGS_FILENAME = "settings.json"; + // Constructors private Settings() { @@ -102,6 +112,23 @@ public class Settings { this.genesisTimestamp = ((Long) json.get("testnetstamp")).longValue(); } } + + // RPC + if(json.containsKey("rpcport")) + { + this.rpcPort = ((Long) json.get("rpcport")).intValue(); + } + + if(json.containsKey("rpcallowed")) + { + JSONArray allowedArray = (JSONArray) json.get("rpcallowed"); + this.rpcAllowed = new ArrayList(allowedArray); + } + + if(json.containsKey("rpcenabled")) + { + this.rpcEnabled = ((Boolean) json.get("rpcenabled")).booleanValue(); + } } public boolean isTestNet() { @@ -122,4 +149,18 @@ public class Settings { return this.userpath; } + public int getRpcPort() + { + return this.rpcPort; + } + + public List getRpcAllowed() + { + return this.rpcAllowed; + } + + public boolean isRpcEnabled() + { + return this.rpcEnabled; + } }