2014.08.14.

This commit is contained in:
Norbi Peti 2016-06-15 23:42:34 +02:00
parent 6f855aaa35
commit 8b439e1d81
7 changed files with 294 additions and 286 deletions

View file

@ -35,6 +35,9 @@
<Reference Include="Cyotek.Data.Nbt"> <Reference Include="Cyotek.Data.Nbt">
<HintPath>..\..\..\..\..\Downloads\Cyotek.Data.Nbt-master\Cyotek.Data.Nbt-master\Cyotek.Data.Nbt\bin\Debug\Cyotek.Data.Nbt.dll</HintPath> <HintPath>..\..\..\..\..\Downloads\Cyotek.Data.Nbt-master\Cyotek.Data.Nbt-master\Cyotek.Data.Nbt\bin\Debug\Cyotek.Data.Nbt.dll</HintPath>
</Reference> </Reference>
<Reference Include="Nito.Async">
<HintPath>..\..\..\..\..\Downloads\Nito.Async.1.4\Nito.Async.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />

View file

@ -6,84 +6,72 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Cyotek.Data.Nbt; using Cyotek.Data.Nbt;
using System.Threading; using System.Threading;
using System.Net;
namespace Connection_forwarder namespace Connection_forwarder
{ {
class ClientConnectionThread class ClientConnectionThread
{ {
public void Run(object sckt) public Int64 ClientID;
public bool DataChanged = false;
public byte[] DataBuffer;
public void Run(object obj)
{ {
Thread.CurrentThread.Name = "ConnectionThread"; Thread.CurrentThread.Name = "ClientConnectionThread";
Form1.Threads.Add(Thread.CurrentThread); //Form1.Threads.Add(Thread.CurrentThread);
//var connsocket = ((Socket[])sckt)[0]; //IPEndPoint RemoteIP = (IPEndPoint)remip;
object connsocket; IPEndPoint RemoteIP = (IPEndPoint)((object[])obj)[0];
if (!Form1.PEVersion) UdpClient connection = (UdpClient)((object[])obj)[1];
connsocket = ((object[])sckt)[0] as Socket; ClientID = (Int64)((object[])obj)[2];
else //Program.ConnThreads.Add(ClientID, new object[] { Thread.CurrentThread, this }); //Remove it over time
connsocket = ((UdpClient)((object[])sckt)[0]); Program.ConnThreads.Add(RemoteIP, new object[] { Thread.CurrentThread, this }); //Remove it over time
//var serversock = new Socket(SocketType.Stream, ProtocolType.Tcp); //UdpClient listenclient;
//serversock.Connect("localhost", Form1.PortNumber); //byte[] buffer;
//while (connsocket.Available > 0) //bool brvar = false;
var serversock = ((Socket)((object[])sckt)[1]); //UdpClient sendsocket;
//new NetworkStream(connsocket).CopyToAsync(new NetworkStream(serversock)); var lasttick = Environment.TickCount;
//new NetworkStream(serversock).CopyTo(new NetworkStream(connsocket)); while (Environment.TickCount - lasttick < 60 * 1000)
if (!Form1.PEVersion)
{ {
try if (DataChanged)
{ { //Now DataBuffer contains the received packet
new NetworkStream(connsocket as Socket).CopyTo(new NetworkStream(serversock)); Console.WriteLine("Data changed...");
Console.WriteLine("PacketID: 0x{0:X}", DataBuffer[0]);
lasttick = Environment.TickCount;
DataChanged = false; //Finished working
} }
catch { } /*do
(connsocket as Socket).Close(); {
serversock.Close(); //new PEPackets(PEPackets.TOCLIENT_LoginStatusPacket, new object[] { 0 }, sendsocket, RemoteIP);
//connsocket[1] = new Socket(SocketType.Stream, ProtocolType.Tcp); //We still need to connect to the server
//((Socket)connsocket[1]).Connect("localhost", Form1.PortNumber);
//After login loop to receive all messages
} while (false);*/
} }
else Program.ConnThreads.Remove(RemoteIP);
}
/*private bool done = false;
void saea_Completed(object sender, SocketAsyncEventArgs e)
{
done = true;
}*/
public void SendData(object data)
{ {
} }
} }
}
class ServerConnectionThread class ServerConnectionThread
{ {
public void Run(object sckt) public void Run(object sckt)
{ {
Thread.CurrentThread.Name = "ConnectionThread"; Thread.CurrentThread.Name = "ServerConnectionThread";
Form1.Threads.Add(Thread.CurrentThread); //Form1.Threads.Add(Thread.CurrentThread);
object connsocket; /*object connsocket;
if (!Form1.PEVersion)
connsocket = ((object[])sckt)[0] as Socket;
else
connsocket = ((UdpClient)((object[])sckt)[0]); connsocket = ((UdpClient)((object[])sckt)[0]);
//var serversock = new Socket(SocketType.Stream, ProtocolType.Tcp);
//serversock.Connect("localhost", Form1.PortNumber);
//while (connsocket.Available > 0)
var serversock = ((Socket)((object[])sckt)[1]); var serversock = ((Socket)((object[])sckt)[1]);
//new NetworkStream(connsocket).CopyToAsync(new NetworkStream(serversock));
try
{
if (!Form1.PEVersion)
new NetworkStream(serversock).CopyTo(new NetworkStream(connsocket as Socket));
/*var nsc = new NetworkStream(connsocket);
var nss = new NetworkStream(serversock);
var tr = new BinaryTagReader(nss, NbtOptions.None);
var tw = new BinaryTagWriter(nsc, NbtOptions.None);
byte[] ba;
while (!tr.ReadString().Contains("Blocks"))
{
nss.CopyTo(nsc);
}
ba = tr.ReadByteArray();
Console.WriteLine("Byte array: " + ba);
for (int i = 0; i < ba.Length; i++)
tw.Write(ba[i]);*/
}
catch
{
}
(connsocket as Socket).Close(); (connsocket as Socket).Close();
serversock.Close(); serversock.Close();*/
} }
} }
} }

View file

@ -17,19 +17,18 @@ namespace Connection_forwarder
{ {
public static int PortNumber; public static int PortNumber;
public static int PortNumber2; public static int PortNumber2;
public static bool PEVersion; //public static bool PEVersion;
public static List<Thread> Threads = new List<Thread>(); //public static List<Thread> Threads = new List<Thread>();
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
Threads.Add(Thread.CurrentThread); //Threads.Add(Thread.CurrentThread);
} }
private void startbtn_Click(object sender, EventArgs e) private void startbtn_Click(object sender, EventArgs e)
{ {
var s = new Socket(SocketType.Stream, ProtocolType.Tcp); var s = new Socket(SocketType.Stream, ProtocolType.Tcp);
//s.Connect("localhost", Int32.Parse(portBox.Text));
s.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), Int32.Parse(portBox.Text))); s.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), Int32.Parse(portBox.Text)));
s.Listen(2); s.Listen(2);
s = s.Accept(); s = s.Accept();
@ -45,25 +44,13 @@ namespace Connection_forwarder
{ {
PortNumber = Int32.Parse(portBox.Text); PortNumber = Int32.Parse(portBox.Text);
PortNumber2 = Int32.Parse(portBox2.Text); PortNumber2 = Int32.Parse(portBox2.Text);
PEVersion = radioButton2.Checked; //PEVersion = radioButton2.Checked;
new Thread(new ThreadStart(ListenThread.Run)).Start(); new Thread(new ThreadStart(ListenThread.Run)).Start();
startforwardbtn.Enabled = false; startforwardbtn.Enabled = false;
} }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{ {
/*for (int i = 1; i < Threads.Count; i++) //For a fairly long time it stopped itself, now it skips that...
{
try
{
Threads[i].Abort();
}
catch
{
}
}
Application.Exit();
Application.ExitThread();*/
Environment.Exit(0); Environment.Exit(0);
} }
} }

View file

@ -1,4 +1,5 @@
using System; using Nito.Async;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -14,36 +15,11 @@ namespace Connection_forwarder
public static void Run() public static void Run()
{ {
Thread.CurrentThread.Name = "ListenThread"; Thread.CurrentThread.Name = "ListenThread";
Form1.Threads.Add(Thread.CurrentThread); //Form1.Threads.Add(Thread.CurrentThread);
Socket listensocket; UdpClient connection;
//UdpClient listenclient = new UdpClient(Form1.PortNumber2); var RemoteIP = new IPEndPoint(IPAddress.Any, Form1.PortNumber2);
UdpClient listenclient; connection = new UdpClient(Form1.PortNumber2);
if (!Form1.PEVersion)
{
//var listensocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
listensocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
listensocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), Form1.PortNumber2));
}
else
{
listensocket = new Socket(SocketType.Dgram, ProtocolType.Udp); //Just for it to not give "no object" error
//listensocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), Form1.PortNumber2));
//listensocket.Bind(new IPEndPoint(Program.GetLocalIP(), Form1.PortNumber2));
}
while (true) while (true)
{
if (!Form1.PEVersion)
{
listensocket.Listen(10);
//var connsocket = listensocket.Accept();
Socket[] connsocket = new Socket[2];
connsocket[0] = listensocket.Accept();
connsocket[1] = new Socket(SocketType.Stream, ProtocolType.Tcp);
connsocket[1].Connect("localhost", Form1.PortNumber);
new Thread(new ParameterizedThreadStart(new ClientConnectionThread().Run)).Start(connsocket);
new Thread(new ParameterizedThreadStart(new ServerConnectionThread().Run)).Start(connsocket);
}
else
{ {
/* /*
* PE connection: * PE connection:
@ -53,20 +29,40 @@ namespace Connection_forwarder
* So basically handle the PE join here to know when to start a new thread * So basically handle the PE join here to know when to start a new thread
*/ */
//byte[] buffer = new byte[1500];
byte[] buffer;
var RemoteIP=new IPEndPoint(IPAddress.Any, Form1.PortNumber2);
listenclient = new UdpClient(Form1.PortNumber2);
buffer = listenclient.Receive(ref RemoteIP);
listenclient.Close();
//RemoteIP.Port = 19132;
//Console.WriteLine(String.Format("Hex: %x", buffer[0]));
//Console.WriteLine("Hex: {0:X}", buffer[0]);
//bool stop = false;
do do
{ {
//connection.Connect(new IPEndPoint(IPAddress.Any, Form1.PortNumber2));
byte[] buffer;
buffer = connection.Receive(ref RemoteIP); //Receive the packet ID to determine IP
/*
* Or receive all data and send it to the matching ConnectionThread determined by Client ID
*
* Actually the Client ID is not used later on...
* So determine IP every time and send the packet to matching thread
*/
//connection.Connect(RemoteIP);
//connection.Send(buffer, buffer.Length);
//connection.Close();
//if (Program.ConnThreads.ContainsKey(RemoteIP))
//if (Program.ConnThreads.ContainsKey(PEPackets.GetClientID(buffer)))
//break; //Let the ConnectionThread handle the request - By detecting packet type...
bool brvar = false; bool brvar = false;
if (buffer[0] != PEPackets.TOSERVER_ID_OPEN_CONNECTION_REQUEST_1) //Int64 cid;
Console.WriteLine("Received packet: 0x{0:X}", buffer[0]);
//if (buffer[0] != PEPackets.TOSERVER_ID_OPEN_CONNECTION_REQUEST_1 && Program.ConnThreads.ContainsKey(cid = PEPackets.GetClientID(buffer)))
if (buffer[0] != PEPackets.TOSERVER_ID_OPEN_CONNECTION_REQUEST_1 && Program.ConnThreads.ContainsKey(RemoteIP))
{
//Handle packets and send data to matching thread
//(new Task(((ClientConnectionThread)Program.ConnThreads[cid][1]).SendData, buffer)).Start(new LimitedConcurrencyLevelTaskScheduler(1));
//new ListenThread().MemberwiseClone();
while (((ClientConnectionThread)Program.ConnThreads[RemoteIP][1]).DataChanged)
; //Wait until thread finishes
((ClientConnectionThread)Program.ConnThreads[RemoteIP][1]).DataBuffer = buffer;
((ClientConnectionThread)Program.ConnThreads[RemoteIP][1]).DataChanged = true;
break;
}
else if (buffer[0] != PEPackets.TOSERVER_ID_OPEN_CONNECTION_REQUEST_1)
break; break;
for (int i = 0; i < PEPackets.MAGIC.Length; i++) for (int i = 0; i < PEPackets.MAGIC.Length; i++)
{ {
@ -80,28 +76,26 @@ namespace Connection_forwarder
if (brvar) if (brvar)
break; break;
Console.WriteLine("Magic accepted."); Console.WriteLine("Magic accepted.");
//var sendsocket = new Socket(SocketType.Dgram, ProtocolType.Udp); //var sendsocket = new UdpClient(Form1.PortNumber2);
var sendsocket = new UdpClient(Form1.PortNumber2); //sendsocket.EnableBroadcast = true;
//sendsocket.Connect(RemoteIP); connection.Connect(RemoteIP); //Make it only communicate with the currently joining player
sendsocket.EnableBroadcast = true;
//sendsocket.Connect(IPAddress.Broadcast, RemoteIP.Port);
sendsocket.Connect(RemoteIP);
if (buffer[PEPackets.MAGIC.Length + 1] != PEPackets.ProtocolVer) if (buffer[PEPackets.MAGIC.Length + 1] != PEPackets.ProtocolVer)
{ {
new PEPackets(PEPackets.TOCLIENT_ID_INCOMPATIBLE_PROTOCOL_VERSION, new object[] { PEPackets.ProtocolVer, PEPackets.MAGIC, 0x00000000372cdc9e }, sendsocket, RemoteIP); //ID_INCOMPATIBLE_PROTOCOL_VERSION (0x1A) new PEPackets(PEPackets.TOCLIENT_ID_INCOMPATIBLE_PROTOCOL_VERSION, new object[] { PEPackets.ProtocolVer, PEPackets.MAGIC, 0x00000000372cdc9e }, connection, RemoteIP); //ID_INCOMPATIBLE_PROTOCOL_VERSION (0x1A)
break; break;
} }
new PEPackets(PEPackets.TOCLIENT_ID_OPEN_CONNECTION_REPLY_1, new object[] { PEPackets.MAGIC, 0x00000000372cdc9e, 0, 1447 }, sendsocket, RemoteIP); new PEPackets(PEPackets.TOCLIENT_ID_OPEN_CONNECTION_REPLY_1, new object[] { PEPackets.MAGIC, 0x00000000372cdc9e, 0, 1447 }, connection, RemoteIP);
sendsocket.Close(); //sendsocket.Close();
Console.WriteLine("Open connection 1 succeed."); Console.WriteLine("Open connection 1 succeed.");
listenclient = new UdpClient(Form1.PortNumber2); /*
buffer = listenclient.Receive(ref RemoteIP); * We must handle the open connection 2 here in order to obtain ClientID
listenclient.Close(); */
//RemoteIP.Port = 19132;
buffer = connection.Receive(ref RemoteIP);
Console.WriteLine("Received packet: " + buffer[0]);
if (buffer[0] != PEPackets.TOSERVER_ID_OPEN_CONNECTION_REQUEST_2) if (buffer[0] != PEPackets.TOSERVER_ID_OPEN_CONNECTION_REQUEST_2)
break; break;
brvar = false;
for (int i = 0; i < PEPackets.MAGIC.Length; i++) for (int i = 0; i < PEPackets.MAGIC.Length; i++)
{ {
if (buffer[i + 1] != PEPackets.MAGIC[i]) if (buffer[i + 1] != PEPackets.MAGIC[i])
@ -114,8 +108,8 @@ namespace Connection_forwarder
if (brvar) if (brvar)
break; break;
byte[] bufferPart = new byte[5]; byte[] bufferPart = new byte[5];
Console.WriteLine("Extracting data...");
Array.Copy(buffer, PEPackets.MAGIC.Length + 1, bufferPart, 0, 5); Array.Copy(buffer, PEPackets.MAGIC.Length + 1, bufferPart, 0, 5);
//if (bufferPart != BitConverter.GetBytes(0x043f57fefd))
for (int i = 0; i < bufferPart.Length; i++) for (int i = 0; i < bufferPart.Length; i++)
{ {
if (bufferPart[i] != 0x00) if (bufferPart[i] != 0x00)
@ -127,61 +121,72 @@ namespace Connection_forwarder
if (brvar) if (brvar)
break; break;
Array.Copy(buffer, PEPackets.MAGIC.Length + 6, bufferPart = new byte[2], 0, 2); Array.Copy(buffer, PEPackets.MAGIC.Length + 6, bufferPart = new byte[2], 0, 2);
//if (bufferPart != BitConverter.GetBytes((short)19132))
//var bytes = BitConverter.GetBytes((short)19132);
/*for (int i = 0; i < 2; i++)
{
if (bufferPart[i] != bytes[i])
{
brvar = true;
break;
}
}
if (brvar)
break;*/
short ServerPortFromClient = BitConverter.ToInt16(bufferPart, 0); short ServerPortFromClient = BitConverter.ToInt16(bufferPart, 0);
Console.WriteLine("ServerPortFromClient: " + ServerPortFromClient); Console.WriteLine("ServerPortFromClient: " + ServerPortFromClient);
Array.Copy(buffer, PEPackets.MAGIC.Length + 8, bufferPart, 0, 2); Array.Copy(buffer, PEPackets.MAGIC.Length + 8, bufferPart, 0, 2);
//if (bufferPart != BitConverter.GetBytes((short)1464))
short MTUSize = BitConverter.ToInt16(bufferPart, 0); short MTUSize = BitConverter.ToInt16(bufferPart, 0);
Console.WriteLine("MTUSize: " + MTUSize); Console.WriteLine("MTUSize: " + MTUSize);
Array.Copy(buffer, PEPackets.MAGIC.Length + 10, bufferPart = new byte[8], 0, 8); Array.Copy(buffer, PEPackets.MAGIC.Length + 10, bufferPart = new byte[8], 0, 8);
//long ClientID = Convert.ToInt64(bufferPart);
long ClientID = BitConverter.ToInt64(bufferPart, 0); long ClientID = BitConverter.ToInt64(bufferPart, 0);
Console.WriteLine("ClientID: " + ClientID); Console.WriteLine("ClientID: " + ClientID);
sendsocket = new UdpClient(Form1.PortNumber2); //sendsocket = new UdpClient(Form1.PortNumber2);
//sendsocket.EnableBroadcast = true; //sendsocket.Connect(RemoteIP);
sendsocket.Connect(RemoteIP); new PEPackets(PEPackets.TOCLIENT_ID_OPEN_CONNECTION_REPLY_2, new object[] { PEPackets.MAGIC, 0x00000000372cdc9e, Form1.PortNumber2, 1464, 0 }, connection, RemoteIP);
new PEPackets(PEPackets.TOCLIENT_ID_OPEN_CONNECTION_REPLY_2, new object[] { PEPackets.MAGIC, 0x00000000372cdc9e, Form1.PortNumber2, 1464, 0 }, sendsocket, RemoteIP); //sendsocket.Close();
sendsocket.Close();
listenclient = new UdpClient(Form1.PortNumber2); //listenclient = new UdpClient(Form1.PortNumber2);
buffer = listenclient.Receive(ref RemoteIP); //buffer = listenclient.Receive(ref RemoteIP);
Console.WriteLine("Received: 0x{0:X}", buffer[0]); buffer = connection.Receive(ref RemoteIP);
Console.WriteLine("Second byte: 0x{0:X}", buffer[1]); //Console.WriteLine("Received: 0x{0:X}", buffer[0]);
listenclient.Close(); //Console.WriteLine("Second byte: 0x{0:X}", buffer[1]);
//listenclient.Close();
sendsocket = new UdpClient(Form1.PortNumber2); while (buffer[0] != 0x84)
//sendsocket.EnableBroadcast = true; { //Send NACK back
sendsocket.Connect(RemoteIP); Console.WriteLine("Received packet: 0x{0:X}", buffer[0]);
new PEPackets(PEPackets.TOCLIENT_LoginStatusPacket, new object[] { 0 }, sendsocket, RemoteIP); Console.WriteLine("Incorrect packet. Resending...");
sendsocket.Close(); var tmpreta = PEPackets.DecodeDataPacket(buffer);
//----------------------------- if (tmpreta == null)
/*object[] connsocket = new object[2]; break;
//if(buffer[0]==PEPackets) byte[] Counta = PEPackets.IntTo3Byte((int)tmpreta[1]);
connsocket[0] = listenclient; //new PEPackets(0xA0, new object[] { (short)1, true, 0x84 }, connection, RemoteIP);
connsocket[1] = new Socket(SocketType.Stream, ProtocolType.Tcp); //We still need to connect to the server new PEPackets(0xA0, new object[] { (short)1, true, Counta }, connection, RemoteIP);
((Socket)connsocket[1]).Connect("localhost", Form1.PortNumber); buffer = connection.Receive(ref RemoteIP);
new Thread(new ParameterizedThreadStart(new ClientConnectionThread().Run)).Start(connsocket);
new Thread(new ParameterizedThreadStart(new ServerConnectionThread().Run)).Start(connsocket);*/
} while (false);
} }
//for (int i = 0; i < 2; i++) /*
//new Thread(new ParameterizedThreadStart(new ClientConnectionThread().Run)).Start(connsocket); * Custom packet 0x84:
//var serversock = new Socket(SocketType.Stream, ProtocolType.Tcp); * Count field (3 bytes) and then data payload:
//serversock.Connect("localhost", Form1.PortNumber); * Encapsulation ID (1 byte) (=0x00) and then length then data packet:
//new Thread(new ParameterizedThreadStart(new ServerConnectionThread().Run)).Start(serversock); * Status (1 byte/8 bits)
//new Thread(new ParameterizedThreadStart(new ServerConnectionThread().Run)).Start(connsocket); */
//var tmp = BitConverter.GetBytes(0x84).ToList<byte>();
//Console.WriteLine("Remove 1st byte from: " + tmp[0] + ", " + tmp[1] + ", " + tmp[2] + ", " + tmp[3]);
//tmp.RemoveAt(3); //We only need 3 bytes
//new PEPackets(0xC0, new object[] { (short)1, true, tmp.ToArray() }, connection, RemoteIP); //Send ACK
var lasttick = Environment.TickCount;
do
{
var tmpret = PEPackets.DecodeDataPacket(buffer);
if (tmpret == null)
break;
Console.WriteLine("Count: " + (int)tmpret[1]);
Console.WriteLine("EncapsulationID: " + tmpret[0]);
Console.WriteLine("Count2: " + tmpret[3]);
byte[] Count = PEPackets.IntTo3Byte((int)tmpret[1]);
Console.WriteLine("Count 1st byte: " + Count[0]);
new PEPackets(0xC0, new object[] { (short)1, true, Count }, connection, RemoteIP);
} while ((buffer = connection.Receive(ref RemoteIP))[0] == 0x84 && Environment.TickCount - lasttick < 6000 * 1000);
//-----------------------------
var threadvar = new Object[3];
connection.Close();
connection = new UdpClient(Form1.PortNumber2);
threadvar[0] = RemoteIP;
threadvar[1] = connection;
threadvar[2] = ClientID;
new Thread(new ParameterizedThreadStart(new ClientConnectionThread().Run)).Start(threadvar);
//new Thread(new ParameterizedThreadStart(new ServerConnectionThread().Run)).Start(threadvar);
//new ActionThread().Do(new ClientConnectionThread().Run(threadvar));
} while (false);
} }
} }
} }

View file

@ -9,8 +9,6 @@ namespace Connection_forwarder
{ {
class PCPackets class PCPackets
{ {
//public static List<PCPacket> Packets = new List<PCPacket>();
//public static readonly Dictionary<string, byte> PacketNames = new Dictionary<string, byte>();
/* http://wiki.vg/Pre-release_protocol */ /* http://wiki.vg/Pre-release_protocol */
/// <summary> /// <summary>
/// <para>Gamemode Unsigned Byte 0: survival, 1: creative, 2: adventure. Bit 3 (0x8) is the hardcore flag</para> /// <para>Gamemode Unsigned Byte 0: survival, 1: creative, 2: adventure. Bit 3 (0x8) is the hardcore flag</para>
@ -139,13 +137,6 @@ namespace Connection_forwarder
public PCPackets(byte packetid, object[] data, Socket sock) public PCPackets(byte packetid, object[] data, Socket sock)
{ {
//Prepare all possible packets
//Packets.Add(new PCPacket(0x02, "Chat"));
//Packets[0].Function = Packets[0].Callback;
/*Packets[0].Function = delegate(object[] asd)
{
return;
};*/
var tmp=new List<byte>(); var tmp=new List<byte>();
tmp.Add(packetid); tmp.Add(packetid);
for(int i=0; i<data.Length; i++) for(int i=0; i<data.Length; i++)
@ -174,24 +165,4 @@ namespace Connection_forwarder
sock.Send(tmp.ToArray()); sock.Send(tmp.ToArray());
} }
} }
/*class PCPacket
{
public byte PacketID { get; private set; }
public object[] Contents { get; set; }
public string PacketName { get; private set; }
internal delegate void FunctionT(params object[] asd);
public FunctionT Function;
//public PCPacket(byte packetid, params object[] contents)
public PCPacket(byte packetid, string packetname)
{
PacketID = packetid;
PacketName = packetname;
}
internal bool Callback(Type[] types, object[] parameters)
{
return true;
}
}*/
} }

View file

@ -11,9 +11,6 @@ namespace Connection_forwarder
{ {
class PEPackets class PEPackets
{ {
//public static byte[] MAGIC = BitConverter.GetBytes(0x00ffff00fefefefefdfdfdfd12345678);
//public static readonly MAGIC MAGIC = (char)Convert.ChangeType("0x00ffff00fefefefefdfdfdfd12345678", typeof(char));
//public static readonly MAGIC MAGIC = new byte[16] { 0x00, 0xff, 0xff, 0x00, 0xfe, 0xfe, 0xfe, 0xfe, 0xfd, 0xfd, 0xfd, 0xfd, 0x12, 0x34, 0x56, 0x78 };
public static readonly byte[] MAGIC = new byte[16] { 0, 255, 255, 0, 254, 254, 254, 254, 253, 253, 253, 253, 18, 52, 86, 120 }; public static readonly byte[] MAGIC = new byte[16] { 0, 255, 255, 0, 254, 254, 254, 254, 253, 253, 253, 253, 18, 52, 86, 120 };
public const byte ProtocolVer = 5; public const byte ProtocolVer = 5;
/// <summary> /// <summary>
@ -50,7 +47,9 @@ namespace Connection_forwarder
{ {
if (!data[i].GetType().IsSubclassOf(typeof(Array))) if (!data[i].GetType().IsSubclassOf(typeof(Array)))
{ {
if (data[i].GetType() == typeof(bool)) if (data[i].GetType() == typeof(byte))
tmp.Add((byte)data[i]);
else if (data[i].GetType() == typeof(bool))
tmp.AddRange(BitConverter.GetBytes((bool)data[i])); tmp.AddRange(BitConverter.GetBytes((bool)data[i]));
else if (data[i].GetType() == typeof(char)) else if (data[i].GetType() == typeof(char))
tmp.AddRange(BitConverter.GetBytes((char)data[i])); tmp.AddRange(BitConverter.GetBytes((char)data[i]));
@ -70,14 +69,14 @@ namespace Connection_forwarder
tmp.AddRange(BitConverter.GetBytes((ushort)data[i])); tmp.AddRange(BitConverter.GetBytes((ushort)data[i]));
else if (data[i].GetType() == typeof(ushort)) else if (data[i].GetType() == typeof(ushort))
tmp.AddRange(BitConverter.GetBytes((ushort)data[i])); tmp.AddRange(BitConverter.GetBytes((ushort)data[i]));
else if (data[i].GetType() == typeof(byte))
tmp.Add((byte)data[i]);
} }
else else
{ {
for (int j = 0; j < ((Array)data[i]).Length; j++) for (int j = 0; j < ((Array)data[i]).Length; j++)
{ {
if (((Array)data[i]).GetValue(j).GetType() == typeof(bool)) if (((Array)data[i]).GetValue(j).GetType() == typeof(byte))
tmp.Add((byte)((Array)data[i]).GetValue(j));
else if (((Array)data[i]).GetValue(j).GetType() == typeof(bool))
tmp.AddRange(BitConverter.GetBytes((bool)((Array)data[i]).GetValue(j))); tmp.AddRange(BitConverter.GetBytes((bool)((Array)data[i]).GetValue(j)));
else if (((Array)data[i]).GetValue(j).GetType() == typeof(char)) else if (((Array)data[i]).GetValue(j).GetType() == typeof(char))
tmp.AddRange(BitConverter.GetBytes((char)((Array)data[i]).GetValue(j))); tmp.AddRange(BitConverter.GetBytes((char)((Array)data[i]).GetValue(j)));
@ -97,23 +96,73 @@ namespace Connection_forwarder
tmp.AddRange(BitConverter.GetBytes((ushort)((Array)data[i]).GetValue(j))); tmp.AddRange(BitConverter.GetBytes((ushort)((Array)data[i]).GetValue(j)));
else if (((Array)data[i]).GetValue(j).GetType() == typeof(ushort)) else if (((Array)data[i]).GetValue(j).GetType() == typeof(ushort))
tmp.AddRange(BitConverter.GetBytes((ushort)((Array)data[i]).GetValue(j))); tmp.AddRange(BitConverter.GetBytes((ushort)((Array)data[i]).GetValue(j)));
else if (((Array)data[i]).GetValue(j).GetType() == typeof(byte))
tmp.Add((byte)((Array)data[i]).GetValue(j));
else Console.WriteLine("Unknown type in array: " + ((Array)data[i]).GetValue(j).GetType()); else Console.WriteLine("Unknown type in array: " + ((Array)data[i]).GetValue(j).GetType());
} }
} }
} }
//sock.Send(tmp.ToArray()); try
//sock.SendTo(tmp.ToArray(), ep); {
//sock.Send(tmp.ToArray(), tmp.Count, ep); sock.Send(tmp.ToArray(), tmp.Count, ep);
}
catch
{
sock.Send(tmp.ToArray(), tmp.Count); sock.Send(tmp.ToArray(), tmp.Count);
} }
} }
/*class MAGIC
internal static Int64 GetClientID(byte[] buffer)
{ //ClientID is in open connection request 2 and in data packets
switch(buffer[0])
{ {
public static implicit operator MAGIC(byte[] c) case 0x84:
{ byte[] tmp = new byte[8];
return (MAGIC)c; Array.Copy(buffer, 17, tmp, 0, 8);
return BitConverter.ToInt64(tmp, 0);
default:
Console.WriteLine("Unable to get ClientID: " + buffer);
return 0;
}
}
public static object[] DecodeDataPacket(byte[] data)
{
byte[] tmp;
//Array.Copy(data, 1, tmp = new byte[4], 1, 3); //Leave the first byte 0 so it will (hopefully) give the correct 4-bit representation
Array.Copy(data, 1, tmp = new byte[4], 0, 3); //Nope, it starts at the first byte
int Count = BitConverter.ToInt32(tmp, 0);
byte EncapsulationID = data[4];
short length;
int Count2;
int Unknown;
switch(EncapsulationID)
{
case 0x00:
length = BitConverter.ToInt16(data, 5);
Array.Copy(data, 7, tmp=new byte[data.Length-8], 0, data.Length-8); //7. byte: data +1
return new object[] { EncapsulationID, Count, length, tmp };
case 0x40:
length = BitConverter.ToInt16(data, 5);
Array.Copy(data, 7, tmp = new byte[4], 0, 3); //7. byte: Count(?)
Count2 = BitConverter.ToInt32(tmp, 0);
Array.Copy(data, 10, tmp=new byte[data.Length-11], 0, data.Length - 11); //10. byte: data + Count +1
return new object[] { EncapsulationID, Count, length, Count2, tmp };
case 0x60:
length = BitConverter.ToInt16(data, 5);
Array.Copy(data, 7, tmp = new byte[4], 0, 3); //7. byte: Count(?)
Count2 = BitConverter.ToInt32(tmp, 0);
Unknown = BitConverter.ToInt32(data, 10); //10. byte: Unknown
Array.Copy(data, 14, tmp=new byte[data.Length-15], 0, data.Length - 15); //14. byte: data +1
return new object[] { EncapsulationID, Count, length, Count2, tmp };
default:
return null;
}
}
public static byte[] IntTo3Byte(int i)
{
byte[] tmp = BitConverter.GetBytes(i);
byte[] ret = new byte[3];
//Array.Copy(tmp, 1, ret, 0, 3);
Array.Copy(tmp, 0, ret, 0, 3);
return ret;
}
} }
}*/
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -29,11 +30,15 @@ namespace Connection_forwarder
{ {
if (ip.AddressFamily == AddressFamily.InterNetwork) if (ip.AddressFamily == AddressFamily.InterNetwork)
{ {
//localIP = ip.ToString();
localIP = ip; localIP = ip;
} }
} }
return localIP; return localIP;
} }
/// <summary>
//// Int64: ClientID
/// IP
/// </summary>
public static Dictionary<IPEndPoint, Object[]> ConnThreads = new Dictionary<IPEndPoint, Object[]>();
} }
} }