aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gq/malwarefight/tokenapp/Main.java
blob: 62a4da3c7c4d0992910c95747dd42c26fd661b2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gq.malwarefight.tokenapp;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService;
import gq.malwarefight.nosession.utils.Utils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.net.*;
import java.util.UUID;

public class Main {
    public static final int BASE_PORT = 47777;
    public static long ID;
    public static YggdrasilMinecraftSessionService sessionService = null;
    public static YggdrasilAuthenticationService authenticationService = null;
    public static GameProfile gameProfile = null;

    public static void setup() throws IOException {
        String token = Utils.readString(System.in, '\n');
        YggdrasilAuthenticationService yas = new YggdrasilAuthenticationService(Proxy.NO_PROXY, token);
        authenticationService = yas;
        sessionService = (YggdrasilMinecraftSessionService) yas.createMinecraftSessionService();
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) (new URL("https://api.minecraftservices.com/minecraft/profile").openConnection());
        httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
        String response = Utils.readString(httpsURLConnection.getInputStream(), null);
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        UUID id = UUID.fromString(
                jsonObject.get("id").getAsString().replaceFirst(
                        "(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"
                )
        );
        String name = jsonObject.get("name").getAsString();
        gameProfile = new GameProfile(id, name);
        System.setProperty("java.net.preferIPv4Stack", "true");
    }

    public static void main(String[] args) {
        ID = Long.parseLong(args[0]);
        try {
            setup();
        } catch (Exception e) {
            System.err.println("Could not setup the server\n" + ExceptionUtils.getStackTrace(e));
            System.exit(1);
        }
        ServerSocket sock = null;
        for (int i = BASE_PORT; i < BASE_PORT + 10; i++) {
            try {
                //noinspection resource
                sock = new ServerSocket(i, 50, InetAddress.getLoopbackAddress());
                break;
            } catch (Exception ignored) {}
        }
        if (sock == null) {
            System.err.println("Could not bind to any valid port");
            System.exit(1);
        }
        while (true) {
            try {
                Socket connection = sock.accept();
                Thread t = new SocketThread(connection);
                t.start();
            } catch (IOException ignored) {
                System.exit(0);
            }
        }
    }
}