summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/RemoteControlSession.java
blob: 85b424e71639cd58c92dd3f0f72822094c6d96b3 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package net.minecraft.server;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class RemoteControlSession extends RemoteConnectionThread {

    private boolean g = false;
    private Socket h;
    private byte[] i = new byte[1460];
    private String j;

    RemoteControlSession(IMinecraftServer iminecraftserver, Socket socket) {
        super(iminecraftserver);
        this.h = socket;
        // CraftBukkit start - set infinite timeout so we sleep until there is data available
        try {
            this.h.setSoTimeout(0);
        } catch (Exception ex) {
            this.running = false;
        }
        // CraftBukkit end
        this.j = iminecraftserver.a("rcon.password", "");
        this.info("Rcon connection from: " + socket.getInetAddress());
    }

    public void run() {
        // while (true) { // CraftBukkit - moved down
            try {
                while (true) { // CraftBukkit - moved from above
                if (!this.running) {
                    return;
                }

                try {
                    BufferedInputStream bufferedinputstream = new BufferedInputStream(this.h.getInputStream());
                    int i = bufferedinputstream.read(this.i, 0, 1460);

                    if (10 <= i) {
                        byte b0 = 0;
                        int j = StatusChallengeUtils.b(this.i, 0, i);

                        if (j != i - 4) {
                            return;
                        }

                        int k = b0 + 4;
                        int l = StatusChallengeUtils.b(this.i, k, i);

                        k += 4;
                        int i1 = StatusChallengeUtils.a(this.i, k);

                        k += 4;
                        switch (i1) {
                        case 2:
                            if (this.g) {
                                String s = StatusChallengeUtils.a(this.i, k, i);

                                try {
                                    this.a(l, this.server.d(s));
                                } catch (Exception exception) {
                                    this.a(l, "Error executing: " + s + " (" + exception.getMessage() + ")");
                                }
                                continue;
                            }

                            this.e();
                            continue;

                        case 3:
                            String s1 = StatusChallengeUtils.a(this.i, k, i);
                            int j1 = k + s1.length();

                            if (0 != s1.length() && s1.equals(this.j)) {
                                this.g = true;
                                this.a(l, 2, "");
                                continue;
                            }

                            this.g = false;
                            this.e();
                            continue;

                        default:
                            this.a(l, String.format("Unknown request %s", new Object[] { Integer.toHexString(i1)}));
                            continue;
                        }
                    }
                    return; // CraftBukkit - return if we don't get enough data
                } catch (SocketTimeoutException sockettimeoutexception) {
                    continue;
                } catch (IOException ioexception) {
                    if (this.running) {
                        this.info("IO: " + ioexception.getMessage());
                    }
                    return; // CraftBukkit - shut down the thread after hitting an exception.
                }
                } // CraftBukkit - Loop shift
            } catch (Exception exception1) {
                System.out.println(exception1);
                return;
            } finally {
                this.f();
            }
        // CraftBukkit - Loop shift
    }

    private void a(int i, int j, String s) throws IOException { // CraftBukkit - throws IOException
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(1248);
        DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);

        dataoutputstream.writeInt(Integer.reverseBytes(s.length() + 10));
        dataoutputstream.writeInt(Integer.reverseBytes(i));
        dataoutputstream.writeInt(Integer.reverseBytes(j));
        dataoutputstream.writeBytes(s);
        dataoutputstream.write(0);
        dataoutputstream.write(0);
        this.h.getOutputStream().write(bytearrayoutputstream.toByteArray());
    }

    private void e() throws IOException { // CraftBukkit - throws IOException
        this.a(-1, 2, "");
    }

    private void a(int i, String s) throws IOException { // CraftBukkit - throws IOException
        int j = s.length();

        do {
            int k = 4096 <= j ? 4096 : j;

            this.a(i, 0, s.substring(0, k));
            s = s.substring(k);
            j = s.length();
        } while (0 != j);

    }

    private void f() {
        if (null != this.h) {
            try {
                this.h.close();
                this.info("Rcon connection closed."); // CraftBukkit
            } catch (IOException ioexception) {
                this.warning("IO: " + ioexception.getMessage());
            }

            this.h = null;
        }
    }
}