diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-04-02 22:23:34 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-04-02 22:24:47 +0300 |
commit | 3c5f444dc8575e7f7c37adfb19a51040410ae395 (patch) | |
tree | c12f02f4d0b4f77c42e3770992aa96f307bb6f1d /lib |
Initial commit of the app
From https://github.com/nextcloud/server/pull/2095
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/IPWhitelistController.php | 123 | ||||
-rw-r--r-- | lib/Settings/IPWhitelist.php | 42 |
2 files changed, 165 insertions, 0 deletions
diff --git a/lib/Controller/IPWhitelistController.php b/lib/Controller/IPWhitelistController.php new file mode 100644 index 0000000..9b5183f --- /dev/null +++ b/lib/Controller/IPWhitelistController.php @@ -0,0 +1,123 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\BruteForceSettings\Controller; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\JSONResponse; +use OCP\IConfig; +use OCP\IRequest; + +class IPWhitelistController extends Controller { + + /** @var IConfig */ + private $config; + + /** + * IPWhitelistController constructor. + * + * @param string $appName + * @param IRequest $request + * @param IConfig $config + */ + public function __construct($appName, + IRequest $request, + IConfig $config) { + parent::__construct($appName, $request); + + $this->config = $config; + } + + /** + * @return JSONResponse + */ + public function getAll() { + $keys = $this->config->getAppKeys('bruteForce'); + $keys = array_filter($keys, function($key) { + $regex = '/^whitelist_/S'; + return preg_match($regex, $key) === 1; + }); + + $result = []; + + foreach ($keys as $key) { + $value = $this->config->getAppValue('bruteForce', $key); + $values = explode('/', $value); + + $result[] = [ + 'id' => (int)substr($key, 10), + 'ip' => $values[0], + 'mask' => $values[1], + ]; + } + + return new JSONResponse($result); + } + + /** + * @param string $ip + * @param int $mask + * @return JSONResponse + */ + public function add($ip, $mask) { + if (!filter_var($ip, FILTER_VALIDATE_IP) || + (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && ($mask < 0 || $mask > 32)) || + (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && ($mask < 0 || $mask > 128))) { + return new JSONResponse([], Http::STATUS_BAD_REQUEST); + } + + $keys = $this->config->getAppKeys('bruteForce'); + $keys = array_filter($keys, function($key) { + $regex = '/^whitelist_/S'; + return preg_match($regex, $key) === 1; + }); + + $id = 0; + foreach ($keys as $key) { + $tmp = (int)substr($key, 10); + if ($tmp > $id) { + $id = $tmp; + } + } + $id++; + + $value = $ip . '/' . $mask; + $this->config->setAppValue('bruteForce', 'whitelist_'.$id, $value); + return new JSONResponse([ + 'id' => $id, + 'ip' => $ip, + 'mask' => $mask, + ]); + } + + /** + * @param int $id + * @return JSONResponse + */ + public function remove($id) { + $this->config->deleteAppValue('bruteForce', 'whitelist_'.$id); + + return new JSONResponse([]); + } +} diff --git a/lib/Settings/IPWhitelist.php b/lib/Settings/IPWhitelist.php new file mode 100644 index 0000000..7e4aab2 --- /dev/null +++ b/lib/Settings/IPWhitelist.php @@ -0,0 +1,42 @@ +<?php +/** + * @copyright 2016, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\BruteForceSettings\Settings; + +use OCP\AppFramework\Http\TemplateResponse; +use OCP\Settings\ISettings; + +class IPWhitelist implements ISettings { + + public function getForm() { + return new TemplateResponse('bruteforcesettings', 'ipwhitelist'); + } + + public function getSection() { + return 'security'; + } + + public function getPriority() { + return 50; + } +} |