Skip to content

Commit 7cfc524

Browse files
Add connect, disconnect, exit commands
1 parent f0201dc commit 7cfc524

10 files changed

+246
-159
lines changed

Commands/ConnectCommand.cs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine;
4+
using FluentFTP;
5+
6+
namespace Ftp.Client.Commands
7+
{
8+
[Verb("connect", HelpText = "Connect to ftp server")]
9+
public class ConnectCommand: ICommand
10+
{
11+
[Option('h', "Host", Required = true)]
12+
public string Host
13+
{
14+
get;
15+
set;
16+
}
17+
18+
[Option('u', "username", Required = true)]
19+
public string UserName
20+
{
21+
get;
22+
set;
23+
}
24+
25+
[Option('s', "password", Required = false)]
26+
public string Password
27+
{
28+
get;
29+
set;
30+
}
31+
32+
[Option('l', "ssl", Required = false, HelpText = "Enable ssl support")]
33+
public bool EnableSsl
34+
{
35+
get;
36+
set;
37+
}
38+
39+
[Option('v', "passive", Required = false, HelpText = "Set true to enable passive mode")]
40+
public bool EnablePassive
41+
{
42+
get;
43+
set;
44+
}
45+
46+
[Option('e', "external-ip", Required = false, HelpText = "Set this parameter to your NAT outgoing IP if you are operating within a NAT")]
47+
public string ExternalIp
48+
{
49+
get;
50+
set;
51+
}
52+
53+
54+
[Option('r', "certificate", Required = false, HelpText = "Certificate string. If this parameter has been set then the validation will occur.")]
55+
public string Certificate
56+
{
57+
get;
58+
set;
59+
}
60+
61+
[Option('d', "debug", Required = false, HelpText = "Enables detailed logging.")]
62+
public bool DebugMode
63+
{
64+
get;
65+
set;
66+
}
67+
68+
[Option('a', "active_ports", Required = false, HelpText = "Comma separated active ports")]
69+
public string ActivePorts
70+
{
71+
get;
72+
set;
73+
}
74+
75+
public void Execute()
76+
{
77+
try
78+
{
79+
var ftpClient = new FtpClient(Host, UserName, Password);
80+
ConfigureFtpClient(ftpClient);
81+
ftpClient.Connect();
82+
83+
Program.Client = ftpClient;
84+
Console.WriteLine("Successfully connected to ftp server...");
85+
86+
}
87+
catch (Exception e)
88+
{
89+
Console.WriteLine(e);
90+
}
91+
}
92+
93+
protected virtual void ConfigureFtpClient(FtpClient ftpClient)
94+
{
95+
if (DebugMode)
96+
ftpClient.OnLogEvent += (level, s) => Console.WriteLine($"{level}: {s}");
97+
98+
if (EnableSsl)
99+
{
100+
ftpClient.EncryptionMode = FtpEncryptionMode.Explicit;
101+
ftpClient.DataConnectionType = FtpDataConnectionType.PORT;
102+
}
103+
104+
if (!string.IsNullOrEmpty(ExternalIp))
105+
ftpClient.AddressResolver = () => ExternalIp;
106+
107+
if (EnablePassive)
108+
ftpClient.DataConnectionType = FtpDataConnectionType.AutoPassive;
109+
110+
if (!string.IsNullOrEmpty(ActivePorts))
111+
{
112+
ftpClient.ActivePorts = ActivePorts.Split(",").Select(s => int.Parse(s.Trim())).ToArray();
113+
}
114+
115+
ftpClient.ValidateCertificate += (control, args) =>
116+
{
117+
Console.BackgroundColor = ConsoleColor.DarkGreen;
118+
Console.WriteLine("Certificate to validate: " + args?.Certificate?.GetCertHashString());
119+
Console.ResetColor();
120+
121+
if (!string.IsNullOrEmpty(Certificate))
122+
{
123+
var isValid = args.Certificate.GetCertHashString()?.ToUpper() == Certificate.Replace(":", "").ToUpper();
124+
if (!isValid)
125+
throw new InvalidOperationException();
126+
}
127+
};
128+
129+
}
130+
}
131+
}

Commands/DisconnectCommand.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine;
4+
using FluentFTP;
5+
6+
namespace Ftp.Client.Commands
7+
{
8+
[Verb("disconnect", HelpText = "Connect to ftp server")]
9+
public class DisconnectCommand : ICommand
10+
{
11+
public void Execute()
12+
{
13+
try
14+
{
15+
if (Program.Client != null)
16+
{
17+
Program.Client.Disconnect();
18+
Console.WriteLine("Successfully connected to ftp server...");
19+
Program.Client = null;
20+
}
21+
else
22+
{
23+
Console.WriteLine("There is no active connection to a ftp server...");
24+
}
25+
}
26+
catch (Exception e)
27+
{
28+
Console.WriteLine(e);
29+
}
30+
}
31+
}
32+
}

Commands/DownloadFolderCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected override void Execute(IFtpClient ftpClient)
4848
Console.ForegroundColor = ConsoleColor.White;
4949
Console.WriteLine($"No files downloaded");
5050
Console.ResetColor();
51+
Console.WriteLine(" ");
5152
}
5253
}
5354
}

Commands/ExitCommand.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using CommandLine;
3+
4+
namespace Ftp.Client.Commands
5+
{
6+
[Verb("exit", HelpText = "Closes the application")]
7+
public class ExitCommand : ICommand
8+
{
9+
public void Execute()
10+
{
11+
Environment.Exit(0);
12+
}
13+
}
14+
}

Commands/FetchFolderCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ protected override void Execute(IFtpClient ftpClient)
1919
{
2020
var listing = ftpClient.GetNameListing(Path);
2121

22-
Console.WriteLine("Result: ");
2322
foreach (var s in listing)
2423
{
2524
Console.WriteLine(s);

Commands/FileUploadCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ protected override void Execute(IFtpClient ftpClient)
2626
{
2727
var result = ftpClient.UploadFile(LocalFile, Path);
2828

29-
Console.WriteLine("Result: ");
3029
Console.WriteLine(result);
3130
}
3231
}

Commands/FtpCommand.cs

Lines changed: 7 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,130 +7,26 @@ namespace Ftp.Client.Commands
77
{
88
public abstract class FtpCommand : ICommand
99
{
10-
[Option('h', "Host", Required = true)]
11-
public string Host
12-
{
13-
get;
14-
set;
15-
}
16-
17-
[Option('u', "username", Required = true)]
18-
public string UserName
19-
{
20-
get;
21-
set;
22-
}
23-
24-
[Option('s', "password", Required = false)]
25-
public string Password
26-
{
27-
get;
28-
set;
29-
}
30-
31-
[Option('l', "ssl", Required = false, HelpText = "Enable ssl support")]
32-
public bool EnableSsl
33-
{
34-
get;
35-
set;
36-
}
37-
38-
[Option('v', "passive", Required = false, HelpText = "Set true to enable passive mode")]
39-
public bool EnablePassive
40-
{
41-
get;
42-
set;
43-
}
44-
45-
[Option('e', "external-ip", Required = false, HelpText = "Set this parameter to your NAT outgoing IP if you are operating within a NAT")]
46-
public string ExternalIp
47-
{
48-
get;
49-
set;
50-
}
51-
52-
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-
}
59-
60-
[Option('d', "debug", Required = false, HelpText = "Enables detailed logging.")]
61-
public bool DebugMode
62-
{
63-
get;
64-
set;
65-
}
66-
67-
[Option('a', "active_ports", Required = false, HelpText = "Comma separated active ports")]
68-
public string ActivePorts
69-
{
70-
get;
71-
set;
72-
}
73-
7410
public void Execute()
7511
{
76-
var ftpClient = new FtpClient(Host, UserName, Password);
77-
ConfigureFtpClient(ftpClient);
78-
ftpClient.Connect();
7912
try
8013
{
81-
Execute(ftpClient);
82-
Console.WriteLine("Press any key to exit...");
83-
Console.ReadLine();
14+
if (Program.Client == null)
15+
{
16+
Console.WriteLine("Ftp command needs a connection run the `connect` command first ...");
17+
return;
18+
}
19+
20+
Execute(Program.Client);
8421
}
8522
catch (Exception ex)
8623
{
8724
Console.WriteLine(ex);
8825
}
89-
finally
90-
{
91-
ftpClient?.Disconnect();
92-
}
93-
9426
}
9527

9628
protected abstract void Execute(IFtpClient ftpClient);
9729

98-
protected virtual void ConfigureFtpClient(FtpClient ftpClient)
99-
{
100-
if (DebugMode)
101-
ftpClient.OnLogEvent += (level, s) => Console.WriteLine($"{level}: {s}");
102-
103-
if (EnableSsl)
104-
{
105-
ftpClient.EncryptionMode = FtpEncryptionMode.Explicit;
106-
ftpClient.DataConnectionType = FtpDataConnectionType.PORT;
107-
}
108-
109-
if (!string.IsNullOrEmpty(ExternalIp))
110-
ftpClient.AddressResolver = () => ExternalIp;
111-
112-
if (EnablePassive)
113-
ftpClient.DataConnectionType = FtpDataConnectionType.AutoPassive;
114-
115-
if (!string.IsNullOrEmpty(ActivePorts))
116-
{
117-
ftpClient.ActivePorts = ActivePorts.Split(",").Select(s => int.Parse(s.Trim())).ToArray();
118-
}
119-
120-
ftpClient.ValidateCertificate += (control, args) =>
121-
{
122-
Console.BackgroundColor = ConsoleColor.DarkGreen;
123-
Console.WriteLine("Certificate to validate: " + args?.Certificate?.GetCertHashString());
124-
Console.ResetColor();
125-
126-
if (!string.IsNullOrEmpty(Certificate))
127-
{
128-
var isValid = args.Certificate.GetCertHashString()?.ToUpper() == Certificate.Replace(":", "").ToUpper();
129-
if (!isValid)
130-
throw new InvalidOperationException();
131-
}
132-
};
13330

134-
}
13531
}
13632
}

Ftp.Client.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>net6.0</TargetFramework>
5-
<AssemblyVersion>1.0.4</AssemblyVersion>
6-
<FileVersion>1.0.4</FileVersion>
7-
<version>1.0.4</version>
5+
<AssemblyVersion>2.0.0</AssemblyVersion>
6+
<FileVersion>2.0.0</FileVersion>
7+
<version>2.0.0</version>
88
</PropertyGroup>
99
<ItemGroup>
1010
<PackageReference Include="CommandLineParser" Version="2.8.0" />

0 commit comments

Comments
 (0)