Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: lib/cros_test_proxy.py

Issue 5632002: Create a transparent proxy for test programs to use, with the (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Readd comment lost in rebase. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « image_to_live.sh ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import select
6 import socket
7 import SocketServer
8 import threading
9
10 class Filter(object):
11 """Base class for data filters.
12
13 Pass subclass of this to CrosTestProxy which will perform whatever
14 connection manipulation you prefer.
15 """
16
17 def setup(self):
18 """This setup method is called once per connection."""
19 pass
20
21 def InBound(self, data):
22 """This method is called once per packet of incoming data.
23
24 The value returned is what is sent through the proxy. If
25 None is returned, the connection will be closed.
26 """
27 return data
28
29 def OutBound(self, data):
30 """This method is called once per packet of outgoing data.
31
32 The value returned is what is sent through the proxy. If
33 None is returned, the connection will be closed.
34 """
35 return data
36
37
38 class CrosTestProxy(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
39 """A transparent proxy for simulating network errors"""
40
41 class _Handler(SocketServer.BaseRequestHandler):
42 """Proxy connection handler that passes data though a filter"""
43
44 def setup(self):
45 """Setup is called once for each connection proxied."""
46 self.server.filter.setup()
47
48 def handle(self):
49 """Handles each incoming connection.
50
51 Opens a new connection to the port we are proxing to, then
52 passes each packet along in both directions after passing
53 them through the filter object passed in.
54 """
55 # Open outgoing socket
56 s_in = self.request
57 s_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
58 s_out.connect((self.server.address_out, self.server.port_out))
59
60 while True:
61 rlist, wlist, xlist = select.select([s_in, s_out], [], [])
62
63 if s_in in rlist:
64 data = s_in.recv(1024)
65 data = self.server.filter.InBound(data)
66 if not data: break
67 try:
68 # If there is any error sending data, close both connections.
69 s_out.sendall(data)
70 except socket.error:
71 break
72
73 if s_out in rlist:
74 data = s_out.recv(1024)
75 data = self.server.filter.OutBound(data)
76 if not data: break
77 try:
78 # If there is any error sending data, close both connections.
79 s_in.sendall(data)
80 except socket.error:
81 break
82
83 s_in.close()
84 s_out.close()
85
86 def __init__(self,
87 filter,
88 port_in=8081,
89 address_out='127.0.0.1', port_out=8080):
90 """Configures the proxy object.
91
92 Args:
93 filter: An instance of a subclass of Filter.
94 port_in: Port on which to listen for incoming connections.
95 address_out: Address to which outgoing connections will go.
96 address_port: Port to which outgoing connections will go.
97 """
98 self.port_in = port_in
99 self.address_out = address_out
100 self.port_out = port_out
101 self.filter = filter
102
103 SocketServer.TCPServer.__init__(self,
104 ('', port_in),
105 self._Handler)
106
107 def serve_forever_in_thread(self):
108 """Helper method to start the server in a new background thread."""
109 server_thread = threading.Thread(target=self.serve_forever)
110 server_thread.setDaemon(True)
111 server_thread.start()
112
113 return server_thread
OLDNEW
« no previous file with comments | « image_to_live.sh ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698