Skip to content

Instantly share code, notes, and snippets.

@foglio1024
Last active May 14, 2019 15:27
Show Gist options
  • Save foglio1024/95cd913ecbf6e85881819464ce3061ce to your computer and use it in GitHub Desktop.
Save foglio1024/95cd913ecbf6e85881819464ce3061ce to your computer and use it in GitHub Desktop.
ToolboxInterface
const net = require('net');
const address = '127.0.0.50';
const port = 9550;
class TcpInterface
{
constructor(mod)
{
this.mod = mod;
this.interface = new net.Socket();
this.interface.connect(port, address);
this.interface.on('error', this.handleError);
this.interface.on('connect', this.handleConnection);
this.interface.on('data', this.handleData);
this.installHooks();
}
installHooks()
{
//TODO: read list from file and install them
opcodes.forEach(opcode =>
{
this.mod.hook(opcode, 'raw', this.send);
});
}
send()
{
this.interface.write(build(data));
}
build(payload)
{
/*
* MessageType [1B]
* Length [2B]
* Data [-B]
*
* | MessageType | Length | Data |
*/
// not sure if this is how you actually build buffers, will look at it later
var data = Buffer.from(payload);
const typeBuf = Buffer.alloc(1);
typeBuf.writeInt8(1);
const lenBuf = Buffer.alloc(2);
lenBuf.writeUInt16LE(data.length);
const buf = Buffer.concat([typeBuf, lenBuf, data]);
console.log("Sending " + buf);
return buf;
}
}
module.exports = TcpInterface;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ToolboxInterface
{
/*
* MessageType [1B]
* Length [2B]
* Data [-B]
*
* | MessageType | Length | Data |
*/
public enum MessageType
{
TeraPacket = 1,
JsonMessage = 2, //example
}
public class ToolboxInterface
{
private readonly TcpListener _server;
public event Action<MessageType, byte[]> MessageReceived;
public ToolboxInterface(string address, int port)
{
_server = new TcpListener(IPAddress.Parse(address), port);
_server.Start();
new Thread(Listen).Start();
}
private void Listen()
{
while (true)
{
var client = _server.AcceptTcpClient();
var stream = client.GetStream();
while (client.Connected)
{
try
{
var typeBuf = new byte[1];
stream.Read(typeBuf, 0, 1);
// need better way to do this
var type = MessageType.TeraPacket;
try { type = (MessageType)typeBuf[0]; }
catch (Exception)
{
Console.WriteLine("Invalid packet!");
continue;
}
var lenBuf = new byte[2];
stream.Read(lenBuf, 0, 2);
var length = BitConverter.ToUInt16(lenBuf, 0);
//Console.WriteLine($"Message len: {length}");
var dataBuf = new byte[length];
stream.Read(dataBuf, 0, length);
MessageReceived?.Invoke(type, dataBuf);
}
catch (Exception)
{
// todo
}
}
}
}
}
public static class Program
{
static void Main(string[] args)
{
var toolboxInterface = new ToolboxInterface("127.0.0.50", 9550);
toolboxInterface.MessageReceived += OnMessageReceived;
}
private static void OnMessageReceived(MessageType type, byte[] data)
{
// enqueue like sniffed packets (not sure if fromServer and timestamp are needed)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment