serial: switch from SSL to CryptUtils
[nikiroo-utils.git] / src / be / nikiroo / utils / main / bridge.java
1 package be.nikiroo.utils.main;
2
3 import be.nikiroo.utils.TraceHandler;
4 import be.nikiroo.utils.serial.server.ServerBridge;
5
6 /**
7 * Serialiser bridge (starts a {@link ServerBridge} and can thus intercept
8 * communication between a client and a server).
9 *
10 * @author niki
11 */
12 public class bridge {
13 /**
14 * Start a bridge between 2 servers.
15 *
16 * @param args
17 * an array containing:
18 * <ul>
19 * <li>The bridge name</li>
20 * <li>The bridge port</li>
21 * <li>a key for an encrypted bridge, PLAIN_TEXT for plain text</li>
22 * <li>The forward server host</li>
23 * <li>The forward server port</li>
24 * <li>a key for an encrypted forward server, PLAIN_TEXT for
25 * plain text</li>
26 * <li>(optional) a trace level</li>
27 * <li>(optional) a truncate size for data</li>
28 * </ul>
29 */
30 public static void main(String[] args) {
31 final TraceHandler tracer = new TraceHandler(true, false, 0);
32 try {
33 if (args.length < 6) {
34 tracer.error("Invalid syntax.\n"
35 + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level]) ([max])\n"
36 + "\tname: the bridge name\n"
37 + "\tport: the bridge port\n"
38 + "\tkey: a key for an encrypted bridge, PLAIN_TEXT for plain text\n"
39 + "\tfhost: the forward server host\n"
40 + "\tfport: the forward server port\n"
41 + "\tfkey: a key for an encrypted forward server, PLAIN_TEXT for plain text\n"
42 + "\ttrace level: the optional trace level (default is 1)\n"
43 + "\tmax: the maximum size after which to truncate data\n");
44 return;
45 }
46
47 int i = 0;
48 String name = args[i++];
49 int port = Integer.parseInt(args[i++]);
50 String key = args[i++];
51 // TODO: bad
52 if ("PLAIN_TEXT".equals(key)) {
53 key = null;
54 }
55 String fhost = args[i++];
56 int fport = Integer.parseInt(args[i++]);
57 String fkey = args[i++];
58 // TODO: bad
59 if ("PLAIN_TEXT".equals(fkey)) {
60 fkey = null;
61 }
62
63 int traceLevel = 1;
64 if (args.length > 6) {
65 traceLevel = Integer.parseInt(args[i++]);
66 }
67 int maxPrintSize = 0;
68 if (args.length > 7) {
69 maxPrintSize = Integer.parseInt(args[i++]);
70 }
71
72 ServerBridge bridge = new ServerBridge(name, port, key, fhost,
73 fport, fkey);
74 bridge.setTraceHandler(new TraceHandler(true, true, traceLevel,
75 maxPrintSize));
76 bridge.run();
77 } catch (Exception e) {
78 tracer.error(e);
79 }
80 }
81 }