|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf -*- |
| 3 | + |
| 4 | + |
| 5 | +import socket |
| 6 | +import fcntl |
| 7 | +import struct |
| 8 | + |
| 9 | +import uuid |
| 10 | +import sys |
| 11 | +import random |
| 12 | + |
| 13 | +import argparse |
| 14 | +import functools |
| 15 | + |
| 16 | + |
| 17 | +from multiprocessing import Pool |
| 18 | + |
| 19 | +from httplib import HTTPConnection |
| 20 | + |
| 21 | +PORT = "2060" |
| 22 | + |
| 23 | + |
| 24 | +def main(targetIF, prefix, maxI): |
| 25 | + target = get_ip_address(targetIF) |
| 26 | + for i in xrange(int(maxI)): |
| 27 | + main_single(target, prefix, i) |
| 28 | + |
| 29 | +def main_single(target, prefix, i): |
| 30 | + source = get_ip_address(prefix + str(i)) |
| 31 | + # source_address requires python 2.7 |
| 32 | + # urllib2 does not nicely expose source_address, so use |
| 33 | + # lower-level API |
| 34 | + conn = HTTPConnection(target, PORT, timeout=10, source_address=(source, 0)) |
| 35 | + conn.connect() |
| 36 | + conn.request("GET", "/") |
| 37 | + try: |
| 38 | + resp = conn.getresponse() |
| 39 | + resp.read() |
| 40 | + except: |
| 41 | + pass |
| 42 | + conn = HTTPConnection(target, PORT, timeout=10, source_address=(source, 0)) |
| 43 | + conn.connect() |
| 44 | + token = str(uuid.uuid4()) |
| 45 | + conn.request("GET", "/wifidog/auth?token=" + token ) |
| 46 | + try: |
| 47 | + resp = conn.getresponse() |
| 48 | + # this causes wifidog to ask our mock auth server if the token is |
| 49 | + # correct |
| 50 | + resp.read() |
| 51 | + except: |
| 52 | + pass |
| 53 | + # log out sometimes |
| 54 | + if random.choice([True, False, False]): |
| 55 | + conn = HTTPConnection(target, PORT, timeout=10, source_address=(source, 0)) |
| 56 | + conn.connect() |
| 57 | + conn.request("GET", "/wifidog/auth?logout=1&token=" + token) |
| 58 | + try: |
| 59 | + resp = conn.getresponse() |
| 60 | + resp.read() |
| 61 | + except: |
| 62 | + pass |
| 63 | + |
| 64 | + |
| 65 | +# http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/ |
| 66 | +def get_ip_address(ifname): |
| 67 | + print "ifname: %s" % ifname |
| 68 | + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 69 | + return socket.inet_ntoa(fcntl.ioctl( |
| 70 | + s.fileno(), |
| 71 | + 0x8915, # SIOCGIFADDR |
| 72 | + struct.pack('256s', ifname[:15]) |
| 73 | + )[20:24]) |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + |
| 77 | + parser = argparse.ArgumentParser(description='Hammer a wifidog instance with requests') |
| 78 | + parser.add_argument('--target-interface', required=True, |
| 79 | + help='Interface where Wifidog is listening') |
| 80 | + parser.add_argument('--source-interface-prefix', required=True, |
| 81 | + help='Prefix of the virtual interfaces from which Wifidog is exercised.') |
| 82 | + parser.add_argument('--source-interface-count', required=True, |
| 83 | + help='Number of virtual interfaces, where interface is prefix+index') |
| 84 | + parser.add_argument('--process-count', required=True, |
| 85 | + help='How many processes to run') |
| 86 | + |
| 87 | + args = parser.parse_args() |
| 88 | + |
| 89 | + target = get_ip_address(args.target_interface) |
| 90 | + p = Pool(int(args.process_count)) |
| 91 | + partial = functools.partial(main_single, target, args.source_interface_prefix) |
| 92 | + while True: |
| 93 | + p.map(partial, list(xrange(int(args.source_interface_count)))) |
| 94 | + |
0 commit comments