PHP curl FTPes con TLS / SSL explícito
He estado tratando de pasar a un FTP remoto con un servidor TLS / SSL explícito y la conexión se agota. Se está conectando pero no sé dónde se está deteniendo, pero supongo que es algo que tiene que ver con el canal de control FTP / TLS / SSL. Cualquier ayuda sería muy apreciada.
A través de FileZilla puedo conectarme sin problemas pero no a través de curl. Probablemente me estoy perdiendo algo y espero que alguien aquí conozca la mejor manera de recrear la sesión usando curl. Aquí está la salida de depuración de FileZilla y el código de muestra en el que he estado trabajando en PHP / curl. Aquí está el código PHP y debajo hay una copia de una sesión exitosa de FileZilla.
Aquí están las versiones, pero lo he probado en diferentes sistemas con los mismos resultados.
Curl: v7.43.0
└─(08:04:00 on master ✹)──> php -v ──(Thu,Sep10)─┘
PHP 5.5.27 (cli) (built: Jul 14 2015 17:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
Aquí está el código real.
<?php
$server_data = array(
'transfer_id' => 123456789,
'post_url' => "ftps://ftps.widgetsltd.com",
'port' => 21,
'username' => 'widgetsftp',
'password' => 'password',
);
$filename = sprintf("%s-%s-%s.csv",
$server_data['transfer_id'],
microtime(TRUE),
rand(1000, 9999));
$temp_filename = sprintf("/tmp/%s", $filename);
$ftp_data = "This is a test";
$fp = fopen($temp_filename, 'w');
fprintf($fp, "%s", $ftp_data);
fclose($fp);
$fp = fopen($temp_filename, 'r');
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, sprintf("%s/%s", $server_data['post_url'], $filename));
curl_setopt($ch, CURLOPT_PORT, 21);
curl_setopt($ch, CURLOPT_USERPWD, sprintf("%s:%s", $server_data['username'], $server_data['password']));
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($temp_filename));
curl_setopt($ch, CURLOPT_USE_SSL, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_0);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'AES-128-CBC');
curl_setopt($ch, CURLOPT_FTP_SSL, CURLOPT_FTPSSLAUTH);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
print_r(array('curl_exec' => curl_exec($ch)));
print_r(array(
'curl_errno' => curl_errno($ch),
'curl_error' => curl_error($ch),
));
Esta es la sesión de FileZilla.
Status: Resolving address of ftps.widgetsltd.com
Status: Connecting to 123.123.123.123:21...
Status: Connection established, waiting for welcome message...
Trace: CFtpControlSocket::OnReceive()
Response: 220-Microsoft FTP Service
Response: 220 Widgets, LTD FTP server
Trace: CFtpControlSocket::SendNextCommand()
Command: AUTH TLS
Trace: CFtpControlSocket::OnReceive()
Response: 234 AUTH command ok. Expecting TLS Negotiation.
Status: Initializing TLS...
Trace: CTlsSocket::Handshake()
Trace: CTlsSocket::ContinueHandshake()
Trace: CTlsSocket::OnSend()
Trace: CTlsSocket::OnRead()
Trace: CTlsSocket::ContinueHandshake()
Trace: CTlsSocket::OnRead()
Trace: CTlsSocket::ContinueHandshake()
Trace: TLS Handshake successful
Trace: Protocol: TLS1.0, Key exchange: RSA, Cipher: AES-128-CBC, MAC: SHA1
Status: Verifying certificate...
Status: TLS connection established.
Trace: CFtpControlSocket::SendNextCommand()
Command: USER s-rokfri
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 331 Password required for s-rokfri.
Trace: CFtpControlSocket::SendNextCommand()
Command: PASS ********
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 230-This service and information contained therein belong to Widgets, LTD.
Response: 230 User logged in.
Trace: CFtpControlSocket::SendNextCommand()
Command: OPTS UTF8 ON
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 200 OPTS UTF8 command successful - UTF8 encoding now ON.
Trace: CFtpControlSocket::SendNextCommand()
Command: PBSZ 0
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 200 PBSZ command successful.
Trace: CFtpControlSocket::SendNextCommand()
Command: PROT P
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 200 PROT command successful.
Status: Connected
Trace: CFtpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Trace: CFileZillaEnginePrivate::ResetOperation(0)
Trace: Measured latency of 141 ms
Status: Retrieving directory listing...
Trace: CFtpControlSocket::SendNextCommand()
Trace: CFtpControlSocket::ChangeDirSend()
Command: PWD
Trace: CTlsSocket::OnRead()
Trace: CFtpControlSocket::OnReceive()
Response: 257 "/" is current directory.
Trace: CFtpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Trace: CFtpControlSocket::ParseSubcommandResult(0)
Trace: CFtpControlSocket::ListSubcommandResult()
Trace: state = 1
Trace: CFtpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: Directory listing of "/" successful
Trace: CFileZillaEnginePrivate::ResetOperation(0)
Aquí está la salida del rizo.
* Trying 123.123.123.123...
* Connected to ftps.widgetsltd.com (123.123.123.123) port 21 (#0)
* SSL connection timeout
* Closing connection 0
Array
(
[curl_exec] =>
)
Array
(
[curl_errno] => 28
[curl_error] => SSL connection timeout
)