OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 * | 56 * |
57 * See the description under {@link sunlabs.brazil.server.Handler#respond | 57 * See the description under {@link sunlabs.brazil.server.Handler#respond |
58 * respond} for a more detailed explanation. | 58 * respond} for a more detailed explanation. |
59 * | 59 * |
60 * Original source by Jochen Luell, PAW (http://paw-project.sourceforge.net/) | 60 * Original source by Jochen Luell, PAW (http://paw-project.sourceforge.net/) |
61 */ | 61 */ |
62 | 62 |
63 public class SSLConnectionHandler extends BaseRequestHandler | 63 public class SSLConnectionHandler extends BaseRequestHandler |
64 { | 64 { |
65 @Override | 65 @Override |
66 public boolean respond(Request request) throws IOException | 66 public boolean respond(final Request request) throws IOException |
67 { | 67 { |
68 if (!request.method.equals("CONNECT")) | 68 if (!request.method.equals("CONNECT")) |
69 return false; | 69 return false; |
70 | 70 |
71 request.log(Server.LOG_LOG, prefix, "SSL connection to " + request.url); | 71 request.log(Server.LOG_LOG, prefix, "SSL connection to " + request.url); |
72 | 72 |
73 String host = null; | 73 String host = null; |
74 int port = 0; | 74 int port = 0; |
75 | 75 |
76 Socket serverSocket; | 76 Socket serverSocket; |
77 try | 77 try |
78 { | 78 { |
79 if (proxyHost != null) | 79 if (proxyHost != null) |
80 { | 80 { |
81 host = proxyHost; | 81 host = proxyHost; |
82 port = proxyPort; | 82 port = proxyPort; |
83 if (auth != null) | 83 if (auth != null) |
84 { | 84 { |
85 request.headers.add("Proxy-Authorization", auth); | 85 request.headers.add("Proxy-Authorization", auth); |
86 } | 86 } |
87 } | 87 } |
88 else | 88 else |
89 { | 89 { |
90 int c = request.url.indexOf(':'); | 90 final int c = request.url.indexOf(':'); |
91 host = request.url.substring(0, c); | 91 host = request.url.substring(0, c); |
92 port = Integer.parseInt(request.url.substring(c + 1)); | 92 port = Integer.parseInt(request.url.substring(c + 1)); |
93 } | 93 } |
94 | 94 |
95 // Connect to server or upstream proxy | 95 // Connect to server or upstream proxy |
96 serverSocket = new Socket(); | 96 serverSocket = new Socket(); |
97 serverSocket.setKeepAlive(true); | 97 serverSocket.setKeepAlive(true); |
98 serverSocket.connect(new InetSocketAddress(host, port)); | 98 serverSocket.connect(new InetSocketAddress(host, port)); |
99 } | 99 } |
100 catch (Exception e) | 100 catch (final Exception e) |
101 { | 101 { |
102 request.sendError(500, "SSL connection failure"); | 102 request.sendError(500, "SSL connection failure"); |
103 return true; | 103 return true; |
104 } | 104 } |
105 | 105 |
106 try | 106 try |
107 { | 107 { |
108 if (proxyHost != null) | 108 if (proxyHost != null) |
109 { | 109 { |
110 // Forward request to upstream proxy | 110 // Forward request to upstream proxy |
111 OutputStream out = serverSocket.getOutputStream(); | 111 final OutputStream out = serverSocket.getOutputStream(); |
112 out.write((request.method + " " + request.url + " " + request.protocol +
"\r\n").getBytes()); | 112 out.write((request.method + " " + request.url + " " + request.protocol +
"\r\n").getBytes()); |
113 request.headers.print(out); | 113 request.headers.print(out); |
114 out.write("\r\n".getBytes()); | 114 out.write("\r\n".getBytes()); |
115 out.flush(); | 115 out.flush(); |
116 } | 116 } |
117 else | 117 else |
118 { | 118 { |
119 // Send response to client | 119 // Send response to client |
120 OutputStream out = request.sock.getOutputStream(); | 120 final OutputStream out = request.sock.getOutputStream(); |
121 out.write((request.protocol + " 200 Connection established\r\n\r\n").get
Bytes()); | 121 out.write((request.protocol + " 200 Connection established\r\n\r\n").get
Bytes()); |
122 out.flush(); | 122 out.flush(); |
123 } | 123 } |
124 | 124 |
125 // Start bi-directional data transfer | 125 // Start bi-directional data transfer |
126 ConnectionHandler client = new ConnectionHandler(request.sock, serverSocke
t); | 126 final ConnectionHandler client = new ConnectionHandler(request.sock, serve
rSocket); |
127 ConnectionHandler server = new ConnectionHandler(serverSocket, request.soc
k); | 127 final ConnectionHandler server = new ConnectionHandler(serverSocket, reque
st.sock); |
128 client.start(); | 128 client.start(); |
129 server.start(); | 129 server.start(); |
130 | 130 |
131 // Wait for connections to close | 131 // Wait for connections to close |
132 client.join(); | 132 client.join(); |
133 server.join(); | 133 server.join(); |
134 } | 134 } |
135 catch (InterruptedException e) | 135 catch (final InterruptedException e) |
136 { | 136 { |
137 request.log(Server.LOG_ERROR, prefix, "Data exchange error: " + e.getMessa
ge()); | 137 request.log(Server.LOG_ERROR, prefix, "Data exchange error: " + e.getMessa
ge()); |
138 } | 138 } |
139 | 139 |
140 // Close connection | 140 // Close connection |
141 serverSocket.close(); | 141 serverSocket.close(); |
142 request.log(Server.LOG_LOG, prefix, "SSL connection closed"); | 142 request.log(Server.LOG_LOG, prefix, "SSL connection closed"); |
143 | 143 |
144 return true; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 private class ConnectionHandler extends Thread | 147 private class ConnectionHandler extends Thread |
148 { | 148 { |
149 private InputStream in; | 149 private final InputStream in; |
150 private OutputStream out; | 150 private final OutputStream out; |
151 | 151 |
152 ConnectionHandler(Socket sin, Socket sout) throws IOException | 152 ConnectionHandler(final Socket sin, final Socket sout) throws IOException |
153 { | 153 { |
154 in = sin.getInputStream(); | 154 in = sin.getInputStream(); |
155 out = sout.getOutputStream(); | 155 out = sout.getOutputStream(); |
156 } | 156 } |
157 | 157 |
158 @Override | 158 @Override |
159 public void run() | 159 public void run() |
160 { | 160 { |
161 byte[] buf = new byte[4096]; | 161 final byte[] buf = new byte[4096]; |
162 int count; | 162 int count; |
163 | 163 |
164 try | 164 try |
165 { | 165 { |
166 while ((count = in.read(buf, 0, buf.length)) != -1) | 166 while ((count = in.read(buf, 0, buf.length)) != -1) |
167 { | 167 { |
168 out.write(buf, 0, count); | 168 out.write(buf, 0, count); |
169 } | 169 } |
170 out.flush(); | 170 out.flush(); |
171 } | 171 } |
172 catch (IOException e) | 172 catch (final IOException e) |
173 { | 173 { |
174 e.printStackTrace(); | 174 e.printStackTrace(); |
175 } | 175 } |
176 } | 176 } |
177 } | 177 } |
178 } | 178 } |
OLD | NEW |