|
| 1 | +package me.jaimemartz.lobbybalancer.utils; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import net.md_5.bungee.api.chat.BaseComponent; |
| 6 | +import net.md_5.bungee.api.chat.TextComponent; |
| 7 | +import net.md_5.bungee.chat.ComponentSerializer; |
| 8 | +import net.md_5.bungee.chat.TextComponentSerializer; |
| 9 | +import net.md_5.bungee.chat.TranslatableComponentSerializer; |
| 10 | + |
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.DataInputStream; |
| 13 | +import java.io.DataOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.InetSocketAddress; |
| 16 | +import java.net.Socket; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class ServerListPing { |
| 20 | + private int timeout = 7000; |
| 21 | + private static Gson gson = new GsonBuilder() |
| 22 | + .registerTypeAdapter(BaseComponent.class, new ComponentSerializer()) |
| 23 | + .registerTypeAdapter(TextComponent.class, new TextComponentSerializer()) |
| 24 | + .registerTypeAdapter(TranslatableComponentSerializer.class, new TranslatableComponentSerializer()) |
| 25 | + .create(); |
| 26 | + |
| 27 | + private static int readVarInt(DataInputStream in) throws IOException { |
| 28 | + int i = 0; |
| 29 | + int j = 0; |
| 30 | + while (true) { |
| 31 | + int k = in.readByte(); |
| 32 | + i |= (k & 0x7F) << j++ * 7; |
| 33 | + if (j > 5) throw new RuntimeException("VarInt too big"); |
| 34 | + if ((k & 0x80) != 128) break; |
| 35 | + } |
| 36 | + return i; |
| 37 | + } |
| 38 | + |
| 39 | + private static void writeVarInt(DataOutputStream out, int paramInt) throws IOException { |
| 40 | + while (true) { |
| 41 | + if ((paramInt & 0xFFFFFF80) == 0) { |
| 42 | + out.writeByte(paramInt); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + out.writeByte(paramInt & 0x7F | 0x80); |
| 47 | + paramInt >>>= 7; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public StatusResponse ping(InetSocketAddress host) throws IOException { |
| 52 | + try (Socket socket = new Socket()) { |
| 53 | + socket.setSoTimeout(timeout); |
| 54 | + socket.connect(host, timeout); |
| 55 | + |
| 56 | + try (DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); |
| 57 | + DataInputStream dataInputStream = new DataInputStream(socket.getInputStream())) { |
| 58 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 59 | + DataOutputStream handshake = new DataOutputStream(b); |
| 60 | + handshake.writeByte(0x00); //packet id for handshake |
| 61 | + writeVarInt(handshake, 4); //protocol version |
| 62 | + writeVarInt(handshake, host.getHostString().length()); //host length |
| 63 | + handshake.writeBytes(host.getHostString()); //host string |
| 64 | + handshake.writeShort(host.getPort()); //port |
| 65 | + writeVarInt(handshake, 1); //state (1 for handshake) |
| 66 | + |
| 67 | + writeVarInt(dataOutputStream, b.size()); //prepend size |
| 68 | + dataOutputStream.write(b.toByteArray()); //write handshake packet |
| 69 | + |
| 70 | + |
| 71 | + dataOutputStream.writeByte(0x01); //size is only 1 |
| 72 | + dataOutputStream.writeByte(0x00); //packet id for ping |
| 73 | + int size = readVarInt(dataInputStream); //size of packet |
| 74 | + int id = readVarInt(dataInputStream); //packet id |
| 75 | + |
| 76 | + if (id == -1) { |
| 77 | + throw new IOException("Premature end of stream."); |
| 78 | + } |
| 79 | + |
| 80 | + if (id != 0x00) { //we want a status response |
| 81 | + throw new IOException("Invalid packetID"); |
| 82 | + } |
| 83 | + |
| 84 | + int length = readVarInt(dataInputStream); //length of json string |
| 85 | + if (length == -1) { |
| 86 | + throw new IOException("Premature end of stream."); |
| 87 | + } |
| 88 | + |
| 89 | + if (length == 0) { |
| 90 | + throw new IOException("Invalid string length."); |
| 91 | + } |
| 92 | + |
| 93 | + byte[] in = new byte[length]; |
| 94 | + dataInputStream.readFully(in); //read json string |
| 95 | + String json = new String(in); |
| 96 | + |
| 97 | + |
| 98 | + long now = System.currentTimeMillis(); |
| 99 | + dataOutputStream.writeByte(0x09); //size of packet |
| 100 | + dataOutputStream.writeByte(0x01); //0x01 for ping |
| 101 | + dataOutputStream.writeLong(now); //time!? |
| 102 | + |
| 103 | + readVarInt(dataInputStream); |
| 104 | + id = readVarInt(dataInputStream); |
| 105 | + if (id == -1) { |
| 106 | + throw new IOException("Premature end of stream."); |
| 107 | + } |
| 108 | + |
| 109 | + if (id != 0x01) { |
| 110 | + throw new IOException("Invalid packetID"); |
| 111 | + } |
| 112 | + long pingtime = dataInputStream.readLong(); //read response |
| 113 | + |
| 114 | + StatusResponse response = gson.fromJson(json, StatusResponse.class); |
| 115 | + response.time = (int) (now - pingtime); |
| 116 | + return response; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public void setTimeout(int timeout) { |
| 122 | + this.timeout = timeout; |
| 123 | + } |
| 124 | + |
| 125 | + public int getTimeout() { |
| 126 | + return timeout; |
| 127 | + } |
| 128 | + |
| 129 | + public static class StatusResponse { |
| 130 | + private BaseComponent description; |
| 131 | + private Players players; |
| 132 | + private Version version; |
| 133 | + private String favicon; |
| 134 | + private int time; |
| 135 | + |
| 136 | + public BaseComponent getDescription() { |
| 137 | + return description; |
| 138 | + } |
| 139 | + |
| 140 | + public Players getPlayers() { |
| 141 | + return players; |
| 142 | + } |
| 143 | + |
| 144 | + public Version getVersion() { |
| 145 | + return version; |
| 146 | + } |
| 147 | + |
| 148 | + public String getFavicon() { |
| 149 | + return favicon; |
| 150 | + } |
| 151 | + |
| 152 | + public int getTime() { |
| 153 | + return time; |
| 154 | + } |
| 155 | + |
| 156 | + public static class Players { |
| 157 | + private int max; |
| 158 | + private int online; |
| 159 | + private List<Player> sample; |
| 160 | + |
| 161 | + public int getMax() { |
| 162 | + return max; |
| 163 | + } |
| 164 | + |
| 165 | + public int getOnline() { |
| 166 | + return online; |
| 167 | + } |
| 168 | + |
| 169 | + public List<Player> getSample() { |
| 170 | + return sample; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public static class Player { |
| 175 | + private String name; |
| 176 | + private String id; |
| 177 | + |
| 178 | + public String getName() { |
| 179 | + return name; |
| 180 | + } |
| 181 | + |
| 182 | + public String getId() { |
| 183 | + return id; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public static class Version { |
| 188 | + private String name; |
| 189 | + private String protocol; |
| 190 | + |
| 191 | + public String getName() { |
| 192 | + return name; |
| 193 | + } |
| 194 | + |
| 195 | + public String getProtocol() { |
| 196 | + return protocol; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments