Commit | Line | Data |
---|---|---|
8537d55a NR |
1 | package be.nikiroo.utils.serial.server; |
2 | ||
564bbbdb | 3 | import java.io.ByteArrayInputStream; |
8537d55a | 4 | import java.io.IOException; |
564bbbdb | 5 | import java.io.InputStream; |
452f38c8 | 6 | import java.lang.reflect.Array; |
8537d55a | 7 | import java.net.Socket; |
f4053377 | 8 | import java.net.UnknownHostException; |
8537d55a | 9 | |
452f38c8 | 10 | import be.nikiroo.utils.StringUtils; |
8537d55a NR |
11 | import be.nikiroo.utils.TraceHandler; |
12 | import be.nikiroo.utils.Version; | |
13 | import be.nikiroo.utils.serial.Importer; | |
14 | ||
15 | /** | |
16 | * This class implements a simple server that can bridge two other | |
17 | * {@link Server}s. | |
18 | * <p> | |
19 | * It can, of course, inspect the data that goes through it (by default, it | |
20 | * prints traces of the data). | |
21 | * <p> | |
22 | * Note: this {@link ServerBridge} has to be discarded after use (cannot be | |
23 | * started twice). | |
24 | * | |
25 | * @author niki | |
26 | */ | |
27 | public class ServerBridge extends Server { | |
28 | private final String forwardToHost; | |
29 | private final int forwardToPort; | |
8468bb79 | 30 | private final String forwardToKey; |
8537d55a NR |
31 | |
32 | /** | |
33 | * Create a new server that will start listening on the network when | |
34 | * {@link ServerBridge#start()} is called. | |
35 | * | |
36 | * @param port | |
37 | * the port to listen on, or 0 to assign any unallocated port | |
38 | * found (which can later on be queried via | |
39 | * {@link ServerBridge#getPort()} | |
8468bb79 NR |
40 | * @param key |
41 | * an optional key to encrypt all the communications (if NULL, | |
42 | * everything will be sent in clear text) | |
452f38c8 NR |
43 | * @param forwardToHost |
44 | * the host server to forward the calls to | |
45 | * @param forwardToPort | |
46 | * the host port to forward the calls to | |
8468bb79 NR |
47 | * @param forwardToKey |
48 | * an optional key to encrypt all the communications (if NULL, | |
49 | * everything will be sent in clear text) | |
8537d55a NR |
50 | * |
51 | * @throws IOException | |
52 | * in case of I/O error | |
f4053377 NR |
53 | * @throws UnknownHostException |
54 | * if the IP address of the host could not be determined | |
55 | * @throws IllegalArgumentException | |
56 | * if the port parameter is outside the specified range of valid | |
57 | * port values, which is between 0 and 65535, inclusive | |
8537d55a | 58 | */ |
8468bb79 NR |
59 | public ServerBridge(int port, String key, String forwardToHost, |
60 | int forwardToPort, String forwardToKey) throws IOException { | |
61 | super(port, key); | |
8537d55a NR |
62 | this.forwardToHost = forwardToHost; |
63 | this.forwardToPort = forwardToPort; | |
8468bb79 | 64 | this.forwardToKey = forwardToKey; |
8537d55a NR |
65 | } |
66 | ||
67 | /** | |
68 | * Create a new server that will start listening on the network when | |
69 | * {@link ServerBridge#start()} is called. | |
70 | * | |
71 | * @param name | |
72 | * the server name (only used for debug info and traces) | |
73 | * @param port | |
74 | * the port to listen on | |
8468bb79 NR |
75 | * @param key |
76 | * an optional key to encrypt all the communications (if NULL, | |
77 | * everything will be sent in clear text) | |
452f38c8 NR |
78 | * @param forwardToHost |
79 | * the host server to forward the calls to | |
80 | * @param forwardToPort | |
81 | * the host port to forward the calls to | |
8468bb79 NR |
82 | * @param forwardToKey |
83 | * an optional key to encrypt all the communications (if NULL, | |
84 | * everything will be sent in clear text) use an SSL connection | |
85 | * for the forward server or not | |
8537d55a NR |
86 | * |
87 | * @throws IOException | |
88 | * in case of I/O error | |
f4053377 NR |
89 | * @throws UnknownHostException |
90 | * if the IP address of the host could not be determined | |
91 | * @throws IllegalArgumentException | |
92 | * if the port parameter is outside the specified range of valid | |
93 | * port values, which is between 0 and 65535, inclusive | |
8537d55a | 94 | */ |
8468bb79 NR |
95 | public ServerBridge(String name, int port, String key, |
96 | String forwardToHost, int forwardToPort, String forwardToKey) | |
8537d55a | 97 | throws IOException { |
8468bb79 | 98 | super(name, port, key); |
8537d55a NR |
99 | this.forwardToHost = forwardToHost; |
100 | this.forwardToPort = forwardToPort; | |
8468bb79 | 101 | this.forwardToKey = forwardToKey; |
8537d55a NR |
102 | } |
103 | ||
104 | /** | |
105 | * The traces handler for this {@link Server}. | |
106 | * <p> | |
452f38c8 | 107 | * The trace levels are handled as follow: |
8537d55a | 108 | * <ul> |
452f38c8 NR |
109 | * <li>1: it will only print basic IN/OUT messages with length</li> |
110 | * <li>2: it will try to interpret it as an object (SLOW) and print the | |
111 | * object class if possible</li> | |
112 | * <li>3: it will try to print the {@link Object#toString()} value, or the | |
113 | * data if it is not an object</li> | |
114 | * <li>4: it will also print the unzipped serialised value if it is an | |
115 | * object</li> | |
8537d55a NR |
116 | * </ul> |
117 | * | |
118 | * @param tracer | |
119 | * the new traces handler | |
120 | */ | |
121 | @Override | |
122 | public void setTraceHandler(TraceHandler tracer) { | |
123 | super.setTraceHandler(tracer); | |
124 | } | |
125 | ||
126 | @Override | |
127 | protected ConnectActionServer createConnectActionServer(Socket s) { | |
564bbbdb | 128 | // Bad impl, not up to date (should work, but not efficient) |
8468bb79 | 129 | return new ConnectActionServerString(s, key) { |
8537d55a NR |
130 | @Override |
131 | public void action(final Version clientVersion) throws Exception { | |
132 | onClientContact(clientVersion); | |
133 | final ConnectActionServerString bridge = this; | |
134 | ||
f4053377 NR |
135 | try { |
136 | new ConnectActionClientString(forwardToHost, forwardToPort, | |
8468bb79 | 137 | forwardToKey, clientVersion) { |
f4053377 NR |
138 | @Override |
139 | public void action(final Version serverVersion) | |
140 | throws Exception { | |
141 | onServerContact(serverVersion); | |
8537d55a | 142 | |
f4053377 NR |
143 | for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge |
144 | .rec()) { | |
145 | onRec(clientVersion, fromClient); | |
146 | String fromServer = send(fromClient); | |
147 | onSend(serverVersion, fromServer); | |
148 | bridge.send(fromServer); | |
149 | } | |
d827da2a | 150 | |
f4053377 NR |
151 | getTraceHandler().trace("=== DONE", 1); |
152 | getTraceHandler().trace("", 1); | |
153 | } | |
0ff71477 | 154 | |
f4053377 NR |
155 | @Override |
156 | protected void onError(Exception e) { | |
157 | ServerBridge.this.onError(e); | |
158 | } | |
159 | }.connect(); | |
160 | } catch (Exception e) { | |
161 | ServerBridge.this.onError(e); | |
162 | } | |
8537d55a NR |
163 | } |
164 | }; | |
165 | } | |
166 | ||
167 | /** | |
168 | * This is the method that is called each time a client contact us. | |
169 | * | |
170 | * @param clientVersion | |
171 | * the client version | |
8537d55a NR |
172 | */ |
173 | protected void onClientContact(Version clientVersion) { | |
d827da2a | 174 | getTraceHandler().trace(">>> CLIENT " + clientVersion); |
8537d55a NR |
175 | } |
176 | ||
177 | /** | |
178 | * This is the method that is called each time a client contact us. | |
179 | * | |
180 | * @param serverVersion | |
181 | * the server version | |
8537d55a NR |
182 | */ |
183 | protected void onServerContact(Version serverVersion) { | |
d827da2a | 184 | getTraceHandler().trace("<<< SERVER " + serverVersion); |
217a3310 | 185 | getTraceHandler().trace(""); |
8537d55a NR |
186 | } |
187 | ||
188 | /** | |
189 | * This is the method that is called each time a client contact us. | |
190 | * | |
191 | * @param clientVersion | |
192 | * the client version | |
193 | * @param data | |
194 | * the data sent by the client | |
195 | */ | |
196 | protected void onRec(Version clientVersion, String data) { | |
d827da2a | 197 | trace(">>> CLIENT (" + clientVersion + ")", data); |
8537d55a NR |
198 | } |
199 | ||
200 | /** | |
201 | * This is the method that is called each time the forwarded server contact | |
202 | * us. | |
203 | * | |
204 | * @param serverVersion | |
205 | * the client version | |
206 | * @param data | |
207 | * the data sent by the client | |
208 | */ | |
209 | protected void onSend(Version serverVersion, String data) { | |
d827da2a | 210 | trace("<<< SERVER (" + serverVersion + ")", data); |
8537d55a NR |
211 | } |
212 | ||
f4053377 NR |
213 | @Override |
214 | public void run() { | |
215 | getTraceHandler().trace( | |
216 | getName() + ": will forward to " + forwardToHost + ":" | |
217 | + forwardToPort + " (" | |
8468bb79 NR |
218 | + (forwardToKey != null ? "encrypted" : "plain text") |
219 | + ")"); | |
f4053377 NR |
220 | super.run(); |
221 | } | |
222 | ||
8537d55a NR |
223 | /** |
224 | * Trace the data with the given prefix. | |
225 | * | |
226 | * @param prefix | |
227 | * the prefix (client, server, version...) | |
228 | * @param data | |
229 | * the data to trace | |
230 | */ | |
231 | private void trace(String prefix, String data) { | |
564bbbdb | 232 | // TODO: we convert to string and back |
f8f2afb4 | 233 | int size = data == null ? 0 : data.length(); |
8468bb79 | 234 | String ssize = StringUtils.formatNumber(size) + "bytes"; |
452f38c8 NR |
235 | |
236 | getTraceHandler().trace(prefix + ": " + ssize, 1); | |
8537d55a NR |
237 | |
238 | if (getTraceHandler().getTraceLevel() >= 2) { | |
239 | try { | |
a359464f NR |
240 | while (data.startsWith("ZIP:") || data.startsWith("B64:")) { |
241 | if (data.startsWith("ZIP:")) { | |
242 | data = StringUtils.unbase64s(data.substring(4), true); | |
243 | } else if (data.startsWith("B64:")) { | |
244 | data = StringUtils.unbase64s(data.substring(4), false); | |
245 | } | |
452f38c8 NR |
246 | } |
247 | ||
564bbbdb NR |
248 | InputStream stream = new ByteArrayInputStream( |
249 | data.getBytes("UTF-8")); | |
250 | try { | |
251 | Object obj = new Importer().read(stream).getValue(); | |
252 | if (obj == null) { | |
253 | getTraceHandler().trace("NULL", 2); | |
254 | getTraceHandler().trace("NULL", 3); | |
255 | getTraceHandler().trace("NULL", 4); | |
452f38c8 | 256 | } else { |
564bbbdb NR |
257 | if (obj.getClass().isArray()) { |
258 | getTraceHandler().trace( | |
259 | "(" + obj.getClass() + ") with " | |
260 | + Array.getLength(obj) | |
261 | + "element(s)", 3); | |
262 | } else { | |
263 | getTraceHandler().trace("(" + obj.getClass() + ")", | |
264 | 2); | |
265 | } | |
266 | getTraceHandler().trace("" + obj.toString(), 3); | |
267 | getTraceHandler().trace(data, 4); | |
452f38c8 | 268 | } |
564bbbdb NR |
269 | } finally { |
270 | stream.close(); | |
8537d55a | 271 | } |
452f38c8 NR |
272 | } catch (NoSuchMethodException e) { |
273 | getTraceHandler().trace("(not an object)", 2); | |
274 | getTraceHandler().trace(data, 3); | |
275 | getTraceHandler().trace("", 4); | |
276 | } catch (NoSuchFieldException e) { | |
277 | getTraceHandler().trace( | |
217a3310 | 278 | "(incompatible: " + e.getMessage() + ")", 2); |
452f38c8 NR |
279 | getTraceHandler().trace(data, 3); |
280 | getTraceHandler().trace("", 4); | |
281 | } catch (ClassNotFoundException e) { | |
282 | getTraceHandler().trace( | |
283 | "(unknown object: " + e.getMessage() + ")", 2); | |
284 | getTraceHandler().trace(data, 3); | |
285 | getTraceHandler().trace("", 4); | |
8537d55a | 286 | } catch (Exception e) { |
217a3310 NR |
287 | getTraceHandler().trace( |
288 | "(decode error: " + e.getMessage() + ")", 2); | |
452f38c8 NR |
289 | getTraceHandler().trace(data, 3); |
290 | getTraceHandler().trace("", 4); | |
8537d55a NR |
291 | } |
292 | ||
452f38c8 | 293 | getTraceHandler().trace("", 2); |
8537d55a NR |
294 | } |
295 | } | |
8537d55a | 296 | } |