Skip to content

Commit

Permalink
[InDev] Added config
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Jun 4, 2024
1 parent bbfb453 commit 21d069d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 6 additions & 7 deletions src/main/java/com/andcool/OAuthServer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,17 +32,15 @@

public class OAuthServer {
public static ExpiringHashMap<String, JSONObject> 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 {
Expand All @@ -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();
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/andcool/config/UserConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/andcool/handlers/EncryptionHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/andcool/handlers/HandshakeHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/andcool/pipeline/EncryptionRequest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/andcool/responses/PingResponse.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 21d069d

Please sign in to comment.