Skip to content

Commit

Permalink
2.0 transition (arthurkushman#54)
Browse files Browse the repository at this point in the history
* arthurkushman#53: Add types to WebSocketClient

* arthurkushman#53: Add types to WebSocketServer

* arthurkushman#53: Fix string type for web socket accept header
  • Loading branch information
arthurkushman authored Mar 7, 2021
1 parent 6ae2ec0 commit 32167a1
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 105 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arthurkushman/php-wss",
"description": "Web-socket server with URI parse and multi-process support",
"description": "Web-socket server/client with URI parse and multi-process support",
"keywords": [
"web-sockets",
"web socket server",
Expand All @@ -13,7 +13,7 @@
"license": "MIT",
"homepage": "https://github.com/arthurkushman/php-wss",
"require": {
"php": ">=7.1",
"php": ">=7.4",
"ext-pcntl": "*"
},
"autoload": {
Expand Down
94 changes: 74 additions & 20 deletions src/Components/ClientConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,75 @@
*/
class ClientConfig
{
private $scheme;
private $host;
private $user;
private $password;
private $port;

private $timeout = WscCommonsContract::DEFAULT_TIMEOUT;
private $headers = [];
private $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE;
/**
* @var string
*/
private string $scheme;

/**
* @var string
*/
private string $host;

/**
* @var string
*/
private string $user;

/**
* @var string
*/
private string $password;

/**
* @var string
*/
private string $port;

/**
* @var int
*/
private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT;

/**
* @var array
*/
private array $headers = [];

/**
* @var int
*/
private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE;

/**
* @var null|resource
*/
private $context;

// proxy settings
private $hasProxy = false;
private $proxyIp;
private $proxyPort;
private $proxyAuth;
/**
* @var bool
*/
private bool $hasProxy = false;

private $contextOptions = [];
/**
* @var string
*/
private string $proxyIp;

/**
* @var string
*/
private string $proxyPort;

/**
* @var string|null
*/
private ?string $proxyAuth;

/**
* @var array
*/
private array $contextOptions = [];

/**
* @return int
Expand Down Expand Up @@ -110,10 +161,10 @@ public function getScheme(): string
}

/**
* @param void $scheme
* @param string $scheme
* @return ClientConfig
*/
public function setScheme($scheme): ClientConfig
public function setScheme(string $scheme): ClientConfig
{
$this->scheme = $scheme;
return $this;
Expand All @@ -128,10 +179,10 @@ public function getHost(): string
}

/**
* @param void $host
* @param string $host
* @return ClientConfig
*/
public function setHost($host): ClientConfig
public function setHost(string $host): ClientConfig
{
$this->host = $host;
return $this;
Expand Down Expand Up @@ -165,6 +216,7 @@ public function getPassword(): string

/**
* @param array $urlParts
* @return ClientConfig
*/
public function setPassword(array $urlParts): ClientConfig
{
Expand All @@ -182,6 +234,7 @@ public function getPort(): string

/**
* @param array $urlParts
* @return ClientConfig
*/
public function setPort(array $urlParts): ClientConfig
{
Expand All @@ -199,7 +252,7 @@ public function getContextOptions(): array

/**
* @param array $contextOptions
* @return ServerConfig
* @return ClientConfig
*/
public function setContextOptions(array $contextOptions): ClientConfig
{
Expand All @@ -210,6 +263,7 @@ public function setContextOptions(array $contextOptions): ClientConfig
/**
* @param string $ip
* @param string $port
* @return ClientConfig
*/
public function setProxy(string $ip, string $port): ClientConfig
{
Expand Down
15 changes: 13 additions & 2 deletions src/Components/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
use WSSC\Contracts\ConnectionContract;
use WSSC\Contracts\WebSocketServerContract;

/**
* Class Connection
* @package WSSC\Components
*/
class Connection implements ConnectionContract, CommonsContract
{
/**
* @var false|resource
*/
private $socketConnection;
private $clients;

/**
* @var array
*/
private array $clients;

/**
* Connection constructor.
Expand Down Expand Up @@ -169,7 +180,7 @@ private function getOpType(string $type): array
* @return string
* @throws \Exception
*/
private function getComposedFrame(array $frameHead, string $payload, int $payloadLength, bool $masked)
private function getComposedFrame(array $frameHead, string $payload, int $payloadLength, bool $masked): string
{
// convert frame-head to string:
foreach (array_keys($frameHead) as $i) {
Expand Down
37 changes: 24 additions & 13 deletions src/Components/OriginComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

namespace WSSC\Components;

use PHPUnit\Framework\OutputError;
use WSSC\Exceptions\ConnectionException;

/**
* Class OriginComponent
* @package WSSC\Components
*/
class OriginComponent
{
/**
* @var false|resource
*/
private $client;
private $config;


/**
* @var ServerConfig
*/
private ServerConfig $config;

/**
* OriginComponent constructor.
* @param ServerConfig $config
* @param $client
* @param false|resource $client
*/
public function __construct(ServerConfig $config, $client)
{
Expand All @@ -25,29 +34,31 @@ public function __construct(ServerConfig $config, $client)
* Checks if there is a compatible origin header came from client
* @param string $headers
* @return bool
* @throws \Exception
*/
public function checkOrigin(string $headers): bool
{
preg_match('/Origin\:\s(.*?)\s/', $headers, $matches);
if (empty($matches[1])) {
$this->sendAndClose('No Origin header found.');
return false;
} else {
$originHost = $matches[1];
$allowedOrigins = $this->config->getOrigins();
if (in_array($originHost, $allowedOrigins, true) === false) {
$this->sendAndClose('Host ' . $originHost . ' is not allowed to pass access control as origin.');
return false;
}
}

$originHost = $matches[1];
$allowedOrigins = $this->config->getOrigins();
if (in_array($originHost, $allowedOrigins, true) === false) {
$this->sendAndClose('Host ' . $originHost . ' is not allowed to pass access control as origin.');
return false;
}

return true;
}

/**
* @param string $msg
* @throws \Exception
*/
private function sendAndClose(string $msg)
private function sendAndClose(string $msg): void
{
$conn = new Connection($this->client);
$conn->send($msg);
Expand Down
29 changes: 15 additions & 14 deletions src/Components/ServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,73 @@ class ServerConfig
/**
* @var int
*/
private $clientsPerFork = WebSocketServerContract::CLIENTS_PER_FORK;
private int $clientsPerFork = WebSocketServerContract::CLIENTS_PER_FORK;

/**
* @var int
*/
private $streamSelectTimeout = WebSocketServerContract::STREAM_SELECT_TIMEOUT;
private int $streamSelectTimeout = WebSocketServerContract::STREAM_SELECT_TIMEOUT;

/**
* @var string
*/
private $host = WebSocketServerContract::DEFAULT_HOST;
private string $host = WebSocketServerContract::DEFAULT_HOST;

/**
* @var int
*/
private $port = WebSocketServerContract::DEFAULT_PORT;
private int $port = WebSocketServerContract::DEFAULT_PORT;

/**
* @var bool
*/
private $isForking = true;
private bool $isForking = true;

/**
* @var string
*/
private $processName = WebSocketServerContract::PROC_TITLE;
private string $processName = WebSocketServerContract::PROC_TITLE;

/**
* @var bool
*/
private $checkOrigin = false;
private bool $checkOrigin = false;

/**
* @var array
*/
private $origins = [];
private array $origins = [];


/**
* @var bool
*/
private $originHeader = false;
private bool $originHeader = false;

/**
* @var bool
*/
private $isSsl = false;
private bool $isSsl = false;

/**
* @var string
*/
private $localCert;
private string $localCert;

/**
* @var string
*/
private $localPk;
private string $localPk;

/**
* @var bool
*/
private $allowSelfSigned;
private bool $allowSelfSigned;

/**
* @var int
*/
private $cryptoType;
private int $cryptoType;

/**
* @return mixed
Expand Down Expand Up @@ -159,6 +159,7 @@ public function isForking(): bool

/**
* @param bool $isForking
* @return ServerConfig
*/
public function setForking(bool $isForking): ServerConfig
{
Expand Down
2 changes: 1 addition & 1 deletion src/Components/WSClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private function getPayloadData(string $data, int $payloadLength): string
* @throws ConnectionException
* @throws \Exception
*/
protected function receiveFragment()
protected function receiveFragment(): ?string
{
// Just read the main fragment information first.
$data = $this->read(2);
Expand Down
Loading

0 comments on commit 32167a1

Please sign in to comment.