update readme
[0tDNS.git] / src / hourly.py
blob34097a329c5dff6ae64f920a7a21061809b42d89
1 #!/bin/python3
3 from sys import argv
4 import subprocess
5 from os import path
7 # our own module used by several scripts in the project
8 from ztdns_db_connectivity import start_db_connection
10 wrapper = '/var/lib/0tdns/vpn_wrapper.sh'
11 perform_queries = '/var/lib/0tdns/perform_queries.py'
13 def sync_ovpn_config(cursor, vpn_id, config_path, config_hash):
14 cursor.execute('''
15 select ovpn_config
16 from user_side_vpn
17 where id = %s and ovpn_config_sha256 = %s
18 ''', (vpn_id, config_hash))
20 (config_contents,) = cursor.fetchone()
22 with open(config_path, "w") as config_file:
23 config_file.write(config_contents)
25 def get_vpn_connections(cursor, hour):
26 # return (
27 # # vpn_id | config_path
28 # (14, "./vpngate_178.254.251.12_udp_1195.ovpn"),
29 # (13, "./vpngate_public-vpn-229.opengw.net_tcp_443.ovpn")
30 # )
31 cursor.execute('''
32 select v.id, v.ovpn_config_sha256
33 from user_side_queries as q join user_side_vpn as v
34 on v.id = q.vpn_id;
35 ''')
36 return cursor.fetchall()
38 connection = start_db_connection()
39 cursor = connection.cursor()
41 hour = argv[1]
42 vpns = get_vpn_connections(cursor, hour)
44 for vpn_id, config_hash in vpns:
45 config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash)
46 if not path.isfile(config_path):
47 sync_ovpn_config(cursor, vpn_id, config_path, config_hash)
49 cursor.close()
50 connection.close()
52 for vpn_id, config_hash in vpns:
53 config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash)
54 subprocess.run([wrapper, config_path, perform_queries, hour, vpn_id])