Skip to content

Commit 36d1563

Browse files
Add certificate validation
1 parent cae2ba4 commit 36d1563

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
obj/Release
33
obj
44
.vs
5+
Properties/PublishProfiles/FolderProfile.pubxml.user

FetchFolderCommand.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public override void Execute()
3232
if (EnablePassive)
3333
ftpClient.DataConnectionType = FtpDataConnectionType.AutoPassive;
3434

35+
ftpClient.ValidateCertificate += (control, args) =>
36+
{
37+
Console.BackgroundColor = ConsoleColor.DarkGreen;
38+
Console.WriteLine("Certificate to validate: " + args?.Certificate?.GetCertHashString());
39+
Console.ResetColor();
40+
41+
if (!string.IsNullOrEmpty(Certificate))
42+
{
43+
var isValid = args.Certificate.GetCertHashString()?.ToUpper() == Certificate.Replace(":", "").ToUpper();
44+
if (!isValid)
45+
throw new InvalidOperationException();
46+
}
47+
};
48+
3549
ftpClient.Connect();
3650

3751
var listing = ftpClient.GetNameListing(Path);
@@ -55,5 +69,6 @@ public override void Execute()
5569
}
5670

5771
}
72+
5873
}
5974
}

FileUploadCommand.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public override void Execute()
3939
if (EnablePassive)
4040
ftpClient.DataConnectionType = FtpDataConnectionType.AutoPassive;
4141

42+
43+
ftpClient.ValidateCertificate += (_, args) =>
44+
{
45+
Console.BackgroundColor = ConsoleColor.DarkGreen;
46+
Console.WriteLine("Certificate to validate: " + args.Certificate.GetCertHashString());
47+
Console.ResetColor();
48+
49+
if (!string.IsNullOrEmpty(Certificate))
50+
{
51+
var isValid = args.Certificate.GetCertHashString()?.ToUpper() == Certificate.Replace(":", "").ToUpper();
52+
if (!isValid)
53+
throw new InvalidOperationException();
54+
}
55+
};
56+
4257
ftpClient.Connect();
4358

4459
var result = ftpClient.UploadFile(LocalFile, Path);

Ftp.Client.csproj

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
6-
</PropertyGroup>
7-
8-
<ItemGroup>
9-
<PackageReference Include="CommandLineParser" Version="2.8.0" />
10-
<PackageReference Include="FluentFTP" Version="35.2.2" />
11-
</ItemGroup>
12-
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<AssemblyVersion>1.0.2</AssemblyVersion>
6+
<FileVersion>1.0.2</FileVersion>
7+
<version>1.0.2</version>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
11+
<PackageReference Include="FluentFTP" Version="35.2.2" />
12+
</ItemGroup>
1313
</Project>

FtpCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public string ExternalIp
5050
}
5151

5252

53+
[Option('r', "certificate", Required = false, HelpText = "Certificate string. If this parameter has been set then the validation will occur.")]
54+
public string Certificate
55+
{
56+
get;
57+
set;
58+
}
5359

5460
public virtual void Execute()
5561
{

Properties/PublishProfiles/FolderProfile.pubxml.user

Lines changed: 0 additions & 9 deletions
This file was deleted.

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Currently there is two ftp commands available in the tool.
2626

2727
-e, --external-ip Set this parameter to your NAT outgoing IP if you are operating within a NAT
2828

29+
-r, --certificate Certificate string. If this parameter has been set then the validation will occur.
30+
2931
--help Display this help screen.
3032

3133
--version Display version information.
@@ -44,8 +46,9 @@ Currently there is two ftp commands available in the tool.
4446

4547
-v, --passive Set true to enable passive mode
4648

47-
-e, --external-ip Set this parameter to your NAT outgoing IP if you are
48-
operating within a NAT
49+
-e, --external-ip Set this parameter to your NAT outgoing IP if you are operating within a NAT
50+
51+
-r, --certificate Certificate string. If this parameter has been set then the validation will occur.
4952

5053
--help Display this help screen.
5154

@@ -60,7 +63,7 @@ In order to call simple ftp commands within this application (using dotnet) to t
6063

6164
## Download
6265

63-
You can download the latest version [here](https://github.com/faridprogrammer/ftp.client/releases/tag/V1.0).
66+
You can download the latest version [here](https://github.com/faridprogrammer/ftp.client/releases/tag/V1.0.1).
6467

6568

6669

0 commit comments

Comments
 (0)