-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrequests.py
executable file
·82 lines (71 loc) · 2.75 KB
/
requests.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'''
Pi-hole DNS traffic visualizer for the Raspberry Pi Sense HAT
By Sam Lindley, 2/21/2018
'''
import json
import os
import socket
import sys
import time
import urllib.request
import config
def global_access(host="8.8.8.8", port=53, timeout=3):
"""
Host: 8.8.8.8 (Google Public DNS A)
Port: 53/TCP
Service: domain
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except socket.error:
return False
def api_request(address, pw_hash):
if not hasattr(api_request, "initial_connection"):
api_request.initial_connection = True
max_attempts = 100 if api_request.initial_connection else 10
attempts = 0
query = "?summary&overTimeData10mins&getQueryTypes&getQuerySources&auth=%s" % pw_hash
#retrieve and decode json data from server
while True:
try:
if attempts == 0 and api_request.initial_connection:
if os.geteuid() == 0:
config.LOGGER.info('Initiating connection with server.')
print('Initiating connection with server.')
with urllib.request.urlopen("http://%s/admin/api.php%s" % (address, query)) as url:
attempts += 1
raw_data = json.loads(url.read().decode())
break
except json.decoder.JSONDecodeError:
if attempts < max_attempts:
time.sleep(1)
continue
else:
if os.geteuid() == 0:
config.LOGGER.error('Exceeded max attempts to connect with server.')
print('Error: Exceeded max attempts to connect with server.')
sys.exit(1)
except urllib.error.URLError:
if os.geteuid() == 0:
config.LOGGER.error('Web server offline or invalid address entered.')
print("Error: Web server offline or invalid address entered.")
if attempts < max_attempts:
time.sleep(1)
continue
else:
sys.exit(1)
if 'domains_over_time' not in raw_data or 'ads_over_time' not in raw_data or \
'ads_percentage_today' not in raw_data:
if os.geteuid() == 0:
config.LOGGER.error('Invalid data returned from server. Check if pihole-FTL service is \
running.')
print('Error: Invalid data returned from server. Check if pihole-FTL service is running.')
sys.exit(1)
if api_request.initial_connection:
if os.geteuid() == 0:
config.LOGGER.info('Successful connection with server.')
print('Successful connection with server.')
api_request.initial_connection = False
return raw_data