Skip to content

Commit

Permalink
Add support for LegacyPing(server ver 1.6.x)
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Jun 18, 2021
1 parent 834ccf6 commit e22bd12
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 26 deletions.
148 changes: 132 additions & 16 deletions BasicInfoAPI/src/main/conn/MinecraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
* A MinecraftServer instance provides methods to create connection to server and communicate with peer.
Expand All @@ -35,18 +36,105 @@ public MinecraftServer(String host,int port)throws Exception {
.addShort(port)
.addVarInt(1).write(dataOutputStream);
new PacketSend(0).write(dataOutputStream);
response=new Gson().fromJson(new PacketRecv(dataInputStream).popString(),Response.class);
}
try {
response = new Gson().fromJson(new PacketRecv(dataInputStream).popString(), Response.class);
}catch (EOFException e){//To change protocol.
System.out.println("ChangeProtocol");
socket=new Socket(host,port);
dataInputStream=new DataInputStream(socket.getInputStream());
dataOutputStream=new DataOutputStream(socket.getOutputStream());

dataOutputStream.write(0xFE);
dataOutputStream.write(0x01);
dataOutputStream.flush();

public class Response{
class description{
if (dataInputStream.readByte()==-1){
dataInputStream.readByte();
dataInputStream.readByte();
byte[] b=new byte[512];
dataInputStream.read(b);
ByteBase bbase=new ByteBase(b);
byte[] end=new byte[]{0,0};
if (new String(bbase.pop(end),StandardCharsets.UTF_16BE).equals("§1")){
System.out.println("packingResponse.");
response=new Response();
response.version=new Response.version();
response.version.protocol=Integer.parseInt
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
response.version.name=new String(bbase.pop(end),StandardCharsets.UTF_16BE);
response.description=new Response.description();
response.description.text=new String(bbase.pop(end),StandardCharsets.UTF_16BE);
response.players=new Response.players();
response.players.online=Integer.parseInt
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
response.players.max=Integer.parseInt
(new String(bbase.pop(end),StandardCharsets.UTF_16BE));
}
}
}
}
private short len(String l){
String utf16be=new String(l.getBytes(StandardCharsets.UTF_8),StandardCharsets.UTF_16BE);
return (short)utf16be.length();
}
private void listByte(byte[] bytes){
System.out.print("list:");
for(byte b:bytes){
System.out.print(b+" ");
}
System.out.println();
}
private class ByteBase{
byte[] arr;
int index=0;
ByteBase(byte[] byteArr){
this.arr=byteArr;
for (byte b:byteArr){
System.out.print(b+" ");
}
System.out.println();
String s=new String(arr,StandardCharsets.UTF_16BE);
System.out.println(s);
}
public byte[] pop(byte[] end){
ArrayList<Byte> byteArrayList=new ArrayList<>();
byte[] b=new byte[2];
while(true){
b=pop(2);
if (b[0]==end[0]&&b[1]==end[1]){
break;
}
byteArrayList.add(b[0]);
byteArrayList.add(b[1]);
}
int len=byteArrayList.size();
byte[] result=new byte[len];
for (int i=0;i<len;i++){
result[i]=byteArrayList.get(i);
}
listByte(result);
return result;
}
public byte pop(){
return arr[index++];
}
public byte[] pop(int len){
byte[] result=new byte[len];
for (int i=0;i<len;i++){
result[i]=pop();
}
return result;
}
}

public static class Response{
static class description{
String text;
String color;
description[] extra;
}
description description;
class players{
static class players{
int max;
int online;
class player{
Expand All @@ -56,7 +144,7 @@ class player{
player[] sample;
}
players players;
class version{
static class version{
String name;
int protocol;
}
Expand All @@ -67,27 +155,43 @@ class version{

@Override
public String getVersionName() {
return response.version.name;
if (response!=null&&response.version!=null) {
return response.version.name;
}else {
return null;
}
}

@Override
public int getVersionProtocol() {
return response.version.protocol;
if (response!=null&&response.version!=null) {
return response.version.protocol;
}else {
return -1;
}
}

@Override
public int getMaxPlayer() {
return response.players.max;
if (response!=null&&response.players!=null) {
return response.players.max;
}else {
return -1;
}
}

@Override
public int getOnlinePlayer() {
return response.players.online;
if (response!=null&&response.players!=null) {
return response.players.online;
}else {
return -1;
}
}

@Override
public Player[] getPlayerList() {
if (response.players.sample!=null) {
if (response!=null&&response.players!=null&&response.players.sample!=null) {
int len = response.players.sample.length;
Player[] players = new Player[len];
for (int i = 0; i < len; i++) {
Expand All @@ -103,15 +207,23 @@ public Player[] getPlayerList() {

@Override
public String getDefaultDescriptionText() {
return response.description.text;
if (response!=null&&response.description!=null) {
return response.description.text;
}else {
return null;
}
}
@Override
public String getDefaultDescriptionColor(){
return response.description.color;
if (response!=null&&response.description!=null) {
return response.description.color;
}else {
return null;
}
}
@Override
public ExtraDescr[] getExtraDescription(){
if (response.description.extra!=null) {
if (response!=null&&response.description!=null&&response.description.extra!=null) {
ExtraDescr[] extraDescrs = new ExtraDescr[response.description.extra.length];
for (int i = 0; i < response.description.extra.length; i++) {
extraDescrs[i] = new ExtraDescr();
Expand All @@ -125,7 +237,11 @@ public ExtraDescr[] getExtraDescription(){
}
@Override
public String getFavicon() {
return response.favicon;
if (response!=null) {
return response.favicon;
}else {
return null;
}
}

}
17 changes: 9 additions & 8 deletions BasicInfoAPI/src/test/conn/FakeServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@
public class FakeServerTest {
static DataInputStream dataInputStream;
public static void main(String[] args)throws Exception {
ServerSocket socket=new ServerSocket(25565);
ServerSocket socket=new ServerSocket(25567);
while (true){
Socket client=socket.accept();
System.out.println("newConn.");
try{
dataInputStream=new DataInputStream(client.getInputStream());
int len=0;
byte[] recv=new byte[2048];
System.out.println("Length(varInt):"+readVarInt());
System.out.println("PacketID(varInt):"+readVarInt());
System.out.println("ProtocolVersion(varInt):"+readVarInt());
System.out.println("ServerAddress(String):"+readString());
System.out.println("ServerPort(UnsignedShort):"+readUnsignedShort());
System.out.println("NextState(varIntEnum):"+readVarInt());
// System.out.println("Length(varInt):"+readVarInt());
// System.out.println("PacketID(varInt):"+readVarInt());
// System.out.println("ProtocolVersion(varInt):"+readVarInt());
// System.out.println("ServerAddress(String):"+readString());
// System.out.println("ServerPort(UnsignedShort):"+readUnsignedShort());
// System.out.println("NextState(varIntEnum):"+readVarInt());
while((len=dataInputStream.read(recv))!=-1){
if (len==0)
continue;
// System.out.println("recv:"+new String(recv, StandardCharsets.UTF_8));
for (int i=0;i<1024;i++){
for (int i=0;i<len;i++){
System.out.print(recv[i]+" ");
}
System.out.println();
recv=new byte[1024];
}
}catch (Exception e){
Expand Down
72 changes: 72 additions & 0 deletions BasicInfoAPI/src/test/conn/LegacyPingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package test.conn;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class LegacyPingTest {
public static void main(String[] args)throws Exception {
sendLegacyPing("localhost",25565);
}
public static void sendLegacyPing(String host,int port)throws Exception{

System.out.println("ChangeProtocol");
Socket socket=new Socket(host,port);
DataInputStream dataInputStream=new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream=new DataOutputStream(socket.getOutputStream());
// dataOutputStream.write(0xFE);
// dataOutputStream.write(0x01);
// dataOutputStream.write(0xFA);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x0B);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x4D);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x43);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x7C);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x50);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x69);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x6E);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x67);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x48);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x6F);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x73);
// dataOutputStream.write(0x00);
// dataOutputStream.write(0x74);//MC|PingHost over
//
// dataOutputStream.write(7+len(host));
// dataOutputStream.write(0x4A);
// dataOutputStream.write(len(host));
// String utf16beHost=new String(host.getBytes(StandardCharsets.UTF_8),StandardCharsets.UTF_16BE);
// dataOutputStream.write(utf16beHost.getBytes());
// dataOutputStream.write(port);
// dataOutputStream.flush();
dataOutputStream.write(0xFE);
dataOutputStream.write(0x01);
dataOutputStream.flush();

if (dataInputStream.readByte()==-1){
dataInputStream.readByte();
dataInputStream.readByte();
int len=0;
byte[] b=new byte[1024];
len=dataInputStream.read(b);
String s=new String(b,StandardCharsets.UTF_16BE);
}
}
private static short len(String l){
String utf16be=new String(l.getBytes(StandardCharsets.UTF_8),StandardCharsets.UTF_16BE);
return (short)utf16be.length();
}

}
4 changes: 2 additions & 2 deletions BasicInfoAPI/src/test/conn/MinecraftServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class MinecraftServerTest {
public static void main(String[] args)throws Exception {
MinecraftServer minecraftServer=new MinecraftServer("cn-cd-dx-1.natfrp.cloud",18904);
MinecraftServer minecraftServer=new MinecraftServer("localhost",25565);
System.out.println("version:name:"+minecraftServer.getVersionName()+" protocol:"+minecraftServer.getVersionProtocol());
System.out.println("defaultDescription:color:"+minecraftServer.getDefaultDescriptionColor()+" text:"+minecraftServer.getDefaultDescriptionText());
IServerInfo.ExtraDescr[] extraDescrs=minecraftServer.getExtraDescription();
Expand All @@ -19,4 +19,4 @@ public static void main(String[] args)throws Exception {
}
System.out.println("favicon:"+minecraftServer.getFavicon());
}
}
}

0 comments on commit e22bd12

Please sign in to comment.