Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backends/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ repositories {
dependencies {
api(project(":core"))
api("com.squareup.okhttp3:okhttp:4.12.0")
api("redis.clients:jedis:7.1.0")
compileOnly("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.21.1-R0.1-SNAPSHOT")
compileOnly("dev.jorel:commandapi-spigot-core:11.1.0")
compileOnly("org.bstats:bstats-bukkit:3.2.0")
}

// plugin.yml and paper-plugin.yml contain @version@ placeholders.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.objz.commandbridge.bukkit;

import dev.objz.commandbridge.backends.net.BackendClient;
import dev.objz.commandbridge.backends.net.RedisClient;
import dev.objz.commandbridge.backends.net.WsClient;
import dev.objz.commandbridge.backends.net.in.ExecuteCommandHandler;
import dev.objz.commandbridge.backends.net.in.RegistrationHandler;
Expand All @@ -9,6 +11,7 @@
import dev.objz.commandbridge.backends.platform.cmd.CommandExecutor;
import dev.objz.commandbridge.config.ConfigManager;
import dev.objz.commandbridge.config.model.BackendsConfig;
import dev.objz.commandbridge.config.model.EndpointType;
import dev.objz.commandbridge.logging.Log;
import dev.objz.commandbridge.net.proto.MessageType;

Expand All @@ -20,7 +23,7 @@
import java.time.Duration;

public final class Adapter implements PlatformAdapter {
private WsClient client;
private BackendClient client;
private BackendsConfig cfg;
private Path dataDir;
private JavaPlugin plugin;
Expand Down Expand Up @@ -72,7 +75,9 @@ public void start(PlatformEnv env) throws Exception {
() -> Bukkit.isPrimaryThread(),
task -> Bukkit.getScheduler().runTask(plugin, task));

this.client = new WsClient(cfg, dataDir, this);
this.client = cfg.endpointType() == EndpointType.REDIS
? new RedisClient(cfg, dataDir, this)
: new WsClient(cfg, dataDir, this);

try {
client.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.objz.commandbridge.folia;

import dev.objz.commandbridge.backends.net.BackendClient;
import dev.objz.commandbridge.backends.net.RedisClient;
import dev.objz.commandbridge.backends.net.WsClient;
import dev.objz.commandbridge.backends.net.in.ExecuteCommandHandler;
import dev.objz.commandbridge.backends.net.in.RegistrationHandler;
Expand All @@ -9,6 +11,7 @@
import dev.objz.commandbridge.backends.platform.cmd.CommandExecutor;
import dev.objz.commandbridge.config.ConfigManager;
import dev.objz.commandbridge.config.model.BackendsConfig;
import dev.objz.commandbridge.config.model.EndpointType;
import dev.objz.commandbridge.logging.Log;
import dev.objz.commandbridge.net.proto.MessageType;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
Expand All @@ -21,7 +24,7 @@
import java.util.concurrent.TimeUnit;

public final class Adapter implements PlatformAdapter {
private WsClient client;
private BackendClient client;
private BackendsConfig cfg;
private Path dataDir;
private JavaPlugin plugin;
Expand Down Expand Up @@ -69,7 +72,9 @@ public void start(PlatformEnv env) throws Exception {
this.commandExecutor = new FoliaExecutor(plugin);
}

this.client = new WsClient(cfg, dataDir, this);
this.client = cfg.endpointType() == EndpointType.REDIS
? new RedisClient(cfg, dataDir, this)
: new WsClient(cfg, dataDir, this);

try {
client.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.objz.commandbridge.paper;

import dev.objz.commandbridge.backends.net.BackendClient;
import dev.objz.commandbridge.backends.net.RedisClient;
import dev.objz.commandbridge.backends.net.WsClient;
import dev.objz.commandbridge.backends.net.in.ExecuteCommandHandler;
import dev.objz.commandbridge.backends.net.in.RegistrationHandler;
Expand All @@ -9,6 +11,7 @@
import dev.objz.commandbridge.backends.platform.cmd.CommandExecutor;
import dev.objz.commandbridge.config.ConfigManager;
import dev.objz.commandbridge.config.model.BackendsConfig;
import dev.objz.commandbridge.config.model.EndpointType;
import dev.objz.commandbridge.logging.Log;
import dev.objz.commandbridge.net.proto.MessageType;

Expand All @@ -20,7 +23,7 @@
import java.time.Duration;

public final class Adapter implements PlatformAdapter {
private WsClient client;
private BackendClient client;
private BackendsConfig cfg;
private Path dataDir;
private JavaPlugin plugin;
Expand Down Expand Up @@ -72,7 +75,9 @@ public void start(PlatformEnv env) throws Exception {
() -> Bukkit.isPrimaryThread(),
task -> Bukkit.getScheduler().runTask(plugin, task));

this.client = new WsClient(cfg, dataDir, this);
this.client = cfg.endpointType() == EndpointType.REDIS
? new RedisClient(cfg, dataDir, this)
: new WsClient(cfg, dataDir, this);

try {
client.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.objz.commandbridge.net.OutNode;
import dev.objz.commandbridge.net.proto.MessageType;
import dev.objz.commandbridge.backends.net.out.ctx.AuthRequestContext;
import io.undertow.websockets.core.WebSocketChannel;

import java.time.Duration;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -22,7 +21,7 @@ public AuthHandler(BackendsConfig cfg, OutNode<Object> outNode, AtomicReference<
this.stateRef = stateRef;
}

public boolean authenticate(WebSocketChannel channel) {
public boolean authenticate() {
if (!Boolean.TRUE.equals(cfg.security().requireAuth())) {
Log.warn("Auth disabled by config; continuing unauthenticated");
stateRef.set(ConnectionState.AUTHENTICATED);
Expand All @@ -41,7 +40,7 @@ public boolean authenticate(WebSocketChannel channel) {
};

Duration timeout = Duration.ofSeconds(cfg.timeouts().authTimeout());
AuthRequestContext context = new AuthRequestContext(channel, timeout, statusUpdater);
AuthRequestContext context = new AuthRequestContext(timeout, statusUpdater);

try {
outNode.send(MessageType.AUTH_REQUEST, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.objz.commandbridge.backends.net;

import dev.objz.commandbridge.net.InNode;
import dev.objz.commandbridge.net.OutNode;
import dev.objz.commandbridge.net.SendOperation;
import dev.objz.commandbridge.net.proto.Envelope;
import dev.objz.commandbridge.scripting.model.enums.Location;

public interface BackendClient extends AutoCloseable {
void start() throws Exception;

void reconnect() throws Exception;

void scheduleReconnection();

SendOperation send(Envelope request);

ClientStatus status();

String serverId();

InNode inboundRouter();

OutNode<Object> outboundRouter();

void setLocation(Location location);

void setServerId(String serverId);

@Override
void close() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ private WebSocketChannel connectInternal(boolean isReconnecting) throws Exceptio
: TlsMode.TOFU;

String scheme = TlsResolver.schemeFor(mode);
String url = scheme + "://" + cfg.host() + ":" + cfg.port() + "/ws";
String host = cfg.endpoints().websocket().host();
int port = cfg.endpoints().websocket().port();
String url = scheme + "://" + host + ":" + port + "/ws";

if (!isReconnecting) {
Log.info("Connecting to {} as '{}'", url, cfg.clientId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.objz.commandbridge.net.OutNode;
import dev.objz.commandbridge.net.ResponseAwaiter;
import dev.objz.commandbridge.net.SendOperation;
import dev.objz.commandbridge.net.endpoints.WsEndpoint;
import dev.objz.commandbridge.net.proto.MessageType;
import dev.objz.commandbridge.scripting.model.enums.Location;
import dev.objz.commandbridge.security.AuthService;
Expand All @@ -27,7 +28,7 @@ public final class MessageRouter {
private final AtomicReference<ConnectionState> stateRef;
private final Runnable reconnectCallback;
private final String secret;
private final Location location;
private volatile Location location;

public MessageRouter(
InNode inNode,
Expand All @@ -46,9 +47,15 @@ public MessageRouter(
this.location = location;
}

public void setLocation(Location location) {
this.location = location;
}

public void setupChannel(WebSocketChannel channel) {
inNode.setSendOperationFactory((ch, envelope) -> new SendOperation(ch, envelope, awaiter));
outNode.setSendOperationFactory(envelope -> new SendOperation(channel, envelope, awaiter));
var endpoint = new WsEndpoint(channel);

inNode.setSendOperationFactory((ep, envelope) -> new SendOperation(ep, envelope, awaiter));
outNode.setSendOperationFactory(envelope -> new SendOperation(endpoint, envelope, awaiter));

inNode.setInboundTap(env -> {
boolean matched = false;
Expand Down Expand Up @@ -76,7 +83,7 @@ public void setupChannel(WebSocketChannel channel) {
@Override
protected void onFullTextMessage(WebSocketChannel ch, BufferedTextMessage message) {
try {
inNode.onText(ch, message.getData());
inNode.onText(endpoint, message.getData());
} catch (Throwable t) {
Log.error(t, "Inbound message handling failed");
}
Expand Down
Loading