|
@@ -0,0 +1,118 @@
|
|
|
+#!/usr/bin/env python
|
|
|
+# -*- encoding: utf-8 -*-
|
|
|
+'''
|
|
|
+@Contact : liuyuqi.gov@msn.cn
|
|
|
+@Time : 2023/03/26 16:50:34
|
|
|
+@License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
|
|
|
+@Desc : ihuan proxy
|
|
|
+'''
|
|
|
+
|
|
|
+from . import Proxy
|
|
|
+import requests
|
|
|
+import time,datetime
|
|
|
+import re,os,sys,re
|
|
|
+from outlook_muti_register import api
|
|
|
+import logging
|
|
|
+
|
|
|
+class IhuaProxy(Proxy):
|
|
|
+
|
|
|
+ def __init__(self, debug=False):
|
|
|
+ self.debug = debug
|
|
|
+ self.logger = logging.getLogger('outlook_muti_register')
|
|
|
+ if debug:
|
|
|
+ self.logger.setLevel(logging.DEBUG)
|
|
|
+ else:
|
|
|
+ self.logger.setLevel(logging.INFO)
|
|
|
+ self.logger.info('init ihuan proxy')
|
|
|
+ # todo
|
|
|
+ self.headers = {}
|
|
|
+
|
|
|
+ def set_global_chek_timeout(self, ip_list, max_bad_number=None):
|
|
|
+ if not ip_list:
|
|
|
+ print(ip_list)
|
|
|
+ raise Exception('ip_list is None')
|
|
|
+ bad_number = 0
|
|
|
+ success_number = 0
|
|
|
+ if max_bad_number is None:
|
|
|
+ max_bad_number = len(ip_list)/2
|
|
|
+ for ip, port, tp in ip_list:
|
|
|
+ tp = tp.lower()
|
|
|
+ try:
|
|
|
+ ip_url = '{}:{}'.format(ip, port)
|
|
|
+ requests.get(url=url, headers=headers, proxies={
|
|
|
+ tp: '{}://'.format(tp)+ip_url}, timeout=check_timeout)
|
|
|
+ success_number += 1
|
|
|
+ except Exception:
|
|
|
+ bad_number += 1
|
|
|
+ print('try to set timeout={} \t success:{} \t fail:{}'.format(
|
|
|
+ check_timeout, success_number, bad_number))
|
|
|
+
|
|
|
+ if bad_number > max_bad_number:
|
|
|
+ check_timeout += 1.5
|
|
|
+ if check_timeout >= 10:
|
|
|
+ raise TimeoutError
|
|
|
+ return self.set_global_chek_timeout(ip_list, max_bad_number)
|
|
|
+
|
|
|
+ if success_number > len(ip_list) - max_bad_number:
|
|
|
+ d = datetime.now()
|
|
|
+ with open('check_timeout.txt', 'w') as f:
|
|
|
+ f.write('{} {} {} {} {}'.format(
|
|
|
+ d.year, d.month, d.day, d.hour, check_timeout))
|
|
|
+ return check_timeout
|
|
|
+
|
|
|
+
|
|
|
+ def get_ip_list(self):
|
|
|
+ '''get ip list from ihuan
|
|
|
+ Returns: ip_list
|
|
|
+ '''
|
|
|
+ d = datetime.now()
|
|
|
+ year = d.year
|
|
|
+ month = d.month
|
|
|
+ day = d.day
|
|
|
+ hour = d.hour
|
|
|
+ req_url = api.proxy_host_ihuan + '/today/{}/{:02d}/{{:02d}}/{{:02d}}.html'.format(
|
|
|
+ year,
|
|
|
+ month
|
|
|
+ )
|
|
|
+ ip_list = []
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ cur_url = req_url.format(day, hour)
|
|
|
+ self.logger.info('try to get ip list page: ' + cur_url)
|
|
|
+ response = requests.get(cur_url, headers=self.headers)
|
|
|
+ ip_list = re.findall(
|
|
|
+ r'<br>([\d\.]*?):(\d*)@(.*?)#',
|
|
|
+ response.text,
|
|
|
+ re.S
|
|
|
+ )
|
|
|
+ if ip_list:
|
|
|
+ break
|
|
|
+ hour -= 1
|
|
|
+ except Exception as ex:
|
|
|
+ print(ex)
|
|
|
+ time.sleep(2)
|
|
|
+ hour -= 1
|
|
|
+ if hour == 0:
|
|
|
+ raise Exception('get ip timeout')
|
|
|
+ self.logger.info('get ip list page success')
|
|
|
+ if os.path.exists('check_timeout.txt'):
|
|
|
+ l = []
|
|
|
+ with open('check_timeout.txt', 'r') as f:
|
|
|
+ f_read = f.read()
|
|
|
+ if f_read:
|
|
|
+ l = list(map(float, f_read.split(' ')))
|
|
|
+ if l and [year, month, day, hour] == l[:-1]:
|
|
|
+ check_timeout = l[-1]
|
|
|
+ else:
|
|
|
+ self.set_global_chek_timeout(ip_list[-5:])
|
|
|
+ else:
|
|
|
+ self.set_global_chek_timeout(ip_list[-5:])
|
|
|
+ used_ip = ''
|
|
|
+ if os.path.exists('used_ip.txt'):
|
|
|
+ with open('used_ip.txt', 'r') as f:
|
|
|
+ used_ip = f.read()
|
|
|
+ return [[tp, '{}:{}'.format(ip, port)] for ip, port, tp in ip_list if ip not in used_ip]
|
|
|
+
|
|
|
+if __name__=='__main__':
|
|
|
+ ihuan_proxy = IhuaProxy()
|
|
|
+ print(ihuan_proxy.get_ip_list())
|