This commit is contained in:
Marcel Pociot
2020-04-22 12:32:29 +02:00
parent 1f8865145f
commit e5e53b8b68
41 changed files with 1593 additions and 915 deletions

View File

@@ -2,10 +2,23 @@
namespace App\Server;
use App\Contracts\ConnectionManager as ConnectionManagerContract;
use App\Contracts\SubdomainGenerator;
use App\HttpServer\HttpServer;
use App\Server\Connections\ConnectionManager;
use App\Server\Http\Controllers\ControlMessageController;
use App\Server\Http\Controllers\TunnelMessageController;
use App\Server\SubdomainGenerator\RandomSubdomainGenerator;
use Ratchet\Http\Router;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory as LoopFactory;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class Factory
{
@@ -54,15 +67,65 @@ class Factory
return $this;
}
protected function getRoutes(): RouteCollection
{
$routes = new RouteCollection();
$routes->add('control',
new Route('/__expose_control__', [
'_controller' => new WsServer(app(ControlMessageController::class))
], [], [], null, [], []
)
);
$routes->add('tunnel',
new Route('/{__catchall__}', [
'_controller' => app(TunnelMessageController::class),
], [
'__catchall__' => '.*'
]));
return $routes;
}
protected function bindConfiguration()
{
app()->singleton(Configuration::class, function ($app) {
return new Configuration($this->hostname, $this->port);
});
}
protected function bindSubdomainGenerator()
{
app()->singleton(SubdomainGenerator::class, function ($app) {
return $app->make(RandomSubdomainGenerator::class);
});
}
protected function bindConnectionManager()
{
app()->singleton(ConnectionManagerContract::class, function ($app) {
return $app->make(ConnectionManager::class);
});
}
public function createServer()
{
$socket = new Server("{$this->host}:{$this->port}", $this->loop);
$connectionManager = new ConnectionManager($this->hostname, $this->port);
$this->bindConfiguration();
$app = new Expose($connectionManager);
$this->bindSubdomainGenerator();
return new IoServer($app, $socket, $this->loop);
$this->bindConnectionManager();
$urlMatcher = new UrlMatcher($this->getRoutes(), new RequestContext);
$router = new Router($urlMatcher);
$http = new HttpServer($router);
return new IoServer($http, $socket, $this->loop);
}
}