wip WS redirection

This commit is contained in:
fbsanchez 2019-10-15 17:20:23 +02:00
parent 3a9e5fa477
commit 06f8f920b8
5 changed files with 109 additions and 68 deletions

View File

@ -51,7 +51,7 @@ class QuickShell
*
* Registers class into system.
*
* @return void
* @return boolean
*/
public function install()
{
@ -66,7 +66,7 @@ class QuickShell
*/
public function interactiveShell()
{
file_get_Str
file_get_Str();
}

View File

@ -102,7 +102,8 @@ class WSProxy extends WebSocketServer
return false;
}
return $this->splitPacket($numBytes, $buffer, $user);
$user->lastRawPacket = $buffer;
return $buffer;
}
@ -117,10 +118,9 @@ class WSProxy extends WebSocketServer
protected function writeSocket($user, $message)
{
if ($user->socket) {
socket_write(
$user->socket,
$message
);
if (!socket_write($user->socket, $message)) {
$this->disconnect($user->socket);
}
} else {
// Failed. Disconnect.
$this->disconnect($user->socket);
@ -186,12 +186,16 @@ class WSProxy extends WebSocketServer
protected function connected($user)
{
echo '** CONNECTED'."\n";
// Disconnect previous sessions.
$this->cleanupSocketByCookie($headers['cookie']);
/*
* $user->intSocket is connected to internal.
* $user->socket is connected to external.
*/
var_dump($user);
// Create a new socket connection (internal).
$intUser = $this->connectInt($this->rawHeaders);
@ -199,6 +203,8 @@ class WSProxy extends WebSocketServer
$user->intUser = $intUser;
// And socket.
$user->intSocket = $intUser->socket;
$user->redirect = $intUser;
$intUser->redirect = $user;
$response = $this->readSocket($intUser);
@ -218,6 +224,36 @@ class WSProxy extends WebSocketServer
}
/**
* Process undecoded user message.
*
* @param object $user User.
* @param string $buffer Message.
*
* @return boolean
*/
protected function processRaw($user, $buffer)
{
unset($user->intUser->lastRawPacket);
unset($user->lastRawPacket);
echo "\n****************** REDIRECT *********************\n";
echo date('D M j G:i:s').' - '.$user->id.' >> '.$user->intUser->id."\n";
echo $this->dump($buffer);
$this->writeSocket(
$user->intUser,
$buffer
);
echo date('D M j G:i:s').' - '.$user->intUser->id.'] << '.$user->id."\n";
$response = $this->readSocket($user->intUser);
$this->writeSocket($user, $user->intUser->lastRawPacket);
echo $this->dump($user->intUser->lastRawPacket);
return true;
}
/**
* From client to socket (internal);
*
@ -228,21 +264,7 @@ class WSProxy extends WebSocketServer
*/
protected function process($user, $message)
{
echo '>> ['.$user->id.'] => ['.$user->intUser->id."]\n";
echo $this->dump($this->rawPacket);
$this->writeSocket(
$user->intUser,
$this->rawPacket
);
echo '<< ['.$user->intUser->id.'] => ['.$user->id."]\n";
$response = $this->readSocket($user->intUser);
$this->send($user, $response);
echo $this->dump($this->rawPacket);
print "\n********************************************\n";
print "\n\n\n";
print "\n\n\n";
print "\n\n\n";
echo 'Procesando..'."\n";
}

View File

@ -19,6 +19,13 @@ class WSProxyUser extends WebSocketUser
*/
public $intSocket;
/**
* Pair resend packages.
*
* @var WSProxyUser
*/
public $redirect;
/**
* User identifier.
*
@ -30,9 +37,8 @@ class WSProxyUser extends WebSocketUser
/**
* Builder.
*
* @param string $id Identifier.
* @param socket $socket Socket (origin).
* @param socket $intSocket Socket (internal).
* @param string $id Identifier.
* @param socket $socket Socket (origin).
*/
public function __construct($id, $socket)
{

View File

@ -138,13 +138,6 @@ abstract class WebSocketServer
*/
public $rawHeaders = [];
/**
* Raw packet for redirection.
*
* @var string
*/
public $rawPacket;
/**
* Builder.
@ -200,6 +193,20 @@ abstract class WebSocketServer
abstract protected function process($user, $message);
/**
* Process undecoded user message.
*
* @param object $user User.
* @param string $buffer Message.
*
* @return void
*/
protected function processRaw($user, $buffer)
{
}
/**
* Called immediately when the data is recieved.
*
@ -245,6 +252,7 @@ abstract class WebSocketServer
*/
protected function send($user, $message)
{
var_dump($user->handshake);
if ($user->handshake) {
$message = $this->frame($message, $user);
$result = socket_write($user->socket, $message, strlen($message));
@ -401,8 +409,6 @@ abstract class WebSocketServer
);
} else {
$user = $this->getUserBySocket($socket);
$pair = $this->getIntUserBySocket($socket);
if (!$user->handshake) {
$tmp = str_replace("\r", '', $buffer);
if (strpos($tmp, "\n\n") === false) {
@ -414,10 +420,7 @@ abstract class WebSocketServer
$this->doHandshake($user, $buffer);
} else {
if (is_array($pair) && $pair['int']) {
echo '~~ RESEND!'."\n";
socket_write($pair['ext']->socket, $buffer);
} else {
if (!$this->processRaw($user, $buffer)) {
// Split packet into frame and send it to deframe.
$this->splitPacket($numBytes, $buffer, $user);
}
@ -742,6 +745,24 @@ abstract class WebSocketServer
}
/**
* Disconnects all users matching target cookie but latest one.
*
* @param string $cookie Cookie identifier.
*
* @return void
*/
protected function cleanupSocketByCookie($cookie)
{
foreach ($this->users as $user) {
if ($user->headers['cookie'] == $cookie) {
$this->disconnectUser($user->socket);
}
}
}
/**
* Return INT user associated to target socket.
*
@ -921,7 +942,7 @@ abstract class WebSocketServer
$length = strlen($packet);
}
$this->rawPacket = $packet;
$user->lastRawPacket = $packet;
$fullpacket = $packet;
$frame_pos = 0;
$frame_id = 1;
@ -1130,6 +1151,22 @@ abstract class WebSocketServer
$header['length'] += (ord($message[8]) * 256);
$header['length'] += ord($message[9]);
} else if ($header['hasmask']) {
if (!isset($message[2])) {
$message[2] = 0x00;
}
if (!isset($message[3])) {
$message[3] = 0x00;
}
if (!isset($message[4])) {
$message[4] = 0x00;
}
if (!isset($message[5])) {
$message[5] = 0x00;
}
$header['mask'] = $message[2].$message[3].$message[4].$message[5];
}
@ -1325,7 +1362,7 @@ abstract class WebSocketServer
// Convert to hexidecimal.
$hexi .= sprintf('%02x ', ord($data[$i]));
// Replace non-viewable bytes with '.'.
if (ord($data[$i]) >= 32) {
if (ord($data[$i]) >= 32 && ord($data[$i]) <= 255) {
$ascii .= $data[$i];
} else {
$ascii .= '.';

View File

@ -115,18 +115,18 @@ class WebSocketUser
public $hasSentClose = false;
/**
* Received raw packet, to be redirected.
* Raw packet for redirection.
*
* @var string
*/
public $rawPacket;
public $lastRawPacket;
/**
* Initializes a websocket user.
*
* @param string $id Id.
* @param Socket $socket Socket.
* @param string $id Id of the new user.
* @param Socket $socket Socket where communication is stablished.
*/
public function __construct($id, $socket)
{
@ -135,28 +135,4 @@ class WebSocketUser
}
/**
* Retun last packet (raw) received
*
* @return string
*/
public function getRawPacket()
{
return $this->rawPacket;
}
/**
* Store raw packet.
*
* @param string $packet received.
*
* @return void
*/
public function setRawPacket($packet)
{
$this->rawPacket = $packet;
}
}