Allow specifying maximum connection counts per user

This commit is contained in:
Marcel Pociot
2021-05-28 16:51:48 +02:00
parent 717e8cf05c
commit a3d1735b6e
10 changed files with 164 additions and 73 deletions

View File

@@ -135,16 +135,10 @@ class TunnelTest extends TestCase
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Marcel',
'can_share_tcp_ports' => 0,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
$this->expectException(\UnexpectedValueException::class);
@@ -163,16 +157,10 @@ class TunnelTest extends TestCase
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Marcel',
'can_share_tcp_ports' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
/**
* We create an expose client that connects to our server and shares
@@ -213,16 +201,10 @@ class TunnelTest extends TestCase
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Marcel',
'can_specify_subdomains' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
$this->createTestHttpServer();
@@ -241,16 +223,10 @@ class TunnelTest extends TestCase
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Marcel',
'can_specify_subdomains' => 0,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
$this->createTestHttpServer();
@@ -269,16 +245,10 @@ class TunnelTest extends TestCase
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Marcel',
'can_specify_subdomains' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/subdomains', [
'Host' => 'expose.localhost',
@@ -289,16 +259,10 @@ class TunnelTest extends TestCase
'auth_token' => $user->auth_token,
])));
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Test-User',
'can_specify_subdomains' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
$this->createTestHttpServer();
@@ -318,16 +282,10 @@ class TunnelTest extends TestCase
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
$user = $this->createUser([
'name' => 'Marcel',
'can_specify_subdomains' => 1,
])));
$user = json_decode($response->getBody()->getContents())->user;
]);
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/subdomains', [
'Host' => 'expose.localhost',
@@ -350,20 +308,11 @@ class TunnelTest extends TestCase
}
/** @test */
public function it_allows_clients_to_use_random_subdomains_if_custom_subdomains_are_forbidden()
public function it_rejects_clients_with_too_many_connections()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode([
'name' => 'Marcel',
'can_specify_subdomains' => 0,
])));
$user = json_decode($response->getBody()->getContents())->user;
$this->expectException(\UnexpectedValueException::class);
$this->app['config']['expose.admin.validate_auth_tokens'] = false;
$this->app['config']['expose.admin.maximum_open_connections_per_user'] = 1;
$this->createTestHttpServer();
@@ -372,9 +321,51 @@ class TunnelTest extends TestCase
* the created test HTTP server.
*/
$client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', '', $user->auth_token));
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel-1'));
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel-2'));
}
$this->assertInstanceOf(\stdClass::class, $response);
/** @test */
public function it_rejects_users_with_custom_max_connection_limit()
{
$this->expectException(\UnexpectedValueException::class);
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$this->app['config']['expose.admin.maximum_open_connections_per_user'] = 5;
$user = $this->createUser([
'name' => 'Marcel',
'can_specify_subdomains' => 1,
'max_connections' => 2,
]);
$this->createTestHttpServer();
/**
* We create an expose client that connects to our server and shares
* the created test HTTP server.
*/
$client = $this->createClient();
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel-1', $user->auth_token));
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel-2', $user->auth_token));
$this->await($client->connectToServer('127.0.0.1:8085', 'tunnel-3', $user->auth_token));
}
/** @test */
public function it_allows_clients_to_use_random_subdomains_if_custom_subdomains_are_forbidden()
{
$this->app['config']['expose.admin.validate_auth_tokens'] = true;
$user = $this->createUser([
'name' => 'Marcel',
'can_specify_subdomains' => 0,
]);
$this->createTestHttpServer();
$client = $this->createClient();
$response = $this->await($client->connectToServer('127.0.0.1:8085', 'foo', $user->auth_token));
$this->assertNotSame('foo', $response->subdomain);
}
protected function startServer()
@@ -405,6 +396,17 @@ class TunnelTest extends TestCase
return app(Client::class);
}
protected function createUser(array $data)
{
$response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [
'Host' => 'expose.localhost',
'Authorization' => base64_encode('username:secret'),
'Content-Type' => 'application/json',
], json_encode($data)));
return json_decode($response->getBody()->getContents())->user;
}
protected function createTestHttpServer()
{
$server = new Server($this->loop, function (ServerRequestInterface $request) {