aboutsummaryrefslogtreecommitdiff
path: root/src/hourly.py
blob: 68788044c5ff2f9e4c807b8c83c3ba4730c3c350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/python3

from sys import argv
import subprocess
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

wrapper = '/var/lib/0tdns/vpn_wrapper.sh'
perform_queries = '/var/lib/0tdns/perform_queries.py'

def sync_ovpn_config(cursor, vpn_id, config_path, config_hash):
    cursor.execute('''
    select ovpn_config
    from user_side_vpn
    where id = %s and ovpn_config_sha256 = %s
    ''', (vpn_id, config_hash))
    
    (config_contents,) = cursor.fetchone()
    
    with open(config_path, "w") as config_file:
        config_file.write(config_contents)

def get_vpn_connections(cursor, hour):
    # return (
    #     # vpn_id | config_path
    #     (14,       "./vpngate_178.254.251.12_udp_1195.ovpn"),
    #     (13,       "./vpngate_public-vpn-229.opengw.net_tcp_443.ovpn")
    # )
    cursor.execute('''
    select v.id, v.ovpn_config_sha256
    from user_side_queries as q join user_side_vpn as v
    on v.id = q.vpn_id;
    ''')
    return cursor.fetchall()

connection = start_db_connection()
cursor = connection.cursor()

# round down to an hour - this datetime format is one
# of the formats accepted by postgres
hour = strftime('%Y-%m-%d %H:00', gmtime())

vpns = get_vpn_connections(cursor, hour)

for vpn_id, config_hash in vpns:
    config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash)
    if not path.isfile(config_path):
        sync_ovpn_config(cursor, vpn_id, config_path, config_hash)

cursor.close()
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])