def testipall(): global special, special_lock, threadcnt, threadcnt_lock try: while True: for i in range(1, iptool.get_config("testip", "special") + 1): special_lock.acquire() if i not in special: special.add(i) special_lock.release() t = threading.Thread(target=testip, args=(i, )) t.setDaemon(True) t.start() else: special_lock.release() while True: threadcnt_lock.acquire() if threadcnt < iptool.get_config("testip", "threads"): threadcnt += 1 threadcnt_lock.release() t = threading.Thread(target=testip, args=(0, )) t.setDaemon(True) t.start() else: threadcnt_lock.release() break time.sleep(2) except KeyboardInterrupt: iptool.stop()
def testip(mode): global special, special_lock, threadcnt, threadcnt_lock try: while True: if mode > 0: if mode > iptool.get_config("testip", "special"): with special_lock: special.remove(mode) return else: with threadcnt_lock: if threadcnt > iptool.get_config("testip", "threads"): threadcnt -= 1 return testipwork(mode) except KeyboardInterrupt: iptool.stop() except: logging.exception("testip exception") if mode > 0: with special_lock: special.remove(mode) else: with threadcnt_lock: threadcnt -= 1
def testipall(): global special,special_lock,threadcnt,threadcnt_lock try: while True: for i in range(1,iptool.get_config("testip","special")+1): special_lock.acquire() if i not in special: special.add(i) special_lock.release() t=threading.Thread(target=testip,args=(i,)) t.setDaemon(True) t.start() else: special_lock.release() while True: threadcnt_lock.acquire() if threadcnt<iptool.get_config("testip","threads"): threadcnt+=1 threadcnt_lock.release() t=threading.Thread(target=testip,args=(0,)) t.setDaemon(True) t.start() else: threadcnt_lock.release() break time.sleep(2) except KeyboardInterrupt: iptool.stop()
def testip(mode): global special,special_lock,threadcnt,threadcnt_lock try: while True: if mode>0: if mode>iptool.get_config("testip","special"): with special_lock: special.remove(mode) return else: with threadcnt_lock: if threadcnt>iptool.get_config("testip","threads"): threadcnt-=1 return testipwork(mode) except KeyboardInterrupt: iptool.stop() except: logging.exception("testip exception") if mode>0: with special_lock: special.remove(mode) else: with threadcnt_lock: threadcnt-=1
def testipwork(mode): global lock,checklst ipvalid=False try: time.sleep(0.01) iperror=True ip="" addip.addip("", 0) try: if mode > 0: ip = [x[2] for x in iptool.global_iplist[:iptool.get_config("testip","special")]] else: ip = [x[2] for x in iptool.global_iplist] with lock: ip = [x for x in ip if x not in checklst] ip = random.choice(ip) checklst.add(ip) except KeyboardInterrupt: raise except: ip = "" if ip: ipvalid=True while time.time()<addip.sleep_before: if(addip.sleep_before-time.time()>iptool.get_config("iptool","sleep_time")):addip.sleep_before=0 time.sleep(5) addip.sleep_before=0 while (not checkconnection(iptool.get_config("testip","checkconn_addr"))): time.sleep(1) costtime=time.time() c = iptool.create_ssl_socket(ip, iptool.get_config("testip","timeout")) costtime=int(time.time()*1000-costtime*1000) result=iptool.test_connection(c) if result==iptool.TEST_OK: iperror=False addip.addip(ip,costtime) testiprecord(costtime) elif result==iptool.TEST_UNDEFINED: iperror=False c.close() else: time.sleep(1) except KeyboardInterrupt: iptool.stop() except: pass if ipvalid: try: if iperror: if checkconnection(iptool.get_config("testip","checkconn_addr")): addip.addip(ip, addip.TIME_INF) testiprecord(addip.TIME_INF) testipsleep() finally: with lock: checklst.remove(ip)
def addtolist(item, iplist, ipset): global dst, lock try: if len(item) >= 2 and (item[0] not in ipset): iplist.append((int(item[1]), len(iplist), item[0])) ipset.add(item[0]) except KeyboardInterrupt: iptool.stop() except: pass
def checkconnection(addr): try: if iptool.get_config("testip","checkconn_timeout")<0: return True c = iptool.create_ssl_socket(addr, iptool.get_config("testip","checkconn_timeout")) c.close() except KeyboardInterrupt: iptool.stop() except: return False return True
def checkconnection(addr): try: if iptool.get_config("testip", "checkconn_timeout") < 0: return True c = iptool.create_ssl_socket( addr, iptool.get_config("testip", "checkconn_timeout")) c.close() except KeyboardInterrupt: iptool.stop() except: return False return True
def checkip(): global threadcnt,threadcnt_lock try: while True: with threadcnt_lock: if threadcnt>iptool.get_config("checkip","threads"): threadcnt-=1 return checkipwork() except KeyboardInterrupt: iptool.stop() except: logging.exception("checkip exception") with threadcnt_lock: threadcnt-=1
def checkipall(): global iplist,threadcnt,threadcnt_lock try: iplist=getiplist() while True: time.sleep(2) while True: threadcnt_lock.acquire() if threadcnt<iptool.get_config("checkip","threads"): threadcnt+=1 threadcnt_lock.release() t=threading.Thread(target=checkip) t.setDaemon(True) t.start() else: threadcnt_lock.release() break except KeyboardInterrupt: iptool.stop()
def checkipwork(): global lock,checklst,iplist ipvalid=False try: time.sleep(0.25) ip=random.choice(iplist) ip=random.randint(ip[0],ip[1]) ip=iptostr(ip) lock.acquire() isin=ip in checklst if not isin: checklst.add(ip) lock.release() ipvalid=True while time.time()<addip.sleep_before: if(addip.sleep_before-time.time()>iptool.get_config("iptool","sleep_time")):addip.sleep_before=0 time.sleep(5) addip.sleep_before=0 costtime=time.time() c = iptool.create_ssl_socket(ip, iptool.get_config("checkip","timeout")) costtime=int(time.time()*1000-costtime*1000) if iptool.test_connection(c)==iptool.TEST_OK: addip.addip(ip,costtime) logging.info("found ip %s, %d ms", ip, costtime) c.close() else: lock.release() except KeyboardInterrupt: iptool.stop() except: pass if ipvalid: try: checkipsleep() finally: with lock: checklst.remove(ip)
def addip(ip, costtime): global dst, lock, stop if stop: iptool.stop() ipset = set([]) iplist = [] if ip: addtolist([ip, str(costtime)], iplist, ipset) try: with lock: with open(dst, "a"): pass with open(dst, "r") as ff: for strs in ff: addtolist( strs.strip("\n").strip("\r").split(" "), iplist, ipset) iplist.sort() iplist = iplist[:min(iptool.get_config("addip", "keep_ip" ), len(iplist))] iptool.global_iplist = iplist if stop: iptool.stop() if ip: with open(tmpdst, "w") as ff: for i in iplist: ff.write(i[2] + " " + (str(i[0]) if i[0] >= 0 else "1000") + "\n") begin_time = time.time() while True: try: os.remove(dst) except OSError: cur_time = time.time() if cur_time >= begin_time and cur_time - begin_time <= 2: continue break os.rename(tmpdst, dst) except KeyboardInterrupt: iptool.stop() except: pass
def addip(ip, costtime): global dst, lock, stop if stop: iptool.stop() ipset = set([]) iplist = [] if ip: addtolist([ip, str(costtime)], iplist, ipset) try: with lock: with open(dst, "a"): pass with open(dst, "r") as ff: for strs in ff: addtolist(strs.strip("\n").strip("\r").split(" "), iplist, ipset) iplist.sort() iplist = iplist[:min(iptool.get_config("addip", "keep_ip"), len(iplist))] iptool.global_iplist = iplist if stop: iptool.stop() if ip: with open(tmpdst, "w") as ff: for i in iplist: ff.write(i[2] + " " + (str(i[0]) if i[0] >= 0 else "1000") + "\n") begin_time = time.time() while True: try: os.remove(dst) except OSError: cur_time = time.time() if cur_time >= begin_time and cur_time-begin_time <= 2: continue break os.rename(tmpdst, dst) except KeyboardInterrupt: iptool.stop() except: pass
def testipwork(mode): global lock, checklst ipvalid = False try: time.sleep(0.01) iperror = True ip = "" addip.addip("", 0) try: if mode > 0: ip = [ x[2] for x in iptool. global_iplist[:iptool.get_config("testip", "special")] ] else: ip = [x[2] for x in iptool.global_iplist] with lock: ip = [x for x in ip if x not in checklst] ip = random.choice(ip) checklst.add(ip) except KeyboardInterrupt: raise except: ip = "" if ip: ipvalid = True while time.time() < addip.sleep_before: if (addip.sleep_before - time.time() > iptool.get_config( "iptool", "sleep_time")): addip.sleep_before = 0 time.sleep(5) addip.sleep_before = 0 while (not checkconnection( iptool.get_config("testip", "checkconn_addr"))): time.sleep(1) costtime = time.time() c = iptool.create_ssl_socket( ip, iptool.get_config("testip", "timeout")) costtime = int(time.time() * 1000 - costtime * 1000) result = iptool.test_connection(c) if result == iptool.TEST_OK: iperror = False addip.addip(ip, costtime) testiprecord(costtime) elif result == iptool.TEST_UNDEFINED: iperror = False c.close() else: time.sleep(1) except KeyboardInterrupt: iptool.stop() except: pass if ipvalid: try: if iperror: if checkconnection( iptool.get_config("testip", "checkconn_addr")): addip.addip(ip, addip.TIME_INF) testiprecord(addip.TIME_INF) testipsleep() finally: with lock: checklst.remove(ip)