<commit_before>'''Implements one evil minion'''

from distutils.dir_util import mkpath
import hashlib
import logging
import os
from uuid import UUID, uuid5

import tornado.gen
import zmq

import salt.transport.client

from evilminions.utils import replace_recursively, fun_call_id

class HydraHead(object):
    '''Replicates the behavior of a minion'''
    def __init__(self, minion_id, io_loop, keysize, opts, grains, ramp_up_delay, slowdown_factor, reactions):
        self.minion_id = minion_id
        self.io_loop = io_loop
        self.ramp_up_delay = ramp_up_delay
        self.slowdown_factor = slowdown_factor
        self.reactions = reactions
        self.current_time = 0

        self.current_jobs = []

        # Compute replacement dict
        self.replacements = {
            grains['id']: minion_id,
            grains['machine_id']: hashlib.md5(minion_id).hexdigest(),
            grains['uuid']: str(uuid5(UUID('d77ed710-0deb-47d9-b053-f2fa2ef78106'), minion_id))
        }

        # Override ID settings
        self.opts = opts.copy()
        self.opts['id'] = minion_id

        # Override calculated settings
        self.opts['master_uri'] = 'tcp://%s:4506' % self.opts['master']
        self.opts['master_ip'] = self.opts['master']

        # Override directory settings
        pki_dir = '/tmp/%s' % minion_id
        mkpath(pki_dir)

        cache_dir = os.path.join(pki_dir, 'cache', 'minion')
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        sock_dir = os.path.join(pki_dir, 'sock', 'minion')
        if not os.path.exists(sock_dir):
            os.makedirs(sock_dir)

        self.opts['pki_dir'] = pki_dir
        self.opts['sock_dir'] = sock_dir
        self.opts['cache_dir'] = cache_dir

        # Override performance settings
        self.opts['keysize'] = keysize
        self.opts['acceptance_wait_time'] = 10
        self.opts['acceptance_wait_time_max'] = 0
        self.opts['zmq_filtering'] = False
        self.opts['tcp_keepalive'] = True
        self.opts['tcp_keepalive_idle'] = 300
        self.opts['tcp_keepalive_cnt'] = -1
        self.opts['tcp_keepalive_intvl'] = -1
        self.opts['recon_max'] = 10000
        self.opts['recon_default'] = 1000
        self.opts['recon_randomize'] = True
        self.opts['ipv6'] = False
        self.opts['zmq_monitor'] = False
        self.opts['open_mode'] = False
        self.opts['verify_master_pubkey_sign'] = False
        self.opts['always_verify_signature'] = False

    @tornado.gen.coroutine
    def start(self):
        '''Opens ZeroMQ sockets, starts listening to PUB events and kicks off initial REQs'''
        self.log = logging.getLogger(__name__)
        yield tornado.gen.sleep(self.ramp_up_delay)
        self.log.info("HydraHead %s started", self.opts['id'])
<commit_msg>Bugfix: be particularly patient when connecting<commit_after>'''Implements one evil minion'''

from distutils.dir_util import mkpath
import hashlib
import logging
import os
from uuid import UUID, uuid5

import tornado.gen
import zmq

import salt.transport.client

from evilminions.utils import replace_recursively, fun_call_id

class HydraHead(object):
    '''Replicates the behavior of a minion'''
    def __init__(self, minion_id, io_loop, keysize, opts, grains, ramp_up_delay, slowdown_factor, reactions):
        self.minion_id = minion_id
        self.io_loop = io_loop
        self.ramp_up_delay = ramp_up_delay
        self.slowdown_factor = slowdown_factor
        self.reactions = reactions
        self.current_time = 0

        self.current_jobs = []

        # Compute replacement dict
        self.replacements = {
            grains['id']: minion_id,
            grains['machine_id']: hashlib.md5(minion_id).hexdigest(),
            grains['uuid']: str(uuid5(UUID('d77ed710-0deb-47d9-b053-f2fa2ef78106'), minion_id))
        }

        # Override ID settings
        self.opts = opts.copy()
        self.opts['id'] = minion_id

        # Override calculated settings
        self.opts['master_uri'] = 'tcp://%s:4506' % self.opts['master']
        self.opts['master_ip'] = self.opts['master']

        # Override directory settings
        pki_dir = '/tmp/%s' % minion_id
        mkpath(pki_dir)

        cache_dir = os.path.join(pki_dir, 'cache', 'minion')
        if not os.path.exists(cache_dir):
            os.makedirs(cache_dir)

        sock_dir = os.path.join(pki_dir, 'sock', 'minion')
        if not os.path.exists(sock_dir):
            os.makedirs(sock_dir)

        self.opts['pki_dir'] = pki_dir
        self.opts['sock_dir'] = sock_dir
        self.opts['cache_dir'] = cache_dir

        # Override performance settings
        self.opts['keysize'] = keysize
        self.opts['acceptance_wait_time'] = 10
        self.opts['acceptance_wait_time_max'] = 0
        self.opts['auth_tries'] = 600
        self.opts['zmq_filtering'] = False
        self.opts['tcp_keepalive'] = True
        self.opts['tcp_keepalive_idle'] = 300
        self.opts['tcp_keepalive_cnt'] = -1
        self.opts['tcp_keepalive_intvl'] = -1
        self.opts['recon_max'] = 10000
        self.opts['recon_default'] = 1000
        self.opts['recon_randomize'] = True
        self.opts['ipv6'] = False
        self.opts['zmq_monitor'] = False
        self.opts['open_mode'] = False
        self.opts['verify_master_pubkey_sign'] = False
        self.opts['always_verify_signature'] = False

    @tornado.gen.coroutine
    def start(self):
        '''Opens ZeroMQ sockets, starts listening to PUB events and kicks off initial REQs'''
        self.log = logging.getLogger(__name__)
        yield tornado.gen.sleep(self.ramp_up_delay)
        self.log.info("HydraHead %s started", self.opts['id'])

{'fraction_non_alphanumeric': 0.11909758656873033, 'fraction_numerical': 0.01626442812172088, 'mean_word_length': 2.728161668839635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4405995', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1854, 'n_words': 357}