From 21d069d960f914faf5fb1308ff51bc43d96ddf62 Mon Sep 17 00:00:00 2001 From: Andcool-Systems Date: Tue, 4 Jun 2024 22:41:27 +0300 Subject: [PATCH] [InDev] Added config --- README.md | 3 +- src/main/java/com/andcool/OAuthServer.java | 13 ++-- .../java/com/andcool/config/UserConfig.java | 67 +++++++++++++++++++ .../andcool/handlers/EncryptionHandler.java | 3 +- .../andcool/handlers/HandshakeHandler.java | 5 +- .../andcool/pipeline/EncryptionRequest.java | 3 +- .../com/andcool/responses/PingResponse.java | 9 +-- 7 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/andcool/config/UserConfig.java diff --git a/README.md b/README.md index 584589f..7b0976e 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,10 @@ C–Client S–Server 7. **S -> C** Code response The server does not have an offline mode setting, as it would not make sense in this case, so it always tries to authenticate the player through Mojang. -Currently, the server has no settings and all parameters are fixed. TODO: - [x] Add server icon support. - [ ] Add the ability to choose the authentication server. -- [ ] Add configuration. +- [x] Add configuration. - [ ] Add a proper logger (instead of the custom SillyLogger). - [x] Add text formatter for MOTD. diff --git a/src/main/java/com/andcool/OAuthServer.java b/src/main/java/com/andcool/OAuthServer.java index 94b21c4..c0c8be7 100644 --- a/src/main/java/com/andcool/OAuthServer.java +++ b/src/main/java/com/andcool/OAuthServer.java @@ -1,5 +1,6 @@ package com.andcool; +import com.andcool.config.UserConfig; import com.andcool.format.MOTDFormatter; import com.andcool.hashMap.ExpiringHashMap; import com.andcool.pipeline.NoopHandler; @@ -31,17 +32,15 @@ public class OAuthServer { public static ExpiringHashMap expiringMap = new ExpiringHashMap<>(5 * 60 * 1000); - private static final int PORT = 25565; - public static final String MOTD = "§6§l§nmc-oauth§6§l.andcool.ru"; - public static final String server_id = "mc-oauth"; public static final KeyPair KEY_PAIR = generateKeyPair(); public static final byte[] VERIFY_TOKEN = generateVerifyToken(); - public static final SillyLogger logger = new SillyLogger(server_id, true, Level.DEBUG); + public static final SillyLogger logger = new SillyLogger("Main Thread", true, Level.DEBUG); public static final MOTDFormatter MOTD_FORMATTER = new MOTDFormatter(); public static final String SERVER_ICON = loadIcon(); public static void main(String[] args) throws Exception { + UserConfig.load(); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { @@ -59,13 +58,13 @@ public void initChannel(SocketChannel ch) { } }); - HttpServer server = HttpServer.create(new InetSocketAddress(8089), 0); + HttpServer server = HttpServer.create(new InetSocketAddress(UserConfig.PORT_API), 0); server.createContext("/", new APIHandler()); server.setExecutor(null); server.start(); - ChannelFuture future = b.bind(PORT).sync(); - logger.log(Level.INFO, "Server started on port " + PORT); + ChannelFuture future = b.bind(UserConfig.PORT_SERVER).sync(); + logger.log(Level.INFO, "Server started on port " + UserConfig.PORT_SERVER); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); diff --git a/src/main/java/com/andcool/config/UserConfig.java b/src/main/java/com/andcool/config/UserConfig.java new file mode 100644 index 0000000..8577676 --- /dev/null +++ b/src/main/java/com/andcool/config/UserConfig.java @@ -0,0 +1,67 @@ +package com.andcool.config; + +import com.andcool.OAuthServer; +import com.andcool.sillyLogger.Level; +import org.json.JSONObject; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class UserConfig { + public static int PORT_SERVER = 25565; + public static int PORT_API = 8089; + public static String SERVER_ID = "mc-oauth"; + public static String MOTD = ""; + public static String SERVER_VERSION = "1.20.4"; + public static int PLAYERS_MAX = 0; + public static int PLAYERS_NOW = 0; + public static int PROTOCOL_VERSION = -1; + + /* + Save config to file + */ + public static void save() { + final File configFile = new File("./config.json"); + JSONObject jsonConfig = new JSONObject(); + jsonConfig.put("PORT_SERVER", PORT_SERVER); + jsonConfig.put("PORT_API", PORT_API); + jsonConfig.put("SERVER_ID", SERVER_ID); + jsonConfig.put("MOTD", MOTD); + jsonConfig.put("SERVER_VERSION", SERVER_VERSION); + jsonConfig.put("PLAYERS_MAX", PLAYERS_MAX); + jsonConfig.put("PLAYERS_NOW", PLAYERS_NOW); + jsonConfig.put("PROTOCOL_VERSION", PROTOCOL_VERSION); + try { + Files.createDirectories(configFile.toPath().getParent()); + Files.writeString(configFile.toPath(), jsonConfig.toString(4)); + } catch (IOException e) { + OAuthServer.logger.log(Level.ERROR, e.toString()); + } + } + + /* + Load config from file + */ + public static void load() { + final File configFile = new File("./config.json"); + try { + JSONObject jsonConfig = new JSONObject(Files.readString(configFile.toPath())); + for (String key : jsonConfig.keySet()) { + switch (key) { + case "PORT_SERVER" -> PORT_SERVER = jsonConfig.getInt(key); + case "PORT_API" -> PORT_API = jsonConfig.getInt(key); + case "SERVER_ID" -> SERVER_ID = jsonConfig.getString(key); + case "MOTD" -> MOTD = jsonConfig.getString(key); + case "SERVER_VERSION" -> SERVER_VERSION = jsonConfig.getString(key); + case "PLAYERS_MAX" -> PLAYERS_MAX = jsonConfig.getInt(key); + case "PLAYERS_NOW" -> PLAYERS_NOW = jsonConfig.getInt(key); + case "PROTOCOL_VERSION" -> PROTOCOL_VERSION = jsonConfig.getInt(key); + } + } + } catch (Exception e) { + OAuthServer.logger.log(Level.WARN, e.toString()); + save(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/andcool/handlers/EncryptionHandler.java b/src/main/java/com/andcool/handlers/EncryptionHandler.java index e726b9b..e729732 100644 --- a/src/main/java/com/andcool/handlers/EncryptionHandler.java +++ b/src/main/java/com/andcool/handlers/EncryptionHandler.java @@ -1,6 +1,7 @@ package com.andcool.handlers; import com.andcool.bytebuf.ByteBufUtils; +import com.andcool.config.UserConfig; import com.andcool.session.Session; import com.andcool.sillyLogger.Level; import io.netty.buffer.ByteBuf; @@ -46,7 +47,7 @@ public static void handleEncryptionResponse(ChannelHandlerContext ctx, ByteBuf i } MessageDigest digest = MessageDigest.getInstance("SHA-1"); - digest.update(OAuthServer.server_id.getBytes()); + digest.update(UserConfig.SERVER_ID.getBytes()); digest.update(sharedSecret.getEncoded()); digest.update(OAuthServer.KEY_PAIR.getPublic().getEncoded()); diff --git a/src/main/java/com/andcool/handlers/HandshakeHandler.java b/src/main/java/com/andcool/handlers/HandshakeHandler.java index dc76e99..a11cee0 100644 --- a/src/main/java/com/andcool/handlers/HandshakeHandler.java +++ b/src/main/java/com/andcool/handlers/HandshakeHandler.java @@ -1,7 +1,9 @@ package com.andcool.handlers; import com.andcool.OAuthServer; +import com.andcool.config.UserConfig; import com.andcool.responses.PingResponse; +import com.andcool.session.SessionHandler; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import com.andcool.bytebuf.ByteBufUtils; @@ -18,13 +20,12 @@ public static void handleHandshake(ChannelHandlerContext ctx, ByteBuf in, Sessio OAuthServer.logger.log(Level.DEBUG, "Received handshake! Protocol version: " + protocolVersion + " Next state: " + nextState); - session.protocolVersion = protocolVersion; session.loginPhase = 1; session.nextState = nextState; switch (nextState){ case 1: - PingResponse.sendPingResponse(ctx, protocolVersion); + PingResponse.sendPingResponse(ctx, UserConfig.PROTOCOL_VERSION == -1 ? protocolVersion : UserConfig.PROTOCOL_VERSION); break; case 2: LoginStartHandler.handleLoginStart(ctx, in, session); diff --git a/src/main/java/com/andcool/pipeline/EncryptionRequest.java b/src/main/java/com/andcool/pipeline/EncryptionRequest.java index ad3a0a8..f625fad 100644 --- a/src/main/java/com/andcool/pipeline/EncryptionRequest.java +++ b/src/main/java/com/andcool/pipeline/EncryptionRequest.java @@ -1,6 +1,7 @@ package com.andcool.pipeline; import com.andcool.OAuthServer; +import com.andcool.config.UserConfig; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import com.andcool.bytebuf.ByteBufUtils; @@ -11,7 +12,7 @@ public class EncryptionRequest { public static void sendEncryptionRequest(ChannelHandlerContext ctx) throws IOException { ByteBuf out = ctx.alloc().buffer(); ByteBufUtils.writeVarInt(out, 0x01); // Packet ID - ByteBufUtils.writeUTF8(out, OAuthServer.server_id); // Server ID + ByteBufUtils.writeUTF8(out, UserConfig.SERVER_ID); // Server ID byte[] publicKey = OAuthServer.KEY_PAIR.getPublic().getEncoded(); ByteBufUtils.writeVarInt(out, publicKey.length); diff --git a/src/main/java/com/andcool/responses/PingResponse.java b/src/main/java/com/andcool/responses/PingResponse.java index 9c2cf48..f748b08 100644 --- a/src/main/java/com/andcool/responses/PingResponse.java +++ b/src/main/java/com/andcool/responses/PingResponse.java @@ -1,6 +1,7 @@ package com.andcool.responses; import com.andcool.OAuthServer; +import com.andcool.config.UserConfig; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import org.json.JSONObject; @@ -19,17 +20,17 @@ public static String Response(int protoVersion){ JSONObject players = new JSONObject(); JSONObject description = new JSONObject(); - version.put("name", "1.20.4"); + version.put("name", UserConfig.SERVER_VERSION); version.put("protocol", protoVersion); json_response.put("version", version); - players.put("max", 0); - players.put("online", 0); + players.put("max", UserConfig.PLAYERS_MAX); + players.put("online", UserConfig.PLAYERS_NOW); players.put("sample", Collections.emptyList()); json_response.put("players", players); description.put("text", ""); - description.put("extra", OAuthServer.MOTD_FORMATTER.format(OAuthServer.MOTD)); + description.put("extra", OAuthServer.MOTD_FORMATTER.format(UserConfig.MOTD)); json_response.put("description", description); json_response.put("favicon", "data:image/png;base64," + OAuthServer.SERVER_ICON);