6 # our own module used by several scripts in the project
7 from ztdns_db_connectivity
import start_db_connection
10 def __init__(self
, dns_IP
, dns_id
, services
):
13 self
.services
= services
16 def __init__(self
, cursor
, vpn_id
, dns_id
, service_id
):
20 self
.service_id
= service_id
22 def query_planned_queries(cursor
, hour
, vpn_id
):
24 # # dns server IP | dns server id | service_id | service_name
25 # dns_queries("195.98.79.117", 23, [[89, "devuan.org"],
27 # [112, "invidio.us"]]),
28 # dns_queries("192.71.245.208", 33, [[77, "debian.org"],
29 # [22, "nie.ma.takiej.domeny"],
33 SELECT DISTINCT d.IP, d.dns_id
34 FROM user_side_queries AS q JOIN user_side_dns AS d
38 dnss
= cursor
.fetchall()
42 for dns_IP
, dns_id
in dnss
:
45 FROM user_side_services AS s JOIN user_side_queries AS q
46 ON s.id = q.service_id
47 WHERE q.vpn_id = %s AND q.dns_id = %s
48 ''', (vpn_id
, dns_id
))
50 queries
= dns_queries(dns_IP
, dns_id
, cursor
.fetchall())
52 dnss_to_query
.append(queries
)
56 def resolve_call_back(mydata
, status
, result
):
59 print("callback called for {}".format(result
.qname
))
60 if status
==0 and result
.havedata
:
61 print("Result:",result
.data
.address_list
)
63 query
.cursor
.execute('''
64 INSERT INTO user_side_responses (date, result, dns_id, service_id, vpn_id)
65 VALUES (current_timestamp, '', %s, %s, %s)
67 ''', (query
.dns_id
, query
.service_id
, query
.vpn_id
))
69 responses_id
= query
.cursor
.fetchone()[0]
71 if status
==0 and result
.havedata
:
72 for address
in data
.address_list
:
73 query
.cursor
.execute('''
74 INSERT INTO user_side_response (returned_ip, responses_id)
76 ''', (address
, responses_id
))
77 # no committing, since auto-commit mode is set on the connection
81 connection
= start_db_connection()
82 cursor
= connection
.cursor()
85 for dns_query
in query_planned_queries(cursor
, hour
, vpn_id
):
86 ctx
= unbound
.ub_ctx()
88 for service_id
, service_name
in dns_query
.services
:
89 print("starting resolution of {} through {}".format(service_name
, dns_IP
))
90 query
= single_query(cursor
, vpn_id
, dns_query
.dns_id
, service_id
)
92 ctx
.resolve_async(service_name
, query
, resolve_call_back
,
93 unbound
.RR_TYPE_A
, unbound
.RR_CLASS_IN
)