mirror of
https://github.com/Melledy/Nebula.git
synced 2025-12-25 10:44:37 +01:00
Add Remote Command API (Use KEY)
This commit is contained in:
@@ -27,11 +27,11 @@ public class HttpServer {
|
||||
private final Javalin app;
|
||||
private ServerType type;
|
||||
private boolean started;
|
||||
|
||||
|
||||
// Cached client diff
|
||||
private PatchList patchlist;
|
||||
private byte[] diff;
|
||||
|
||||
|
||||
public HttpServer(ServerType type) {
|
||||
this.type = type;
|
||||
this.app = Javalin.create(javalinConfig -> {
|
||||
@@ -44,7 +44,7 @@ public class HttpServer {
|
||||
this.loadPatchList();
|
||||
this.addRoutes();
|
||||
}
|
||||
|
||||
|
||||
public HttpServerConfig getServerConfig() {
|
||||
return Nebula.getConfig().getHttpServer();
|
||||
}
|
||||
@@ -65,26 +65,26 @@ public class HttpServer {
|
||||
sslContextFactory.setRenegotiationAllowed(false);
|
||||
return sslContextFactory;
|
||||
}
|
||||
|
||||
|
||||
// Patch list
|
||||
|
||||
|
||||
public long getDataVersion() {
|
||||
return getPatchlist() != null ? getPatchlist().getVersion() : GameConstants.getDataVersion();
|
||||
}
|
||||
|
||||
|
||||
public synchronized void loadPatchList() {
|
||||
// Clear
|
||||
this.patchlist = null;
|
||||
this.diff = null;
|
||||
|
||||
|
||||
// Get file
|
||||
File file = new File(Nebula.getConfig().getPatchListPath());
|
||||
|
||||
|
||||
if (!file.exists()) {
|
||||
this.diff = ClientDiff.newInstance().toByteArray();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Load
|
||||
try (FileReader reader = new FileReader(file)) {
|
||||
this.patchlist = JsonUtils.loadToClass(reader, PatchList.class);
|
||||
@@ -93,21 +93,23 @@ public class HttpServer {
|
||||
this.patchlist = null;
|
||||
this.diff = ClientDiff.newInstance().toByteArray();
|
||||
}
|
||||
|
||||
|
||||
if (this.patchlist != null) {
|
||||
Nebula.getLogger().info("Loaded patchlist (Data version: " + patchlist.getVersion() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Start server
|
||||
|
||||
|
||||
public void start() {
|
||||
if (this.started) return;
|
||||
if (this.started)
|
||||
return;
|
||||
this.started = true;
|
||||
|
||||
// Http server
|
||||
if (getServerConfig().isUseSSL()) {
|
||||
ServerConnector sslConnector = new ServerConnector(getApp().jettyServer().server(), getSSLContextFactory(), getHttpFactory());
|
||||
ServerConnector sslConnector = new ServerConnector(getApp().jettyServer().server(), getSSLContextFactory(),
|
||||
getHttpFactory());
|
||||
sslConnector.setHost(getServerConfig().getBindAddress());
|
||||
sslConnector.setPort(getServerConfig().getBindPort());
|
||||
getApp().jettyServer().server().addConnector(sslConnector);
|
||||
@@ -120,19 +122,20 @@ public class HttpServer {
|
||||
// Done
|
||||
Nebula.getLogger().info("Http Server started on " + getServerConfig().getBindPort());
|
||||
}
|
||||
|
||||
|
||||
// Server endpoints
|
||||
|
||||
private void addRoutes() {
|
||||
|
||||
// Add routes
|
||||
if (this.getType().runLogin()) {
|
||||
this.addLoginServerRoutes();
|
||||
}
|
||||
|
||||
|
||||
if (this.getType().runGame()) {
|
||||
this.addGameServerRoutes();
|
||||
}
|
||||
|
||||
|
||||
// Exception handler
|
||||
getApp().exception(Exception.class, (e, c) -> {
|
||||
e.printStackTrace();
|
||||
@@ -141,29 +144,40 @@ public class HttpServer {
|
||||
// Fallback handler
|
||||
getApp().error(404, this::notFoundHandler);
|
||||
}
|
||||
|
||||
|
||||
private void addLoginServerRoutes() {
|
||||
// https://en-sdk-api.yostarplat.com/
|
||||
getApp().post("/common/config", new CommonConfigHandler(this));
|
||||
getApp().post("/common/version", new HttpJsonResponse("{\"Code\":200,\"Data\":{\"Agreement\":[{\"Version\":\"0.1\",\"Type\":\"user_agreement\",\"Title\":\"用户协议\",\"Content\":\"\",\"Lang\":\"en\"},{\"Version\":\"0.1\",\"Type\":\"privacy_agreement\",\"Title\":\"隐私政策\",\"Content\":\"\",\"Lang\":\"en\"}],\"ErrorCode\":\"4.4\"},\"Msg\":\"OK\"}"));
|
||||
|
||||
getApp().post("/common/version", new HttpJsonResponse(
|
||||
"{\"Code\":200,\"Data\":{\"Agreement\":[{\"Version\":\"0.1\",\"Type\":\"user_agreement\",\"Title\":\"用户协议\",\"Content\":\"\",\"Lang\":\"en\"},{\"Version\":\"0.1\",\"Type\":\"privacy_agreement\",\"Title\":\"隐私政策\",\"Content\":\"\",\"Lang\":\"en\"}],\"ErrorCode\":\"4.4\"},\"Msg\":\"OK\"}"));
|
||||
|
||||
getApp().post("/user/detail", new UserLoginHandler());
|
||||
getApp().post("/user/set", new UserSetDataHandler());
|
||||
getApp().post("/user/login", new UserLoginHandler());
|
||||
getApp().post("/user/quick-login", new UserLoginHandler());
|
||||
|
||||
|
||||
getApp().post("/yostar/get-auth", new GetAuthHandler());
|
||||
getApp().post("/yostar/send-code", new HttpJsonResponse("{\"Code\":200,\"Data\":{},\"Msg\":\"OK\"}")); // Dummy handler
|
||||
|
||||
getApp().post("/yostar/send-code", new HttpJsonResponse("{\"Code\":200,\"Data\":{},\"Msg\":\"OK\"}")); // Dummy
|
||||
// handler
|
||||
|
||||
// https://nova-static.stellasora.global/
|
||||
getApp().get("/meta/serverlist.html", new MetaServerlistHandler(this));
|
||||
getApp().get("/meta/win.html", new MetaWinHandler(this));
|
||||
// if (!Nebula.getConfig().getRemoteCommand().useRemoteServices) {
|
||||
// getApp().post("/api/command", new RemoteHandler());
|
||||
// }
|
||||
getApp().post("/api/command", new RemoteHandler());
|
||||
// getApp.get("/notice/noticelist.html");
|
||||
getApp().get("/webchatv3/*", ctx -> {
|
||||
ctx.redirect("https://google.com");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void addGameServerRoutes() {
|
||||
getApp().post("/agent-zone-1/", new AgentZoneHandler());
|
||||
}
|
||||
|
||||
|
||||
private void notFoundHandler(Context ctx) {
|
||||
ctx.status(404);
|
||||
ctx.contentType(ContentType.APPLICATION_JSON);
|
||||
|
||||
@@ -10,7 +10,7 @@ import emu.nebula.net.GameSession;
|
||||
|
||||
@HandlerId(NetMsgId.player_login_req)
|
||||
public class HandlerPlayerLoginReq extends NetHandler {
|
||||
|
||||
|
||||
public boolean requirePlayer() {
|
||||
return false;
|
||||
}
|
||||
@@ -20,21 +20,21 @@ public class HandlerPlayerLoginReq extends NetHandler {
|
||||
// Parse request
|
||||
var req = LoginReq.parseFrom(message);
|
||||
var loginToken = req.getOfficialOverseas().getToken();
|
||||
|
||||
|
||||
// Login
|
||||
boolean result = session.login(loginToken);
|
||||
|
||||
|
||||
if (!result) {
|
||||
return session.encodeMsg(NetMsgId.player_login_failed_ack);
|
||||
}
|
||||
|
||||
|
||||
// Regenerate session token because we are switching encrpytion method
|
||||
Nebula.getGameContext().generateSessionToken(session);
|
||||
|
||||
|
||||
// Create rsp
|
||||
var rsp = LoginResp.newInstance()
|
||||
.setToken(session.getToken());
|
||||
|
||||
|
||||
// Encode and send to client
|
||||
return session.encodeMsg(NetMsgId.player_login_succeed_ack, rsp);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,24 @@ public class HandlerPlayerSignatureEdit extends NetHandler {
|
||||
|
||||
// Check if we need to handle a command
|
||||
if (signature.charAt(0) == '!' || signature.charAt(0) == '/') {
|
||||
String commandLabel = signature.toLowerCase().trim();
|
||||
if (commandLabel.startsWith("!") || commandLabel.startsWith("/")) {
|
||||
commandLabel = commandLabel.substring(1).split(" ")[0];
|
||||
}
|
||||
|
||||
Nebula.getCommandManager().invoke(session.getPlayer(), signature);
|
||||
|
||||
// If this is the remote command, return the message
|
||||
if ("remote".equals(commandLabel)) {
|
||||
String remoteMessage = emu.nebula.command.commands.RemoteKeyCommand.getLastMessage();
|
||||
if (remoteMessage != null) {
|
||||
return session.encodeMsg(
|
||||
NetMsgId.player_signature_edit_failed_ack,
|
||||
Error.newInstance().setCode(119902).addArguments("\n" + remoteMessage)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return session.encodeMsg(
|
||||
NetMsgId.player_signature_edit_failed_ack,
|
||||
Error.newInstance().setCode(119902).addArguments("\nCommand Success")
|
||||
|
||||
94
src/main/java/emu/nebula/server/routes/RemoteHandler.java
Normal file
94
src/main/java/emu/nebula/server/routes/RemoteHandler.java
Normal file
@@ -0,0 +1,94 @@
|
||||
package emu.nebula.server.routes;
|
||||
|
||||
import emu.nebula.Nebula;
|
||||
import emu.nebula.game.player.Player;
|
||||
import emu.nebula.util.JsonUtils;
|
||||
import io.javalin.http.ContentType;
|
||||
import io.javalin.http.Context;
|
||||
import io.javalin.http.Handler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class RemoteHandler implements Handler {
|
||||
|
||||
static class RemoteCommandRequest {
|
||||
public String token;
|
||||
public String command;
|
||||
}
|
||||
|
||||
// Cache: Token -> UID
|
||||
private static final java.util.Map<String, Integer> tokenCache = new java.util.concurrent.ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void handle(@NotNull Context ctx) throws Exception {
|
||||
if (!Nebula.getConfig().getRemoteCommand().useRemoteServices) {
|
||||
ctx.status(403);
|
||||
ctx.result("{\"Code\":403,\"Msg\":\"RemoteServer not enable\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse body
|
||||
RemoteCommandRequest req = JsonUtils.decode(ctx.body(), RemoteCommandRequest.class);
|
||||
if (req == null || req.token == null || req.command == null) {
|
||||
ctx.status(400);
|
||||
ctx.result("{\"Code\":400,\"Msg\":\"Invalid request\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
String token = req.token;
|
||||
String command = req.command;
|
||||
String adminKey = Nebula.getConfig().getRemoteCommand().getServerAdminKey();
|
||||
|
||||
// Check admin key
|
||||
if (token.equals(adminKey)) {
|
||||
Nebula.getCommandManager().invoke(null, command);
|
||||
Nebula.getLogger().warn(
|
||||
"\u001B[38;2;252;186;3mRemote Server (Using Admin Key) sent command: /" + command + "\u001B[0m");
|
||||
ctx.status(200);
|
||||
ctx.contentType(ContentType.APPLICATION_JSON);
|
||||
ctx.result("{\"Code\":200,\"Data\":{},\"Msg\":\"Command executed\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check player
|
||||
Player player = null;
|
||||
|
||||
// 1. Try cache
|
||||
Integer cachedUid = tokenCache.get(token);
|
||||
if (cachedUid != null) {
|
||||
player = Nebula.getGameContext().getPlayerModule().getPlayer(cachedUid);
|
||||
// Verify token matches (in case player changed token or cache is stale)
|
||||
if (player != null && !token.equals(player.getPlayerRemoteToken())) {
|
||||
player = null;
|
||||
tokenCache.remove(token);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to DB if not in cache or cache invalid
|
||||
if (player == null) {
|
||||
player = Nebula.getGameDatabase().getObjectByField(Player.class, "playerRemoteToken", token);
|
||||
if (player != null) {
|
||||
tokenCache.put(token, player.getUid());
|
||||
}
|
||||
}
|
||||
|
||||
if (player != null) {
|
||||
// Append target UID to command to ensure it targets the player
|
||||
// CommandArgs parses @UID to set the target
|
||||
String finalCommand = command + " @" + player.getUid();
|
||||
|
||||
Nebula.getLogger().info("Remote Player Request [" + player.getUid() + "]: " + finalCommand);
|
||||
|
||||
// Execute as console (null sender) but targeting the player
|
||||
Nebula.getCommandManager().invoke(null, finalCommand);
|
||||
|
||||
ctx.status(200);
|
||||
ctx.contentType(ContentType.APPLICATION_JSON);
|
||||
ctx.result("{\"Code\":200,\"Data\":{},\"Msg\":\"Command executed\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
// Invalid token
|
||||
ctx.status(403);
|
||||
ctx.result("{\"Code\":403,\"Msg\":\"Invalid token\"}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user