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