-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-tor.py
executable file
·75 lines (63 loc) · 2.57 KB
/
is-tor.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
#!/usr/bin/env python
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/is-tor.py
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/is-tor.py
## https://davoudarsalani.ir
from json import loads
from os import path, getenv
from sys import argv
from requests import Session
from gp import Color, msgn, get_headers
title = path.basename(__file__).replace('.py', '')
istor = False
ip = None
url = 'https://check.torproject.org/api/ip'
tor_proxy = 'socks5h://127.0.0.1:9050'
country_url = 'http://ip-api.com/json'
Col = Color()
Ses = Session()
hdrs = get_headers()
Ses.proxies = {'http': tor_proxy, 'https': tor_proxy}
script_args = argv[1:]
if script_args:
first_arg = script_args[0]
else:
first_arg = None
if first_arg == 'msg':
msgn(f'<span color=\"{getenv("orange")}\">{title}</span> checking')
attempts = 10
else:
attempts = 1000
print(Col.heading(title))
for attempt in range(1, attempts + 1):
if istor and ip: ## JUMP_1
break
else:
if attempt > 1:
if first_arg == 'msg':
msgn(f'<span color=\"{getenv("orange")}\">{title}</span> attempt {attempt}/{attempts}')
else:
print(f'attempt {attempt}/{attempts}')
## get tor status and ip
try:
response = Ses.get(url, headers=hdrs, timeout=20)
response = loads(response.text) ## {'IsTor': True, 'IP': 'YOUR-IP'}
istor = response.get('IsTor', False) ## True/False
ip = response.get('IP')
except Exception as exc:
pass
if istor and ip:
## get country of ip
try:
response = Ses.get(f'{country_url}/{ip}', headers=hdrs, timeout=20)
response = loads(
response.text
) ## {'status': 'success', 'country': 'Sweden', 'countryCode': 'SE', 'region': 'AB', 'regionName': 'Stockholm County', 'city': 'Stockholm', 'zip': '100 05', 'lat': 59.3293, 'lon': 18.0686, 'timezone': 'Europe/Stockholm', 'isp': 'Svea Hosting AB', 'org': 'Svea Hosting AB', 'as': 'AS41634 Svea Hosting AB', 'query': '193.239.232.102'}
country = response.get('country')
except Exception as exc:
pass
if first_arg == 'msg':
msgn(f'<span color=\"{getenv("green")}\">✔</span> <span color=\"{getenv("orange")}\">{title}</span> = true ({ip}, {country})')
else:
print(Col.green('✔ ') + Col.grey(f'true ({ip}, {country})'))