diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/hourly.py | 18 | ||||
-rw-r--r-- | src/ztdns_db_connectivity.py | 9 |
2 files changed, 18 insertions, 9 deletions
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 |