From 24604392c3aa6d9298aacd10fde7bb2df1322a10 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 29 May 2019 21:47:42 +0200 Subject: [PATCH] fix bridge util todos --- src/be/nikiroo/utils/Downloader.java | 1 - src/be/nikiroo/utils/main/bridge.java | 141 +++++++++++++------ src/be/nikiroo/utils/resources/Bundle.java | 4 +- src/be/nikiroo/utils/serial/SerialUtils.java | 3 +- 4 files changed, 101 insertions(+), 48 deletions(-) diff --git a/src/be/nikiroo/utils/Downloader.java b/src/be/nikiroo/utils/Downloader.java index 68e4dd7..7ee551f 100644 --- a/src/be/nikiroo/utils/Downloader.java +++ b/src/be/nikiroo/utils/Downloader.java @@ -1,6 +1,5 @@ package be.nikiroo.utils; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; diff --git a/src/be/nikiroo/utils/main/bridge.java b/src/be/nikiroo/utils/main/bridge.java index f2f3df2..1b7ab85 100644 --- a/src/be/nikiroo/utils/main/bridge.java +++ b/src/be/nikiroo/utils/main/bridge.java @@ -10,64 +10,118 @@ import be.nikiroo.utils.serial.server.ServerBridge; * @author niki */ public class bridge { + /** + * The optional options that can be passed to the program. + * + * @author niki + */ + private enum Option { + /** + * The encryption key for the input data (optional, but can also be + * empty which is different (it will then use an empty encryption + * key)). + */ + KEY, + /** + * The encryption key for the output data (optional, but can also be + * empty which is different (it will then use an empty encryption + * key)). + */ + FORWARD_KEY, + /** The trace level (1, 2, 3.. default is 1). */ + TRACE_LEVEL, + /** + * The maximum length after which to truncate data to display (the whole + * data will still be sent). + */ + MAX_DISPLAY_SIZE, + /** The help message. */ + HELP, + } + + static private String getSyntax() { + return "Syntax: (--options) (--) [NAME] [PORT] [FORWARD_HOST] [FORWARD_PORT]\n"// + + "\tNAME : the bridge name for display/debug purposes\n"// + + "\tPORT : the port to listen on\n"// + + "\tFORWARD_HOST : the host to connect to\n"// + + "\tFORWARD_PORT : the port to connect to\n"// + + "\n" // + + "\tOptions: \n" // + + "\t-- : no more options in the rest of the parameters\n" // + + "\t--help : this help message\n" // + + "\t--key : the INCOMING encryption key\n" // + + "\t--forward-key : the OUTGOING encryption key\n" // + + "\t--trace-level : the trace level (1, 2, 3... default is 1)\n" // + + "\t--max-display-size : the maximum size after which to \n"// + + "\t truncate the messages to display (the full message will still be sent)\n" // + ; + } + /** * Start a bridge between 2 servers. * * @param args - * an array containing: - * + * the parameters, which can be seen by passing "--help" or just + * calling the program without parameters */ public static void main(String[] args) { final TraceHandler tracer = new TraceHandler(true, false, 0); try { - if (args.length < 6) { - tracer.error("Invalid syntax.\n" - + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level]) ([max])\n" - + "\tname: the bridge name\n" - + "\tport: the bridge port\n" - + "\tkey: a key for an encrypted bridge, PLAIN_TEXT for plain text\n" - + "\tfhost: the forward server host\n" - + "\tfport: the forward server port\n" - + "\tfkey: a key for an encrypted forward server, PLAIN_TEXT for plain text\n" - + "\ttrace level: the optional trace level (default is 1)\n" - + "\tmax: the maximum size after which to truncate data\n"); - return; + if (args.length == 0) { + tracer.error(getSyntax()); + System.exit(0); } + String key = null; + String fkey = null; + int traceLevel = 1; + int maxPrintSize = 0; + int i = 0; + while (args[i].startsWith("--")) { + String arg = args[i]; + i++; + + if (arg.equals("--")) { + break; + } + + arg = arg.substring(2).toUpperCase().replace("-", "_"); + try { + Option opt = Enum.valueOf(Option.class, arg); + switch (opt) { + case HELP: + tracer.trace(getSyntax()); + System.exit(0); + break; + case FORWARD_KEY: + fkey = args[i++]; + break; + case KEY: + key = args[i++]; + break; + case MAX_DISPLAY_SIZE: + maxPrintSize = Integer.parseInt(args[i++]); + break; + case TRACE_LEVEL: + traceLevel = Integer.parseInt(args[i++]); + break; + } + } catch (Exception e) { + tracer.error(getSyntax()); + System.exit(1); + } + } + + if ((args.length - i) != 4) { + tracer.error(getSyntax()); + System.exit(2); + } + String name = args[i++]; int port = Integer.parseInt(args[i++]); - String key = args[i++]; - // TODO: bad - if ("PLAIN_TEXT".equals(key)) { - key = null; - } String fhost = args[i++]; int fport = Integer.parseInt(args[i++]); - String fkey = args[i++]; - // TODO: bad - if ("PLAIN_TEXT".equals(fkey)) { - fkey = null; - } - - int traceLevel = 1; - if (args.length > 6) { - traceLevel = Integer.parseInt(args[i++]); - } - int maxPrintSize = 0; - if (args.length > 7) { - maxPrintSize = Integer.parseInt(args[i++]); - } ServerBridge bridge = new ServerBridge(name, port, key, fhost, fport, fkey); @@ -76,6 +130,7 @@ public class bridge { bridge.run(); } catch (Exception e) { tracer.error(e); + System.exit(42); } } } diff --git a/src/be/nikiroo/utils/resources/Bundle.java b/src/be/nikiroo/utils/resources/Bundle.java index 68c236f..21021e0 100644 --- a/src/be/nikiroo/utils/resources/Bundle.java +++ b/src/be/nikiroo/utils/resources/Bundle.java @@ -143,7 +143,8 @@ public class Bundle> { /** * Return the value associated to the given id as a {@link String}. *

- * If no value is associated, take the default one if any. + * If no value is associated (or if it is empty!), take the default one if + * any. * * @param id * the id of the value to get @@ -168,7 +169,6 @@ public class Bundle> { } } - //TODO: is it ok? need to jDoc? if (rep == null || rep.isEmpty()) { return def; } diff --git a/src/be/nikiroo/utils/serial/SerialUtils.java b/src/be/nikiroo/utils/serial/SerialUtils.java index 43aafb2..c4b7cf9 100644 --- a/src/be/nikiroo/utils/serial/SerialUtils.java +++ b/src/be/nikiroo/utils/serial/SerialUtils.java @@ -670,7 +670,6 @@ public class SerialUtils { static void encodeString(OutputStream out, String raw) throws IOException { // TODO: not. efficient. out.write('\"'); - // TODO !! utf-8 required for (char car : raw.toCharArray()) { encodeString(out, car); } @@ -691,7 +690,7 @@ public class SerialUtils { out.write('\"'); } - // for encode string, NOT to encode a char by itself! + // for encoding string, NOT to encode a char by itself! static void encodeString(OutputStream out, char raw) throws IOException { switch (raw) { case '\\': -- 2.27.0