def check(): global ONLINE_STATUS_CHECKED global ONLINE_STATUS if ONLINE_STATUS_CHECKED is not None: if time.time() - ONLINE_STATUS_CHECKED < 3600: return ONLINE_STATUS logger.debug('Online check started') headers = { 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)' } ctx = tinyhttp.HTTP(proxy=True, noverify=False, timeout=15, headers=headers) ctx_nocert = tinyhttp.HTTP(proxy=True, timeout=15, noverify=True, headers=headers) ctx_noproxy = tinyhttp.HTTP(proxy=False, noverify=False, timeout=15, headers=headers) ctx_mitm = tinyhttp.HTTP(proxy=True, noverify=False, timeout=15, cadata=CHECKS['https']['ca'].decode('base64'), headers=headers) result = 0 mintime = None offset = 0 ok = 0 now = time.time() for url in CAPTIVE_URLS: try: data, code = ctx.get(url, code=True) t = time.time() if mintime is None or mintime > t - now: mintime = t - now now = t if data == '' and code == 204: ok += 1 if code == 302: result |= HOTSPOT except Exception, e: logger.debug('Captive check failed {}: {}'.format(url, e))
def external_headers(): logger.debug('Retrieve external headers') try: ctx = tinyhttp.HTTP(timeout=15, headers={'User-Agent': 'curl/7.12.3'}) data = ctx.get('http://httpbin.org/headers') data = json.loads(data) return data['headers'] except Exception, e: logger.debug('External headers failed: {}'.format(e))
def online(): headers = { 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)' } ctx = tinyhttp.HTTP(timeout=5, headers=headers) try: data = ctx.get(CHECKS['msonline']['url']) if data == CHECKS['msonline']['text']: return True except Exception, e: logger.debug('MS Online check failed: {}'.format(e))
logger.debug('Retrieve IP using external services') try: stun_ip = stun.get_ip(stun_host=STUN_HOST, stun_port=STUN_PORT) if stun_ip != None: stun_ip = netaddr.IPAddress(stun_ip) LAST_EXTERNAL_IP = stun_ip LAST_EXTERNAL_IP_TIME = time.time() return LAST_EXTERNAL_IP except Exception, e: logger.debug('external_ip: STUN failed: {}'.format(e)) ctx = tinyhttp.HTTP(timeout=15, headers={'User-Agent': 'curl/7.12.3'}) for service in OWN_IP: for scheme in [ 'https', 'http' ]: try: data, code = ctx.get(scheme + '://' + service, code=True) if code == 200: addr = netaddr.IPAddress(data.strip()) if force_ipv4 and addr.version == 6: continue LAST_EXTERNAL_IP = addr LAST_EXTERNAL_IP_TIME = time.time() return LAST_EXTERNAL_IP except Exception, e:
def __init__(self, url=GOOGLE, validation=False): self.client = tinyhttp.HTTP() self.url = url # Validation enabled self.cd = 0 if bool(validation) else 1