diff options
author | Mike Crute <mcrute@gmail.com> | 2015-12-13 17:44:39 -0800 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2015-12-13 17:44:39 -0800 |
commit | 9e38188d57b584a993eb78caa15c886c01ea8852 (patch) | |
tree | b01b34c30a7cc8a24756194638d72a4dbe41d2b3 | |
download | ubiquiti_mongodb-9e38188d57b584a993eb78caa15c886c01ea8852.tar.bz2 ubiquiti_mongodb-9e38188d57b584a993eb78caa15c886c01ea8852.tar.xz ubiquiti_mongodb-9e38188d57b584a993eb78caa15c886c01ea8852.zip |
Initial import
-rwxr-xr-x | redirecting_proxy.py | 84 | ||||
-rwxr-xr-x | translating_proxy.py | 81 |
2 files changed, 165 insertions, 0 deletions
diff --git a/redirecting_proxy.py b/redirecting_proxy.py new file mode 100755 index 0000000..29f5df4 --- /dev/null +++ b/redirecting_proxy.py | |||
@@ -0,0 +1,84 @@ | |||
1 | #!/usr/bin/python | ||
2 | import time | ||
3 | import select | ||
4 | import socket | ||
5 | |||
6 | |||
7 | class Proxy: | ||
8 | |||
9 | buffer_size = 4096 * 2 | ||
10 | delay = 0.0001 | ||
11 | |||
12 | def __init__(self, host_port, to_host_port): | ||
13 | self.forward_to = to_host_port | ||
14 | self.input_list = [] | ||
15 | self.channel = {} | ||
16 | |||
17 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
18 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
19 | self.server.bind(host_port) | ||
20 | self.server.listen(200) | ||
21 | |||
22 | def main_loop(self): | ||
23 | self.input_list.append(self.server) | ||
24 | |||
25 | while time.sleep(self.delay) or True: | ||
26 | for self.s in select.select(self.input_list, [], [])[0]: | ||
27 | if self.s == self.server: | ||
28 | self.on_accept() | ||
29 | break | ||
30 | |||
31 | self.data = self.s.recv(self.buffer_size) | ||
32 | if len(self.data) == 0: | ||
33 | self.on_close() | ||
34 | break | ||
35 | else: | ||
36 | self.on_recv() | ||
37 | |||
38 | def on_accept(self): | ||
39 | forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
40 | clientsock, clientaddr = self.server.accept() | ||
41 | try: | ||
42 | forward.connect(self.forward_to) | ||
43 | |||
44 | self.input_list.append(clientsock) | ||
45 | self.input_list.append(forward) | ||
46 | self.channel[clientsock] = forward | ||
47 | self.channel[forward] = clientsock | ||
48 | except Exception: | ||
49 | clientsock.close() | ||
50 | |||
51 | def on_close(self): | ||
52 | out = self.channel[self.s] | ||
53 | |||
54 | self.input_list.remove(self.s) | ||
55 | self.input_list.remove(self.channel[self.s]) | ||
56 | |||
57 | self.channel[out].close() | ||
58 | self.channel[self.s].close() | ||
59 | |||
60 | del self.channel[out] | ||
61 | del self.channel[self.s] | ||
62 | |||
63 | def on_recv(self): | ||
64 | if self.allow_data(): | ||
65 | print repr(self.data) | ||
66 | self.channel[self.s].send(self.data) | ||
67 | |||
68 | def allow_data(self): | ||
69 | return not ("admin.$cmd" in self.data and "shutdown" in self.data) | ||
70 | |||
71 | |||
72 | if __name__ == '__main__': | ||
73 | import os, sys | ||
74 | |||
75 | if len(sys.argv) < 3 or ":" not in sys.argv[2]: | ||
76 | program = os.path.basename(sys.argv[0]) | ||
77 | print "usage: {} <port> <to_host>:<to_port>".format(program) | ||
78 | sys.exit(1) | ||
79 | |||
80 | try: | ||
81 | to_host, to_port = sys.argv[2].split(":") | ||
82 | Proxy(('', int(sys.argv[1])), (to_host, int(to_port))).main_loop() | ||
83 | except KeyboardInterrupt: | ||
84 | sys.exit(1) | ||
diff --git a/translating_proxy.py b/translating_proxy.py new file mode 100755 index 0000000..7e03e1b --- /dev/null +++ b/translating_proxy.py | |||
@@ -0,0 +1,81 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | import struct | ||
4 | from bson import BSON | ||
5 | from StringIO import StringIO | ||
6 | |||
7 | |||
8 | DB_OFFSET = 16 + 4 # headers + flags | ||
9 | |||
10 | OP_REPLY = 1 | ||
11 | OP_UPDATE = 2001 | ||
12 | OP_INSERT = 2002 | ||
13 | OP_QUERY = 2004 | ||
14 | OP_GET_MORE = 2005 | ||
15 | OP_DELETE = 2006 | ||
16 | |||
17 | # Touchup DB | ||
18 | OP_TOUCHUP_DB = set((OP_UPDATE, OP_INSERT, OP_QUERY, OP_GET_MORE, OP_DELETE)) | ||
19 | |||
20 | def fixup_ns(raw_data, replace_with): | ||
21 | try: | ||
22 | data = BSON(raw_data).decode() | ||
23 | except: | ||
24 | return | ||
25 | |||
26 | if "ns" in data: | ||
27 | db, collection = data["ns"].split(".", 1) | ||
28 | data["ns"] = ".".join((replace_with, collection)) | ||
29 | return BSON.encode(data) | ||
30 | else: | ||
31 | return raw_data | ||
32 | |||
33 | |||
34 | def fixup_db(data, replace_with): | ||
35 | null = data.index("\x00") | ||
36 | db, collection = data[:null].split(".", 1) | ||
37 | replace_with = "admin" if db == "admin" else replace_with | ||
38 | fixed_up = "{}.{}\x00".format(replace_with, collection) | ||
39 | return fixed_up, (collection == "system.indexes") | ||
40 | |||
41 | |||
42 | def handle_line(line): | ||
43 | output = StringIO() | ||
44 | |||
45 | length, req_id, resp_to, msg_type, flags = struct.unpack_from("<iiiii", line) | ||
46 | |||
47 | print msg_type | ||
48 | |||
49 | if msg_type not in OP_TOUCHUP_DB: | ||
50 | return | ||
51 | |||
52 | db_collection, rewrite_body = fixup_db(line[DB_OFFSET:], "mfi") | ||
53 | |||
54 | output.write(struct.pack("<iiii", req_id, resp_to, msg_type, flags)) | ||
55 | output.write(db_collection) | ||
56 | |||
57 | if rewrite_body: | ||
58 | body_start = DB_OFFSET + len(db_collection) + 8 | ||
59 | print repr(line[body_start:]) | ||
60 | print BSON(line[body_start:]).decode() | ||
61 | output.write(fixup_ns(line[body_start:], "mfi")) | ||
62 | else: | ||
63 | body_start = DB_OFFSET + len(db_collection) | ||
64 | body = struct.pack("<i", 2280) + line[body_start+4:] | ||
65 | print struct.unpack_from("<i", line), len(body) | ||
66 | print repr(body) | ||
67 | print BSON(body).decode() | ||
68 | output.write(line[body_start:]) | ||
69 | |||
70 | value = output.getvalue() | ||
71 | # print repr(struct.pack("<i", len(value)) + value) | ||
72 | |||
73 | |||
74 | |||
75 | |||
76 | line = '' | ||
77 | |||
78 | handle_line(line) | ||
79 | |||
80 | #for line in open("unifi_snapshot.log"): | ||
81 | # handle_line(eval(line)) | ||