I am trying to communicate with imap via ssh in PHP. The following code demonstrates how I connect to imap via ssh:
$ssh_conn = ssh2_connect($sshIP, $sshPORT);
ssh2_auth_password($ssh_conn, $sshUSERNAME, $sshPASSWORD);
$stream = ssh2_tunnel($c, $imapSERVER, $imapPORT);
And seems everything is ok, I get connected to imap via ssh. No errors, and I have resource in $stream.
Then I try to write LOGIN command to stream, like this:
fwrite($stream, "A1 LOGIN username password
");
Problem is that server doesn't response anything.
I try to get response like this:
while (true) {
echo fread($stream, 4096);
}
Don't worry about codes for writing and reading data to imap server, this I have written just for example. This is not important here. The problem is that I don't get any response from imap. Always empty string.
I am sure, writing and reading methods work well, because I use big HORDE library https://dev.horde.org/imap_client/, and when I use stream of SOCKS5 or HTTPS - it work well!
Also I tried different SSH servers - not success.
So, where might be a problem ? Also the next information may be useful: I get different information when using stream_get_meta_data() for ssh and socks5 stream.
SSH:
array(8) {
["exit_status"]=>
int(0)
["timed_out"]=>
bool(false)
["blocked"]=>
bool(true)
["eof"]=>
bool(false)
["stream_type"]=>
string(12) "SSH2 Channel"
["mode"]=>
string(2) "r+"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(false)
}
SOCKS5:
array(8) {
["crypto"]=>
array(4) {
["protocol"]=>
string(7) "TLSv1.2"
["cipher_name"]=>
string(27) "ECDHE-RSA-AES128-GCM-SHA256"
["cipher_bits"]=>
int(128)
["cipher_version"]=>
string(7) "TLSv1.2"
}
["timed_out"]=>
bool(false)
["blocked"]=>
bool(true)
["eof"]=>
bool(false)
["stream_type"]=>
string(14) "tcp_socket/ssl"
["mode"]=>
string(2) "r+"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(false)
}
Also I notified important thing, it is working if I use port 143 for imap server. And doesn't work if I use port 993. But 143 is not ssl. Therefore I can't even use LOGIN command. I think cause touches with ssl. But how to set ssl for ssh tunnel..... ?