diff options
Diffstat (limited to 'src/ztdnslib.py')
-rw-r--r-- | src/ztdnslib.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/ztdnslib.py b/src/ztdnslib.py new file mode 100644 index 0000000..34e1e00 --- /dev/null +++ b/src/ztdnslib.py @@ -0,0 +1,26 @@ +import yaml +import psycopg2 + +db_config_path = '/etc/0tdns/db_connection_config.yml' + +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']) + # we might later decide that each user of start_db_connection() + # should set it themselves - but for now, set it here + connection.autocommit = True + return connection + +# we'll use it for setting SNAT +# https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib +def get_default_host_address(remote_address): + import socket + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect((remote_address, 80)) + hostaddr = s.getsockname()[0] + s.close() + return hostaddr |