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
):
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
):
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")
32 select v.id, v.ovpn_config_sha256
33 from user_side_queries as q join user_side_vpn as v
36 return cursor
.fetchall()
38 connection
= start_db_connection()
39 cursor
= connection
.cursor()
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
)
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
])