Add groundwork for custom protocols such as forge in a really cool manner - has not been tested, so may be a regression on prior build.

This commit is contained in:
md_5
2013-02-10 21:26:49 +11:00
parent 9f4fc4dfac
commit 9c5e02e20a
6 changed files with 44 additions and 53 deletions

View File

@@ -15,6 +15,7 @@ import net.md_5.bungee.packet.DefinedPacket;
import net.md_5.bungee.packet.PacketFAPluginMessage;
import net.md_5.bungee.packet.PacketFFKick;
import net.md_5.bungee.packet.PacketStream;
import net.md_5.mendax.PacketDefinitions;
public class BungeeServerInfo extends ServerInfo
{
@@ -56,7 +57,7 @@ public class BungeeServerInfo extends ServerInfo
out.write( 0xFE );
out.write( 0x01 );
PacketStream in = new PacketStream( socket.getInputStream() );
PacketStream in = new PacketStream( socket.getInputStream(), PacketDefinitions.VANILLA_PROTOCOL );
PacketFFKick response = new PacketFFKick( in.readPacket() );
String[] split = response.message.split( "\00" );

View File

@@ -26,6 +26,7 @@ import net.md_5.bungee.packet.PacketFEPing;
import net.md_5.bungee.packet.PacketFFKick;
import net.md_5.bungee.packet.PacketHandler;
import net.md_5.bungee.packet.PacketStream;
import net.md_5.mendax.PacketDefinitions;
import org.bouncycastle.crypto.io.CipherInputStream;
import org.bouncycastle.crypto.io.CipherOutputStream;
@@ -39,12 +40,13 @@ public class InitialHandler extends PacketHandler implements Runnable, PendingCo
private Packet2Handshake handshake;
private PacketFDEncryptionRequest request;
private State thisState = State.HANDSHAKE;
private int protocol = PacketDefinitions.VANILLA_PROTOCOL;
public InitialHandler(Socket socket, ListenerInfo info) throws IOException
{
this.socket = socket;
this.listener = info;
stream = new PacketStream( socket.getInputStream(), socket.getOutputStream() );
stream = new PacketStream( socket.getInputStream(), socket.getOutputStream(), protocol );
}
private enum State
@@ -128,7 +130,7 @@ public class InitialHandler extends PacketHandler implements Runnable, PendingCo
stream.write( new PacketFCEncryptionResponse() );
stream = new PacketStream( new CipherInputStream( socket.getInputStream(),
EncryptionUtil.getCipher( false, shared ) ), new CipherOutputStream( socket.getOutputStream(), EncryptionUtil.getCipher( true, shared ) ) );
EncryptionUtil.getCipher( false, shared ) ), new CipherOutputStream( socket.getOutputStream(), EncryptionUtil.getCipher( true, shared ) ), protocol );
thisState = State.LOGIN;
}

View File

@@ -19,6 +19,7 @@ import net.md_5.bungee.packet.PacketCDClientStatus;
import net.md_5.bungee.packet.PacketFAPluginMessage;
import net.md_5.bungee.packet.PacketFFKick;
import net.md_5.bungee.packet.PacketStream;
import net.md_5.mendax.PacketDefinitions;
/**
* Class representing a connection from the proxy to the server; ie upstream.
@@ -46,7 +47,7 @@ public class ServerConnection extends GenericConnection implements Server
socket.connect( info.getAddress(), BungeeCord.getInstance().config.getTimeout() );
BungeeCord.getInstance().setSocketOptions( socket );
PacketStream stream = new PacketStream( socket.getInputStream(), socket.getOutputStream() );
PacketStream stream = new PacketStream( socket.getInputStream(), socket.getOutputStream(), PacketDefinitions.VANILLA_PROTOCOL );
stream.write( handshake );
stream.write( PacketCDClientStatus.CLIENT_LOGIN );

View File

@@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import lombok.Getter;
import lombok.Setter;
import net.md_5.mendax.datainput.DataInputPacketReader;
/**
@@ -19,19 +20,22 @@ public class PacketStream implements AutoCloseable
private final DataInputStream dataInput;
@Getter
private OutputStream out;
@Setter
private int protocol;
private final TrackingInputStream tracker;
private final byte[] buffer = new byte[ 1 << 18 ];
public PacketStream(InputStream in)
public PacketStream(InputStream in, int protocol)
{
this( in, null );
this( in, null, protocol );
}
public PacketStream(InputStream in, OutputStream out)
public PacketStream(InputStream in, OutputStream out, int protocol)
{
tracker = new TrackingInputStream( in );
dataInput = new DataInputStream( tracker );
this.out = out;
this.protocol = protocol;
}
public void write(byte[] b) throws IOException
@@ -53,7 +57,7 @@ public class PacketStream implements AutoCloseable
public byte[] readPacket() throws IOException
{
tracker.out.reset();
DataInputPacketReader.readPacket( dataInput, buffer );
DataInputPacketReader.readPacket( dataInput, buffer, protocol );
return tracker.out.toByteArray();
}