Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Implement debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdikSS committed Mar 1, 2017
1 parent 09c6529 commit a6320fd
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions blockcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
# End configuration

ipv6_available = False
debug = False

try:
import tkinter as tk
Expand Down Expand Up @@ -119,6 +120,10 @@ def print(*args, **kwargs):
]
__builtins__.print(*args, **kwargs)

def print_debug(*args, **kwargs):
if debug:
print(*args, **kwargs)

def _get_a_record(site, querytype='A', dnsserver=None):
resolver = dns.resolver.Resolver()
resolver.timeout = 5
Expand All @@ -144,6 +149,7 @@ def _get_a_record_over_google_api(site, querytype='A'):
result = []

response = _get_url(google_dns_api + "?name={}&type={}".format(site, querytype))
print_debug("Google API: {}".format(response))
if (response[0] != 200):
return ''
response_js = json.loads(response[1])
Expand All @@ -160,6 +166,7 @@ def _get_a_records(sitelist, querytype='A', dnsserver=None, googleapi=False):
try:
if googleapi:
responses = _get_a_record_over_google_api(site, querytype)
print_debug("Google API вернул {}".format(responses))
else:
responses = _get_a_record(site, querytype, dnsserver)

Expand Down Expand Up @@ -191,9 +198,11 @@ def _get_url(url, proxy=None, ip=None):
conn.settimeout(10)
try:
conn.connect((ip if ip else host, 443))
except ssl.CertificateError:
except ssl.CertificateError as e:
print_debug("_get_url: ssl.CertificateError", repr(e))
return (-1, '')
except (socket.timeout, socket.error):
except (socket.timeout, socket.error) as e:
print_debug("_get_url: socket exception", repr(e))
return (0, '')
finally:
try:
Expand Down Expand Up @@ -228,9 +237,11 @@ def _get_url(url, proxy=None, ip=None):
try:
opened = opener.open(req, timeout=15)
output = opened.read()
except ssl.CertificateError:
except ssl.CertificateError as e:
print_debug("_get_url: late ssl.CertificateError", repr(e))
return (-1, '')
except (urllib.error.URLError, socket.error, socket.timeout) as e:
print_debug("_get_url: late socket exception", repr(e))
if 'CERTIFICATE_VERIFY_FAILED' in str(e):
return (-1, '')
return (0, '')
Expand Down Expand Up @@ -781,6 +792,7 @@ def print_http_result(symbol, message):
help='Не проверять доступность сайтов через {}.' \
.format(isup_server))
parser.add_argument('--force-dpi-check', action='store_true', help='Выполнить проверку DPI, даже если провайдер не блокирует сайты.')
parser.add_argument('--debug', action='store_true', help='Включить режим отладки.')
args = parser.parse_args()

if args.console:
Expand All @@ -795,6 +807,9 @@ def print_http_result(symbol, message):
if args.force_dpi_check:
force_dpi_check = True

if args.debug:
debug = True

if tkusable:
root = tk.Tk()
root.title("BlockCheck")
Expand Down

0 comments on commit a6320fd

Please sign in to comment.