Skip to content

Commit

Permalink
Merge branch 'master' of github.com:beyondcode/phunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jun 17, 2020
2 parents b5b18a2 + e870878 commit 6ab526d
Show file tree
Hide file tree
Showing 56 changed files with 184 additions and 311 deletions.
23 changes: 11 additions & 12 deletions app/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
use App\Logger\CliRequestLogger;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use function Ratchet\Client\connect;
use Ratchet\Client\WebSocket;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use function Ratchet\Client\connect;

class Client
{
Expand All @@ -26,7 +25,7 @@ class Client
/** @var CliRequestLogger */
protected $logger;

/** @var int */
/** @var int */
protected $connectionRetries = 0;

/** @var int */
Expand Down Expand Up @@ -73,7 +72,7 @@ public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''):
$deferred = new Deferred();
$promise = $deferred->promise();

$wsProtocol = $this->configuration->port() === 443 ? "wss" : "ws";
$wsProtocol = $this->configuration->port() === 443 ? 'wss' : 'ws';

connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [
'X-Expose-Control' => 'enabled',
Expand All @@ -85,7 +84,7 @@ public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''):

$connection->authenticate($sharedUrl, $subdomain);

$clientConnection->on('close', function() use ($deferred, $sharedUrl, $subdomain, $authToken) {
$clientConnection->on('close', function () use ($deferred, $sharedUrl, $subdomain, $authToken) {
$this->logger->error('Connection to server closed.');

$this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken);
Expand All @@ -106,7 +105,7 @@ public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''):
$connection->on('setMaximumConnectionLength', function ($data) {
$timeoutSection = $this->logger->getOutput()->section();

$this->loop->addPeriodicTimer(1, function() use ($data, $timeoutSection) {
$this->loop->addPeriodicTimer(1, function () use ($data, $timeoutSection) {
$this->timeConnected++;

$carbon = Carbon::createFromFormat('s', str_pad($data->length * 60 - $this->timeConnected, 2, 0, STR_PAD_LEFT));
Expand All @@ -117,7 +116,7 @@ public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''):
});

$connection->on('authenticated', function ($data) use ($deferred, $sharedUrl) {
$httpProtocol = $this->configuration->port() === 443 ? "https" : "http";
$httpProtocol = $this->configuration->port() === 443 ? 'https' : 'http';
$host = $this->configuration->host();

if ($httpProtocol !== 'https') {
Expand All @@ -134,13 +133,13 @@ public function connectToServer(string $sharedUrl, $subdomain, $authToken = ''):

$deferred->resolve($data);
});

}, function (\Exception $e) use ($deferred, $sharedUrl, $subdomain, $authToken) {
if ($this->connectionRetries > 0) {
$this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken);

return;
}
$this->logger->error("Could not connect to the server.");
$this->logger->error('Could not connect to the server.');
$this->logger->error($e->getMessage());

$this->exit($deferred);
Expand All @@ -153,7 +152,7 @@ protected function exit(Deferred $deferred)
{
$deferred->reject();

$this->loop->futureTick(function(){
$this->loop->futureTick(function () {
exit(1);
});
}
Expand All @@ -163,8 +162,8 @@ protected function retryConnectionOrExit(string $sharedUrl, $subdomain, $authTok
$this->connectionRetries++;

if ($this->connectionRetries <= static::MAX_CONNECTION_RETRIES) {
$this->loop->addTimer($this->connectionRetries, function() use ($sharedUrl, $subdomain, $authToken) {
$this->logger->info("Retrying connection ({$this->connectionRetries}/".static::MAX_CONNECTION_RETRIES.")");
$this->loop->addTimer($this->connectionRetries, function () use ($sharedUrl, $subdomain, $authToken) {
$this->logger->info("Retrying connection ({$this->connectionRetries}/".static::MAX_CONNECTION_RETRIES.')');

$this->connectToServer($sharedUrl, $subdomain, $authToken);
});
Expand Down
2 changes: 1 addition & 1 deletion app/Client/Connections/ControlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Client\Connections;

use App\Client\ProxyManager;
use Evenement\EventEmitterTrait;
use Ratchet\Client\WebSocket;
use Ratchet\ConnectionInterface;
use Evenement\EventEmitterTrait;
use Ratchet\RFC6455\Messaging\Message;

class ControlConnection
Expand Down
9 changes: 4 additions & 5 deletions app/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace App\Client;

use App\Client\Http\Controllers\AttachDataToLogController;
use App\Client\Http\Controllers\ClearLogsController;
use App\Client\Http\Controllers\CreateTunnelController;
use App\Client\Http\Controllers\PushLogsToDashboardController;
use App\Http\App;
use App\Client\Http\Controllers\AttachDataToLogController;
use App\Client\Http\Controllers\DashboardController;
use App\Client\Http\Controllers\LogController;
use App\Client\Http\Controllers\PushLogsToDashboardController;
use App\Client\Http\Controllers\ReplayLogController;
use App\Http\App;
use App\Http\RouteGenerator;
use App\WebSockets\Socket;
use Ratchet\WebSocket\WsServer;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as LoopFactory;
use React\EventLoop\LoopInterface;

class Factory
{
Expand Down Expand Up @@ -158,5 +158,4 @@ public function run()
{
$this->loop->run();
}

}
7 changes: 4 additions & 3 deletions app/Client/Http/Controllers/AttachDataToLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace App\Client\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Logger\RequestLogger;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use App\Logger\RequestLogger;
use Ratchet\ConnectionInterface;
use function GuzzleHttp\Psr7\str;

class AttachDataToLogController extends Controller
{
Expand All @@ -24,11 +24,12 @@ public function handle(Request $request, ConnectionInterface $httpConnection)
$loggedRequest = $this->requestLogger->findLoggedRequest($request->get('request_id', ''));

if (! is_null($loggedRequest)) {
$loggedRequest->setAdditionalData((array)$request->get('data', []));
$loggedRequest->setAdditionalData((array) $request->get('data', []));

$this->requestLogger->pushLoggedRequest($loggedRequest);

$httpConnection->send(str(new Response(200)));

return;
}

Expand Down
10 changes: 2 additions & 8 deletions app/Client/Http/Controllers/CreateTunnelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
namespace App\Client\Http\Controllers;

use App\Http\Controllers\Controller;
use Exception;
use App\WebSockets\Socket;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
use function GuzzleHttp\Psr7\str;
use Psr\Http\Message\RequestInterface;

class CreateTunnelController extends Controller
{

protected $keepConnectionOpen = true;

public function handle(Request $request, ConnectionInterface $httpConnection)
Expand All @@ -24,10 +19,9 @@ public function handle(Request $request, ConnectionInterface $httpConnection)
->then(function ($data) use ($httpConnection) {
$httpConnection->send(respond_json($data));
$httpConnection->close();
}, function() use ($httpConnection) {
}, function () use ($httpConnection) {
$httpConnection->send(str(new Response(500)));
$httpConnection->close();
});

}
}
4 changes: 0 additions & 4 deletions app/Client/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

use App\Client\Client;
use App\Http\Controllers\Controller;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request;
use function GuzzleHttp\Psr7\str;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;

class DashboardController extends Controller
{

public function handle(Request $request, ConnectionInterface $httpConnection)
{
$httpConnection->send(respond_html($this->getView($httpConnection, 'client.dashboard', [
Expand Down
3 changes: 0 additions & 3 deletions app/Client/Http/Controllers/LogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

use App\Http\Controllers\Controller;
use App\Logger\RequestLogger;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;
use function GuzzleHttp\Psr7\str;
use Psr\Http\Message\RequestInterface;

class LogController extends Controller
{
Expand Down
7 changes: 2 additions & 5 deletions app/Client/Http/Controllers/PushLogsToDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
namespace App\Client\Http\Controllers;

use App\Http\Controllers\Controller;
use Exception;
use App\WebSockets\Socket;
use Exception;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
use function GuzzleHttp\Psr7\str;
use Psr\Http\Message\RequestInterface;

class PushLogsToDashboardController extends Controller
{

public function handle(Request $request, ConnectionInterface $httpConnection)
{
try {
Expand Down
5 changes: 2 additions & 3 deletions app/Client/Http/Controllers/ReplayLogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use App\Client\Http\HttpClient;
use App\Http\Controllers\Controller;
use App\Http\QueryParameters;
use App\Logger\RequestLogger;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use Ratchet\ConnectionInterface;
use function GuzzleHttp\Psr7\str;
use Psr\Http\Message\RequestInterface;

class ReplayLogController extends Controller
{
Expand All @@ -32,6 +30,7 @@ public function handle(Request $request, ConnectionInterface $httpConnection)

if (is_null($loggedRequest)) {
$httpConnection->send(str(new Response(404)));

return;
}

Expand Down
16 changes: 7 additions & 9 deletions app/Client/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
use App\Client\Http\Modifiers\CheckBasicAuthentication;
use App\Logger\RequestLogger;
use Clue\React\Buzz\Browser;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Arr;
use function GuzzleHttp\Psr7\parse_request;
use function GuzzleHttp\Psr7\str;
use Laminas\Http\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Ratchet\Client\WebSocket;
use Ratchet\RFC6455\Messaging\Frame;
use React\EventLoop\LoopInterface;
use React\Socket\Connector;
use function GuzzleHttp\Psr7\parse_request;
use function GuzzleHttp\Psr7\str;

class HttpClient
{
Expand Down Expand Up @@ -67,13 +65,13 @@ protected function passRequestThroughModifiers(RequestInterface $request, ?WebSo

protected function createConnector(): Connector
{
return new Connector($this->loop, array(
return new Connector($this->loop, [
'dns' => '127.0.0.1',
'tls' => array(
'tls' => [
'verify_peer' => false,
'verify_peer_name' => false
)
));
'verify_peer_name' => false,
],
]);
}

protected function sendRequestToApplication(RequestInterface $request, $proxyConnection = null)
Expand Down
14 changes: 8 additions & 6 deletions app/Client/Http/Modifiers/CheckBasicAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use App\Client\Configuration;
use GuzzleHttp\Psr7\Response;
use function GuzzleHttp\Psr7\str;
use Illuminate\Support\Arr;
use Psr\Http\Message\RequestInterface;
use Ratchet\Client\WebSocket;
use function GuzzleHttp\Psr7\str;

class CheckBasicAuthentication
{
Expand All @@ -30,10 +30,11 @@ public function handle(RequestInterface $request, ?WebSocket $proxyConnection):
if (is_null($username)) {
$proxyConnection->send(
str(new Response(401, [
'WWW-Authenticate' => 'Basic realm=Expose'
'WWW-Authenticate' => 'Basic realm=Expose',
], 'Unauthorized'))
);
$proxyConnection->close();

return null;
}

Expand All @@ -49,7 +50,7 @@ protected function getAuthorizationUsername(RequestInterface $request): ?string
return null;
}

if (!array_key_exists($authorization['username'], $credentials)) {
if (! array_key_exists($authorization['username'], $credentials)) {
return null;
}

Expand All @@ -63,13 +64,13 @@ protected function getAuthorizationUsername(RequestInterface $request): ?string
protected function parseAuthorizationHeader(string $header)
{
if (strpos($header, 'Basic') !== 0) {
return null;
return;
}

$header = base64_decode(substr($header, 6));

if ($header === false) {
return null;
return;
}

$header = explode(':', $header, 2);
Expand All @@ -82,13 +83,14 @@ protected function parseAuthorizationHeader(string $header)

protected function requiresAuthentication(): bool
{
return !empty($this->getCredentials());
return ! empty($this->getCredentials());
}

protected function getCredentials()
{
try {
$credentials = explode(':', $this->configuration->auth());

return [
$credentials[0] => $credentials[1],
];
Expand Down
Loading

0 comments on commit 6ab526d

Please sign in to comment.