Skip to content

Commit

Permalink
Fixed moderate bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 30, 2024
1 parent 1bff237 commit 5edae9c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/andcool/OAuthServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void initChannel(SocketChannel ch) {
});

HttpServer server = HttpServer.create(new InetSocketAddress(UserConfig.PORT_API), 0);
server.createContext("/", new APIHandler());
server.createContext("/code/", new APIHandler());
server.setExecutor(null);
server.start();

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/andcool/handlers/API/APIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public void handle(HttpExchange exchange) throws IOException {
JSONObject result = OAuthServer.expiringMap.get(code);
if (result == null) {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "error");
jsonResponse.put("message", "Code not found");
jsonResponse.put("status_code", 404);
jsonResponse.put("statusCode", 404);
response = jsonResponse.toString();
status_code = 404;
} else {
Expand All @@ -38,9 +37,8 @@ public void handle(HttpExchange exchange) throws IOException {
}
} else {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "error");
jsonResponse.put("message", "Not found");
jsonResponse.put("status_code", 404);
jsonResponse.put("message", "Cannot GET " + path);
jsonResponse.put("statusCode", 404);
response = jsonResponse.toString();
status_code = 404;
}
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/andcool/handlers/EncryptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

Expand All @@ -30,6 +31,15 @@
import io.netty.channel.ChannelHandlerContext;

public class EncryptionHandler {
public static String getRandomCode() {
SecureRandom secureRandom = new SecureRandom();
StringBuilder code = new StringBuilder();
for (int i = 0; i < 6; i++) {
code.append(secureRandom.nextInt(10)); // Генерация случайной цифры от 0 до 9
}
return code.toString();
}

public static void handleEncryptionResponse(ChannelHandlerContext ctx, ByteBuf in, Session session) throws Exception {
try {
int sharedSecretLength = ByteBufUtils.readVarInt(in);
Expand All @@ -48,7 +58,7 @@ public static void handleEncryptionResponse(ChannelHandlerContext ctx, ByteBuf i
byte[] verifyToken = rsaCipher.doFinal(encryptedVerifyToken);

if (!Arrays.equals(OAuthServer.VERIFY_TOKEN, verifyToken)) {
OAuthServer.logger.log(Level.DEBUG, "Invalid verify token");
OAuthServer.logger.log(Level.ERROR, "Invalid verify token");
SessionHandler.disconnect(ctx, "Error while encryption!");
return;
} else {
Expand All @@ -65,25 +75,21 @@ public static void handleEncryptionResponse(ChannelHandlerContext ctx, ByteBuf i
ctx.pipeline().replace("encryption", "encryption", new Encryption(sharedSecret));

if (response == null) {
SessionHandler.disconnect(ctx, "§cYou are using unlicensed copy of Minecraft!");
SessionHandler.disconnect(ctx, "You are using unlicensed copy of Minecraft!");
return;
}

Random random = new Random();
int code = 100000 + random.nextInt(900000);

SessionHandler.disconnect(ctx, "§l§aYour code is: §n" + code + "§r");
String code = getRandomCode();
SessionHandler.disconnect(ctx, String.format("Hello, %s. Your code is: %s", session.nickname, code));
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "success");
jsonResponse.put("statusCode", 200);
jsonResponse.put("nickname", response.getString("name"));
jsonResponse.put("UUID", response.getString("id"));
OAuthServer.expiringMap.put(String.valueOf(code), jsonResponse);
OAuthServer.expiringMap.put(code, jsonResponse);

OAuthServer.logger.log(Level.INFO, "Created code " + code + " for " + session.nickname);
} catch (IOException | InterruptedException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | JSONException e) {
OAuthServer.logger.log(Level.DEBUG, "Exception in handleEncryptionResponse: " + e.toString());
} finally {
//in.release(); // Освобождение буфера
OAuthServer.logger.log(Level.DEBUG, "Exception in handleEncryptionResponse: " + e);
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/andcool/session/SessionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Except
Session session = SessionUtil.getSession(ctx.channel());
int packetLength = ByteBufUtils.readVarInt(in);
int packetId = ByteBufUtils.readVarInt(in);
OAuthServer.logger.log(Level.DEBUG, "packet id: " + packetId + " packet length: " + packetLength);
OAuthServer.logger.log(Level.DEBUG, "Packet id: " + packetId + " packet length: " + packetLength);

switch (packetId) {
case 0x00 -> // Handshake
Expand All @@ -55,8 +55,9 @@ protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Except
}
default -> OAuthServer.logger.log(Level.DEBUG, "Invalid packet ID: " + packetId);
}
}catch (Exception e){
OAuthServer.logger.log(Level.DEBUG, e.toString());
} catch (Exception e){
//disconnect(ctx, "§cInternal server exception");
OAuthServer.logger.log(Level.ERROR, e.toString());
}
}

Expand Down

0 comments on commit 5edae9c

Please sign in to comment.