Guest User

Minecraft client

a guest
May 17th, 2020
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. package TestProject;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.net.InetSocketAddress;
  8. import java.net.Socket;
  9. import java.nio.charset.Charset;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.Arrays;
  12.  
  13. public class Main {
  14.     public static void main(String[] args) throws IOException, InterruptedException {
  15.         String address = "localhost";
  16.         int port = 25565;
  17.         int version = 578; // 1.15.2's protocol version is 578.
  18.         String username = "username";
  19.  
  20.         System.out.println("Connecting to Address: " + address + ":" + port + " ...");
  21.  
  22.         InetSocketAddress host = new InetSocketAddress(address, port);
  23.         Socket socket = new Socket();
  24.         System.out.println("Connecting to Server...");
  25.  
  26.         while (!socket.isConnected()) {
  27.             socket.connect(host, 4000);
  28.             Thread.sleep(500);
  29.         }
  30.  
  31.         System.out.println("Making Streams...");
  32.         DataOutputStream output = new DataOutputStream(socket.getOutputStream());
  33.         DataInputStream input = new DataInputStream(socket.getInputStream());
  34.  
  35.         System.out.println("Attempting Handshake with: " + host.getAddress().toString() + " ...");
  36.  
  37.         int packetId = getHandshakeAndReturn(version, address, port, 1, output, input);
  38.  
  39.         if (packetId == -1) {
  40.             throw new IOException("Error: Premature End of Stream.");
  41.         }
  42.  
  43.         if (packetId != 0x00) { // we want a status response
  44.             throw new IOException("Error: Invalid packetID; expected 0x00 but got " + packetId + " instead.");
  45.         }
  46.         int length = readVarInt(input); // length of json string
  47.  
  48.         if (length == -1) {
  49.             throw new IOException("Error: Premature End of Stream.");
  50.         }
  51.  
  52.         if (length == 0) {
  53.             throw new IOException("Error: Invalid String Length.");
  54.         }
  55.  
  56.         byte[] in = new byte[length];
  57.         input.readFully(in);  // read json string
  58.         String json = new String(in);
  59.  
  60.         // C->S : Ping
  61.         long now = System.currentTimeMillis();
  62.         output.writeByte(0x09); // size of packet
  63.         output.writeByte(0x01); // 0x01 for ping
  64.         output.writeLong(now); // send system time in ms
  65.  
  66.         // S->C : Pong
  67.         readVarInt(input);
  68.         packetId = readVarInt(input);
  69.         if (packetId == -1) {
  70.             throw new IOException("Error: Premature End of Stream.");
  71.         }
  72.  
  73.         if (packetId != 0x01) {
  74.             throw new IOException("Error: Invalid packetID; expected 0x01 but got " + packetId + " instead.");
  75.         }
  76.         long pingtime = input.readLong(); // read response
  77.  
  78.         if (pingtime == now) {
  79.             System.out.println("Successfully Connected to Server...");
  80.         } else {
  81.             throw new IOException("Error: Ping-Pong Mismatch.");
  82.         }
  83.  
  84.         // print out server info
  85.         System.out.println(json);
  86.  
  87.         System.out.println("Logging In to: " + address + ":" + port + " ...");
  88.  
  89.         System.out.println("Logging In... (1/3)"); // C -> S handshake2
  90.  
  91.         socket.close();
  92.         Socket socket2 = new Socket();
  93.  
  94.         while (!socket2.isConnected()) {
  95.             socket2.connect(host, 4000);
  96.             Thread.sleep(500);
  97.         }
  98.  
  99.         DataOutputStream out2 = new DataOutputStream(socket2.getOutputStream());
  100.         DataInputStream in2 = new DataInputStream(socket2.getInputStream());
  101.  
  102.         int packetId2 = getHandshakeAndReturn(version, address, port, 2, out2, in2);
  103.         byte[] test = new byte[readVarInt(in2)];
  104.         in2.readFully(test);
  105.         System.out.println(new String(test));
  106.  
  107.         System.out.println("Logging In... (2/3)"); // C -> S login start
  108.  
  109.         int userLength = username.getBytes(StandardCharsets.UTF_8).length; //get username length
  110.  
  111.         if (userLength > 16) {
  112.             throw new IOException("Error: Username is Too Long.");
  113.         } else if (userLength < 3) {
  114.             throw new IOException("Error: Username is Too Short.");
  115.         }
  116.  
  117.         out2.writeByte(userLength + 1);
  118.         out2.writeByte(0x00);
  119.         writeString(out2, username, StandardCharsets.UTF_8);
  120.  
  121.         System.out.println("Logging In... (3/3)"); // S -> C login success
  122.  
  123.         Thread.sleep(500);
  124.  
  125.         int loginSuccessSize = readVarInt(in2);
  126.         int loginSuccessPacketId = readVarInt(in2);
  127.  
  128.         if (loginSuccessPacketId == -1) {
  129.             throw new IOException("Error: Premature End of Stream; Login Failed.");
  130.         }
  131.  
  132.         if (loginSuccessPacketId != 0x02) {
  133.             throw new IOException("Error: Invalid packetID; expected 0x02 but got " + packetId + " instead; Login Failed.");
  134.         }
  135.  
  136.         byte[] loginSuccessIn = new byte[readVarInt(in2)];
  137.         in2.readFully(loginSuccessIn);
  138.         String loginSuccessJson = new String(loginSuccessIn);
  139.  
  140.         System.out.println("Successfully Logged In to Server:");
  141.  
  142.         System.out.println(loginSuccessJson);
  143.     }
  144.  
  145.     public static int getHandshakeAndReturn(int version, String address, int port, int state, DataOutputStream output, DataInputStream input) throws IOException, InterruptedException {
  146.         byte[] handshakeMessage = createHandshakeMessage(version, address, port, state);
  147.  
  148.         // print handshake message as string
  149.         System.out.println("Sending Handshake: " + Arrays.toString(handshakeMessage) + " ...");
  150.  
  151.         // C->S : Handshake State=state
  152.         // send packet length and packet
  153.         writeVarInt(output, handshakeMessage.length);
  154.         output.write(handshakeMessage);
  155.  
  156.         // C->S : Request
  157.         output.writeByte(0x01); // size is only 1
  158.         output.writeByte(0x00); // packet id for ping
  159.  
  160.         Thread.sleep(500);
  161.  
  162.         // S->C : Response
  163.         readVarInt(input);
  164.         return readVarInt(input);
  165.     }
  166.  
  167.     public static byte[] createHandshakeMessage(int version, String host, int port, int state) throws IOException {
  168.         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  169.  
  170.         DataOutputStream handshake = new DataOutputStream(buffer);
  171.         handshake.writeByte(0x00); //packet id for handshake
  172.         writeVarInt(handshake, version); //protocol version
  173.         writeString(handshake, host, StandardCharsets.UTF_8);
  174.         handshake.writeShort(port); //port
  175.         writeVarInt(handshake, state); //state (1 for handshake)
  176.  
  177.         return buffer.toByteArray();
  178.     }
  179.  
  180.     public static void writeString(DataOutputStream out, String string, Charset charset) throws IOException {
  181.         byte[] bytes = string.getBytes(charset);
  182.         writeVarInt(out, bytes.length);
  183.         out.write(bytes);
  184.     }
  185.  
  186.     public static void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
  187.         while (true) {
  188.             if ((paramInt & 0xFFFFFF80) == 0) {
  189.                 out.writeByte(paramInt);
  190.                 return;
  191.             }
  192.  
  193.             out.writeByte(paramInt & 0x7F | 0x80);
  194.             paramInt >>>= 7;
  195.         }
  196.     }
  197.  
  198.     public static int readVarInt(DataInputStream in) throws IOException {
  199.         int i = 0;
  200.         int j = 0;
  201.         while (true) {
  202.             int k = in.readByte();
  203.             i |= (k & 0x7F) << j++ * 7;
  204.             if (j > 5) throw new RuntimeException("VarInt is Too Big.");
  205.             if ((k & 0x80) != 128) break;
  206.         }
  207.         return i;
  208.     }
  209. }
Add Comment
Please, Sign In to add comment