How to use remove_rule method in localstack

Best Python code snippet using localstack_python

brushtask.py

Source:brushtask.py Github

copy

Full Screen

...223 avg_upspeed = int(uploaded / dltime)224 # 下载量225 downloaded = torrent.get("downloaded")226 total_downloaded += downloaded227 if self.__check_remove_rule(remove_rule=remove_rule,228 seeding_time=seeding_time,229 ratio=ratio,230 uploaded=uploaded,231 avg_upspeed=avg_upspeed):232 log.info("【BRUSH】%s 做种达到删种条件,删除下载任务..." % torrent.get('name'))233 if torrent_id not in delete_ids:234 delete_ids.append(torrent_id)235 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))236 # 检查下载中状态的237 torrents = downloader.get_torrents(ids=torrent_ids, status=["downloading"])238 for torrent in torrents:239 # ID240 torrent_id = torrent.get("hash")241 # 下载耗时 秒242 dltime = int(time.time() - torrent.get("added_on"))243 # 上传量 Byte244 uploaded = torrent.get("uploaded") or 0245 total_uploaded += uploaded246 # 平均上传速度 Byte/s247 avg_upspeed = int(uploaded / dltime)248 # 下载量249 downloaded = torrent.get("downloaded")250 total_downloaded += downloaded251 if self.__check_remove_rule(remove_rule=remove_rule,252 dltime=dltime,253 avg_upspeed=avg_upspeed):254 log.info("【BRUSH】%s 下载耗时达到删种条件,删除下载任务..." % torrent.get('name'))255 if torrent_id not in delete_ids:256 delete_ids.append(torrent_id)257 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))258 # transmission259 else:260 # 检查完成状态261 downloader = Transmission(user_config=downloader_cfg)262 torrents = downloader.get_torrents(ids=torrent_ids, status=["seeding", "seed_pending"])263 for torrent in torrents:264 # ID265 torrent_id = torrent.id266 # 做种时间267 date_done = torrent.date_done if torrent.date_done else torrent.date_added268 dltime = (datetime.now().astimezone() - torrent.date_added).seconds269 seeding_time = (datetime.now().astimezone() - date_done).seconds270 # 下载量271 downloaded = int(torrent.total_size * torrent.progress / 100)272 total_downloaded += downloaded273 # 分享率274 ratio = torrent.ratio or 0275 # 上传量276 uploaded = int(downloaded * torrent.ratio)277 total_uploaded += uploaded278 # 平均上传速度279 avg_upspeed = int(uploaded / dltime)280 if self.__check_remove_rule(remove_rule=remove_rule,281 seeding_time=seeding_time,282 ratio=ratio,283 uploaded=uploaded,284 avg_upspeed=avg_upspeed):285 log.info("【BRUSH】%s 做种达到删种条件,删除下载任务..." % torrent.name)286 if torrent_id not in delete_ids:287 delete_ids.append(torrent_id)288 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))289 # 检查下载状态290 torrents = downloader.get_torrents(ids=torrent_ids,291 status=["downloading", "download_pending", "stopped"])292 for torrent in torrents:293 # ID294 torrent_id = torrent.id295 # 下载耗时296 dltime = (datetime.now().astimezone() - torrent.date_added).seconds297 # 下载量298 downloaded = int(torrent.total_size * torrent.progress / 100)299 total_downloaded += downloaded300 # 上传量301 uploaded = int(downloaded * torrent.ratio)302 total_uploaded += uploaded303 # 平均上传速度304 avg_upspeed = int(uploaded / dltime)305 if self.__check_remove_rule(remove_rule=remove_rule,306 dltime=dltime,307 avg_upspeed=avg_upspeed):308 log.info("【BRUSH】%s 下载耗时达到删种条件,删除下载任务..." % torrent.name)309 if torrent_id not in delete_ids:310 delete_ids.append(torrent_id)311 update_torrents.append(("%s,%s" % (uploaded, downloaded), taskid, torrent_id))312 # 更新上传下载量和删除种子数313 add_brushtask_upload_count(brush_id=taskid,314 upload_size=total_uploaded,315 download_size=total_downloaded,316 remove_count=len(delete_ids))317 # 更新种子状态为已删除318 update_brushtask_torrent_state(update_torrents)319 # 删除种子320 if delete_ids:321 downloader.delete_torrents(delete_file=True, ids=delete_ids)322 log.info("【BRUSH】任务 %s 共删除 %s 个刷流下载任务" % (task_name, len(delete_ids)))323 except Exception as e:324 log.console(str(e) + " - " + traceback.format_exc())325 def __is_allow_new_torrent(self, taskid, taskname, downloadercfg, seedsize, dlcount):326 """327 检查是否还能添加新的下载328 """329 if not taskid:330 return False331 # 判断大小332 total_size = get_brushtask_totalsize(taskid)333 if seedsize:334 if int(seedsize) * 1024 ** 3 <= int(total_size):335 log.warn("【BRUSH】刷流任务 %s 已达到保种体积 %sGB,不再新增下载" % (taskname, seedsize))336 return False337 # 检查正在下载的任务数338 if dlcount:339 downloading_count = self.__get_downloading_count(downloadercfg)340 if downloading_count is None:341 log.error("【BRUSH】任务 %s 下载器 %s 无法连接" % (taskname, downloadercfg.get("name")))342 return False343 if int(downloading_count) > int(dlcount):344 log.warn("【BRUSH】下载器 %s 正在下载任务数:%s,超过设定上限,暂不添加下载" % (downloadercfg.get("name"), downloading_count))345 return False346 return True347 @staticmethod348 def __get_downloader_config(dlid):349 """350 获取下载器的参数351 """352 if not dlid:353 return None354 downloader_info = get_user_downloaders(dlid)355 if downloader_info:356 userconfig = {"id": downloader_info[0][0],357 "name": downloader_info[0][1],358 "type": downloader_info[0][2],359 "host": downloader_info[0][3],360 "port": downloader_info[0][4],361 "username": downloader_info[0][5],362 "password": downloader_info[0][6],363 "save_dir": downloader_info[0][7]}364 return userconfig365 return None366 def __get_downloading_count(self, downloadercfg):367 """368 查询当前正在下载的任务数369 """370 if not downloadercfg:371 return 0372 if downloadercfg.get("type") == self._qb_client:373 downloader = Qbittorrent(user_config=downloadercfg)374 if not downloader.qbc:375 return None376 dlitems = downloader.get_downloading_torrents()377 if dlitems is not None:378 return int(len(dlitems))379 else:380 downloader = Transmission(user_config=downloadercfg)381 if not downloader.trc:382 return None383 dlitems = downloader.get_downloading_torrents()384 if dlitems is not None:385 return int(len(dlitems))386 return None387 def __download_torrent(self, downloadercfg, title, enclosure, size, taskid, transfer, taskname):388 """389 添加下载任务,更新任务数据390 :param downloadercfg: 下载器的所有参数391 :param title: 种子名称392 :param enclosure: 种子地址393 :param size: 种子大小394 :param taskid: 任务ID395 :param transfer: 是否要转移,为False时直接添加已整理的标签396 :param taskname: 任务名称397 """398 if not downloadercfg:399 return False400 # 标签401 tag = "已整理" if not transfer else None402 # 下载任务ID403 download_id = None404 # 添加下载405 if downloadercfg.get("type") == self._qb_client:406 # 初始化下载器407 downloader = Qbittorrent(user_config=downloadercfg)408 if not downloader.qbc:409 log.error("【BRUSH】任务 %s 下载器 %s 无法连接" % (taskname, downloadercfg.get("name")))410 return False411 torrent_tag = str(round(datetime.now().timestamp()))412 if tag:413 tag = [tag, torrent_tag]414 else:415 tag = torrent_tag416 ret = downloader.add_torrent(content=enclosure, mtype=None, tag=tag, is_paused=True)417 if ret:418 # QB添加下载后需要时间,重试5次每次等待5秒419 for i in range(1, 6):420 sleep(5)421 download_id = downloader.get_last_add_torrentid_by_tag(tag)422 if download_id is None:423 continue424 else:425 downloader.remove_torrents_tag(download_id, torrent_tag)426 downloader.start_torrents(download_id)427 downloader.torrents_set_force_start(download_id)428 break429 else:430 # 初始化下载器431 downloader = Transmission(user_config=downloadercfg)432 if not downloader.trc:433 log.error("【BRUSH】任务 %s 下载器 %s 无法连接" % (taskname, downloadercfg.get("name")))434 return False435 ret = downloader.add_torrent(content=enclosure, mtype=None)436 if ret:437 download_id = ret.id438 if download_id and tag:439 downloader.set_torrent_tag(tid=download_id, tag=tag)440 if not download_id:441 log.warn("【BRUSH】%s 添加下载任务出错" % title)442 return False443 else:444 log.info("【BRUSH】成功添加下载:%s" % title)445 # 插入种子数据446 if insert_brushtask_torrent(brush_id=taskid,447 title=title,448 enclosure=enclosure,449 downloader=downloadercfg.get("id"),450 download_id=download_id,451 size=size):452 # 更新下载次数453 add_brushtask_download_count(brush_id=taskid)454 else:455 log.info("【BRUSH】%s 已下载过" % title)456 return True457 @staticmethod458 def __check_rss_rule(rss_rule, title, description, torrent_url, torrent_size, cookie):459 """460 检查种子是否符合刷流过滤条件461 :param rss_rule: 过滤条件字典462 :param title: 种子名称463 :param description: 种子副标题464 :param torrent_url: 种子页面地址465 :param torrent_size: 种子大小466 :param cookie: Cookie467 :return: 是否命中468 """469 if not rss_rule:470 return True471 # 检查种子大小472 try:473 if rss_rule.get("size"):474 rule_sizes = rss_rule.get("size").split("#")475 if rule_sizes[0]:476 if len(rule_sizes) > 1 and rule_sizes[1]:477 min_max_size = rule_sizes[1].split(',')478 min_size = min_max_size[0]479 if len(min_max_size) > 1:480 max_size = min_max_size[1]481 else:482 max_size = 0483 if rule_sizes[0] == "gt" and float(torrent_size) < float(min_size) * 1024 ** 3:484 return False485 if rule_sizes[0] == "lt" and float(torrent_size) > float(min_size) * 1024 ** 3:486 return False487 if rule_sizes[0] == "bw" and not float(min_size) * 1024 ** 3 < float(torrent_size) < float(488 max_size) * 1024 ** 3:489 return False490 # 检查包含规则491 if rss_rule.get("include"):492 if not re.search(r"%s" % rss_rule.get("include"), "%s %s" % (title, description), re.IGNORECASE):493 return False494 # 检查排除规则495 if rss_rule.get("exclude"):496 if re.search(r"%s" % rss_rule.get("exclude"), "%s %s" % (title, description), re.IGNORECASE):497 return False498 attr_type = Torrent.check_torrent_attr(torrent_url=torrent_url, cookie=cookie)499 log.debug("【BRUSH】%s 解析详情, %s" % (title, attr_type))500 # 检查免费状态501 if rss_rule.get("free") == "FREE":502 if not attr_type.is_free():503 return False504 elif rss_rule.get("free") == "2XFREE":505 if not attr_type.is_free2x():506 return False507 # 检查HR状态508 if rss_rule.get("hr"):509 if attr_type.is_hr():510 return False511 log.debug("【BRUSH】%s `判断做种数, 当前做种人数%s, 当前人数阈值%s" % (title, attr_type.peer_count, rss_rule.get("peercount")))512 if rss_rule.get("peercount"):513 if attr_type.peer_count > int(rss_rule.get("peercount")):514 return False515 except Exception as err:516 log.console(str(err) + " - " + traceback.format_exc())517 return True518 @staticmethod519 def __check_remove_rule(remove_rule, seeding_time=None, ratio=None, uploaded=None, dltime=None, avg_upspeed=None):520 """521 检查是否符合删种规则522 :param remove_rule: 删种规则523 :param seeding_time: 做种时间524 :param ratio: 分享率525 :param uploaded: 上传量526 :param dltime: 下载耗时527 :param avg_upspeed: 上传平均速度528 """529 if not remove_rule:530 return False531 try:532 if remove_rule.get("time") and seeding_time:533 rule_times = remove_rule.get("time").split("#")...

Full Screen

Full Screen

text_filter.py

Source:text_filter.py Github

copy

Full Screen

1import re2import pdb3# Add rules as patterns.4# remove : remove matched substring from string5# change : change matched substring as substute resource6#7#8# Order of adding rule -> Order of filtering9# To filter with rules, call filter() method10class TextFilter:11 def __init__(self):12 self.rules = []13 self.rules_for_detect = []14 # Private inner classes15 class Remove_rule:16 def __init__(self, pattern):17 self.pattern = re.compile(pattern)18 def filter(self, sentence):19 return self.pattern.sub('', sentence)20 class Replace_rule:21 def __init__(self, pattern, substitute, flags=None, unicode=False):22 if unicode:23 self.pattern = re.compile(pattern, re.UNICODE)24 else:25 self.pattern = re.compile(pattern)26 self.substitute = substitute27 self.flags = flags28 def filter(self, sentence):29 if self.flags is not None:30 return self.pattern.sub(self.substitute, sentence, self.flags)31 else:32 # print(sentence)33 # if 'include' in sentence:34 # pdb.set_trace()35 return self.pattern.sub(self.substitute, sentence)36 class Detection_rule:37 def __init__(self, detect_field, pattern):38 self.detect_field = detect_field39 self.pattern = re.compile(pattern)40 def detect(self, sentence):41 out = self.pattern.search(sentence)42 data = dict()43 if out is not None:44 data[self.detect_field] = True45 return data46 data[self.detect_field] = False47 return data48 # regex sub, string replace, string pos 잡고 컷, 리무브...49 def filter(self, sentence):50 for rule in self.rules:51 sentence = rule.filter(sentence)52 sentence = sentence.strip()53 return sentence54 def detect(self, sentence):55 check_dict = dict()56 for rule in self.rules_for_detect:57 out = rule.detect(sentence)58 check_dict.update(out)59 return check_dict60 """61 위키피디아 전처리를 위한 Replace/ Remove Rule (doc open, close)62 """63 def detect_doc_open_pattern(self):64 pattern = r"^<doc.*>$"65 self.rules_for_detect.append(self.Detection_rule('doc_open', pattern))66 def detect_doc_close_pattern(self):67 pattern = r"</doc>"68 self.rules_for_detect.append(self.Detection_rule('doc_close', pattern))69 def change_match_pattern_to_subs(self, pattern, substitute):70 self.rules.append(self.Replace_rule(pattern, substitute))71 def remove_match_pattern(self, pattern):72 self.rules.append(self.Remove_rule(pattern))73 def remove_from_pattern_to_space(self, pattern):74 pattern = pattern + r'.*?($|[ \t\n\r])'75 self.rules.append(self.Remove_rule(pattern))76 def remove_from_space_to_pattern(self, pattern):77 pass78 def remove_from_pattern_to_pattern(self, pattern_start, pattern_end):79 pattern = pattern_start + r'.*?' + pattern_end80 self.rules.append(self.Remove_rule(pattern))81 def remove_http_url(self):82 pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&#+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'83 self.rules.append(self.Remove_rule(pattern))84 def remove_ftp_url(self):85 pattern = r'ftp://(?:[a-zA-Z]|[0-9]|[$-_@.&#+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'86 self.rules.append(self.Remove_rule(pattern))87 def remove_unstructured_url(self):88 pass89 def remove_emails(self):90 pattern = r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'91 self.rules.append(self.Remove_rule(pattern))92 def remove_phone_number(self):93 pattern = r'[0-9]+(-[0-9]+)+'94 self.rules.append(self.Remove_rule(pattern))95 # 한글, 한글자모, 영어대소문자, 숫자, 키보드 내 특수문자 제외하고 모두 삭제96 def remove_trash_char(self):97 pattern = r"[^가-힣a-zA-Z0-9ㄱ-ㅣ\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\|\\\[\]\{\}\;\:\'\"\,\.\<\>\?\/\t ]+"98 self.rules.append(self.Remove_rule(pattern))99 # handle wiki100 def remove_doc_open_tag(self):101 pattern = r"^<doc.*>$"102 self.rules.append(self.Remove_rule(pattern))103 # handle wiki104 def remove_doc_close_tag(self):105 pattern = r"</doc>"106 self.rules.append(self.Remove_rule(pattern))107 """108 나무위키 전처리를 위한 Replace/ Remove Rule109 """110 # handle namu-wiki111 def _replace_chinese(self):112 pattern = u'[⺀-⺙⺛-⻳⼀-⿕々〇〡-〩〸-〺〻㐀-䶵一-鿃豈-鶴侮-頻並-龎]'113 substitute = ''114 flags=None115 self.rules.append(self.Replace_rule(pattern, substitute, flags, unicode=True))116 # handle namu-wiki117 def _replace_japanese(self):118 pattern = u'[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]'119 substitute = ''120 flags=None121 self.rules.append(self.Replace_rule(pattern, substitute, flags, unicode=True))122 # remove html123 def _replace_html(self):124 pattern = r"\{\{\{#\!html[^\}]*\}\}\}"125 substitute = ''126 flags=re.IGNORECASE|re.MULTILINE|re.DOTALL127 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))128 # remove redirect129 def _replace_redirect(self):130 pattern = r"#redirect .*"131 substitute = ''132 flags=re.IGNORECASE133 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))134 # remove 분류135 def _replace_tag0(self):136 pattern = r"틀:.*"137 substitute = ''138 flags = None139 self.rules.append(self.Replace_rule(pattern, substitute, flags=flags))140 # remove 분류141 def _replace_tag1(self):142 pattern = r"\[\[분류:.*"143 substitute = ''144 flags=None145 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))146 # remove 파일147 def _replace_tag2(self):148 pattern = r"\[\[파일:.*"149 substitute = ''150 flags=None151 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))152 # remove 상위문서153 def _replace_tag3(self):154 pattern = r"\* 상위 문서 ?:.*"155 substitute = ''156 flags=None157 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))158 # remove youtube159 def _replace_tag4(self):160 pattern = r"\[youtube\(\w+\)\]"161 substitute = ''162 flags=re.IGNORECASE163 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))164 # remove include165 def _replace_tag5(self):166 pattern = r"\[(i|I)nclude\(([^\]|]*)(\|[^]]*)?\]"167 substitute = r'\1'168 flags=re.IGNORECASE169 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))170 # remove link171 def _replace_tag6(self):172 pattern = r"\[\[(?:[^\]|]*\|)?([^\]|]+)\]\]"173 substitute = r'\1'174 flags=None175 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))176 # remove 각주177 def _replace_tag7(self):178 # pattern = r"\[([^\]]*)\]"179 pattern = r"\[.*\]"180 substitute = ''181 flags = None182 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))183 # remove text color/size184 def _replace_tag8(self):185 pattern = r"\{\{\{([^\ }|]*) ([^\}|]*)\}\}\}"186 substitute = r'\2'187 flags = None188 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))189 # remove text bold190 def _replace_tag9(self):191 pattern = r"'''([^']*)'''"192 substitute = r'\1'193 flags=None194 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))195 # remove strike-through196 def _replace_tag10(self):197 pattern = r"(~~|--)([^']*)(~~|--)"198 substitute = ''199 flags=None200 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))201 # remove table202 def _replace_tag11(self):203 pattern = r"\|[\S\s]*\|"204 substitute = ''205 flags=None206 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))207 # 문단 제거208 def _replace_tag12(self):209 pattern = r"=(.*)="210 substitute = ''211 flags=None212 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))213 # 동영상 제거214 def _replace_tag13(self):215 pattern = r"width.*\/iframe>"216 substitute = ''217 flags=None218 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))219 # html tag220 def _replace_tag14(self):221 pattern = r"<.*?>"222 substitute = ''223 flags=None224 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))225 # html tag226 def _replace_tag15(self):227 pattern = r"\{\{\|([^|]*)\|\}\}"228 substitute = ''229 flags=None230 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))231 # 수식 표현 제거232 def _replace_tag16(self):233 pattern = r"\\[^가-힣]*"234 substitute = ''235 flags=None236 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))237 # 괄호 내부 내용 제거238 def _replace_tag17(self):239 pattern = r"\([^)]*\)"240 substitute = ''241 flags=None242 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))243 # include tag 제거244 def _replace_tag18(self):245 pattern = r"\[(i|I)nclude.*\]"246 substitute = ''247 flags=None248 self.rules.append(self.Replace_rule(pattern, substitute,flags=flags))249 def replace_namu_tags(self):250 self._replace_tag18()251 self._replace_chinese()252 self._replace_japanese()253 self._replace_html()254 self._replace_redirect()255 self._replace_tag0()256 self._replace_tag1()257 self._replace_tag2()258 self._replace_tag3()259 self._replace_tag4()260 self._replace_tag5()261 self._replace_tag6()262 self._replace_tag7()263 self._replace_tag8()264 self._replace_tag9()265 self._replace_tag10()266 self._replace_tag11()267 self._replace_tag12()268 self._replace_tag13()269 self._replace_tag14()270 self._replace_tag15()271 self._replace_tag16()272 self._replace_tag17()273 def spacing_on_both_side_of_mark(self):274 pattern = r'[\`\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\[\{\]\}\\\|\;\:\'\"\,\.\/\<\>\?]'275 substitute = r' \g<0> '276 self.rules.append(self.Replace_rule(pattern, substitute))277 def spacing_on_both_side_of_hangul_jamo(self):278 pattern = r'[ㄱ-ㅣ]+'279 substitute = r' \g<0> '280 self.rules.append(self.Replace_rule(pattern, substitute))281 # 문장 내부에 공백(' ', \n \t \r)이 여러번 있는 경우 space 하나로 치환282 def strip_inside(self):283 pattern = r'[ \t\n\r]+'284 substitute = ' '...

Full Screen

Full Screen

iparrest.py

Source:iparrest.py Github

copy

Full Screen

...78 if block:79 deny_rule('10.1.1.68', 150)80 deny_rule('10.1.1.6', 151)81 else:82 remove_rule(150)83 remove_rule(151)84 85def block_xbox(block):86 if block:87 deny_rule('10.1.1.90', 152)88 deny_rule('10.1.1.14', 153)89 else:90 remove_rule(152)91 remove_rule(153)92 93def block_ghome(block):94 if block:95 deny_rule('10.1.1.75', 154)96 deny_rule('10.1.1.76', 155)97 else:98 remove_rule(154)99 remove_rule(155)100 101def block_tv(block):102 if block:103 deny_rule('10.1.1.128', 156)104 else:105 remove_rule(156)106 107def clear_list():108 global black_list109 110 for ip, rule_number in black_list:111 remove_rule(rule_number)112 black_list = {}113 114def block_ip(ip, block):115 global black_list116 if block and ip not in black_list.keys():117 block_addr(ip)118 else:119 unblock_addr(ip)120 del black_list[ip]121 122def reset_fw():123 global ed_blocked, xbox_blocked, ghome_blocked, tv_blocked, reject_number, root, debug124 head = """125#!/bin/sh126 127#/sbin/natd -config /etc/natd.conf128 129/sbin/ipfw -f flush130/sbin/ipfw add 100 divert natd all from any to any via $cfg->{iface}\n131#/sbin/ipfw add allow tcp from any to any 18081 in\n132#/sbin/ipfw add fwd 10.1.1.103, 18081 tcp from any to any 18081 in\n133\n"""134 tail = """135/sbin/ipfw add 50000 allow gre from any to any keep-state136/sbin/ipfw add 50100 pass all from any to any\n"""137 if ed_blocked:138 head += '/sbin/ipfw add 150 deny ip from 10.1.1.68 to any\n'139 head += '/sbin/ipfw add 151 deny ip from 10.1.1.6 to any\n'140 if xbox_blocked:141 head += '/sbin/ipfw add 152 deny ip from 10.1.1.90 to any\n'142 head += '/sbin/ipfw add 153 deny ip from 10.1.1.14 to any\n'143 if ghome_blocked:144 head += '/sbin/ipfw add 154 deny ip from 10.1.1.75 to any\n'145 head += '/sbin/ipfw add 155 deny ip from 10.1.1.76 to any\n'146 if tv_blocked:147 head += '/sbin/ipfw add 156 deny ip from 10.1.1.128 to any\n'148 if root and not debug:149 tmp_natd = '/tmp/natd.sh'150 natd = '/usr/local/etc/rc.d/fw.sh'151 else:152 tmp_natd = './natd.sh'153 natd = './fw.sh'154 try:155 f = open(tmp_natd, 'w')156 f.write(head)157 f.write(tail)158 f.close()159 except Exception as err:160 print_out("error in 'reset_fw' : " + err)161 except:162 print_out("unexpected error in 'reset_fw' : " + sys.exc_info()[0])163 reject_number = 1000164 165 now = datetime.now()166 timestamp = str(datetime.timestamp(now))167 168 cmd = "mv {0} {1}.{2}~".format(natd, natd, timestamp)169 try:170 if root and not debug:171 os.system(cmd)172 else:173 print_out(cmd)174 cmd = 'mv {0} {1}'.format(tmp_natd, natd)175 if root and not debug:176 os.system(cmd)177 else:178 print_out(cmd)179 os.chmod(natd, stat.stat.S_ISUID)180 181 cmd = natd182 if root and not debug:183 os.system(cmd)184 else:185 print_out(cmd)186 except Exception as err:187 print_out("error in 'reset_fw' : " + err)188 except:189 print_out("unexpected error in 'reset_fw' : " + sys.exc_info()[0])190def handle_cmd(cmd, ip):191 print_out("iparrest command found : " + cmd + " " + ip)192 193 if 'add-white':194 add_white(ip)195 update_fw_file()196 elif 'show-list':197 show_list()198 update_fw_file()199 elif 'block-ed':200 block_ed(True)201 update_fw_file()202 elif 'unblock-ed':203 block_ed(False)204 update_fw_file()205 elif 'block-xbox':206 block_xbox(True)207 update_fw_file()208 elif 'unblock-xbox':209 block_xbox(False)210 update_fw_file()211 elif 'block-ghome':212 block_ghome(True)213 update_fw_file()214 elif 'unblock-ghome':215 block_ghome(False)216 update_fw_file()217 elif 'block-tv':218 block_tv(True)219 update_fw_file()220 elif 'unblock-tv':221 block_tv(False)222 update_fw_file()223 elif 'clear-list':224 clear_list()225 update_fw_file()226 elif 'unblock-ip':227 block_ip(ip, False)228 update_fw_file()229 elif 'block-ip':230 block_ip(ip, True)231 update_fw_file()232 elif 'reset-fw':233 reset_fw()234 update_fw_file()235 else:236 print_out('unknown command : ' + cmd)237def unblock_addr(ip):238 global remove_rule, black_list, cfg239 if ip in black_list.keys():240 rule_number = black_list[ip]241 cmd = remove_rule.format(cfg['ipfw'], rule_number)242 print_out(cmd)243 del black_list[ip]244def remove_rule(rule_number):245 global remove_rule, cfg246 cmd = remove_rule.format(cfg['ipfw'], rule_number)247 print_out(cmd)248def block_addr(ip):249 global reject_rule, reject_number, cfg, black_list250 cmd = reject_rule.format(cfg['ipfw'], ++reject_number, ip, cfg['if'])251 print_out(cmd)252 253 black_list[ip] = reject_number254 255def deny_addr(ip, rule_number):256 global deny_rule, cfg257 cmd = deny_rule.format(cfg['ipfw'], rule_number, ip)258 print_out(cmd)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful