Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/brazil/SSLConnectionHandler.java

Issue 8484110: ABP/Android proxy service (Closed)
Patch Set: Created Oct. 5, 2012, 9:28 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 package org.adblockplus.brazil;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.Socket;
8 import java.util.Properties;
9
10 import sunlabs.brazil.server.Handler;
11 import sunlabs.brazil.server.Request;
12 import sunlabs.brazil.server.Server;
13 import sunlabs.brazil.util.MatchString;
14 import android.util.Log;
15
16 /**
17 * <code>RequestHandler</code> implements a SSL tunnel.
18 *
19 * The following configuration parameters are used to initialize this
20 * <code>Handler</code>:
21 * <dl class=props>
22 *
23 * <dt>prefix, suffix, glob, match
24 * <dd>Specify the URL that triggers this handler. (See {@link MatchString}).
25 * <dt>auth
26 * <dd>The value of the proxy-authenticate header (if any) sent to the upstream
27 * proxy
28 * <dt>proxyHost
29 * <dd>If specified, the name of the upstream proxy
30 * <dt>proxyPort
31 * <dd>The upstream proxy port, if a proxyHost is specified (defaults to 80)
32 *
33 * </dl>
34 *
35 * A sample set of configuration parameters illustrating how to use this
36 * handler follows:
37 *
38 * <pre>
39 * handler=https
40 * https.class=org.adblockplus.brazil.SSLConnectionHandler
41 * </pre>
42 *
43 * See the description under {@link sunlabs.brazil.server.Handler#respond
44 * respond} for a more detailed explanation.
45 *
46 * Original source by Jochen Luell, PAW (http://paw-project.sourceforge.net/)
47 */
48
49 public class SSLConnectionHandler implements Handler
50 {
51 public static final String PROXY_HOST = "proxyHost";
52 public static final String PROXY_PORT = "proxyPort";
53 public static final String AUTH = "auth";
54
55 private String prefix;
56
57 private String proxyHost;
58 private int proxyPort = 80;
59 private String auth;
60
61 @Override
62 public boolean init(Server server, String prefix)
63 {
64 this.prefix = prefix;
65
66 Properties props = server.props;
67
68 proxyHost = props.getProperty(prefix + PROXY_HOST);
69
70 String s = props.getProperty(prefix + PROXY_PORT);
71 try
72 {
73 proxyPort = Integer.decode(s).intValue();
74 }
75 catch (Exception e)
76 {
77 }
78
79 auth = props.getProperty(prefix + AUTH);
80
81 return true;
82 }
83
84 @Override
85 public boolean respond(Request request) throws IOException
86 {
87 if (!request.method.equals("CONNECT"))
88 return false;
89
90 request.log(Server.LOG_LOG, prefix, "SSL connection to " + reque st.url);
91
92 String host = null;
93 int port = 0;
94
95 Socket serverSocket;
96 try
97 {
98 if (proxyHost != null)
99 {
100 host = proxyHost;
101 port = proxyPort;
102 if (auth != null)
103 {
104 request.headers.add("Proxy-Authorization ", auth);
105 }
106 }
107 else
108 {
109 int c = request.url.indexOf(':');
110 host = request.url.substring(0, c);
111 port = Integer.parseInt(request.url.substring(c+ 1));
112 }
113
114 // Connect to server or upstream proxy
115 InetAddress addr = InetAddress.getByName(host);
116 serverSocket = new Socket(addr, port);
117 }
118 catch (Exception e)
119 {
120 request.sendError(500, "SSL connection failure");
121 return true;
122 }
123
124 try
125 {
126 if (proxyHost != null)
127 {
128 // Forward request to upstream proxy
129 OutputStream out = serverSocket.getOutputStream( );
130 out.write((request.method + " " + request.url + " " + request.protocol + "\r\n").getBytes());
131 request.headers.print(out);
132 out.write("\r\n".getBytes());
133 out.flush();
134 }
135 else
136 {
137 // Send response to client
138 OutputStream out = request.sock.getOutputStream( );
139 out.write((request.protocol + " 200 Connection e stablished\r\n\r\n").getBytes());
140 out.flush();
141 }
142
143 // Start bi-directional data transfer
144 ConnectionHandler client = new ConnectionHandler(request .sock, serverSocket);
145 ConnectionHandler server = new ConnectionHandler(serverS ocket, request.sock);
146 client.start();
147 server.start();
148
149 // Wait for connections to close
150 client.join();
151 server.join();
152 }
153 catch (InterruptedException e)
154 {
155 Log.e(prefix, "Data exchange error", e);
156 }
157
158 // Close connection
159 serverSocket.close();
160 request.log(Server.LOG_LOG, prefix, "SSL connection closed");
161
162 return true;
163 }
164
165 private class ConnectionHandler extends Thread
166 {
167 private InputStream in;
168 private OutputStream out;
169
170 ConnectionHandler(Socket sin, Socket sout) throws IOException
171 {
172 in = sin.getInputStream();
173 out = sout.getOutputStream();
174 }
175
176 @Override
177 public void run()
178 {
179 byte[] buf = new byte[4096];
180 int count;
181
182 try
183 {
184 while ((count = in.read(buf, 0, buf.length)) != -1)
185 {
186 out.write(buf, 0, count);
187 }
188 out.flush();
189 }
190 catch (IOException e)
191 {
192 e.printStackTrace();
193 }
194 }
195 }
196 }
OLDNEW
« no previous file with comments | « src/org/adblockplus/brazil/RequestHandler.java ('k') | src/org/adblockplus/brazil/TransparentProxyHandler.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld