From 6b119f76190cb2f5d38f9fb52ec5b60e65d29cc4 Mon Sep 17 00:00:00 2001 From: Wojciech Kosior Date: Fri, 12 Jun 2020 11:51:12 +0200 Subject: [PATCH] make it possible to disable execution of installed 0tdns --- db_connection_config.yml | 3 ++- src/hourly.py | 18 +++++++++++++----- src/ztdns_db_connectivity.py | 9 +++++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/db_connection_config.yml b/db_connection_config.yml index a9170ae..abe2500 100644 --- a/db_connection_config.yml +++ b/db_connection_config.yml @@ -2,4 +2,5 @@ user: postgres password: postgres host: "127.0.0.1" port: "5432" -database: "ztdns" \ No newline at end of file +database: "ztdns" +enabled: no \ No newline at end of file diff --git a/src/hourly.py b/src/hourly.py index 6878804..2c87bee 100755 --- a/src/hourly.py +++ b/src/hourly.py @@ -6,7 +6,8 @@ from os import path from time import gmtime, strftime # our own module used by several scripts in the project -from ztdns_db_connectivity import start_db_connection, get_default_host_address +from ztdns_db_connectivity import start_db_connection, \ + get_default_host_address, get_ztdns_config wrapper = '/var/lib/0tdns/vpn_wrapper.sh' perform_queries = '/var/lib/0tdns/perform_queries.py' @@ -36,7 +37,11 @@ def get_vpn_connections(cursor, hour): ''') return cursor.fetchall() -connection = start_db_connection() +ztdns_config = get_ztdns_config() +if ztdns_config['enabled'] != 'yes': + exit() + +connection = start_db_connection(ztdns_config) cursor = connection.cursor() # round down to an hour - this datetime format is one @@ -55,6 +60,9 @@ connection.close() for vpn_id, config_hash in vpns: config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) - subprocess.run([wrapper, get_default_host_address, - get_default_host_address() + '/32', - config_path, perform_queries, hour, vpn_id]) + physical_ip = get_default_host_address(ztdns_config['database']) + route_through_veth = ztdns_config['database'] + "/32" + command_in_namespace = [perform_queries, hour, vpn_id] + + subprocess.run([wrapper, config_path, physical_ip, + route_through_veth] + command_in_namespace) diff --git a/src/ztdns_db_connectivity.py b/src/ztdns_db_connectivity.py index 86f9a44..34e1e00 100644 --- a/src/ztdns_db_connectivity.py +++ b/src/ztdns_db_connectivity.py @@ -3,8 +3,10 @@ import psycopg2 db_config_path = '/etc/0tdns/db_connection_config.yml' -def start_db_connection(): - config = yaml.safe_load(open(db_config_path, 'r')) +def get_ztdns_config(): + return yaml.safe_load(open(db_config_path, 'r')) + +def start_db_connection(config): connection = psycopg2.connect(user=config['user'], password=config['password'], host=config['host'], port=config['port'], database=config['database']) @@ -17,9 +19,8 @@ def start_db_connection(): # https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib def get_default_host_address(remote_address): import socket - config = yaml.safe_load(open(db_config_path, 'r')) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect((config['database'], 80)) + s.connect((remote_address, 80)) hostaddr = s.getsockname()[0] s.close() return hostaddr -- 2.11.4.GIT