Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 21d069d

Browse files
[InDev] Added config
1 parent bbfb453 commit 21d069d

File tree

7 files changed

+86
-17
lines changed

7 files changed

+86
-17
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ C–Client S–Server
2222
7. **S -> C** Code response
2323

2424
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.
25-
Currently, the server has no settings and all parameters are fixed.
2625

2726
TODO:
2827
- [x] Add server icon support.
2928
- [ ] Add the ability to choose the authentication server.
30-
- [ ] Add configuration.
29+
- [x] Add configuration.
3130
- [ ] Add a proper logger (instead of the custom SillyLogger).
3231
- [x] Add text formatter for MOTD.

src/main/java/com/andcool/OAuthServer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.andcool;
22

3+
import com.andcool.config.UserConfig;
34
import com.andcool.format.MOTDFormatter;
45
import com.andcool.hashMap.ExpiringHashMap;
56
import com.andcool.pipeline.NoopHandler;
@@ -31,17 +32,15 @@
3132

3233
public class OAuthServer {
3334
public static ExpiringHashMap<String, JSONObject> expiringMap = new ExpiringHashMap<>(5 * 60 * 1000);
34-
private static final int PORT = 25565;
35-
public static final String MOTD = "§6§l§nmc-oauth§6§l.andcool.ru";
36-
public static final String server_id = "mc-oauth";
3735

3836
public static final KeyPair KEY_PAIR = generateKeyPair();
3937
public static final byte[] VERIFY_TOKEN = generateVerifyToken();
40-
public static final SillyLogger logger = new SillyLogger(server_id, true, Level.DEBUG);
38+
public static final SillyLogger logger = new SillyLogger("Main Thread", true, Level.DEBUG);
4139
public static final MOTDFormatter MOTD_FORMATTER = new MOTDFormatter();
4240
public static final String SERVER_ICON = loadIcon();
4341

4442
public static void main(String[] args) throws Exception {
43+
UserConfig.load();
4544
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
4645
EventLoopGroup workerGroup = new NioEventLoopGroup();
4746
try {
@@ -59,13 +58,13 @@ public void initChannel(SocketChannel ch) {
5958
}
6059
});
6160

62-
HttpServer server = HttpServer.create(new InetSocketAddress(8089), 0);
61+
HttpServer server = HttpServer.create(new InetSocketAddress(UserConfig.PORT_API), 0);
6362
server.createContext("/", new APIHandler());
6463
server.setExecutor(null);
6564
server.start();
6665

67-
ChannelFuture future = b.bind(PORT).sync();
68-
logger.log(Level.INFO, "Server started on port " + PORT);
66+
ChannelFuture future = b.bind(UserConfig.PORT_SERVER).sync();
67+
logger.log(Level.INFO, "Server started on port " + UserConfig.PORT_SERVER);
6968
future.channel().closeFuture().sync();
7069
} finally {
7170
workerGroup.shutdownGracefully();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.andcool.config;
2+
3+
import com.andcool.OAuthServer;
4+
import com.andcool.sillyLogger.Level;
5+
import org.json.JSONObject;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
11+
public class UserConfig {
12+
public static int PORT_SERVER = 25565;
13+
public static int PORT_API = 8089;
14+
public static String SERVER_ID = "mc-oauth";
15+
public static String MOTD = "";
16+
public static String SERVER_VERSION = "1.20.4";
17+
public static int PLAYERS_MAX = 0;
18+
public static int PLAYERS_NOW = 0;
19+
public static int PROTOCOL_VERSION = -1;
20+
21+
/*
22+
Save config to file
23+
*/
24+
public static void save() {
25+
final File configFile = new File("./config.json");
26+
JSONObject jsonConfig = new JSONObject();
27+
jsonConfig.put("PORT_SERVER", PORT_SERVER);
28+
jsonConfig.put("PORT_API", PORT_API);
29+
jsonConfig.put("SERVER_ID", SERVER_ID);
30+
jsonConfig.put("MOTD", MOTD);
31+
jsonConfig.put("SERVER_VERSION", SERVER_VERSION);
32+
jsonConfig.put("PLAYERS_MAX", PLAYERS_MAX);
33+
jsonConfig.put("PLAYERS_NOW", PLAYERS_NOW);
34+
jsonConfig.put("PROTOCOL_VERSION", PROTOCOL_VERSION);
35+
try {
36+
Files.createDirectories(configFile.toPath().getParent());
37+
Files.writeString(configFile.toPath(), jsonConfig.toString(4));
38+
} catch (IOException e) {
39+
OAuthServer.logger.log(Level.ERROR, e.toString());
40+
}
41+
}
42+
43+
/*
44+
Load config from file
45+
*/
46+
public static void load() {
47+
final File configFile = new File("./config.json");
48+
try {
49+
JSONObject jsonConfig = new JSONObject(Files.readString(configFile.toPath()));
50+
for (String key : jsonConfig.keySet()) {
51+
switch (key) {
52+
case "PORT_SERVER" -> PORT_SERVER = jsonConfig.getInt(key);
53+
case "PORT_API" -> PORT_API = jsonConfig.getInt(key);
54+
case "SERVER_ID" -> SERVER_ID = jsonConfig.getString(key);
55+
case "MOTD" -> MOTD = jsonConfig.getString(key);
56+
case "SERVER_VERSION" -> SERVER_VERSION = jsonConfig.getString(key);
57+
case "PLAYERS_MAX" -> PLAYERS_MAX = jsonConfig.getInt(key);
58+
case "PLAYERS_NOW" -> PLAYERS_NOW = jsonConfig.getInt(key);
59+
case "PROTOCOL_VERSION" -> PROTOCOL_VERSION = jsonConfig.getInt(key);
60+
}
61+
}
62+
} catch (Exception e) {
63+
OAuthServer.logger.log(Level.WARN, e.toString());
64+
save();
65+
}
66+
}
67+
}

src/main/java/com/andcool/handlers/EncryptionHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.andcool.handlers;
22

33
import com.andcool.bytebuf.ByteBufUtils;
4+
import com.andcool.config.UserConfig;
45
import com.andcool.session.Session;
56
import com.andcool.sillyLogger.Level;
67
import io.netty.buffer.ByteBuf;
@@ -46,7 +47,7 @@ public static void handleEncryptionResponse(ChannelHandlerContext ctx, ByteBuf i
4647
}
4748

4849
MessageDigest digest = MessageDigest.getInstance("SHA-1");
49-
digest.update(OAuthServer.server_id.getBytes());
50+
digest.update(UserConfig.SERVER_ID.getBytes());
5051
digest.update(sharedSecret.getEncoded());
5152
digest.update(OAuthServer.KEY_PAIR.getPublic().getEncoded());
5253

src/main/java/com/andcool/handlers/HandshakeHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.andcool.handlers;
22

33
import com.andcool.OAuthServer;
4+
import com.andcool.config.UserConfig;
45
import com.andcool.responses.PingResponse;
6+
import com.andcool.session.SessionHandler;
57
import io.netty.buffer.ByteBuf;
68
import io.netty.channel.ChannelHandlerContext;
79
import com.andcool.bytebuf.ByteBufUtils;
@@ -18,13 +20,12 @@ public static void handleHandshake(ChannelHandlerContext ctx, ByteBuf in, Sessio
1820
OAuthServer.logger.log(Level.DEBUG, "Received handshake! Protocol version: " +
1921
protocolVersion +
2022
" Next state: " + nextState);
21-
2223
session.protocolVersion = protocolVersion;
2324
session.loginPhase = 1;
2425
session.nextState = nextState;
2526
switch (nextState){
2627
case 1:
27-
PingResponse.sendPingResponse(ctx, protocolVersion);
28+
PingResponse.sendPingResponse(ctx, UserConfig.PROTOCOL_VERSION == -1 ? protocolVersion : UserConfig.PROTOCOL_VERSION);
2829
break;
2930
case 2:
3031
LoginStartHandler.handleLoginStart(ctx, in, session);

src/main/java/com/andcool/pipeline/EncryptionRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.andcool.pipeline;
22

33
import com.andcool.OAuthServer;
4+
import com.andcool.config.UserConfig;
45
import io.netty.buffer.ByteBuf;
56
import io.netty.channel.ChannelHandlerContext;
67
import com.andcool.bytebuf.ByteBufUtils;
@@ -11,7 +12,7 @@ public class EncryptionRequest {
1112
public static void sendEncryptionRequest(ChannelHandlerContext ctx) throws IOException {
1213
ByteBuf out = ctx.alloc().buffer();
1314
ByteBufUtils.writeVarInt(out, 0x01); // Packet ID
14-
ByteBufUtils.writeUTF8(out, OAuthServer.server_id); // Server ID
15+
ByteBufUtils.writeUTF8(out, UserConfig.SERVER_ID); // Server ID
1516
byte[] publicKey = OAuthServer.KEY_PAIR.getPublic().getEncoded();
1617

1718
ByteBufUtils.writeVarInt(out, publicKey.length);

src/main/java/com/andcool/responses/PingResponse.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.andcool.responses;
22

33
import com.andcool.OAuthServer;
4+
import com.andcool.config.UserConfig;
45
import io.netty.buffer.ByteBuf;
56
import io.netty.channel.ChannelHandlerContext;
67
import org.json.JSONObject;
@@ -19,17 +20,17 @@ public static String Response(int protoVersion){
1920
JSONObject players = new JSONObject();
2021
JSONObject description = new JSONObject();
2122

22-
version.put("name", "1.20.4");
23+
version.put("name", UserConfig.SERVER_VERSION);
2324
version.put("protocol", protoVersion);
2425
json_response.put("version", version);
2526

26-
players.put("max", 0);
27-
players.put("online", 0);
27+
players.put("max", UserConfig.PLAYERS_MAX);
28+
players.put("online", UserConfig.PLAYERS_NOW);
2829
players.put("sample", Collections.emptyList());
2930
json_response.put("players", players);
3031

3132
description.put("text", "");
32-
description.put("extra", OAuthServer.MOTD_FORMATTER.format(OAuthServer.MOTD));
33+
description.put("extra", OAuthServer.MOTD_FORMATTER.format(UserConfig.MOTD));
3334
json_response.put("description", description);
3435

3536
json_response.put("favicon", "data:image/png;base64," + OAuthServer.SERVER_ICON);

0 commit comments

Comments
 (0)