Skip to content

Commit c8e38c1

Browse files
author
Salim Lachdhaf
committed
Merge remote-tracking branch 'origin/master'
2 parents d3c3068 + 6f36716 commit c8e38c1

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
test/test_res_files
2+
13
# Miscellaneous
24
*.class
35
*.log

lib/src/ftp_socket.dart

+28-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:convert';
33
import 'dart:io';
44

55
import '/src/ftp_reply.dart';
6-
76
import '../ftpconnect.dart';
87

98
class FTPSocket {
@@ -89,12 +88,24 @@ class FTPSocket {
8988
Future<bool> connect(String user, String pass, {String? account}) async {
9089
logger.log('Connecting...');
9190

91+
final timeout = Duration(seconds: this.timeout);
92+
9293
try {
93-
_socket = await RawSocket.connect(
94-
host,
95-
port,
96-
timeout: Duration(seconds: timeout),
97-
);
94+
// FTPS starts secure
95+
if (securityType == SecurityType.FTPS) {
96+
_socket = await RawSecureSocket.connect(
97+
host,
98+
port,
99+
timeout: timeout,
100+
onBadCertificate: (certificate) => true,
101+
);
102+
} else {
103+
_socket = await RawSocket.connect(
104+
host,
105+
port,
106+
timeout: timeout,
107+
);
108+
}
98109
} catch (e) {
99110
throw FTPConnectException(
100111
'Could not connect to $host ($port)', e.toString());
@@ -103,22 +114,23 @@ class FTPSocket {
103114
logger.log('Connection established, waiting for welcome message...');
104115
await readResponse();
105116

106-
if (securityType == SecurityType.FTPS ||
107-
securityType == SecurityType.FTPES) {
108-
if (securityType == SecurityType.FTPES) {
109-
FTPReply lResp = await sendCommand('AUTH TLS');
117+
// FTPES needs to be upgraded prior to getting a welcome
118+
if (securityType == SecurityType.FTPES) {
119+
FTPReply lResp = await sendCommand('AUTH TLS');
120+
if (!lResp.isSuccessCode()) {
121+
lResp = await sendCommand('AUTH SSL');
110122
if (!lResp.isSuccessCode()) {
111-
lResp = await sendCommand('AUTH SSL');
112-
if (!lResp.isSuccessCode()) {
113-
throw FTPConnectException(
114-
'FTPES cannot be applied: the server refused both AUTH TLS and AUTH SSL commands',
115-
lResp.message);
116-
}
123+
throw FTPConnectException(
124+
'FTPES cannot be applied: the server refused both AUTH TLS and AUTH SSL commands',
125+
lResp.message);
117126
}
118127
}
128+
119129
_socket = await RawSecureSocket.secure(_socket,
120130
onBadCertificate: (certificate) => true);
131+
}
121132

133+
if ([SecurityType.FTPES, SecurityType.FTPS].contains(securityType)) {
122134
await sendCommand('PBSZ 0');
123135
await sendCommand('PROT P');
124136
}

lib/src/ftpconnect_base.dart

+19-12
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,30 @@ class FTPConnect {
1717
/// Create a FTP Client instance
1818
///
1919
/// [host]: Hostname or IP Address
20-
/// [port]: Port number (Defaults to 21)
20+
/// [port]: Port number (Defaults to 21 for FTP and FTPES, 990 for FTPS)
2121
/// [user]: Username (Defaults to anonymous)
2222
/// [pass]: Password if not anonymous login
2323
/// [debug]: Enable Debug Logging
2424
/// [timeout]: Timeout in seconds to wait for responses
25-
FTPConnect(String host,
26-
{int port = 21,
27-
String user = 'anonymous',
28-
String pass = '',
29-
bool showLog = false,
30-
SecurityType securityType = SecurityType.FTP,
31-
Logger? logger,
32-
int timeout = 30})
33-
: _user = user,
25+
FTPConnect(
26+
String host, {
27+
int? port,
28+
String user = 'anonymous',
29+
String pass = '',
30+
bool showLog = false,
31+
SecurityType securityType = SecurityType.FTP,
32+
Logger? logger,
33+
int timeout = 30,
34+
}) : _user = user,
3435
_pass = pass {
35-
_socket = FTPSocket(host, port, securityType,
36-
logger != null ? logger : Logger(isEnabled: showLog), timeout);
36+
port ??= securityType == SecurityType.FTPS ? 990 : 21;
37+
_socket = FTPSocket(
38+
host,
39+
port,
40+
securityType,
41+
logger != null ? logger : Logger(isEnabled: showLog),
42+
timeout,
43+
);
3744
}
3845

3946
set transferMode(TransferMode pTransferMode) {

test/ftpConnect_test.dart

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ void main() async {
1414
);
1515
_ftpConnect.supportIPV6 = true;
1616

17+
final FTPConnect _ftpsConnect = new FTPConnect(
18+
"test.rebex.net",
19+
user: "demo",
20+
pass: "password",
21+
securityType: SecurityType.FTPS,
22+
showLog: true,
23+
);
24+
_ftpConnect.supportIPV6 = true;
25+
1726
final FTPConnect _ftpConnectSecured = new FTPConnect(
1827
"ftp.dlptest.com",
1928
user: "dlpuser",
@@ -57,6 +66,10 @@ void main() async {
5766
throwsA(isA<FTPConnectException>()));
5867
});
5968

69+
test('test ftps', () async {
70+
expect(await _ftpsConnect.connect(), equals(true));
71+
});
72+
6073
test('test ftpConnect No log', () async {
6174
final FTPConnect _ftpConnectNoLog = new FTPConnect("users.on.net",
6275
user: "pvpt", pass: "Lachdhaf", showLog: true);

0 commit comments

Comments
 (0)