diff options
author | Wojciech Kosior <kwojtus@protonmail.com> | 2020-06-12 14:22:23 +0200 |
---|---|---|
committer | Wojciech Kosior <kwojtus@protonmail.com> | 2020-06-12 14:22:23 +0200 |
commit | 191c4a0dbbf10a508a65b500a00f0064b29d9c94 (patch) | |
tree | 7b53f662701f007c334c8c19279f388d19bbffba /src | |
parent | 6006e6a460ea0692df7d74936d388b479bf26ad9 (diff) | |
download | 0tdns-191c4a0dbbf10a508a65b500a00f0064b29d9c94.tar.gz 0tdns-191c4a0dbbf10a508a65b500a00f0064b29d9c94.zip |
fix bugs in hourly scripts
Diffstat (limited to 'src')
-rwxr-xr-x | src/hourly.py | 29 | ||||
-rwxr-xr-x | src/perform_queries.py | 24 |
2 files changed, 29 insertions, 24 deletions
diff --git a/src/hourly.py b/src/hourly.py index 7396bc2..80ad9f5 100755 --- a/src/hourly.py +++ b/src/hourly.py @@ -20,9 +20,11 @@ def sync_ovpn_config(cursor, vpn_id, config_path, config_hash): ''', (vpn_id, config_hash)) (config_contents,) = cursor.fetchone() - - with open(config_path, "w") as config_file: - config_file.write(config_contents) + + print(config_contents.tobytes()) + + with open(config_path, "wb") as config_file: + config_file.write(config_contents.tobytes()) def get_vpn_connections(cursor, hour): # return ( @@ -31,9 +33,9 @@ def get_vpn_connections(cursor, hour): # (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; + SELECT DISTINCT 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() @@ -49,8 +51,9 @@ cursor = connection.cursor() hour = strftime('%Y-%m-%d %H:00', gmtime()) vpns = get_vpn_connections(cursor, hour) - -for vpn_id, config_hash in vpns if vpn_id in ztdns_config['handled_vpns']: +vpns = [vpn for vpn in vpns if vpn[0] in ztdns_config['handled_vpns']] +print(vpns) +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) @@ -58,11 +61,11 @@ for vpn_id, config_hash in vpns if vpn_id in ztdns_config['handled_vpns']: cursor.close() connection.close() -for vpn_id, config_hash in vpns if vpn_id in ztdns_config['handled_vpns']: +for vpn_id, config_hash in vpns: config_path = "/var/lib/0tdns/{}.ovpn".format(config_hash) - physical_ip = get_default_host_address(ztdns_config['database']) - route_through_veth = ztdns_config['database'] + "/32" - command_in_namespace = [perform_queries, hour, vpn_id] - + physical_ip = get_default_host_address(ztdns_config['host']) + route_through_veth = ztdns_config['host'] + "/32" + command_in_namespace = [perform_queries, hour, str(vpn_id)] + print('RUNNING OPENVPN FOR {}'.format(vpn_id)) subprocess.run([wrapper, config_path, physical_ip, route_through_veth] + command_in_namespace) diff --git a/src/perform_queries.py b/src/perform_queries.py index 8262ef0..32c4edf 100755 --- a/src/perform_queries.py +++ b/src/perform_queries.py @@ -4,7 +4,7 @@ from sys import argv import unbound # our own module used by several scripts in the project -from ztdns_db_connectivity import start_db_connection +from ztdns_db_connectivity import start_db_connection, get_ztdns_config class dns_queries: def __init__(self, dns_IP, dns_id, services): @@ -30,9 +30,9 @@ def query_planned_queries(cursor, hour, vpn_id): # [100, "onet.pl"]]) # ] cursor.execute(''' - SELECT DISTINCT d.IP, d.dns_id + SELECT DISTINCT d."IP", d.id FROM user_side_queries AS q JOIN user_side_dns AS d - ON dns.id = q.dns_id + ON d.id = q.dns_id WHERE q.vpn_id = %s ''', (vpn_id,)) dnss = cursor.fetchall() @@ -42,7 +42,7 @@ def query_planned_queries(cursor, hour, vpn_id): for dns_IP, dns_id in dnss: cursor.execute(''' SELECT s.id, s.name - FROM user_side_services AS s JOIN user_side_queries AS q + FROM user_side_service AS s JOIN user_side_queries AS q ON s.id = q.service_id WHERE q.vpn_id = %s AND q.dns_id = %s ''', (vpn_id, dns_id)) @@ -69,7 +69,7 @@ def resolve_call_back(mydata, status, result): responses_id = query.cursor.fetchone()[0] if status==0 and result.havedata: - for address in data.address_list: + for address in result.data.address_list: query.cursor.execute(''' INSERT INTO user_side_response (returned_ip, responses_id) VALUES(%s, %s) @@ -78,16 +78,18 @@ def resolve_call_back(mydata, status, result): hour = argv[1] vpn_id = argv[2] -connection = start_db_connection() +config = get_ztdns_config() +connection = start_db_connection(config) cursor = connection.cursor() contexts = [] -for dns_query in query_planned_queries(cursor, hour, vpn_id): +for dns_queries in query_planned_queries(cursor, hour, vpn_id): ctx = unbound.ub_ctx() - ctx.set_fwd(dns_IP) - for service_id, service_name in dns_query.services: - print("starting resolution of {} through {}".format(service_name, dns_IP)) - query = single_query(cursor, vpn_id, dns_query.dns_id, service_id) + ctx.set_fwd(dns_queries.dns_IP) + for service_id, service_name in dns_queries.services: + print("starting resolution of {} through {}".format(service_name, + dns_queries.dns_IP)) + query = single_query(cursor, vpn_id, dns_queries.dns_id, service_id) ctx.resolve_async(service_name, query, resolve_call_back, unbound.RR_TYPE_A, unbound.RR_CLASS_IN) |