@@ -7,130 +7,26 @@ namespace Ftp.Client.Commands
7
7
{
8
8
public abstract class FtpCommand : ICommand
9
9
{
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
-
74
10
public void Execute ( )
75
11
{
76
- var ftpClient = new FtpClient ( Host , UserName , Password ) ;
77
- ConfigureFtpClient ( ftpClient ) ;
78
- ftpClient . Connect ( ) ;
79
12
try
80
13
{
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 ) ;
84
21
}
85
22
catch ( Exception ex )
86
23
{
87
24
Console . WriteLine ( ex ) ;
88
25
}
89
- finally
90
- {
91
- ftpClient ? . Disconnect ( ) ;
92
- }
93
-
94
26
}
95
27
96
28
protected abstract void Execute ( IFtpClient ftpClient ) ;
97
29
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
- } ;
133
30
134
- }
135
31
}
136
32
}
0 commit comments