-
-
Save TWarszawski/a0d8dd8aea9eb5b774d64c9f826de6db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python26 | |
# Script for adding artificial delay to mysql using unix sockets | |
# Adds another socket and effectively man-in-the-middles the connection | |
# To use, change the settings on the application to point the database to | |
# to the temporary socket location, then run network_delay.py, then start | |
# the application | |
import os, os.path | |
import signal | |
import socket | |
import stat | |
import sys | |
from threading import Thread, Lock | |
import time | |
class Forwarder(Thread): | |
def __init__(self, sockin, sockout): | |
Thread.__init__(self) | |
self.sockin = sockin | |
self.sockout = sockout | |
def run(self): | |
while True: | |
data = self.sockin.recv(4096) | |
try: | |
time.sleep(0.1) | |
self.sockout.sendall(data) | |
except: | |
pass | |
mysql_sockfile = # mysql socket location here | |
proxy_sockfile = # temp socket location here | |
def signal_handler(signal, frame): | |
print "-" * 20 | |
print "Shutting down..." | |
mysql_server.close() | |
proxy_server.close() | |
os.remove( proxy_sockfile ) | |
print "Done" | |
sys.exit(0) | |
signal.signal(signal.SIGINT, signal_handler) | |
if os.path.exists( proxy_sockfile ): | |
os.remove( proxy_sockfile ) | |
print "Opening socket..." | |
proxy_server = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM ) | |
proxy_server.bind(proxy_sockfile) | |
proxy_server.listen(5) | |
os.chmod(proxy_sockfile, os.stat(proxy_sockfile).st_mode | stat.S_IWOTH) | |
print "Listening..." | |
while True: | |
conn, addr = proxy_server.accept() | |
mysql_server = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM ) | |
mysql_server.connect(mysql_sockfile) | |
print 'accepted connection' | |
f1 = Forwarder(conn, mysql_server) | |
f2 = Forwarder(mysql_server, conn) | |
f1.start() | |
f2.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment