【发布时间】:2021-04-12 04:08:04
【问题描述】:
我尝试使用 apache-commons-net-3.7.2(隐式 TLS,使用客户端证书 + 登录名/密码的双因素身份验证)连接 FTP 服务器。
我可以验证自己,进入被动模式,但是客户端没有成功连接到服务器以通过数据套接字获取数据。
我可以在同一台计算机上使用 WinSCP(相同的设置)连接自己。我已激活 WinSCP 日志以查看协议详细信息,并使用相同的选项调整了我的源代码。我可以使用ProtocolCommandListener 验证我的协议是否正常。我知道被动模式是必需的,因为 WinSCP 会发出 PASV 命令。
我可以看到 WinSCP 连接到端口 62564 上的数据套接字(我已将 FTP IP 地址替换为 XXX)
2021-01-06 10:25:35.575 227 Entering Passive Mode (192,168,4,122,244,100).
2021-01-06 10:25:35.575 Server sent passive reply with unroutable address 192.168.4.122, using host address instead.
2021-01-06 10:25:35.575 MLSD
2021-01-06 10:25:35.575 Connexion à 83.XXX.XXX.XXX:62564...
2021-01-06 10:25:35.604 150 Transferring directory
我还可以看到服务器为PASV 命令发送的回复不包括要连接的端口。
public class TestApi {
public static void _parseExtendedPassiveModeReply(String reply)
{
reply = reply.substring(reply.indexOf('(') + 1,
reply.indexOf(')')).trim();
char delim1, delim2, delim3, delim4;
delim1 = reply.charAt(0);
delim2 = reply.charAt(1);
delim3 = reply.charAt(2);
delim4 = reply.charAt(reply.length()-1);
if (!(delim1 == delim2) || !(delim2 == delim3)
|| !(delim3 == delim4)) {
System.out.println("Could not parse extended passive host information.\nServer Reply: " + reply);
}
int port;
try
{
port = Integer.parseInt(reply.substring(3, reply.length()-1));
}
catch (NumberFormatException e)
{
System.out.println("Could not parse extended passive host information.\nServer Reply: " + reply);
}
}
public static void main(String[] args) throws SocketException, IOException, GeneralSecurityException {
String hostname = args[0];
int port = Integer.parseInt(args[1]);
String login = args[2];
String pwd = args[3];
FTPSClient client = new FTPSClient("TLS",true);
File clientCertStore = new File("myJCEKS keystore");
KeyManager keyManager = KeyManagerUtils.createClientKeyManager("JCEKS",clientCertStore,"","myalias","");
client.setKeyManager(keyManager);
client.connect(hostname, port);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
} else {
if (client.login(login, pwd)) {
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.err), true));
client.sendCommand("OPTS","UTF8 ON");
client.sendCommand("PBSZ","0");
client.sendCommand("PROT","P");
int retour = client.pasv();
System.out.println(retour);
_parseExtendedPassiveModeReply(client.getReplyString());
System.out.println(client.printWorkingDirectory());
reply = client.getReplyCode();
System.out.println(reply);
System.out.println(client.listHelp());
//it freezes here, after sending MLDS command
//same thing using regular api for listing files (WinSCP use MLSD while regular api uses LIST)
client.sendCommand("MLSD");
//and so on
System.out.println("LOGOUT");
client.logout();
} else {
System.out.println("echec login");
}
}
}
}
我想 API 不知道必须在哪个端口发送数据请求,并且使用了一个不合适的默认值。不知道WinSCP是如何成功计算62564端口号的。
OPTS UTF8 ON
200 Command OPTS succeed
PBSZ 0
200 PBSZ=0
PROT P
200 PRIVATE data channel protection level set
PASV
227 Entering Passive Mode (192,168,4,122,245,74).
227
PWD
Could not parse extended passive host information.
Server Reply: 192,168,4,122,245,74
Could not parse extended passive host information.
Server Reply: 192,168,4,122,245,74
257 "/" is current directory
/
257
HELP
214-The following commands are implemented
ABOR ACCT ALLO* APPE CDUP CWD DELE FEAT+ HELP
HOST+ LANG+ LIST MDTM+ MLST+ MKD MODE NLST NOOP
OPTS+ PASS PASV PORT PWD QUIT REIN REST RETR
RMD RNFR RNTO SITE SIZE SMNT STAT STOR STOU
STRU* SYST TYPE USER XCUP XCRC XCWD XMD5 XMKD
XPWD XRMD XSIGN XSHA1 XSHA256 XSHA512 XQUOTA
214 Help complete
214-The following commands are implemented
ABOR ACCT ALLO* APPE CDUP CWD DELE FEAT+ HELP
HOST+ LANG+ LIST MDTM+ MLST+ MKD MODE NLST NOOP
OPTS+ PASS PASV PORT PWD QUIT REIN REST RETR
RMD RNFR RNTO SITE SIZE SMNT STAT STOR STOU
STRU* SYST TYPE USER XCUP XCRC XCWD XMD5 XMKD
XPWD XRMD XSIGN XSHA1 XSHA256 XSHA512 XQUOTA
214 Help complete
MLSD
在 API 文档、源代码、FTP RFC 中搜索数小时后,我不知道该怎么做。
【问题讨论】:
标签: java ftp apache-commons-net