Commit cd1b93a4 authored by Vitaly Lipatov's avatar Vitaly Lipatov

redis: configure port and secure autoban CLI

parent 4eb6d00b
...@@ -5,6 +5,7 @@ session_start(); ...@@ -5,6 +5,7 @@ session_start();
$ip = $_SERVER['REMOTE_ADDR'] ?? ''; $ip = $_SERVER['REMOTE_ADDR'] ?? '';
$settings = parse_ini_file('/etc/eterban/settings.ini'); $settings = parse_ini_file('/etc/eterban/settings.ini');
$host_redis = is_array($settings) ? ($settings['redis_server'] ?? '') : ''; $host_redis = is_array($settings) ? ($settings['redis_server'] ?? '') : '';
$port_redis = is_array($settings) ? (int)($settings['redis_port'] ?? 6379) : 6379;
$redis_username = is_array($settings) ? ($settings['redis_username'] ?? '') : ''; $redis_username = is_array($settings) ? ($settings['redis_username'] ?? '') : '';
$redis_password = is_array($settings) ? ($settings['redis_password'] ?? '') : ''; $redis_password = is_array($settings) ? ($settings['redis_password'] ?? '') : '';
$redis_tls = is_array($settings) && filter_var($settings['redis_tls'] ?? false, FILTER_VALIDATE_BOOLEAN); $redis_tls = is_array($settings) && filter_var($settings['redis_tls'] ?? false, FILTER_VALIDATE_BOOLEAN);
...@@ -64,7 +65,7 @@ if (!is_string($nonce) || !is_string($proof) || time() > $expires || ...@@ -64,7 +65,7 @@ if (!is_string($nonce) || !is_string($proof) || time() > $expires ||
try { try {
$redis = new Redis(); $redis = new Redis();
$redis_host = $redis_tls ? 'tls://' . $host_redis : $host_redis; $redis_host = $redis_tls ? 'tls://' . $host_redis : $host_redis;
if (!$redis->connect($redis_host, 6379, 2.5, null, 0, 0, $redis_tls ? ['stream' => ['verify_peer' => true]] : [])) { if (!$redis->connect($redis_host, $port_redis, 2.5, null, 0, 0, $redis_tls ? ['stream' => ['verify_peer' => true]] : [])) {
throw new RedisException('connection failed'); throw new RedisException('connection failed');
} }
if ($redis_password !== '' && !$redis->auth($redis_username !== '' ? [$redis_username, $redis_password] : $redis_password)) { if ($redis_password !== '' && !$redis->auth($redis_username !== '' ? [$redis_username, $redis_password] : $redis_password)) {
......
[Settings] [Settings]
# blocking requests queue # blocking requests queue
#redis_server = 10.20.30.101 #redis_server = 10.20.30.101
#redis_port = 6379
# Optional Redis ACL/TLS settings. Keep this file root-readable only when a # Optional Redis ACL/TLS settings. Keep this file root-readable only when a
# password is configured. # password is configured.
#redis_username = default #redis_username = default
......
...@@ -46,7 +46,16 @@ def get_redis(): ...@@ -46,7 +46,16 @@ def get_redis():
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_PATH) config.read(CONFIG_PATH)
redis_server = config.get('Settings', 'redis_server', fallback='localhost') redis_server = config.get('Settings', 'redis_server', fallback='localhost')
return redis.Redis(host=redis_server) options = {'port': config.getint('Settings', 'redis_port', fallback=6379)}
username = config.get('Settings', 'redis_username', fallback='').strip()
password = config.get('Settings', 'redis_password', fallback='')
if username:
options['username'] = username
if password:
options['password'] = password
if config.getboolean('Settings', 'redis_tls', fallback=False):
options['ssl'] = True
return redis.Redis(host=redis_server, **options)
def cmd_info(ip): def cmd_info(ip):
......
...@@ -109,7 +109,7 @@ def parse_config (path_to_config, path_to_log): ...@@ -109,7 +109,7 @@ def parse_config (path_to_config, path_to_log):
def redis_connection_options(config): def redis_connection_options(config):
options = {} options = {'port': config.getint('Settings', 'redis_port', fallback=6379)}
username = config.get('Settings', 'redis_username', fallback='').strip() username = config.get('Settings', 'redis_username', fallback='').strip()
password = config.get('Settings', 'redis_password', fallback='') password = config.get('Settings', 'redis_password', fallback='')
if username: if username:
......
...@@ -18,7 +18,7 @@ def get_settings (path_to_config): ...@@ -18,7 +18,7 @@ def get_settings (path_to_config):
# Читаем некоторые значения из конфиг. файла. # Читаем некоторые значения из конфиг. файла.
redis_server = config.get("Settings", "redis_server", fallback = "localhost") redis_server = config.get("Settings", "redis_server", fallback = "localhost")
hostname = config.get("Settings", "hostname", fallback = socket.gethostname()) hostname = config.get("Settings", "hostname", fallback = socket.gethostname())
options = {} options = {'port': config.getint("Settings", "redis_port", fallback=6379)}
username = config.get("Settings", "redis_username", fallback="").strip() username = config.get("Settings", "redis_username", fallback="").strip()
password = config.get("Settings", "redis_password", fallback="") password = config.get("Settings", "redis_password", fallback="")
if username: if username:
......
...@@ -24,7 +24,7 @@ def get_settings (path_to_config): ...@@ -24,7 +24,7 @@ def get_settings (path_to_config):
# Читаем некоторые значения из конфиг. файла. # Читаем некоторые значения из конфиг. файла.
redis_server = config.get("Settings", "redis_server", fallback = "localhost") redis_server = config.get("Settings", "redis_server", fallback = "localhost")
hostname = config.get("Settings", "hostname", fallback = socket.gethostname()) hostname = config.get("Settings", "hostname", fallback = socket.gethostname())
options = {} options = {'port': config.getint("Settings", "redis_port", fallback=6379)}
username = config.get("Settings", "redis_username", fallback="").strip() username = config.get("Settings", "redis_username", fallback="").strip()
password = config.get("Settings", "redis_password", fallback="") password = config.get("Settings", "redis_password", fallback="")
if username: if username:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment