Beispiel #1
0
def _main():
    """Checks arguments, permissions and runs a network emulation."""
    if os.name != 'posix':
        print >> sys.stderr, 'This script is only supported on Linux and Mac.'
        return 1

    options = _parse_args()

    # Build a configuration object. Override any preset configuration settings if
    # a value of a setting was also given as a flag.
    connection_config = _PRESETS_DICT[options.preset]
    if options.receive_bw:
        connection_config.receive_bw_kbps = options.receive_bw
    if options.send_bw:
        connection_config.send_bw_kbps = options.send_bw
    if options.delay:
        connection_config.delay_ms = options.delay
    if options.packet_loss:
        connection_config.packet_loss_percent = options.packet_loss
    if options.queue:
        connection_config.queue_slots = options.queue

    emulator = network_emulator.NetworkEmulator(connection_config,
                                                options.port_range)
    try:
        emulator.check_permissions()
    except network_emulator.NetworkEmulatorError as e:
        logging.error('Error: %s\n\nCause: %s', e.msg, e.error)
        return -1

    if not options.target_ip:
        external_ip = _get_external_ip()
    else:
        external_ip = options.target_ip

    logging.info('Constraining traffic to/from IP: %s', external_ip)
    try:
        emulator.emulate(external_ip)
        logging.info(
            'Started network emulation with the following configuration:\n'
            '  Receive bandwidth: %s kbps (%s kB/s)\n'
            '  Send bandwidth   : %s kbps (%s kB/s)\n'
            '  Delay            : %s ms\n'
            '  Packet loss      : %s %%\n'
            '  Queue slots      : %s', connection_config.receive_bw_kbps,
            connection_config.receive_bw_kbps / 8,
            connection_config.send_bw_kbps, connection_config.send_bw_kbps / 8,
            connection_config.delay_ms, connection_config.packet_loss_percent,
            connection_config.queue_slots)
        logging.info('Affected traffic: IP traffic on ports %s-%s',
                     options.port_range[0], options.port_range[1])
        raw_input('Press Enter to abort Network Emulation...')
        logging.info('Flushing all Dummynet rules...')
        emulator.cleanup()
        logging.info('Completed Network Emulation.')
        return 0
    except network_emulator.NetworkEmulatorError as e:
        logging.error('Error: %s\n\nCause: %s', e.msg, e.error)
        return -2
Beispiel #2
0
def main():
    options = _ParseArgs()

    # Build a configuration object. Override any preset configuration settings if
    # a value of a setting was also given as a flag.
    connection_config = _PRESETS_DICT[options.preset]
    if options.receive_bw is not _DEFAULT_PRESET.receive_bw_kbps:
        connection_config.receive_bw_kbps = options.receive_bw
    if options.send_bw is not _DEFAULT_PRESET.send_bw_kbps:
        connection_config.send_bw_kbps = options.send_bw
    if options.delay is not _DEFAULT_PRESET.delay_ms:
        connection_config.delay_ms = options.delay
    if options.packet_loss is not _DEFAULT_PRESET.packet_loss_percent:
        connection_config.packet_loss_percent = options.packet_loss
    if options.queue is not _DEFAULT_PRESET.queue_slots:
        connection_config.queue_slots = options.queue
    emulator = network_emulator.NetworkEmulator(connection_config,
                                                options.port_range)
    try:
        emulator.CheckPermissions()
    except network_emulator.NetworkEmulatorError as e:
        logging.error('Error: %s\n\nCause: %s', e.fail_msg, e.error)
        return -1

    if not options.target_ip:
        external_ip = _GetExternalIp()
    else:
        external_ip = options.target_ip

    logging.info('Constraining traffic to/from IP: %s', external_ip)
    try:
        emulator.Emulate(external_ip)
        logging.info(
            'Started network emulation with the following configuration:\n'
            '  Receive bandwidth: %s kbps (%s kB/s)\n'
            '  Send bandwidth   : %s kbps (%s kB/s)\n'
            '  Delay            : %s ms\n'
            '  Packet loss      : %s %%\n'
            '  Queue slots      : %s', connection_config.receive_bw_kbps,
            connection_config.receive_bw_kbps / 8,
            connection_config.send_bw_kbps, connection_config.send_bw_kbps / 8,
            connection_config.delay_ms, connection_config.packet_loss_percent,
            connection_config.queue_slots)
        logging.info('Affected traffic: IP traffic on ports %s-%s',
                     options.port_range[0], options.port_range[1])
        input('Press Enter to abort Network Emulation...')
        logging.info('Flushing all Dummynet rules...')
        network_emulator.Cleanup()
        logging.info('Completed Network Emulation.')
        return 0
    except network_emulator.NetworkEmulatorError as e:
        logging.error('Error: %s\n\nCause: %s', e.fail_msg, e.error)
        return -2
Beispiel #3
0
def main(input_file, runs, launchChrome=False):
    start = timeit.default_timer()
    _domains_dir = 'domains_list/'
    archive_file = '/home/savargas/DevRegions/data/02_26_18_Africa_sites/testbed/recordings/recording1/archive.wprgo'
    # archive_file = "/home/savargas/DevRegions/data/02_03_18_US_sites/testbed/recordings/recording1/archive.wprgo"
    config_file = 'confs/netProfiles.2g.json'
    _change_resolv_conf('127.0.0.1')
    with open(config_file, 'r') as f:
        default_net_profile = json.load(f)[0]
        _path = os.path.join(default_net_profile['device_type'] + '_' +
                             default_net_profile['name'])
        webDnsSetup.clear_folder(_path)
    with open(input_file) as _sites:
        # _sites = [x for x in _sites]
        for _site in _sites:
            _site = _site.strip()
            print('Navigating to: ' + _site)
            s1 = urlparse(_site)
            print(s1)
            _site_data_folder = os.path.join(_path, s1.netloc)
            if not os.path.isdir(_site_data_folder):
                os.mkdir(_site_data_folder)
                os.mkdir(os.path.join(_site_data_folder, 'dns'))
            with open(os.path.join(_domains_dir, s1.netloc + '.txt'),
                      newline='') as f:
                _domains = csv.reader(f, delimiter=',')
                _domains = [x for x in _domains][0]
            _domains = [x.rstrip('.') for x in _domains if x[:-1]]
            _domains.sort()
            ### ping Delays
            netp = webDnsSetup.ping_delays(_domains, default_net_profile)
            netns = network_emulator.NetworkEmulator('desktop', _domains, netp,
                                                     default_net_profile)
            print('Setting up namespaces ...')
            netns.setup_namespace()
            print('Setting up link profiles ...')
            netns.set_profile()
            ### DNS delays
            time.sleep(5)
            dnsHandler = webDnsSetup.setup_dns(_domains)
            webDnsSetup.setup_replay(_domains, archive_file)
            time.sleep(30)
            for run_no in range(runs):
                _run_data_folder = os.path.join(_site_data_folder,
                                                'run_' + str(run_no))
                if not os.path.isdir(_run_data_folder):
                    os.mkdir(_run_data_folder)
                    _subfolders = [
                        'trace', 'screenshot', 'analysis', 'summary'
                    ]
                    for folder in _subfolders:
                        os.mkdir(os.path.join(_run_data_folder, folder))
                print('Current profile: ' +
                      default_net_profile['device_type'] + ' - ' +
                      default_net_profile['name'] + ' run_no: ' + str(run_no) +
                      ' site: ' + _site)
                time.sleep(15)

                _trace_folder = os.path.join(_run_data_folder, 'trace')
                _screenshot_folder = os.path.join(_run_data_folder,
                                                  'screenshot')
                _summary_folder = os.path.join(_run_data_folder, 'summary')
                _trace_file = os.path.join(_trace_folder,
                                           str(run_no) + '_' + s1.netloc)
                _screenshot_file = os.path.join(_screenshot_folder,
                                                str(run_no) + '_' + s1.netloc)
                _summary_file = os.path.join(_summary_folder,
                                             str(run_no) + '_' + s1.netloc)
                _launch_chrome = str(launchChrome)
                logging.info(_trace_file, _screenshot_file, _summary_file)
                time.sleep(5)
                try:
                    node = 'node'
                    _node_cmd = [
                        node, 'chrome_launcher.js', _site, _trace_file,
                        _summary_file, _screenshot_file, _launch_chrome
                    ]
                    _cmd = _node_cmd
                    # print("Node Command", _cmd)
                    subprocess.call(_cmd, timeout=110)
                except subprocess.TimeoutExpired:
                    print("Timeout:  ", _site, run_no)
                    with open(os.path.join(_site_data_folder, 'log.txt'),
                              'w+') as _log:
                        _log.write("Timed out:  " + _site + ' ' + str(run_no) +
                                   '\n')
                finally:
                    os.system('pkill node')
            os.system('pkill wpr')
            dnsHandler.terminate()
            time.sleep(5)
    webDnsSetup.clear_ip_tables()
    stop = timeit.default_timer()
    logging.info(100 * '-' + '\nTotal time: ' + str(stop - start))
    _change_resolv_conf('8.8.8.8')