From b4a74e68bd4ce03da6d9240e1de22a696e371506 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Tue, 14 Nov 2023 23:28:46 +0300 Subject: [PATCH] initial commit --- .gitignore | 2 + LICENSE | 17 + README.md | 136 ++++ composer.json | 21 + composer.lock | 618 ++++++++++++++++++ src/Exceptions/OutlineApiException.php | 8 + src/Exceptions/OutlineException.php | 11 + src/Exceptions/OutlineKeyException.php | 8 + .../OutlineKeyNotFoundException.php | 8 + src/Outline.php | 46 ++ src/OutlineApiClient.php | 136 ++++ src/OutlineKey.php | 304 +++++++++ 12 files changed, 1315 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/Exceptions/OutlineApiException.php create mode 100644 src/Exceptions/OutlineException.php create mode 100644 src/Exceptions/OutlineKeyException.php create mode 100644 src/Exceptions/OutlineKeyNotFoundException.php create mode 100644 src/Outline.php create mode 100644 src/OutlineApiClient.php create mode 100644 src/OutlineKey.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7f8ac3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +/vendor/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7601188 --- /dev/null +++ b/LICENSE @@ -0,0 +1,17 @@ +Copyright (c) 2022 Roman Grinko +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fca89c1 --- /dev/null +++ b/README.md @@ -0,0 +1,136 @@ +# Outline API Client + +Simple API client for [OutlineVPN](https://getoutline.org/ru/). + +## Installation + +Install the latest version with + +```bash +$ composer require rsgrinko/outline-api-client +``` + +## Requirements + +PHP >= 7.4 + +## How to use + +### Usage API Client + +```php + +require 'vendor/autoload.php'; + +use OutlineApiClient\OutlineApiClient; + +try { + + // Your Outline server address + $serverUrl = 'https://127.0.0.1:3333/YZwl3D1r-B6cNYzQ'; + + $api = new OutlineApiClient($serverUrl); + + // Get an array of all server keys + $keysList = $api->getKeys(); + + // Create new key + $key = $api->create(); + + // Rename exist key. + // Passing key id and new name + $api->setName($key['id'], 'New key name'); + + // Set transfer data limit for key. + // Passing key id and limit in bytes. + // In the example set 5MB + $api->setLimit($key['id'], 5 * 1024 * 1024); + + // Remove key limit + // Passing key id + $api->deleteLimit($key['id']); + + // Delete key + $api->delete($key['id']); + + // Get an array of used traffic for all keys + $transferData = $api->metricsTransfer(); +} catch (\Exception $e) { + // Handle exception +} +``` + +### Usage OutlineVPN key wrapper + +Interaction with an existing key + +```php +load($keyId); + + // Get key id + $key->getId(); + + // Get key name + $key->getName(); + + // Get key transfer traffic + $key->getTransfer(); + + // Get access link + $key->getAccessUrl(); + + // Rename exist key. + // Passing key id and new name + $key->rename('New name'); + + // Set transfer data limit for key. + // Passing limit in bytes. + // In the example set 5MB + $key->limit(5 * 1024 * 1024); + + // Remove key traffic limit + $key->deleteLimit(); + + // Delete key + $key->delete(); + +} catch (\Exception $e) { + // Handle exception +} + +``` + +Creating a new key on the server + +```php +create('Key name', 5 * 1024 * 1024); + +} catch (\Exception $e) { + +} +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2ba537e --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "rsgrinko/outline-api-client", + "description": "Simple API client for Outline", + "keywords": ["outline","vpn","outline php"], + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Roman Grinko", + "email": "rsgrinko@gmail.com" + } + ], + "autoload": { + "psr-4": {"Outline\\OutlineApiClient\\": "src/"} + }, + "require": { + "php": ">=7.4", + "ext-json": "*", + "guzzlehttp/guzzle": "^7.0" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..95303dc --- /dev/null +++ b/composer.lock @@ -0,0 +1,618 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "618add5f4b4ef9475eb4445ee73e6771", + "packages": [ + { + "name": "guzzlehttp/guzzle", + "version": "7.8.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "1110f66a6530a40fe7aea0378fe608ee2b2248f9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/1110f66a6530a40fe7aea0378fe608ee2b2248f9", + "reference": "1110f66a6530a40fe7aea0378fe608ee2b2248f9", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/promises": "^1.5.3 || ^2.0.1", + "guzzlehttp/psr7": "^1.9.1 || ^2.5.1", + "php": "^7.2.5 || ^8.0", + "psr/http-client": "^1.0", + "symfony/deprecation-contracts": "^2.2 || ^3.0" + }, + "provide": { + "psr/http-client-implementation": "1.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.1", + "ext-curl": "*", + "php-http/client-integration-tests": "dev-master#2c025848417c1135031fdf9c728ee53d0a7ceaee as 3.0.999", + "php-http/message-factory": "^1.1", + "phpunit/phpunit": "^8.5.29 || ^9.5.23", + "psr/log": "^1.1 || ^2.0 || ^3.0" + }, + "suggest": { + "ext-curl": "Required for CURL handler support", + "ext-intl": "Required for Internationalized Domain Name (IDN) support", + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Jeremy Lindblom", + "email": "jeremeamia@gmail.com", + "homepage": "https://github.com/jeremeamia" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "psr-18", + "psr-7", + "rest", + "web service" + ], + "support": { + "issues": "https://github.com/guzzle/guzzle/issues", + "source": "https://github.com/guzzle/guzzle/tree/7.8.0" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle", + "type": "tidelift" + } + ], + "time": "2023-08-27T10:20:53+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "111166291a0f8130081195ac4556a5587d7f1b5d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/111166291a0f8130081195ac4556a5587d7f1b5d", + "reference": "111166291a0f8130081195ac4556a5587d7f1b5d", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.1", + "phpunit/phpunit": "^8.5.29 || ^9.5.23" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "support": { + "issues": "https://github.com/guzzle/promises/issues", + "source": "https://github.com/guzzle/promises/tree/2.0.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", + "type": "tidelift" + } + ], + "time": "2023-08-03T15:11:55+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "2.6.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "be45764272e8873c72dbe3d2edcfdfcc3bc9f727" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/be45764272e8873c72dbe3d2edcfdfcc3bc9f727", + "reference": "be45764272e8873c72dbe3d2edcfdfcc3bc9f727", + "shasum": "" + }, + "require": { + "php": "^7.2.5 || ^8.0", + "psr/http-factory": "^1.0", + "psr/http-message": "^1.1 || ^2.0", + "ralouphie/getallheaders": "^3.0" + }, + "provide": { + "psr/http-factory-implementation": "1.0", + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.8.1", + "http-interop/http-factory-tests": "^0.9", + "phpunit/phpunit": "^8.5.29 || ^9.5.23" + }, + "suggest": { + "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" + }, + "type": "library", + "extra": { + "bamarni-bin": { + "bin-links": true, + "forward-command": false + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://sagikazarmark.hu" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "support": { + "issues": "https://github.com/guzzle/psr7/issues", + "source": "https://github.com/guzzle/psr7/tree/2.6.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", + "type": "tidelift" + } + ], + "time": "2023-08-27T10:13:57+00:00" + }, + { + "name": "psr/http-client", + "version": "1.0.3", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-client.git", + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-client/zipball/bb5906edc1c324c9a05aa0873d40117941e5fa90", + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0", + "psr/http-message": "^1.0 || ^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP clients", + "homepage": "https://github.com/php-fig/http-client", + "keywords": [ + "http", + "http-client", + "psr", + "psr-18" + ], + "support": { + "source": "https://github.com/php-fig/http-client" + }, + "time": "2023-09-23T14:17:50+00:00" + }, + { + "name": "psr/http-factory", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-factory.git", + "reference": "e616d01114759c4c489f93b099585439f795fe35" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-factory/zipball/e616d01114759c4c489f93b099585439f795fe35", + "reference": "e616d01114759c4c489f93b099585439f795fe35", + "shasum": "" + }, + "require": { + "php": ">=7.0.0", + "psr/http-message": "^1.0 || ^2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interfaces for PSR-7 HTTP message factories", + "keywords": [ + "factory", + "http", + "message", + "psr", + "psr-17", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-factory/tree/1.0.2" + }, + "time": "2023-04-10T20:10:41+00:00" + }, + { + "name": "psr/http-message", + "version": "2.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/402d35bcb92c70c026d1a6a9883f06b2ead23d71", + "reference": "402d35bcb92c70c026d1a6a9883f06b2ead23d71", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-message/tree/2.0" + }, + "time": "2023-04-04T09:54:51+00:00" + }, + { + "name": "ralouphie/getallheaders", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "120b605dfeb996808c31b6477290a714d356e822" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", + "reference": "120b605dfeb996808c31b6477290a714d356e822", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5 || ^6.5" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "support": { + "issues": "https://github.com/ralouphie/getallheaders/issues", + "source": "https://github.com/ralouphie/getallheaders/tree/develop" + }, + "time": "2019-03-08T08:55:37+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "v2.5.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/e8b495ea28c1d97b5e0c121748d6f9b53d075c66", + "reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.5-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.2" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-01-02T09:53:40+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=7.4", + "ext-json": "*" + }, + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/src/Exceptions/OutlineApiException.php b/src/Exceptions/OutlineApiException.php new file mode 100644 index 0000000..00514c6 --- /dev/null +++ b/src/Exceptions/OutlineApiException.php @@ -0,0 +1,8 @@ +clientObject = new OutlineApiClient($url); + $this->keyObject = new OutlineKey($url); + } + + /** + * Get OutlineApiClient object + * + * @return OutlineApiClient + */ + public function getClientObject(): OutlineApiClient + { + return $this->clientObject; + } + + /** + * Get OutlineKey object + * + * @param int $id Key ID + * @throws OutlineException + */ + public function getKeyObject(int $id): OutlineKey + { + return $this->keyObject->load($id); + } + + + } \ No newline at end of file diff --git a/src/OutlineApiClient.php b/src/OutlineApiClient.php new file mode 100644 index 0000000..fd6e84f --- /dev/null +++ b/src/OutlineApiClient.php @@ -0,0 +1,136 @@ +serverUrl = $url; + } + + protected function makeRequestUrl($uri): string + { + return $this->serverUrl . $uri; + } + + /** + * @throws OutlineApiException + */ + protected function request($uri, $method = 'GET', $data = []): \Psr\Http\Message\ResponseInterface + { + $requestUrl = $this->makeRequestUrl($uri); + try { + $client = new Client([ + 'verify' => false, + ]); + + $requestData = []; + + if ($method === 'GET') { + $requestData['query'] = $data; + } else { + if (!empty($data)) { + $requestData = [ + RequestOptions::JSON => $data, + ]; + } + } + + return $client->request($method, $requestUrl, $requestData); + } catch (ClientException|GuzzleException $e) { + throw new OutlineApiException('Error sending request. Detail: ' . $e->getMessage()); + } + } + + /** + * @throws OutlineApiException + */ + public function getKeys() + { + $response = $this->request('/access-keys/'); + + return json_decode($response->getBody()->getContents(), true); + } + + /** + * @throws OutlineApiException + */ + public function create() + { + $response = $this->request('/access-keys/', 'POST'); + + if ($response->getStatusCode() === 201) { + return json_decode($response->getBody()->getContents(), true); + } else { + return false; + } + } + + /** + * @throws OutlineApiException + */ + public function delete($keyId): bool + { + $response = $this->request('/access-keys/' . $keyId . '', 'DELETE'); + + return $response->getStatusCode() === 204; + } + + /** + * @throws OutlineApiException + */ + public function metricsTransfer() + { + $response = $this->request('/metrics/transfer'); + + return json_decode($response->getBody()->getContents(), true); + } + + /** + * @throws OutlineApiException + */ + public function setName($keyId, $name): bool + { + $response = $this->request('/access-keys/' . $keyId . '/name', 'PUT', ['name' => $name]); + + return $response->getStatusCode() === 204; + } + + /** + * @throws OutlineApiException + */ + public function setLimit($keyId, $limit = 0): bool + { + $response = $this->request('/access-keys/' . $keyId . '/data-limit', 'PUT', ['limit' => ['bytes' => $limit]]); + + return $response->getStatusCode() === 204; + } + + /** + * @throws OutlineApiException + */ + public function deleteLimit($keyId): bool + { + $response = $this->request('/access-keys/' . $keyId . '/data-limit', 'DELETE'); + return $response->getStatusCode() === 204; + } + } diff --git a/src/OutlineKey.php b/src/OutlineKey.php new file mode 100644 index 0000000..2b7924c --- /dev/null +++ b/src/OutlineKey.php @@ -0,0 +1,304 @@ + -1, + 'name' => '', + 'password' => '', + 'port' => -1, + 'method' => '', + 'accessUrl' => '', + ]; + + /** @var bool $isLoaded Flag is key loaded */ + protected bool $isLoaded = false; + + + /** + * @throws OutlineApiException + */ + public function __construct(string $server) + { + $this->api = new OutlineApiClient($server); + } + + /** + * Set key data + * + * @param array $setData Key data + */ + protected function setData(array $setData): void + { + $this->data = array_merge($this->data, $setData); + } + + /** + * Get key data + * + * @return array Key data + */ + public function getData(): array + { + return $this->data; + } + + /** + * Checking is key loaded + * + * @return bool + */ + protected function isLoaded(): bool + { + return $this->isLoaded; + } + + /** + * Get key + * + * @param int $keyId Key ID + * + * @throws OutlineException + */ + public function get(int $keyId, string $searchKey = 'id'): array + { + $getKeyList = $this->api->getKeys(); + $findKeyData = []; + + if (!empty($getKeyList)) { + $list = $getKeyList['accessKeys']; + + foreach ($list as $item) { + if ($keyId === (int)$item[$searchKey]) { + $findKeyData = $item; + break; + } + } + + if (empty($findKeyData)) { + throw new OutlineKeyNotFoundException('Key not found.'); + } + } else { + throw new OutlineKeyException('Not transferred keys list'); + } + + return $findKeyData; + } + + + /** + * Get key by name + * + * @param string $name Name + * @throws OutlineException + */ + public function getByName(string $name): array + { + return $this->get($name, 'name'); + } + + /** + * Load key + * + * @param int $keyId Key ID + * @throws OutlineException + */ + public function load(int $keyId): OutlineKey + { + $data = $this->get($keyId); + $this->setData($data); + $this->isLoaded = true; + + return $this; + } + + /** + * Get key ID + * + * @return int + */ + public function getId(): int + { + return (int)$this->data['id']; + } + + /** + * Get key name + * + * @return string + */ + public function getName(): string + { + return $this->data['name']; + } + + + /** + * Get transfer stat + * + * @throws OutlineException + */ + public function getTransfer(): int + { + $transfer = 0; + + $transferData = $this->api->metricsTransfer(); + + if (isset($transferData['bytesTransferredByUserId']) + && array_key_exists($this->getId(), $transferData['bytesTransferredByUserId'])) { + $transfer = $transferData['bytesTransferredByUserId'][$this->getId()]; + } + + return (int)$transfer; + } + + /** + * Get traffic limit + * + * @return int + */ + public function getLimit(): int + { + return (int)$this->data['dataLimit']['bytes']; + } + + /** + * Get access url + * + * @return string + */ + public function getAccessUrl(): string + { + return $this->data['accessUrl']; + } + + + /** + * @throws OutlineException + */ + public function rename($newName) + { + if ($this->isLoaded()) { + $setName = $this->api->setName($this->getId(), $newName); + if (!$setName) { + throw new OutlineKeyException('Rename key error'); + } else { + $this->setData(['name' => $newName]); + } + } else { + throw new OutlineKeyException('Failed rename key. Need load data key.'); + } + } + + /** + * @throws OutlineKeyException + * @throws OutlineApiException + */ + public function limit($limitValue) + { + if ($this->isLoaded()) { + $setLimit = $this->api->setLimit($this->getId(), $limitValue); + + if (!$setLimit) { + throw new OutlineKeyException('Error set limit. Please contact administrator'); + } else { + $this->setData([ + 'dataLimit' => [ + 'bytes' => $limitValue, + ], + ]); + } + } else { + throw new OutlineKeyException('Failed set limit for key. Need load data key'); + } + } + + /** + * @throws OutlineKeyException + * @throws OutlineApiException + */ + public function deleteLimit() + { + if ($this->isLoaded()) { + $deleteLimit = $this->api->delete($this->getId()); + + if (!$deleteLimit) { + throw new OutlineKeyException('Error delete key limit'); + } else { + $this->setData([ + 'dataLimit' => [], + ]); + } + } else { + throw new OutlineKeyException('Failed delete limit for key. Need load data key'); + } + } + + /** + * @throws OutlineKeyException + * @throws OutlineApiException + */ + public function create(string $name, ?int $limit = null): OutlineKey + { + if (!empty($name)) { + $create = $this->api->create(); + + if (!empty($create)) { + $this->setData($create); + + $setName = $this->api->setName($create['id'], $name); + + if ($setName) { + $this->setData(['name' => $name]); + + if ($limit !== null) { + $setLimit = $this->api->setLimit($create['id'], $limit); + + if ($setLimit) { + $this->setData( + [ + 'dataLimit' => [ + 'bytes' => $limit, + ], + ] + ); + } else { + throw new OutlineKeyException('Error set limit key'); + } + } + } else { + throw new OutlineKeyException('Error set key name'); + } + } else { + throw new OutlineKeyException('Error create key'); + } + } + + return $this; + } + + + /** + * @throws OutlineKeyException + * @throws OutlineApiException + */ + public function delete(): bool + { + if ($this->api->delete($this->getId())) { + return true; + } else { + throw new OutlineKeyException('Error delete key id=' . $this->getId()); + } + } + } \ No newline at end of file