aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gq/malwarefight/nosession/mixin/client/YggdrasilSessionMixin.java
blob: ff07cabd906a544dbe14e443401f522b2961f74a (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
package gq.malwarefight.nosession.mixin.client;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService;
import gq.malwarefight.nosession.utils.Utils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Objects;


@Mixin(YggdrasilMinecraftSessionService.class)
public class YggdrasilSessionMixin {
    @Inject(method = "joinServer", at = @At("HEAD"), cancellable = true, remap = false)
    public void joinServer(GameProfile profile, String authenticationToken, String serverId, CallbackInfo ci) throws AuthenticationException {
        if (authenticationToken.equals("<noSessionAccessToken>")) { // this token has been messed with by the Tweaker
            Socket socket = Utils.getProperSocket(profile.getId());
            try {
                assert socket != null;
                socket.getOutputStream().write(("login " + serverId + "\n").getBytes(StandardCharsets.UTF_8));
                socket.getOutputStream().flush();
                String line = Utils.readString(socket.getInputStream(), '\n');
                String[] parts = line.split(" ");
                if (Objects.equals(parts[0], "200")) {
                    ci.cancel();
                } else if (Objects.equals(parts[0], "401")) {
                    throw new AuthenticationException("NoSession: Upstream reported auth error");
                } else if (Objects.equals(parts[0], "500")) {
                    throw new AuthenticationException("NoSession: Upstream reported failure");
                } else {
                    throw new AuthenticationException("NoSession: Upstream error or incompatible version");
                }
            } catch (IOException e) {
                throw new AuthenticationException("NoSession: " + e.getMessage());
            }

            try {
                socket.getOutputStream().write("disconnect\n".getBytes(StandardCharsets.UTF_8));
                socket.close();
            } catch (IOException ignored) {
                // it doesn't matter if the socket is disconnected
                // if there's an IOException, the socket is either closed or
                // impossible to close/unusable
            }
        }
    }
}