Skip to content

Commit

Permalink
Merge pull request #303 from Sehkah/feature/add-include-filter-to-pac…
Browse files Browse the repository at this point in the history
…ket-cli

Add support for packet ID / name filtering to the packet command.
  • Loading branch information
Sehkah authored May 10, 2024
2 parents 7b42aea + db52e12 commit 9b80115
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Arrowgene.Ddon.Cli/Command/PacketCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public class PacketCommand : ICommand
private static string ByteDumpPrefixSwitch => "--byte-dump-prefix";
private static string Utf8StringDumpSwitch => "--utf8-dump";
private static string StructureDumpSwitch => "--structure-dump";
private static string PacketIncludeFilterSwitch => "--packet-include-filter";

public string Description =>
$"Usage: `{Key} \"E:\\dumps\\58_9.yaml` [{DecryptionKeySwitch}=]J2g4pE2_heyqIAengWy0N6D1SEklxz8I [{ByteDumpSwitch}[=,]] [{ByteDumpPrefixSwitch}=0x] [{Utf8StringDumpSwitch}] [{StructureDumpSwitch}[=JSON|YAML]]";
$"Usage: `{Key} \"E:\\dumps\\58_9.yaml` [{DecryptionKeySwitch}=]J2g4pE2_heyqIAengWy0N6D1SEklxz8I [{ByteDumpSwitch}[=,]] [{ByteDumpPrefixSwitch}=0x] [{Utf8StringDumpSwitch}] [{StructureDumpSwitch}[=JSON|YAML]] [{PacketIncludeFilterSwitch}=11.21.2,S2C_QUEST_QUEST_PROGRESS_RES,...]";


public CommandResultType Run(CommandParameter parameter)
Expand All @@ -41,10 +42,11 @@ public CommandResultType Run(CommandParameter parameter)
bool addUtf8StringDump = parameter.Switches.Contains(Utf8StringDumpSwitch) || parameter.SwitchMap.ContainsKey(Utf8StringDumpSwitch);
bool addStructureDump = parameter.Switches.Contains(StructureDumpSwitch) || parameter.SwitchMap.ContainsKey(StructureDumpSwitch);
string structureDumpFormat = parameter.SwitchMap.GetValueOrDefault(StructureDumpSwitch, "JSON").ToLowerInvariant();
string packetIncludeFilter = parameter.SwitchMap.GetValueOrDefault(PacketIncludeFilterSwitch, "");

List<PcapPacket> decryptedPcapPackets = DecryptPackets(yamlPath, camelliaKeyBytes);
string annotatedDump = GetAnnotatedPacketDump(decryptedPcapPackets, addByteDump, addUtf8StringDump, addStructureDump, byteDumpSeparator, byteDumpPrefix,
structureDumpFormat);
structureDumpFormat, packetIncludeFilter);
string outputPath = yamlPath + ".annotated.txt";
File.WriteAllText(outputPath, annotatedDump);

Expand Down Expand Up @@ -108,14 +110,20 @@ public List<PcapPacket> DecryptPackets(List<PcapPacket> pcapPackets, byte[] came
}

public string GetAnnotatedPacketDump(List<PcapPacket> decryptedPcapPackets, bool addByteDump, bool addUtf8ByteDump, bool addStructureDump, string byteDumpSeparator = "",
string byteDumpPrefix = "", string structureDumpFormat = "")
string byteDumpPrefix = "", string structureDumpFormat = "", string packetIncludeFilter = "")
{
HashSet<string> packetIncludeFilterSet = new HashSet<string>(packetIncludeFilter.ToLowerInvariant().Split(','));
StringBuilder annotated = new StringBuilder();
{
foreach (PcapPacket pcapPacket in decryptedPcapPackets)
{
foreach (IPacket resolvedPacket in pcapPacket.ResolvedPackets)
{
if (packetIncludeFilter != "" && !packetIncludeFilterSet.Contains(resolvedPacket.Id.Name.ToLowerInvariant()) && !packetIncludeFilterSet.Contains(resolvedPacket.Id.ToString()))
{
continue;
}

annotated.AppendLine($"{resolvedPacket.PrintHeader()} Pcap(No:{pcapPacket.Packet} Ts:{pcapPacket.TimeStamp})");
annotated.Append(resolvedPacket.PrintData());
if (addStructureDump && resolvedPacket is IStructurePacket)
Expand Down

0 comments on commit 9b80115

Please sign in to comment.