Skip to content

Commit

Permalink
Add SSL plugin for REST API Server
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 19, 2024
1 parent 6fe5e97 commit 36ab76f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/data/ACell.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ public boolean isEmbedded() {
}

/**
* Returns true if this Cell is in a canonical representation for message writing.
* Returns true if this Cell is in a canonical representation for encoding.
*
* Non-canonical objects may be used on a temporary internal basis, they must always
* be converted to canonical representations for external use (e.g. Encoding).
*
Expand Down
10 changes: 10 additions & 0 deletions convex-core/src/main/java/convex/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,16 @@ public static long mulDiv(long a, long b, long c) {
return checkedLong(result);
}

private static Path homePath=null;
public static Path getHomePath() {
if (homePath!=null) return homePath;
String homeDir=System.getProperty("user.home");
Path p=new File(homeDir).toPath();

homePath=p;
return p;
}




Expand Down
2 changes: 1 addition & 1 deletion convex-gui/src/main/java/convex/gui/MainGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public MainGUI() {
JComponent wallet=createLaunchButton("Wallet",Toolkit.WALLET_ICON,this::launchTestNet);
actionPanel.add(wallet);

JComponent testNet=createLaunchButton("Launch TestNet",Toolkit.TESTNET_ICON,this::launchTestNet);
JComponent testNet=createLaunchButton("Peer Manager",Toolkit.TESTNET_ICON,this::launchTestNet);
actionPanel.add(testNet);

JComponent latticeFS=createLaunchButton("Lattice Filesystem",Toolkit.DLFS_ICON,this::launchDLFS);
Expand Down
9 changes: 4 additions & 5 deletions convex-gui/src/main/java/convex/gui/PeerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,11 @@ public void run() {

} catch (InterruptedException e) {
//
log.warn("Update thread interrupted abnormally: "+e.getMessage());
e.printStackTrace();
Thread.currentThread().interrupt();
log.trace("Update thread interrupted, presumably shutting down");
updateRunning=false;
}
}
log.debug("GUI Peer Manager update thread ended");
log.debug("GUI Peer Manager update thread ending");
}
};

Expand All @@ -238,7 +237,7 @@ public DefaultListModel<ConvexLocal> getPeerList() {

@Override
public void finalize() {
// terminate the update thread
// terminate the update thread if needed
updateRunning = false;
}

Expand Down
5 changes: 5 additions & 0 deletions convex-restapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<artifactId>javalin-redoc-plugin</artifactId>
<version>${javalin.version}</version>
</dependency>
<dependency>
<groupId>io.javalin.community.ssl</groupId>
<artifactId>ssl-plugin</artifactId>
<version>${javalin.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
29 changes: 29 additions & 0 deletions convex-restapi/src/main/java/convex/restapi/RESTServer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package convex.restapi;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.api.Convex;
import convex.api.ConvexLocal;
import convex.core.util.Utils;
import convex.peer.Server;
import convex.restapi.api.ChainAPI;
import convex.restapi.api.DepAPI;
import io.javalin.Javalin;
import io.javalin.community.ssl.SslPlugin;
import io.javalin.config.JavalinConfig;
import io.javalin.http.staticfiles.Location;
import io.javalin.openapi.plugin.OpenApiPlugin;
Expand All @@ -26,6 +32,25 @@ private RESTServer(Server server) {
this.server = server;
this.convex = ConvexLocal.create(server, server.getPeerController(), server.getKeyPair());

boolean useSSL=true;
SslPlugin sslPlugin=null;
if (useSSL) try {
Path certFile=Utils.getHomePath().resolve("ssl/certificate.pem");
Path privateFile=Utils.getHomePath().resolve("ssl/certificate.pem");
if (Files.exists(certFile)&&Files.exists(privateFile)) {
InputStream certS=Files.newInputStream(certFile);
InputStream privateS=Files.newInputStream(privateFile);
sslPlugin = new SslPlugin(conf -> {
conf.pemFromInputStream(certS, privateS);
});
} else {
log.warn("Failed to find SSL cerificates, defaulting back to HTTP");
}
} catch (Exception e) {
log.warn("Failed to create SSL plugin, will use insecure HTTP only", e);
}
SslPlugin finalSSLPlugin=sslPlugin; // final version of plugin

app = Javalin.create(config -> {
config.staticFiles.enableWebjars();
config.bundledPlugins.enableCors(cors -> {
Expand All @@ -35,6 +60,10 @@ private RESTServer(Server server) {
});
});

if (finalSSLPlugin!=null) {
config.registerPlugin(finalSSLPlugin);
}

addOpenApiPlugins(config);

config.staticFiles.add(staticFiles -> {
Expand Down

0 comments on commit 36ab76f

Please sign in to comment.