| 1 |
import os |
| 2 |
import subprocess |
| 3 |
import xmlrpc.server as rpc |
| 4 |
import time |
| 5 |
import sys |
| 6 |
import multiprocessing as mp |
| 7 |
|
| 8 |
try: |
| 9 |
import pydivert |
| 10 |
except ImportError: |
| 11 |
pass |
| 12 |
|
| 13 |
|
| 14 |
class LinuxDelayHandler: |
| 15 |
@staticmethod |
| 16 |
def get_interface_name(): |
| 17 |
""" Get the first interface name that is not the localhost. """ |
| 18 |
net = os.listdir("/sys/class/net") |
| 19 |
net.remove("lo") |
| 20 |
if "eth0" in net: |
| 21 |
return "eth0" |
| 22 |
return net[0] |
| 23 |
|
| 24 |
def set_delay(self, sec, duration=10.0): |
| 25 |
""" Set network delay, return with the call's result. """ |
| 26 |
try: |
| 27 |
subprocess.check_call( |
| 28 |
[ |
| 29 |
"tc", |
| 30 |
"qdisc", |
| 31 |
"add", |
| 32 |
"dev", |
| 33 |
self.get_interface_name(), |
| 34 |
"root", |
| 35 |
"handle", |
| 36 |
"1:", |
| 37 |
"prio", |
| 38 |
] |
| 39 |
) |
| 40 |
subprocess.check_call( |
| 41 |
[ |
| 42 |
"tc", |
| 43 |
"qdisc", |
| 44 |
"add", |
| 45 |
"dev", |
| 46 |
self.get_interface_name(), |
| 47 |
"parent", |
| 48 |
"1:3", |
| 49 |
"handle", |
| 50 |
"30:", |
| 51 |
"netem", |
| 52 |
"delay", |
| 53 |
("%dmsec" % (sec * 1000)), |
| 54 |
] |
| 55 |
) |
| 56 |
for port in ("389", "636"): |
| 57 |
subprocess.check_call( |
| 58 |
[ |
| 59 |
"tc", |
| 60 |
"filter", |
| 61 |
"add", |
| 62 |
"dev", |
| 63 |
self.get_interface_name(), |
| 64 |
"protocol", |
| 65 |
"ip", |
| 66 |
"parent", |
| 67 |
"1:0", |
| 68 |
"u32", |
| 69 |
"match", |
| 70 |
"ip", |
| 71 |
"sport", |
| 72 |
port, |
| 73 |
"0xffff", |
| 74 |
"flowid", |
| 75 |
"1:3", |
| 76 |
] |
| 77 |
) |
| 78 |
|
| 79 |
return True |
| 80 |
except subprocess.CalledProcessError: |
| 81 |
return False |
| 82 |
|
| 83 |
def remove_delay(self): |
| 84 |
""" Remove network delay. """ |
| 85 |
try: |
| 86 |
subprocess.check_call( |
| 87 |
["tc", "qdisc", "del", "dev", self.get_interface_name(), "root"] |
| 88 |
) |
| 89 |
return True |
| 90 |
except subprocess.CalledProcessError: |
| 91 |
return False |
| 92 |
|
| 93 |
|
| 94 |
class MacDelayHandler: |
| 95 |
def set_delay(self, sec, duration=10.0): |
| 96 |
with open("/etc/pf.conf") as fp: |
| 97 |
conf = fp.read() |
| 98 |
conf += '\ndummynet-anchor "mop"\nanchor "mop"\n' |
| 99 |
rule = ( |
| 100 |
"dummynet in quick proto tcp from any to any port {389, 636} pipe 1\n" |
| 101 |
) |
| 102 |
|
| 103 |
try: |
| 104 |
subprocess.run( |
| 105 |
["pfctl", "-f", "-"], input=conf, encoding="utf-8", check=True |
| 106 |
) |
| 107 |
subprocess.run( |
| 108 |
["pfctl", "-a", "mop", "-f", "-"], |
| 109 |
input=rule, |
| 110 |
encoding="utf-8", |
| 111 |
check=True, |
| 112 |
) |
| 113 |
subprocess.check_call( |
| 114 |
["dnctl", "pipe", "1", "config", "delay", "%d" % int(sec * 1000)] |
| 115 |
) |
| 116 |
|
| 117 |
return True |
| 118 |
except subprocess.CalledProcessError: |
| 119 |
return False |
| 120 |
|
| 121 |
def remove_delay(self): |
| 122 |
try: |
| 123 |
subprocess.check_call(["dnctl", "-q", "flush"]) |
| 124 |
subprocess.check_call(["pfctl", "-f", "/etc/pf.conf"]) |
| 125 |
return True |
| 126 |
except subprocess.CalledProcessError: |
| 127 |
return False |
| 128 |
|
| 129 |
|
| 130 |
class WinDelayHandler: |
| 131 |
proc = None |
| 132 |
|
| 133 |
def delay(self, sec, duration=10.0): |
| 134 |
netfil = "tcp.DstPort == 389 or tcp.SrcPort == 389" |
| 135 |
start = time.time() |
| 136 |
with pydivert.WinDivert(netfil) as divert: |
| 137 |
for packet in divert: |
| 138 |
time.sleep(sec) |
| 139 |
divert.send(packet) |
| 140 |
if time.time() - start >= duration: |
| 141 |
break |
| 142 |
|
| 143 |
def set_delay(self, sec, duration=10.0): |
| 144 |
""" Set network delay, return with the call's result. """ |
| 145 |
self.proc = mp.Process(target=self.delay, args=(sec, duration)) |
| 146 |
self.proc.start() |
| 147 |
return True |
| 148 |
|
| 149 |
def remove_delay(self): |
| 150 |
""" Remove network delay, return with the call's result. """ |
| 151 |
if self.proc is not None and self.proc.is_alive(): |
| 152 |
self.proc.terminate() |
| 153 |
return True |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
if sys.platform == "win32": |
| 158 |
handler = WinDelayHandler() |
| 159 |
elif sys.platform == "darwin": |
| 160 |
handler = MacDelayHandler() |
| 161 |
else: |
| 162 |
handler = LinuxDelayHandler() |
| 163 |
|
| 164 |
subprocess.call( |
| 165 |
["ip", "link", "set", handler.get_interface_name(), "qlen", "1000"] |
| 166 |
) |
| 167 |
|
| 168 |
server = rpc.SimpleXMLRPCServer(("0.0.0.0", 8000)) |
| 169 |
server.register_function(handler.set_delay, "set_delay") |
| 170 |
server.register_function(handler.remove_delay, "remove_delay") |
| 171 |
server.serve_forever() |