from .paramminer_headers import paramminer_headers


class paramminer_cookies(paramminer_headers):
    """
    Inspired by https://github.com/PortSwigger/param-miner
    """

    watched_events = ["HTTP_RESPONSE"]
    produced_events = ["FINDING"]
    flags = ["active", "aggressive", "slow", "web-paramminer"]
    meta = {
        "description": "Smart brute-force to check for common HTTP cookie parameters",
    }
    options = {"wordlist": ""}  # default is defined separately
    options_desc = {"wordlist": "Define the wordlist to be used to derive cookies"}
    scanned_hosts = []
    boringlist = []
    max_event_handlers = 12
    in_scope_only = True
    compare_mode = "cookie"
    default_wordlist = "paramminer_parameters.txt"

    async def check_batch(self, compare_helper, url, cookie_list):
        cookies = {p: self.rand_string(14) for p in cookie_list}
        return await compare_helper.compare(url, cookies=cookies, check_reflection=(len(cookie_list) == 1))

    def gen_count_args(self, url):
        cookie_count = 40
        while 1:
            if cookie_count < 0:
                break
            fake_cookies = {self.rand_string(14): self.rand_string(14) for _ in range(0, cookie_count)}
            yield cookie_count, (url,), {"cookies": fake_cookies}
            cookie_count -= 5
