kaniini / antissh

An IRC bot which monitors for compromised embedded devices being used as proxies.
Other
126 stars 18 forks source link

Performance: Allow early termination in check_with_credentials_group #38

Closed ghost closed 6 years ago

ghost commented 6 years ago

See https://github.com/kaniini/antissh/blob/465b43f6953766a7ede16b41198fc35ea16b422b/antissh.py#L228

Right now, it uses the following code:

async def check_with_credentials_group(ip, target_ip, target_port, credentials_group=DEFAULT_CREDENTIALS):
    futures = [check_with_credentials(ip, target_ip, target_port, c[0], c[1]) for c in credentials_group]
    results = await asyncio.gather(*futures)

    return True in results

This unnecessarily waits for results to be populated before performing the check True in results. A better approach would be something like:

async def check_with_credentials_group(ip, target_ip, target_port, credentials_group=DEFAULT_CREDENTIALS):
    futures = asyncio.as_completed(map(lambda c: check_with_credentials(ip, target_ip, target_port, c[0], c[1]), credentials_group))
    for future in futures:
        result = await future
        if result:
            return True

    return False