route database connection to bypass vpn
[0tDNS.git] / src / ztdns_db_connectivity.py
blob86f9a4458d01780786e373d245439112d79da615
1 import yaml
2 import psycopg2
4 db_config_path = '/etc/0tdns/db_connection_config.yml'
6 def start_db_connection():
7 config = yaml.safe_load(open(db_config_path, 'r'))
8 connection = psycopg2.connect(user=config['user'], password=config['password'],
9 host=config['host'], port=config['port'],
10 database=config['database'])
11 # we might later decide that each user of start_db_connection()
12 # should set it themselves - but for now, set it here
13 connection.autocommit = True
14 return connection
16 # we'll use it for setting SNAT
17 # https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
18 def get_default_host_address(remote_address):
19 import socket
20 config = yaml.safe_load(open(db_config_path, 'r'))
21 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
22 s.connect((config['database'], 80))
23 hostaddr = s.getsockname()[0]
24 s.close()
25 return hostaddr