I'm working with a websocket connection and I can't get it to return a correct connection. I have no errors. It just tells me that the connection is failed.
The test token is real: $token = 'TYcagYNyseG4aFooYjU1hy0lIsXDrrk34mWcDn4N2VE=';
<?php
$urlwebsocket = 'wss.remarkets.primary.com.ar';
$host = 'wss.remarkets.primary.com.ar';
$port = 443;
$token = 'TYcagYNyseG4aFooYjU1hy0lIsXDrrk34mWcDn4N2VE=';
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$socket){
echo "Error socket";
exit;
}
// Connect host
$result = socket_connect($socket, $host, $port);
if(!$result){
echo "Error connect host";
socket_close($socket);
exit;
}
// send auth
$request = [
'event' => 'auth',
'data' => [
'token' => $token
]
];
$request = json_encode($request);
$request = "\x00" . $request . "\xff";
socket_write($socket, $request, strlen($request));
// good connect
$response = socket_read($socket, 1024);
if(strpos($response, 'Welcome') !== false){
echo "Connect";
} else{
echo "No connect";
}
// Close socket
socket_close($socket);
?>
Related
php - phpseclib - how to connect to ssh host via socks5 proxy (proxy with basic authentication user:password)
we know method for ssh login via socks5 , but who knows how to connect via socks5 with basic authentication ?
in official example script there is no authentication method in use... :
$fsock = fsockopen('127.0.0.1', 1080, $errno, $errstr, 1);
if (!$fsock) {
throw new \Exception($errstr);
}
$port = pack('n', $port);
$address = chr(strlen($address)) . $address;
$request = "\5\1\0";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, 2);
if ($response != "\5\0") {
throw new \Exception('Unsupported protocol or unsupported method');
}
$request = "\5\1\0\3$address$port";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, strlen($address) + 6);
if (substr($response, 0, 2) != "\5\0") {
echo bin2hex($response) . "\n";
throw new \Exception("Unsupported protocol or connection refused");
}
$ssh = new SSH2($fsock);
$ssh->login('username', 'password');
echo $ssh->exec('ls -latr');
answer is:
$fsock = fsockopen('127.0.0.1', 1080, $errno, $errstr, 1);
if (!$fsock) {
throw new \Exception($errstr);
}
$port = pack('n', $port);
$address = chr(strlen($address)) . $address;
$request = "\5\1\2"; //2 - authentication method is user/password , 0 - no authentication
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, 2);
if ($response != "\5\2") { //acceptance from server that user/password method (2) is supported
throw new \Exception('Unsupported protocol or unsupported method');
}
//login password
$request = pack('C2',0x01,strlen($proxyUser)).$proxyUser.pack('C1',strlen($proxyPass)).$proxyPass;
if (fputs($fsock,$request) != strlen($request)) {
exit("premature termination");
}
$response = fgets($fsock,3);
echo "login confirmation status rcvd\n";
if ($response != "\1\0") {
exit("Login password incorrect");
}
// end section for authentication , authentication done with success
$request = "\5\1\0\3$address$port";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, strlen($address) + 6);
if (substr($response, 0, 2) != "\5\0") {
echo bin2hex($response) . "\n";
throw new \Exception("Unsupported protocol or connection refused");
}
$ssh = new SSH2($fsock);
$ssh->login('username', 'password');
echo $ssh->exec('ls -latr');
I'm trying to create a connection between the client and the server (with 2-way TLS).
Client.php
$stream_context = stream_context_create(['ssl' => [
'local_cert' => "path/to/cer.pem",
'verify_peer' => true,
'verify_peer_name' => false,
'passphrase' => "password to cert",
'verify_depth' => 0
]]);
$socket = stream_socket_client("tlsv1.2://127.0.0.1:8000", $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $stream_context);
if ($socket === false) {
return false;
}
$req = "POST /Serwer.php HTTP/1.1\r\n" .
"Content-Type: text/xml;charset=UTF-8\r\n" .
"Host: 127.0.0.1\r\n" .
"Connection: Close\r\n" .
"Hello world!\r\n";
$start = time();
fwrite($socket, $req);
$resp = '';
while (!feof($socket)) {
if (time() - $start > 15) {
break;
}
$f = fgets($socket);
$resp .= $f;
}
fclose($socket);
echo $resp;
Server.php
$stream_context = stream_context_create(['ssl' => [
'local_cert' => "path/to/cert.pem",
'passphrase' => "password to cert",
'allow_self_signed' => true,
'verify_peer' => true
]]);
$server = stream_socket_server("tlsv1.2://0.0.0.0:8000",$errno, $error, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $stream_context);
if ($server === false) {
return false;
}
$connects = array();
while (true) {
$read = $connects;
$read []= $server;
$write = $except = null;
$mod_fd = stream_select($read, $write, $except, 3); // return always 0, I don't know why
if ($mod_fd === false) {
break;
}
if (in_array($server, $read)) {
$connect = stream_socket_accept($server, -1);
$connects[] = $connect;
unset($read[ array_search($server, $read) ]);
}
foreach($read as $connect) {
$headers = '';
while ($buffer = rtrim(fgets($connect))) {
$headers .= $buffer;
}
fwrite($connect, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nHello!");
fclose($connect);
unset($connects[ array_search($connect, $connects) ]);
}
fclose($server);
In this example server and client are on different machines.
I must to use TLS v 1.2.
Address and port on the client and the server must be the same or different? (For example in server.php I use address 0.0.0.0 and port 8000, but in client.php 127.0.0.1 and port 8000)
How the server and the client must accept the certificates TLS?
How read on server side phrase "Hello world" from client?
I'm trying to connect to sftp with rsa private key auth, through proxy.
use phpseclib\Net\SFTP;
use phpseclib\Crypt\RSA;
$proxyHost = 'proxy-host';
$proxyPort = 1080;
$fsock = fsockopen($proxyHost, $proxyPort);
$host = 'sftp.hostname.com';
$login = 'sftp_login';
$privatekey = file_get_contents('sftp_private.ppk');
$password = new RSA();
$password->loadKey($privatekey);
$request = "CONNECT $host:22 HTTP/1.0\r\nContent-Length: 0\r\n\r\n";
if(fputs($fsock, $request) != strlen($request)) {
exit("premature termination");
}
while ($line = fgets($fsock, 1024)) {
if ($line == "\r\n") {
break;
}
//echo $line;
}
$sftp = new SFTP($fsock);
if (!$sftp->login($login, $password)) {
exit('Login Failed');
}
I get the "Login Failed" exit.
Thanks
Quoting
https://github.com/phpseclib/phpseclib/issues/1460 :
Port 1080 is typically used for SOCKS5 proxies - not HTTP proxies.
Assuming you're using a SOCKS5 proxy then something like this should
work:
// SSH connection info
$port = 22;
$address = 'localhost';
// SOCKS5 connection info
$fsock = fsockopen('127.0.0.1', 1080, $errno, $errstr, 1);
if (!$fsock) {
throw new \Exception($errstr);
}
$port = pack('n', $port);
$address = chr(strlen($address)) . $address;
$request = "\5\1\0";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, 2);
if ($response != "\5\0") {
throw new \Exception('Unsupported protocol or unsupported method');
}
$request = "\5\1\0\3$address$port";
if (fwrite($fsock, $request) != strlen($request)) {
throw new \Exception('Premature termination');
}
$response = fread($fsock, strlen($address) + 6);
if (substr($response, 0, 2) != "\5\0") {
echo bin2hex($response) . "\n";
throw new \Exception("Unsupported protocol or connection refused");
}
$ssh = new SSH2($fsock);
$ssh->login('username', 'password');
echo $ssh->exec('ls -latr');
That said, SOCKS5 has a lot more bells and whistles then this sample
code currently accommodates. Really, what'd be good is a full on
SOCKS5 class.
Hope it will help someone else.
I want to send something to my connected clients over another php script. But when I use the function "send", clientlist is null.
Can you help me about this please?
Socket.php:
$clientlist = array();
function run() {
global $clientlist;
set_time_limit(0);
$address = '127.0.0.1';
$port = 80;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address');
socket_listen($sock);
printf("Listening...\r\n");
while (true) {
$client = socket_accept($sock);
$input = socket_read($client, 1024000);
$clientlist[] = $client;
}
}
function send($msg) {
global $clientlist;
printf("Count: " . count($clientlist) . "\r\n");
socket_write($clientlist[0], "Hey");
}
Msg.php:
include("socket.php");
send($_GET['msg']);
I have one PHP application which have socket connection. I am thinking for integrate proxy in it so I can make connection using proxy. My current code for connection is like below
public function connect()
{
if ($this->isConnected()) {
return true;
}
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket !== false) {
$result = socket_connect($socket, 'e'.rand(1, 16).'.myserver.net', Constants::PORT);
if ($result === false) {
$socket = false;
}
}
if ($socket !== false) {
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => Constants::TIMEOUT_SEC, 'usec' => Constants::TIMEOUT_USEC]);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => Constants::TIMEOUT_SEC, 'usec' => Constants::TIMEOUT_USEC]);
$this->socket = $socket;
$this->eventManager()->fire('onConnect',
[
$this->phoneNumber,
$this->socket,
]
);
$this->logFile('info', 'Connected to server');
return true;
} else {
$this->logFile('error', 'Failed to connect server');
$this->eventManager()->fire('onConnectError',
[
$this->phoneNumber,
$this->socket,
]
);
return false;
}
}
Let me know if someone have reliable idea for do it. Thanks