1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.lang
.reflect
.Array
;
7 import java
.net
.Socket
;
8 import java
.net
.UnknownHostException
;
10 import be
.nikiroo
.utils
.StringUtils
;
11 import be
.nikiroo
.utils
.TraceHandler
;
12 import be
.nikiroo
.utils
.Version
;
13 import be
.nikiroo
.utils
.serial
.Importer
;
16 * This class implements a simple server that can bridge two other
19 * It can, of course, inspect the data that goes through it (by default, it
20 * prints traces of the data).
22 * Note: this {@link ServerBridge} has to be discarded after use (cannot be
27 public class ServerBridge
extends Server
{
28 private final String forwardToHost
;
29 private final int forwardToPort
;
30 private final String forwardToKey
;
33 * Create a new server that will start listening on the network when
34 * {@link ServerBridge#start()} is called.
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()}
41 * an optional key to encrypt all the communications (if NULL,
42 * everything will be sent in clear text)
43 * @param forwardToHost
44 * the host server to forward the calls to
45 * @param forwardToPort
46 * the host port to forward the calls to
48 * an optional key to encrypt all the communications (if NULL,
49 * everything will be sent in clear text)
52 * in case of I/O error
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
59 public ServerBridge(int port
, String key
, String forwardToHost
,
60 int forwardToPort
, String forwardToKey
) throws IOException
{
62 this.forwardToHost
= forwardToHost
;
63 this.forwardToPort
= forwardToPort
;
64 this.forwardToKey
= forwardToKey
;
68 * Create a new server that will start listening on the network when
69 * {@link ServerBridge#start()} is called.
72 * the server name (only used for debug info and traces)
74 * the port to listen on
76 * an optional key to encrypt all the communications (if NULL,
77 * everything will be sent in clear text)
78 * @param forwardToHost
79 * the host server to forward the calls to
80 * @param forwardToPort
81 * the host port to forward the calls to
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
88 * in case of I/O error
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
95 public ServerBridge(String name
, int port
, String key
,
96 String forwardToHost
, int forwardToPort
, String forwardToKey
)
98 super(name
, port
, key
);
99 this.forwardToHost
= forwardToHost
;
100 this.forwardToPort
= forwardToPort
;
101 this.forwardToKey
= forwardToKey
;
105 * The traces handler for this {@link Server}.
107 * The trace levels are handled as follow:
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
119 * the new traces handler
122 public void setTraceHandler(TraceHandler tracer
) {
123 super.setTraceHandler(tracer
);
127 protected ConnectActionServer
createConnectActionServer(Socket s
) {
128 // Bad impl, not up to date (should work, but not efficient)
129 return new ConnectActionServerString(s
, key
) {
131 public void action(Version clientVersion
) throws Exception
{
132 onClientContact(clientVersion
);
133 final ConnectActionServerString bridge
= this;
136 new ConnectActionClientString(forwardToHost
, forwardToPort
,
139 public void action(Version serverVersion
)
141 onServerContact(serverVersion
);
143 for (String fromClient
= bridge
.rec(); fromClient
!= null; fromClient
= bridge
146 String fromServer
= send(fromClient
);
148 bridge
.send(fromServer
);
151 getTraceHandler().trace("=== DONE", 1);
152 getTraceHandler().trace("", 1);
156 protected void onError(Exception e
) {
157 ServerBridge
.this.onError(e
);
160 } catch (Exception e
) {
161 ServerBridge
.this.onError(e
);
168 * This is the method that is called each time a client contact us.
170 protected void onClientContact(Version clientVersion
) {
171 getTraceHandler().trace(">>> CLIENT " + clientVersion
);
175 * This is the method that is called each time a client contact us.
177 protected void onServerContact(Version serverVersion
) {
178 getTraceHandler().trace("<<< SERVER " + serverVersion
);
179 getTraceHandler().trace("");
183 * This is the method that is called each time a client contact us.
186 * the data sent by the client
188 protected void onRec(String data
) {
189 trace(">>> CLIENT", data
);
193 * This is the method that is called each time the forwarded server contact
197 * the data sent by the client
199 protected void onSend(String data
) {
200 trace("<<< SERVER", data
);
204 protected ConnectActionClient
getConnectionToMe()
205 throws UnknownHostException
, IOException
{
206 return new ConnectActionClientString(new Socket((String
) null,
212 getTraceHandler().trace(
213 getName() + ": will forward to " + forwardToHost
+ ":"
214 + forwardToPort
+ " ("
215 + (forwardToKey
!= null ?
"encrypted" : "plain text")
221 * Trace the data with the given prefix.
224 * the prefix (client, server, version...)
228 private void trace(String prefix
, String data
) {
229 // TODO: we convert to string and back
230 int size
= data
== null ?
0 : data
.length();
231 String ssize
= StringUtils
.formatNumber(size
) + "bytes";
233 getTraceHandler().trace(prefix
+ ": " + ssize
, 1);
235 if (getTraceHandler().getTraceLevel() >= 2) {
237 while (data
.startsWith("ZIP:") || data
.startsWith("B64:")) {
238 if (data
.startsWith("ZIP:")) {
239 data
= StringUtils
.unzip64s(data
.substring(4));
240 } else if (data
.startsWith("B64:")) {
241 data
= StringUtils
.unzip64s(data
.substring(4));
245 InputStream stream
= new ByteArrayInputStream(
246 StringUtils
.getBytes(data
));
248 Object obj
= new Importer().read(stream
).getValue();
250 getTraceHandler().trace("NULL", 2);
251 getTraceHandler().trace("NULL", 3);
252 getTraceHandler().trace("NULL", 4);
254 if (obj
.getClass().isArray()) {
255 getTraceHandler().trace(
256 "(" + obj
.getClass() + ") with "
257 + Array
.getLength(obj
)
260 getTraceHandler().trace("(" + obj
.getClass() + ")",
263 getTraceHandler().trace("" + obj
.toString(), 3);
264 getTraceHandler().trace(data
, 4);
269 } catch (NoSuchMethodException e
) {
270 getTraceHandler().trace("(not an object)", 2);
271 getTraceHandler().trace(data
, 3);
272 getTraceHandler().trace("", 4);
273 } catch (NoSuchFieldException e
) {
274 getTraceHandler().trace(
275 "(incompatible: " + e
.getMessage() + ")", 2);
276 getTraceHandler().trace(data
, 3);
277 getTraceHandler().trace("", 4);
278 } catch (ClassNotFoundException e
) {
279 getTraceHandler().trace(
280 "(unknown object: " + e
.getMessage() + ")", 2);
281 getTraceHandler().trace(data
, 3);
282 getTraceHandler().trace("", 4);
283 } catch (Exception e
) {
284 getTraceHandler().trace(
285 "(decode error: " + e
.getMessage() + ")", 2);
286 getTraceHandler().trace(data
, 3);
287 getTraceHandler().trace("", 4);
290 getTraceHandler().trace("", 2);