| 48 | self._latency, self._success_rate = math.inf, 0.0 |
| 49 | |
| 50 | def validate_proxy(self): |
| 51 | protocol = 'https' if self._using_https else 'http' |
| 52 | proxy_str = '{}://{}:{}'.format(protocol, self._host, self._port) |
| 53 | try: |
| 54 | checking_api = IP_CHECKER_API_SSL if self._using_https else IP_CHECKER_API |
| 55 | |
| 56 | # First request for checking IP |
| 57 | r = requests.get(checking_api, proxies={'https': proxy_str, 'http': proxy_str}, verify=False, timeout=15) |
| 58 | if r.ok: |
| 59 | j = json.loads(r.text) |
| 60 | |
| 61 | if j['ip'] != get_current_ip(): |
| 62 | self._anonymous = True |
| 63 | self._valid = True |
| 64 | |
| 65 | # A second request for meta info |
| 66 | r2 = requests.get('https://api.ip.sb/geoip/{}'.format(j['ip']), timeout=15) |
| 67 | jresponse = r2.json() |
| 68 | |
| 69 | # Load meta data |
| 70 | # TODO: better location check |
| 71 | meta = { |
| 72 | 'location': '{},{}'.format(jresponse['latitude'], jresponse['longitude']), |
| 73 | 'organization': jresponse['organization'] if 'organization' in jresponse else None, |
| 74 | 'region': jresponse['region'], |
| 75 | 'country': jresponse['country_code'], |
| 76 | 'city': jresponse['city'], |
| 77 | } |
| 78 | self._meta = meta |
| 79 | |
| 80 | except requests.Timeout: |
| 81 | logger.debug('Catch requests.Timeout for proxy ip: {}'.format(self._host)) |
| 82 | except requests.RequestException as e: |
| 83 | logger.debug('Catch requests.RequestException for proxy ip: {}'.format(self._host)) |
| 84 | logger.debug(e.__str__()) |
| 85 | |
| 86 | def validate(self): |
| 87 | self.validate_latency() |