Skip to content

Commit

Permalink
Add tls for webserver block
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Dec 4, 2023
1 parent f57a928 commit 55be5a3
Show file tree
Hide file tree
Showing 44 changed files with 347 additions and 49 deletions.
39 changes: 32 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java'
id 'fabric-loom' version '1.3-SNAPSHOT'
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish'
}

Expand All @@ -18,7 +19,6 @@ repositories {
name = "CottonMC"
url = "https://server.bbkr.space/artifactory/libs-release"
}

maven { url 'https://jitpack.io' }
}

Expand All @@ -28,20 +28,40 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

// bundle the external mods
modImplementation include("io.github.cottonmc:LibGui:${project.libgui_version}")

modImplementation include("com.github.hashicraft:fabric-stateful-mod:${stateful_version}")

implementation include("org.postgresql:postgresql:42.6.0");
implementation include("io.javalin:javalin:5.6.1");

// add the external dependencies to the fat jar
shadow("org.postgresql:postgresql:42.6.0");
shadow("io.javalin:javalin:5.6.3");
shadow("io.javalin.community.ssl:ssl-plugin:5.6.3");

// some dependencies are not pulled in with shadow
implementation include('org.conscrypt:conscrypt-openjdk-uber:2.5.2');
implementation include('org.eclipse.jetty:jetty-alpn-java-server:11.0.17');
implementation include('org.eclipse.jetty:jetty-alpn-conscrypt-server:11.0.17');
implementation include('org.eclipse.jetty.http2:http2-server:11.0.17');

testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
}

shadowJar {
configurations = [project.configurations.shadow]
exclude("META-INF")
}

remapJar {
// wait until the shadowJar is done
dependsOn(shadowJar)
mustRunAfter(shadowJar)
// Set the input jar for the task. Here use the shadow Jar that include the .class of the transitive dependency
inputFile = file(shadowJar.archivePath)
}

processResources {
inputs.property "version", project.version

Expand Down Expand Up @@ -94,6 +114,11 @@ publishing {
}
}

task copyToLib(type: Copy) {
into "$buildDir/output/lib"
from configurations.runtimeClasspath
}

//test {
// useJUnitPlatform()
// testLogging {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx4G

# Mod Properties
mod_version = 1.0.0
mod_version = 1.1.0
maven_group = com.github.hashicraft
archives_base_name = fabric-microservices-mod

Expand Down
Binary file added jumppad/config/mods/fabric-api-0.87.0+1.20.1.jar
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions jumppad/config/webservers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"-26_67_-83": {
"port": "8081",
"path": "/",
"method": "GET",
"tlsCert": "/etc/certs/minecraft_leaf-leaf.cert",
"tlsKey": "/etc/certs/minecraft_leaf-leaf.key"
}
}
Binary file added jumppad/config/world/DIM-1/data/raids.dat
Binary file not shown.
Binary file added jumppad/config/world/DIM1/data/raids_end.dat
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"minecraft:recipes/decorations/crafting_table": {
"criteria": {
"unlock_right_away": "2023-12-04 15:13:15 +0000"
},
"done": true
},
"minecraft:adventure/adventuring_time": {
"criteria": {
"minecraft:old_growth_birch_forest": "2023-12-04 15:13:16 +0000"
},
"done": false
},
"minecraft:recipes/redstone/redstone_torch": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/tools/clock": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/redstone/dropper": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/redstone/redstone_block": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/redstone/note_block": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/redstone/target": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/tools/compass": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"minecraft:recipes/redstone/piston": {
"criteria": {
"has_redstone": "2023-12-04 15:13:36 +0000"
},
"done": true
},
"DataVersion": 3465
}
Binary file added jumppad/config/world/data/raids.dat
Binary file not shown.
Binary file added jumppad/config/world/entities/r.-1.-1.mca
Binary file not shown.
Binary file added jumppad/config/world/entities/r.-1.0.mca
Binary file not shown.
Empty file.
Binary file added jumppad/config/world/entities/r.0.-1.mca
Binary file not shown.
Binary file added jumppad/config/world/entities/r.0.0.mca
Binary file not shown.
Empty file.
Binary file added jumppad/config/world/level.dat
Binary file not shown.
Binary file added jumppad/config/world/level.dat_old
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added jumppad/config/world/poi/r.-1.-1.mca
Binary file not shown.
Empty file.
Empty file.
Binary file added jumppad/config/world/poi/r.0.-1.mca
Binary file not shown.
Empty file.
Empty file.
Binary file added jumppad/config/world/region/r.-1.-1.mca
Binary file not shown.
Binary file added jumppad/config/world/region/r.-1.0.mca
Binary file not shown.
Binary file added jumppad/config/world/region/r.-1.1.mca
Binary file not shown.
Binary file added jumppad/config/world/region/r.0.-1.mca
Binary file not shown.
Binary file added jumppad/config/world/region/r.0.0.mca
Binary file not shown.
Binary file added jumppad/config/world/region/r.0.1.mca
Binary file not shown.
1 change: 1 addition & 0 deletions jumppad/config/world/session.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"stats":{"minecraft:used":{"minecraft:redstone":1,"minecraft:redstone_lamp":4,"microservices:webserver_block":1},"minecraft:custom":{"minecraft:time_since_rest":10359,"minecraft:leave_game":3,"minecraft:play_time":10359,"minecraft:time_since_death":10359,"minecraft:walk_one_cm":516,"minecraft:total_world_time":10359}},"DataVersion":3465}
91 changes: 91 additions & 0 deletions jumppad/main.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,97 @@ resource "network" "local" {
subnet = "10.10.0.0/16"
}

resource "certificate_ca" "minecraft_ca" {
output = data("certs")
}

resource "certificate_leaf" "minecraft_leaf" {
ca_key = resource.certificate_ca.minecraft_ca.private_key.path
ca_cert = resource.certificate_ca.minecraft_ca.certificate.path

ip_addresses = ["127.0.0.1"]

dns_names = [
"localhost",
"minecraft.container.jumppad.dev",
]

output = data("certs")
}

resource "container" "minecraft" {
image {
name = "hashicraft/minecraftservice:v0.0.3"
}

network {
id = resource.network.local.id
}

# Minecraft
port {
remote = 25565
host = 25565
local = 25565
}

# Microservice
port {
remote = 8081
host = 8081
local = 8081
}

environment = {
GAME_MODE = "creative"
WHITELIST_ENABLED = "false"
ONLINE_MODE = "false"
RCON_ENABLED = "true"
RCON_PASSWORD = "password"
SPAWN_ANIMALS = "true"
SPAWN_NPCS = "true"
VAULT_ADDR = "http://vault.container.jumppad.dev:8200"
VAULT_TOKEN = "root"
HASHICRAFT_env = "local"
MICROSERVICES_db_host = "postgres.container.jumppad.dev:5432"
MICROSERVICES_db_password = "password"
MICROSERVICES_db_database = "mydb"
}

# Mount the secrets that contain the certs
volume {
source = data("certs")
destination = "/etc/certs"
}

# Mount the secrets that contain the db connection info
volume {
source = "./config/db_secrets"
destination = "/etc/db_secrets"
}

# Mount the local world and config files
volume {
source = "./config/world"
destination = "/minecraft/world"
}

volume {
source = "./config/mods"
destination = "/minecraft/mods"
}

volume {
source = "./config/databases.json"
destination = "/minecraft/config/databases.json"
}

volume {
source = "./config/webservers.json"
destination = "/minecraft/config/webservers.json"
}
}

resource "container" "postgres" {
image {
name = "postgres:15.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
Expand All @@ -24,7 +23,6 @@
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

Expand Down Expand Up @@ -57,7 +55,7 @@ public class MicroservicesMod implements ModInitializer {
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
System.out.println("Microservices v1.0.0 loading...");
System.out.println("Microservices v1.1.0 loading...");

Registry.register(Registries.ITEM_GROUP, ITEM_GROUP, FabricItemGroup.builder()
.icon(() -> new ItemStack(DATABASE_BLOCK))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.github.hashicraft.stateful.blocks.StatefulBlock;

import io.javalin.Javalin;
import io.javalin.community.ssl.SSLPlugin;
import io.javalin.http.Context;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
Expand Down Expand Up @@ -222,9 +223,11 @@ public static void handleBlockUpdate(BlockPos pos, ServerWorld world) {
blockEntity.getServerPath(), blockEntity.getServerMethod());

// update the context
val.setServerPort(blockEntity.getServerPort());
val.setServerPath(blockEntity.getServerPath());
val.setServerMethod(blockEntity.getServerMethod());
val.setPort(blockEntity.getServerPort());
val.setPath(blockEntity.getServerPath());
val.setMethod(blockEntity.getServerMethod());
val.setTlsCert(blockEntity.getTlsCert());
val.setTlsKey(blockEntity.getTlsKey());

startServer(pos, world, val);

Expand All @@ -238,9 +241,11 @@ public static void handleBlockUpdate(BlockPos pos, ServerWorld world) {

public static void startServer(BlockPos pos, ServerWorld world, WebserverContext wctx) {
// read the values from interpolation
String port = Interpolate.getValue(wctx.getServerPort());
String path = Interpolate.getValue(wctx.getServerPath());
String method = Interpolate.getValue(wctx.getServerMethod());
String port = Interpolate.getValue(wctx.getPort());
String path = Interpolate.getValue(wctx.getPath());
String method = Interpolate.getValue(wctx.getMethod());
String tlsCert = Interpolate.getValue(wctx.getTlsCert());
String tlsKey = Interpolate.getValue(wctx.getTlsKey());

// get the port as an integer
int iPort = 0;
Expand All @@ -251,19 +256,39 @@ public static void startServer(BlockPos pos, ServerWorld world, WebserverContext
return;
}

final int serverPort = iPort;

if (path.isEmpty() || method.isEmpty()) {
MicroservicesMod.LOGGER.error("path or method is empty, not starting");
return;
}

// if the server already exists, close it
if (wctx.getServer() != null) {
wctx.getServer().close();
}

// create the server and set the port
Javalin javalin = Javalin.create();
javalin.jettyServer().setServerPort(iPort);
javalin.jettyServer().setServerHost("0.0.0.0");

// do we need to configure tls?
if (!tlsCert.isEmpty() && !tlsKey.isEmpty()) {
MicroservicesMod.LOGGER.info("configuring with TLS");

SSLPlugin plugin = new SSLPlugin(conf -> {
conf.pemFromPath(tlsCert, tlsKey);
conf.securePort = serverPort;
conf.insecure = false;
conf.host = "0.0.0.0";
});

javalin.updateConfig(javalinConfig -> {
javalinConfig.plugins.register(plugin);
});
} else {
javalin.jettyServer().setServerPort(serverPort);
javalin.jettyServer().setServerHost("0.0.0.0");
}

// start the server async
service.submit(() -> {
Expand Down
Loading

0 comments on commit 55be5a3

Please sign in to comment.