def isApplicable(userscript: Userscript) -> bool: for regex in userscript.excludePatternRegexes: if isSomething(regex.search(url)): return False for regex in userscript.includePatternRegexes: if isSomething(regex.search(url)): return True for pattern in userscript.matchPatterns: if urlmatch(pattern, url): return True return False
def inject(script: Userscript, soup: BeautifulSoup, options: Options) -> Union[BeautifulSoup, Exception]: useInline = options.inline or script.downloadURL is None tag = soup.new_tag("script") if isSomething(options.nonce): tag["nonce"] = options.nonce # Used to bypass CSP for inline-injected userscripts. tag[C.ATTRIBUTE_UP_VERSION] = C.VERSION withLoadListenerIfRunAtIdle = userscript.withEventListener( "load") if script.runAt == document_idle else idem withNoframesIfNoframes = userscript.withNoframes if script.noframes else idem try: if useInline: tag.string = "\n" + withNoframesIfNoframes( withLoadListenerIfRunAtIdle(script.content)) if script.runAt == document_end: insertLateIn(soup, tag) else: insertEarlyIn(soup, tag) else: s = "s" # JS variable name src = userscript.withVersionSuffix(script.downloadURL, script.version) JS_insertScriptTag = f"""document.head.appendChild({s});""" JS_insertionCode = (stripIndentation(f""" const {s} = document.createElement("script"); {s}.setAttribute("{C.ATTRIBUTE_UP_VERSION}", "{C.VERSION}"); {s}.src = "{src}"; {withLoadListenerIfRunAtIdle(JS_insertScriptTag)} """)) if script.runAt == document_idle or script.noframes: tag.string = withNoframesIfNoframes(JS_insertionCode) else: tag["src"] = src # Tag prepared. Insert it: if script.runAt == document_end: insertLateIn(soup, tag) else: insertEarlyIn(soup, tag) return soup except Exception as e: return e
required=False, predicate=None, ) tag_version: Tag_string = Tag_string( name=directive_version, unique=True, default=None, required=False, predicate=None, ) tag_downloadURL: Tag_string = Tag_string( name=directive_downloadURL, unique=True, default=None, required=False, predicate=lambda val: isSomething(REGEX_URL.match(val)), ) METADATA_TAGS: List[Tag] = [ tag_name, tag_run_at, tag_match, tag_noframes, tag_include, tag_exclude, tag_version, tag_downloadURL, ] validateMetadata: Callable[[Metadata], Metadata] = metadata.validator(METADATA_TAGS)
def isIncludePattern_regex(pattern: str) -> bool: return isSomething(re.compile(REGEX_INCLUDE_REGEX).match(pattern))
def isIncludePattern(pattern: str) -> bool: return isSomething(REGEX_INCLUDE_PATTERN.match(pattern))
def isMatchPattern(pattern: str) -> bool: return isSomething(REGEX_MATCH_PATTERN.match(pattern))
def printWelcomeMessage(): print("") print("╔═" + "═" * len(T.INFO_MESSAGE) + "═╗") print("║ " + T.INFO_MESSAGE + " ║") print("╚═" + "═" * len(T.INFO_MESSAGE) + "═╝") print("") try: args = argparser.parse_args() printWelcomeMessage() glob_ignore = args.ignore glob_intercept = args.intercept globPattern = (glob_intercept if isSomething(glob_intercept) else glob_ignore if isSomething(glob_ignore) else None) useFiltering = globPattern is not None regex: str = MATCH_NO_HOSTS if useFiltering: useIntercept = isSomething(glob_intercept) print(f"Reading {'intercept' if useIntercept else 'ignore'} rules ...") filenames: List[str] = [ shlex.quote(unsafeFilename) for unsafeFilename in glob.glob(globPattern) ] ruleFilesContent: str = reduce(readRuleFile, filenames, "") maybeNegate = ignore.negate if useIntercept else idem regex = maybeNegate(ignore.entireIgnoreRegex(ruleFilesContent)) print( f"Traffic from hosts matching any of these rules will be {'INTERCEPTED' if useIntercept else 'IGNORED'} by mitmproxy:"
def isCommentLine(s: str) -> bool: return isSomething(re.compile(r"^\s*" + PREFIX_COMMENT + r".*$").match(s))
def isWhitespaceLine(s: str) -> bool: return isSomething(re.compile(r"^\s*$").match(s))
def source(injection: Injection) -> str: if isSomething(injection.nonce): return f"'nonce-{injection.nonce}'" else: # MDN about host (i.e. download URL) sources: "Unlike other values below, single quotes shouldn't be used." return injection.userscript.downloadURL