OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
sosa
2010/12/07 17:49:23
Is this meant to be called directly? If it's just
dgarrett
2010/12/09 00:48:38
Um... I had a main I used for initial testing, but
| |
2 | |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 import select | |
8 import socket | |
9 import SocketServer | |
10 import threading | |
11 | |
12 class Filter(object): | |
13 """Base class for data filters. | |
14 | |
15 Pass subclass of this to CrosTestProxy which will perform whatever | |
16 connection manipulation you prefer. | |
17 """ | |
18 | |
19 def setup(self): | |
20 """This setup method is called once per connection.""" | |
21 pass | |
22 | |
23 def InBound(self, data): | |
24 """This method is called once per packet of incoming data. | |
25 | |
26 The value returned is what is sent through the proxy. If | |
27 None is returned, the connection will be closed. | |
28 """ | |
29 return data | |
30 | |
31 def OutBound(self, data): | |
32 """This method is called once per packet of outgoing data. | |
33 | |
34 The value returned is what is sent through the proxy. If | |
35 None is returned, the connection will be closed. | |
36 """ | |
37 return data | |
38 | |
39 | |
40 class CrosTestProxy(SocketServer.ThreadingMixIn, SocketServer.TCPServer): | |
41 """A transparent proxy for simulating network errors""" | |
42 | |
43 class _Handler(SocketServer.BaseRequestHandler): | |
44 """Proxy connection handler that passes data though a filter""" | |
45 | |
46 def setup(self): | |
47 """Setup is called once for each connection proxied.""" | |
48 self.server.filter.setup() | |
49 | |
50 def handle(self): | |
51 """Handles each incoming connection. | |
52 | |
53 Opens a new connection to the port we are proxing to, then | |
54 passes each packet along in both directions after passing | |
55 them through the filter object passed in. | |
56 """ | |
57 # Open outgoing socket | |
58 s_in = self.request | |
59 s_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
60 s_out.connect((self.server.address_out, self.server.port_out)) | |
61 | |
62 while True: | |
sosa
2010/12/07 17:49:23
Is there any better way to end this? i.e. create
dgarrett
2010/12/09 00:48:38
The only reason I see for introducing another thre
| |
63 rlist, wlist, xlist = select.select([s_in, s_out], [], []) | |
64 | |
65 if s_in in rlist: | |
66 data = s_in.recv(1024) | |
67 data = self.server.filter.InBound(data) | |
68 if not data: break | |
69 try: | |
70 # If there is any error sending data, close both connections. | |
sosa
2010/12/07 17:49:23
Is there a way to be more specific with the except
dgarrett
2010/12/09 00:48:38
Done.
| |
71 s_out.sendall(data) | |
72 except: | |
73 break | |
74 | |
75 if s_out in rlist: | |
76 data = s_out.recv(1024) | |
77 data = self.server.filter.OutBound(data) | |
78 if not data: break | |
79 try: | |
80 # If there is any error sending data, close both connections. | |
81 s_in.sendall(data) | |
82 except: | |
83 break | |
84 | |
85 s_in.close() | |
86 s_out.close() | |
87 | |
88 def __init__(self, | |
89 filter, | |
90 port_in=8081, | |
91 address_out='127.0.0.1', port_out=8080): | |
92 """Configures the proxy object. | |
93 | |
94 Args: | |
95 filter: An instance of a subclass of Filter. | |
96 port_in: Port on which to listen for incoming connections. | |
97 address_out: Address to which outgoing connections will go. | |
98 address_port: Port to which outgoing connections will go. | |
99 """ | |
100 self.port_in = port_in | |
101 self.address_out = address_out | |
102 self.port_out = port_out | |
103 self.filter = filter | |
104 | |
105 SocketServer.TCPServer.__init__(self, | |
106 ('', port_in), | |
107 self._Handler) | |
108 | |
109 def serve_forever_in_thread(self): | |
110 """Helper method to start the server in a new background thread.""" | |
111 server_thread = threading.Thread(target=self.serve_forever) | |
112 server_thread.setDaemon(True) | |
113 server_thread.start() | |
114 | |
115 return server_thread | |
116 | |
117 | |
118 def main(): | |
119 s = CrosTestProxy(port_in=2222, port_out=22, filter=Filter()) | |
sosa
2010/12/07 17:49:23
Can you comment on what are these constants?
| |
120 s.serve_forever() | |
121 | |
122 | |
123 if __name__ == '__main__': | |
124 main() | |
OLD | NEW |