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
.serial
.Importer
;
15 * This class implements a simple server that can bridge two other
18 * It can, of course, inspect the data that goes through it (by default, it
19 * prints traces of the data).
21 * Note: this {@link ServerBridge} has to be discarded after use (cannot be
26 public class ServerBridge
extends Server
{
27 private final String forwardToHost
;
28 private final int forwardToPort
;
29 private final String forwardToKey
;
32 * Create a new server that will start listening on the network when
33 * {@link ServerBridge#start()} is called.
36 * the port to listen on, or 0 to assign any unallocated port
37 * found (which can later on be queried via
38 * {@link ServerBridge#getPort()}
40 * an optional key to encrypt all the communications (if NULL,
41 * everything will be sent in clear text)
42 * @param forwardToHost
43 * the host server to forward the calls to
44 * @param forwardToPort
45 * the host port to forward the calls to
47 * an optional key to encrypt all the communications (if NULL,
48 * everything will be sent in clear text)
51 * in case of I/O error
52 * @throws UnknownHostException
53 * if the IP address of the host could not be determined
54 * @throws IllegalArgumentException
55 * if the port parameter is outside the specified range of valid
56 * port values, which is between 0 and 65535, inclusive
58 public ServerBridge(int port
, String key
, String forwardToHost
,
59 int forwardToPort
, String forwardToKey
) throws IOException
{
61 this.forwardToHost
= forwardToHost
;
62 this.forwardToPort
= forwardToPort
;
63 this.forwardToKey
= forwardToKey
;
67 * Create a new server that will start listening on the network when
68 * {@link ServerBridge#start()} is called.
71 * the server name (only used for debug info and traces)
73 * the port to listen on
75 * an optional key to encrypt all the communications (if NULL,
76 * everything will be sent in clear text)
77 * @param forwardToHost
78 * the host server to forward the calls to
79 * @param forwardToPort
80 * the host port to forward the calls to
82 * an optional key to encrypt all the communications (if NULL,
83 * everything will be sent in clear text) use an SSL connection
84 * for the forward server or not
87 * in case of I/O error
88 * @throws UnknownHostException
89 * if the IP address of the host could not be determined
90 * @throws IllegalArgumentException
91 * if the port parameter is outside the specified range of valid
92 * port values, which is between 0 and 65535, inclusive
94 public ServerBridge(String name
, int port
, String key
,
95 String forwardToHost
, int forwardToPort
, String forwardToKey
)
97 super(name
, port
, key
);
98 this.forwardToHost
= forwardToHost
;
99 this.forwardToPort
= forwardToPort
;
100 this.forwardToKey
= forwardToKey
;
104 * The traces handler for this {@link Server}.
106 * The trace levels are handled as follow:
108 * <li>1: it will only print basic IN/OUT messages with length</li>
109 * <li>2: it will try to interpret it as an object (SLOW) and print the
110 * object class if possible</li>
111 * <li>3: it will try to print the {@link Object#toString()} value, or the
112 * data if it is not an object</li>
113 * <li>4: it will also print the unzipped serialised value if it is an
118 * the new traces handler
121 public void setTraceHandler(TraceHandler tracer
) {
122 super.setTraceHandler(tracer
);
126 protected ConnectActionServer
createConnectActionServer(Socket s
) {
127 // Bad impl, not up to date (should work, but not efficient)
128 return new ConnectActionServerString(s
, key
) {
130 public void action() throws Exception
{
132 final ConnectActionServerString bridge
= this;
135 new ConnectActionClientString(forwardToHost
, forwardToPort
,
138 public void action() throws Exception
{
141 for (String fromClient
= bridge
.rec(); fromClient
!= null; fromClient
= bridge
144 String fromServer
= send(fromClient
);
146 bridge
.send(fromServer
);
149 getTraceHandler().trace("=== DONE", 1);
150 getTraceHandler().trace("", 1);
154 protected void onError(Exception e
) {
155 ServerBridge
.this.onError(e
);
158 } catch (Exception e
) {
159 ServerBridge
.this.onError(e
);
166 * This is the method that is called each time a client contact us.
168 protected void onClientContact() {
169 getTraceHandler().trace(">>> CLIENT ");
173 * This is the method that is called each time a client contact us.
175 protected void onServerContact() {
176 getTraceHandler().trace("<<< SERVER");
177 getTraceHandler().trace("");
181 * This is the method that is called each time a client contact us.
184 * the data sent by the client
186 protected void onRec(String data
) {
187 trace(">>> CLIENT", data
);
191 * This is the method that is called each time the forwarded server contact
195 * the data sent by the client
197 protected void onSend(String data
) {
198 trace("<<< SERVER", data
);
202 protected ConnectActionClient
getConnectionToMe()
203 throws UnknownHostException
, IOException
{
204 return new ConnectActionClientString(new Socket((String
) null,
210 getTraceHandler().trace(
211 getName() + ": will forward to " + forwardToHost
+ ":"
212 + forwardToPort
+ " ("
213 + (forwardToKey
!= null ?
"encrypted" : "plain text")
219 * Trace the data with the given prefix.
222 * the prefix (client, server, version...)
226 private void trace(String prefix
, String data
) {
227 // TODO: we convert to string and back
228 int size
= data
== null ?
0 : data
.length();
229 String ssize
= StringUtils
.formatNumber(size
) + "bytes";
231 getTraceHandler().trace(prefix
+ ": " + ssize
, 1);
233 if (getTraceHandler().getTraceLevel() >= 2) {
235 while (data
.startsWith("ZIP:") || data
.startsWith("B64:")) {
236 if (data
.startsWith("ZIP:")) {
237 data
= StringUtils
.unzip64s(data
.substring(4));
238 } else if (data
.startsWith("B64:")) {
239 data
= StringUtils
.unzip64s(data
.substring(4));
243 InputStream stream
= new ByteArrayInputStream(
244 StringUtils
.getBytes(data
));
246 Object obj
= new Importer().read(stream
).getValue();
248 getTraceHandler().trace("NULL", 2);
249 getTraceHandler().trace("NULL", 3);
250 getTraceHandler().trace("NULL", 4);
252 if (obj
.getClass().isArray()) {
253 getTraceHandler().trace(
254 "(" + obj
.getClass() + ") with "
255 + Array
.getLength(obj
)
258 getTraceHandler().trace("(" + obj
.getClass() + ")",
261 getTraceHandler().trace("" + obj
.toString(), 3);
262 getTraceHandler().trace(data
, 4);
267 } catch (NoSuchMethodException e
) {
268 getTraceHandler().trace("(not an object)", 2);
269 getTraceHandler().trace(data
, 3);
270 getTraceHandler().trace("", 4);
271 } catch (NoSuchFieldException e
) {
272 getTraceHandler().trace(
273 "(incompatible: " + e
.getMessage() + ")", 2);
274 getTraceHandler().trace(data
, 3);
275 getTraceHandler().trace("", 4);
276 } catch (ClassNotFoundException e
) {
277 getTraceHandler().trace(
278 "(unknown object: " + e
.getMessage() + ")", 2);
279 getTraceHandler().trace(data
, 3);
280 getTraceHandler().trace("", 4);
281 } catch (Exception e
) {
282 getTraceHandler().trace(
283 "(decode error: " + e
.getMessage() + ")", 2);
284 getTraceHandler().trace(data
, 3);
285 getTraceHandler().trace("", 4);
288 getTraceHandler().trace("", 2);