Skip to content

Commit

Permalink
Fix the bug when exe path contains space will result error (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
firejox authored Apr 21, 2024
1 parent 0580ae4 commit b7e32fa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Tests/ProcPiperInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public void InvalidInputParseTest(string input)

[TestCase(@"EXEC:C:\Foo.exe bar", ExpectedResult = @"C:\Foo.exe")]
[TestCase(@"EXEC:C:\Foo.exe", ExpectedResult = @"C:\Foo.exe")]
[TestCase(@"EXEC:""C:\foo\space dir\bar.exe"" arg1 arg2", ExpectedResult = @"C:\foo\space dir\bar.exe")]
public string FileNamePatternMatchTest(string input)
{
var element = AddressElement.TryParse(input);
return Firejox.App.WinSocat.ProcPiperInfo.TryParse(element).FileName;
}

[TestCase(@"EXEC:C:\Foo.exe bar1 bar2", ExpectedResult = "bar1 bar2")]
[TestCase(@"EXEC:""C:\foo\space dir\bar.exe"" arg1 arg2", ExpectedResult = @"arg1 arg2")]
public string ArgumentPatternMatchTest(string input)
{
var element = AddressElement.TryParse(input);
Expand Down
2 changes: 1 addition & 1 deletion winsocat/AddressElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static AddressElement TryParse(string input)
return null!;

string tag = tagSplits[0];
string address = tagSplits[1].Substring(0, addressSepOffset);
string address = tagSplits[1].Substring(0, addressSepOffset).Trim();
var options = GetOptions(tagSplits[1].Substring(addressSepOffset));

return new AddressElement(tag, address, options);
Expand Down
22 changes: 6 additions & 16 deletions winsocat/Process.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.IO.Pipelines;
using System.CommandLine.Parsing;

namespace Firejox.App.WinSocat;

Expand All @@ -20,25 +21,14 @@ public ProcPiperInfo(string filename, string arguments)
public static ProcPiperInfo TryParse(AddressElement element)
{
if (!element.Tag.Equals("EXEC", StringComparison.OrdinalIgnoreCase)) return null!;

string execPattern = element.Address.Trim('\'', '\"');
int sepIndex = execPattern.IndexOf(' ');
string filename;
string arguments;

if (sepIndex != -1)
{
filename = execPattern.Substring(0, sepIndex);
arguments = execPattern.Substring(sepIndex + 1);
}
else
{
filename = execPattern;
arguments = "";
}
var execPattern = element.Address;
var cmdLine = CommandLineStringSplitter.Instance.Split(execPattern);
string filename = cmdLine.First();
string arguments = String.Join(' ', cmdLine.Skip(1));

return new ProcPiperInfo(filename, arguments);
}

}

public class ProcPiper : IPiper
Expand Down

0 comments on commit b7e32fa

Please sign in to comment.