Created
March 5, 2021 00:59
-
-
Save nathan130200/9f5b5cd168312f281f84de53ded4b4ff to your computer and use it in GitHub Desktop.
cryproxy protect protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
namespace Cry.Proxy | |
{ | |
public enum MessageType | |
{ | |
Plain = 0, | |
Encrypted = 1, | |
ClientKey = 2, | |
ServerKey = 3, | |
ClientAck = 4 | |
} | |
public static class Utilities | |
{ | |
public static byte[] GetBytes(this string s) | |
=> Encoding.UTF8.GetBytes(s); | |
static readonly byte[] Magic = BitConverter.GetBytes(0xFEEDDEAD); | |
public static void WriteProtect(this Stream stream, MessageType type, byte[] buffer) | |
{ | |
var result = new byte[12 + buffer.Length]; | |
Array.Copy(Magic, 0, result, 0, Magic.Length); | |
var size = BitConverter.GetBytes(buffer.Length); | |
Array.Copy(size, 0, result, 4, size.Length); | |
var tp = BitConverter.GetBytes((uint)type); | |
Array.Copy(tp, 0, result, 8, tp.Length); | |
if (buffer != null) | |
Array.Copy(buffer, 0, result, 12, buffer.Length); | |
stream.Write(result, 0, result.Length); | |
if (type == MessageType.Plain) | |
Console.WriteLine("send >>\n{0}\n", Encoding.UTF8.GetString(buffer)); | |
} | |
public static bool ReadProtect(this Stream stream, out uint size, out MessageType type, out byte[] raw) | |
{ | |
var buffer = new byte[12]; | |
size = default; | |
type = default; | |
raw = default; | |
if (stream.Read(buffer, 0, buffer.Length) != buffer.Length) | |
return false; | |
byte[] p1 = new byte[sizeof(uint)], | |
p2 = new byte[sizeof(uint)], | |
p3 = new byte[sizeof(uint)]; | |
try | |
{ | |
Array.Copy(buffer, 0, p1, 0, p1.Length); | |
Array.Copy(buffer, 4, p2, 0, p2.Length); | |
Array.Copy(buffer, 8, p3, 0, p3.Length); | |
uint magic = BitConverter.ToUInt32(p1, 0); | |
if (magic != 0xFEEDDEAD) | |
return false; | |
size = BitConverter.ToUInt32(p2, 0); | |
type = (MessageType)BitConverter.ToUInt32(p3, 0); | |
raw = new byte[size]; | |
if (stream.Read(raw, 0, raw.Length) != raw.Length) | |
return false; | |
return true; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment