|
| 1 | +package mcping; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.DataInputStream; |
| 5 | +import java.io.DataOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.net.InetSocketAddress; |
| 10 | +import java.net.Socket; |
| 11 | + |
| 12 | +class Pinger |
| 13 | +{ |
| 14 | + private InetSocketAddress host; |
| 15 | + private int timeout; |
| 16 | + |
| 17 | + void setAddress(InetSocketAddress host) |
| 18 | + { |
| 19 | + this.host = host; |
| 20 | + } |
| 21 | + void setTimeout(int timeout) |
| 22 | + { |
| 23 | + this.timeout = timeout; |
| 24 | + } |
| 25 | + |
| 26 | + private int readVarInt(DataInputStream in) throws IOException { |
| 27 | + int i = 0; |
| 28 | + int j = 0; |
| 29 | + int k; |
| 30 | + do |
| 31 | + { |
| 32 | + k = in.readByte(); |
| 33 | + i |= (k & 0x7F) << j++ * 7; |
| 34 | + if (j > 5) { |
| 35 | + //throw new RuntimeException("VarInt too big"); |
| 36 | + return -1; |
| 37 | + } |
| 38 | + } while ((k & 0x80) == 128); |
| 39 | + return i; |
| 40 | + } |
| 41 | + |
| 42 | + private void writeVarInt(DataOutputStream out, int paramInt) throws IOException { |
| 43 | + for (;;) |
| 44 | + { |
| 45 | + if ((paramInt & 0xFFFFFF80) == 0) |
| 46 | + { |
| 47 | + out.writeByte(paramInt); |
| 48 | + return; |
| 49 | + } |
| 50 | + out.writeByte(paramInt & 0x7F | 0x80); |
| 51 | + paramInt >>>= 7; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public String fetchData() throws IOException { |
| 56 | + Socket socket = new Socket(); |
| 57 | + socket.setSoTimeout(this.timeout); |
| 58 | + socket.connect(this.host, this.timeout); |
| 59 | + OutputStream outputStream = socket.getOutputStream(); |
| 60 | + DataOutputStream dataOutputStream = new DataOutputStream(outputStream); |
| 61 | + InputStream inputStream = socket.getInputStream(); |
| 62 | + ByteArrayOutputStream b = new ByteArrayOutputStream(); |
| 63 | + DataOutputStream handshake = new DataOutputStream(b); |
| 64 | + handshake.writeByte(0); |
| 65 | + writeVarInt(handshake, 4); |
| 66 | + writeVarInt(handshake, this.host.getHostString().length()); |
| 67 | + handshake.writeBytes(this.host.getHostString()); |
| 68 | + handshake.writeShort(this.host.getPort()); |
| 69 | + writeVarInt(handshake, 1); |
| 70 | + writeVarInt(dataOutputStream, b.size()); |
| 71 | + dataOutputStream.write(b.toByteArray()); |
| 72 | + dataOutputStream.writeByte(1); |
| 73 | + dataOutputStream.writeByte(0); |
| 74 | + DataInputStream dataInputStream = new DataInputStream(inputStream); |
| 75 | + int size = readVarInt(dataInputStream); |
| 76 | + if(size == -1) return null; |
| 77 | + int id = readVarInt(dataInputStream); |
| 78 | + if (id == -1) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + if (id != 0) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + int length = readVarInt(dataInputStream); |
| 85 | + if(length == -1) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + if (length == 0) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + byte[] in = new byte[length]; |
| 92 | + dataInputStream.readFully(in); |
| 93 | + |
| 94 | + b.close(); |
| 95 | + dataInputStream.close(); |
| 96 | + handshake.close(); |
| 97 | + dataOutputStream.close(); |
| 98 | + outputStream.close(); |
| 99 | + inputStream.close(); |
| 100 | + socket.close(); |
| 101 | + return new String(in); //ritorna il json |
| 102 | + } |
| 103 | +} |
0 commit comments