From 8468bb79f0fc9c88fa21355509731625732eb10e Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 22 Apr 2019 20:38:58 +0200 Subject: [PATCH] serial: switch from SSL to CryptUtils --- changelog.md | 1 + src/be/nikiroo/utils/main/bridge.java | 25 ++-- .../utils/serial/server/ConnectAction.java | 67 ++++++---- .../serial/server/ConnectActionClient.java | 32 +++-- .../server/ConnectActionClientObject.java | 32 +++-- .../server/ConnectActionClientString.java | 32 +++-- .../serial/server/ConnectActionServer.java | 16 ++- .../server/ConnectActionServerObject.java | 14 ++- .../server/ConnectActionServerString.java | 14 ++- .../nikiroo/utils/serial/server/Server.java | 114 +++++++----------- .../utils/serial/server/ServerBridge.java | 59 +++++---- .../utils/serial/server/ServerObject.java | 20 +-- .../utils/serial/server/ServerString.java | 20 +-- .../nikiroo/utils/test/SerialServerTest.java | 90 +++++++------- 14 files changed, 289 insertions(+), 247 deletions(-) diff --git a/changelog.md b/changelog.md index 8ae7248..79e97c2 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ - new: server: count the bytes we rec/send - new: CryptUtils +- serial: SSL -> CryptUtils ## Version 4.7.2 diff --git a/src/be/nikiroo/utils/main/bridge.java b/src/be/nikiroo/utils/main/bridge.java index 8aaa93f..f2f3df2 100644 --- a/src/be/nikiroo/utils/main/bridge.java +++ b/src/be/nikiroo/utils/main/bridge.java @@ -18,10 +18,11 @@ public class bridge { * @@ -34,10 +35,10 @@ public class bridge { + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level]) ([max])\n" + "\tname: the bridge name\n" + "\tport: the bridge port\n" - + "\tssl: TRUE for an SSL bridge, FALSE for plain text\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" - + "\tfssl: TRUE for an SSL forward server, FALSE for plain text\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; @@ -46,10 +47,18 @@ public class bridge { int i = 0; String name = args[i++]; int port = Integer.parseInt(args[i++]); - boolean ssl = Boolean.parseBoolean(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++]); - boolean fssl = Boolean.parseBoolean(args[i++]); + String fkey = args[i++]; + // TODO: bad + if ("PLAIN_TEXT".equals(fkey)) { + fkey = null; + } int traceLevel = 1; if (args.length > 6) { @@ -60,8 +69,8 @@ public class bridge { maxPrintSize = Integer.parseInt(args[i++]); } - ServerBridge bridge = new ServerBridge(name, port, ssl, fhost, - fport, fssl); + ServerBridge bridge = new ServerBridge(name, port, key, fhost, + fport, fkey); bridge.setTraceHandler(new TraceHandler(true, true, traceLevel, maxPrintSize)); bridge.run(); diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index cef10ad..7d72424 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -6,8 +6,7 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; -import javax.net.ssl.SSLException; - +import be.nikiroo.utils.CryptUtils; import be.nikiroo.utils.Version; import be.nikiroo.utils.serial.Exporter; import be.nikiroo.utils.serial.Importer; @@ -27,6 +26,8 @@ abstract class ConnectAction { private Version version; private Version clientVersion; + private CryptUtils crypt; + private Object lock = new Object(); private BufferedReader in; private OutputStreamWriter out; @@ -77,12 +78,19 @@ abstract class ConnectAction { * @param server * TRUE for a server action, FALSE for a client action (will * impact the process) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the version of this client-or-server */ - protected ConnectAction(Socket s, boolean server, Version version) { + protected ConnectAction(Socket s, boolean server, String key, + Version version) { this.s = s; this.server = server; + if (key != null) { + crypt = new CryptUtils(key); + } if (version == null) { this.version = new Version(); @@ -131,7 +139,7 @@ abstract class ConnectAction { out = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); try { if (server) { - String line = in.readLine(); + String line = readLine(in); if (line != null && line.startsWith("VERSION ")) { // "VERSION client-version" (VERSION 1.0.0) Version clientVersion = new Version( @@ -163,19 +171,6 @@ abstract class ConnectAction { in = null; } } catch (Exception e) { - if (e instanceof SSLException) { - String ciphers = ""; - for (String cipher : Server.getAnonCiphers()) { - if (!ciphers.isEmpty()) { - ciphers += ", "; - } - ciphers += cipher; - } - - e = new SSLException("SSL error (available SSL ciphers: " - + ciphers + ")", e); - } - onError(e); } finally { try { @@ -271,9 +266,7 @@ abstract class ConnectAction { */ protected String sendString(String line) throws IOException { synchronized (lock) { - out.write(line); - out.write("\n"); - bytesSent += line.length() + 1; + writeLine(out, line); if (server) { out.flush(); @@ -307,15 +300,37 @@ abstract class ConnectAction { contentToSend = false; } - String line = in.readLine(); - if (line != null) { - bytesReceived += line.length(); - } - - return line; + return readLine(in); } return null; } } + + private String readLine(BufferedReader in) throws IOException { + String line = in.readLine(); + if (line != null) { + bytesReceived += line.length(); + if (crypt != null) { + line = crypt.decrypt64s(line, false); + } + } + + return line; + } + + private void writeLine(OutputStreamWriter out, String line) + throws IOException { + if (crypt == null) { + out.write(line); + bytesSent += line.length(); + } else { + // TODO: how NOT to create so many big Strings? + String b64 = crypt.encrypt64(line, false); + out.write(b64); + bytesSent += b64.length(); + } + out.write("\n"); + bytesSent++; + } } \ No newline at end of file diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java index db06a9f..c56dddd 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java @@ -28,9 +28,12 @@ abstract class ConnectActionClient { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionClient(Socket s) { - this(s, Version.getCurrentVersion()); + public ConnectActionClient(Socket s, String key) { + this(s, key, Version.getCurrentVersion()); } /** @@ -41,8 +44,9 @@ abstract class ConnectActionClient { * the host to bind to * @param port * the port to bind to - * @param ssl - * TRUE for an SSL connection, FALSE for plain text + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -52,9 +56,9 @@ abstract class ConnectActionClient { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ConnectActionClient(String host, int port, boolean ssl) + public ConnectActionClient(String host, int port, String key) throws IOException { - this(Server.createSocket(host, port, ssl), Version.getCurrentVersion()); + this(new Socket(host, port), key, Version.getCurrentVersion()); } /** @@ -64,8 +68,9 @@ abstract class ConnectActionClient { * the host to bind to * @param port * the port to bind to - * @param ssl - * TRUE for an SSL connection, FALSE for plain text + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the client version * @@ -77,9 +82,9 @@ abstract class ConnectActionClient { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ConnectActionClient(String host, int port, boolean ssl, + public ConnectActionClient(String host, int port, String key, Version version) throws IOException { - this(Server.createSocket(host, port, ssl), version); + this(new Socket(host, port), key, version); } /** @@ -87,11 +92,14 @@ abstract class ConnectActionClient { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the client version */ - public ConnectActionClient(Socket s, Version version) { - action = new ConnectAction(s, false, version) { + public ConnectActionClient(Socket s, String key, Version version) { + action = new ConnectAction(s, false, key, version) { @Override protected void action(Version serverVersion) throws Exception { ConnectActionClient.this.action(serverVersion); diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java index dd6f917..f8eaae1 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java @@ -22,9 +22,12 @@ public class ConnectActionClientObject extends ConnectActionClient { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionClientObject(Socket s) { - super(s); + public ConnectActionClientObject(Socket s, String key) { + super(s, key); } /** @@ -36,8 +39,9 @@ public class ConnectActionClientObject extends ConnectActionClient { * the host to bind to * @param port * the port to bind to - * @param ssl - * TRUE for an SSL connection, FALSE for plain text + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -47,9 +51,9 @@ public class ConnectActionClientObject extends ConnectActionClient { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ConnectActionClientObject(String host, int port, boolean ssl) + public ConnectActionClientObject(String host, int port, String key) throws IOException { - super(host, port, ssl); + super(host, port, key); } /** @@ -59,8 +63,9 @@ public class ConnectActionClientObject extends ConnectActionClient { * the host to bind to * @param port * the port to bind to - * @param ssl - * TRUE for an SSL connection, FALSE for plain text + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the client version * @@ -72,9 +77,9 @@ public class ConnectActionClientObject extends ConnectActionClient { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ConnectActionClientObject(String host, int port, boolean ssl, + public ConnectActionClientObject(String host, int port, String key, Version version) throws IOException { - super(host, port, ssl, version); + super(host, port, key, version); } /** @@ -84,9 +89,12 @@ public class ConnectActionClientObject extends ConnectActionClient { * the socket to bind to * @param version * the client version + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionClientObject(Socket s, Version version) { - super(s, version); + public ConnectActionClientObject(Socket s, String key, Version version) { + super(s, key, version); } /** diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java index 8b5ec2a..35a01d8 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java @@ -22,9 +22,12 @@ public class ConnectActionClientString extends ConnectActionClient { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionClientString(Socket s) { - super(s); + public ConnectActionClientString(Socket s, String key) { + super(s, key); } /** @@ -36,8 +39,9 @@ public class ConnectActionClientString extends ConnectActionClient { * the host to bind to * @param port * the port to bind to - * @param ssl - * TRUE for an SSL connection, FALSE for plain text + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -47,9 +51,9 @@ public class ConnectActionClientString extends ConnectActionClient { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ConnectActionClientString(String host, int port, boolean ssl) + public ConnectActionClientString(String host, int port, String key) throws IOException { - super(host, port, ssl); + super(host, port, key); } /** @@ -59,8 +63,9 @@ public class ConnectActionClientString extends ConnectActionClient { * the host to bind to * @param port * the port to bind to - * @param ssl - * TRUE for an SSL connection, FALSE for plain text + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the client version * @@ -72,9 +77,9 @@ public class ConnectActionClientString extends ConnectActionClient { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ConnectActionClientString(String host, int port, boolean ssl, + public ConnectActionClientString(String host, int port, String key, Version version) throws IOException { - super(host, port, ssl, version); + super(host, port, key, version); } /** @@ -82,11 +87,14 @@ public class ConnectActionClientString extends ConnectActionClient { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the client version */ - public ConnectActionClientString(Socket s, Version version) { - super(s, version); + public ConnectActionClientString(Socket s, String key, Version version) { + super(s, key, version); } /** diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java index 10d3440..699f307 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java @@ -28,9 +28,12 @@ abstract class ConnectActionServer { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionServer(Socket s) { - this(s, Version.getCurrentVersion()); + public ConnectActionServer(Socket s, String key) { + this(s, key, Version.getCurrentVersion()); } /** @@ -38,11 +41,14 @@ abstract class ConnectActionServer { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the server version */ - public ConnectActionServer(Socket s, Version version) { - action = new ConnectAction(s, true, version) { + public ConnectActionServer(Socket s, String key, Version version) { + action = new ConnectAction(s, true, key, version) { @Override protected void action(Version clientVersion) throws Exception { ConnectActionServer.this.action(clientVersion); @@ -87,6 +93,8 @@ abstract class ConnectActionServer { *

* Example of usage: the client failed an authentication check, cut the * connection here and now. + * + * @return TRUE when it is */ public boolean isClosing() { return closing; diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java b/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java index e0e4276..79fa38c 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java @@ -21,9 +21,12 @@ public class ConnectActionServerObject extends ConnectActionServer { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionServerObject(Socket s) { - super(s); + public ConnectActionServerObject(Socket s, String key) { + super(s, key); } /** @@ -31,11 +34,14 @@ public class ConnectActionServerObject extends ConnectActionServer { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the server version */ - public ConnectActionServerObject(Socket s, Version version) { - super(s, version); + public ConnectActionServerObject(Socket s, String key, Version version) { + super(s, key, version); } /** diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java b/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java index 35dd4dd..545e4b7 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java @@ -21,9 +21,12 @@ public class ConnectActionServerString extends ConnectActionServer { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) */ - public ConnectActionServerString(Socket s) { - super(s); + public ConnectActionServerString(Socket s, String key) { + super(s, key); } /** @@ -31,11 +34,14 @@ public class ConnectActionServerString extends ConnectActionServer { * * @param s * the socket to bind to + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param version * the server version */ - public ConnectActionServerString(Socket s, Version version) { - super(s, version); + public ConnectActionServerString(Socket s, String key, Version version) { + super(s, key, version); } /** diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index 2f23c28..ed27557 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -4,13 +4,6 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; - -import javax.net.ssl.SSLServerSocket; -import javax.net.ssl.SSLServerSocketFactory; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.SSLSocketFactory; import be.nikiroo.utils.TraceHandler; @@ -24,10 +17,9 @@ import be.nikiroo.utils.TraceHandler; * @author niki */ abstract class Server implements Runnable { - static private final String[] ANON_CIPHERS = getAnonCiphers(); + protected final String key; private final String name; - private final boolean ssl; private final Object lock = new Object(); private final Object counterLock = new Object(); @@ -61,8 +53,9 @@ abstract class Server implements Runnable { * the port to listen on, or 0 to assign any unallocated port * found (which can later on be queried via * {@link Server#getPort()} - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -72,20 +65,20 @@ abstract class Server implements Runnable { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public Server(int port, boolean ssl) throws IOException { - this((String) null, port, ssl); + public Server(int port, String key) throws IOException { + this((String) null, port, key); } /** * Create a new server that will start listening on the network when * {@link Server#start()} is called. + *

+ * All the communications will happen in plain text. * * @param name * the server name (only used for debug info and traces) * @param port * the port to listen on - * @param ssl - * use a SSL connection (or not) * * @throws IOException * in case of I/O error @@ -95,11 +88,35 @@ abstract class Server implements Runnable { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public Server(String name, int port, boolean ssl) throws IOException { + public Server(String name, int port) throws IOException { + this(name, port, null); + } + + /** + * Create a new server that will start listening on the network when + * {@link Server#start()} is called. + * + * @param name + * the server name (only used for debug info and traces) + * @param port + * the port to listen on + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) + * + * @throws IOException + * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive + */ + public Server(String name, int port, String key) throws IOException { this.name = name; this.port = port; - this.ssl = ssl; - this.ss = createSocketServer(port, ssl); + this.key = key; + this.ss = new ServerSocket(port); if (this.port == 0) { this.port = this.ss.getLocalPort(); @@ -213,7 +230,7 @@ abstract class Server implements Runnable { try { tracer.trace(name + ": server starting on port " + port + " (" - + (ssl ? "SSL" : "plain text") + ")"); + + (key != null ? "encrypted" : "plain text") + ")"); while (started && !exiting) { count(1); @@ -306,8 +323,8 @@ abstract class Server implements Runnable { exiting = true; try { - new ConnectActionClientObject(createSocket(null, port, ssl)) - .connect(); + new ConnectActionClientObject(new Socket((String) null, + port), key).connect(); long time = 0; while (ss != null && timeout > 0 && timeout > time) { Thread.sleep(10); @@ -366,8 +383,6 @@ abstract class Server implements Runnable { * the host to connect to * @param port * the port to connect to - * @param ssl - * TRUE for SSL mode (or FALSE for plain text mode) * * @return the {@link Socket} * @@ -379,20 +394,9 @@ abstract class Server implements Runnable { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - static Socket createSocket(String host, int port, boolean ssl) - throws IOException { - Socket s; - if (ssl) { - s = SSLSocketFactory.getDefault().createSocket(host, port); - if (s instanceof SSLSocket) { - // Should always be the case - ((SSLSocket) s).setEnabledCipherSuites(ANON_CIPHERS); - } - } else { - s = new Socket(host, port); - } - - return s; + @Deprecated + static Socket createSocket(String host, int port) throws IOException { + return new Socket(host, port); } /** @@ -400,8 +404,6 @@ abstract class Server implements Runnable { * * @param port * the port to accept connections on - * @param ssl - * TRUE for SSL mode (or FALSE for plain text mode) * * @return the {@link ServerSocket} * @@ -413,36 +415,8 @@ abstract class Server implements Runnable { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - static ServerSocket createSocketServer(int port, boolean ssl) - throws IOException { - ServerSocket ss; - if (ssl) { - ss = SSLServerSocketFactory.getDefault().createServerSocket(port); - if (ss instanceof SSLServerSocket) { - // Should always be the case - ((SSLServerSocket) ss).setEnabledCipherSuites(ANON_CIPHERS); - } - } else { - ss = new ServerSocket(port); - } - - return ss; - } - - /** - * Return all the supported ciphers that do not use authentication. - * - * @return the list of such supported ciphers - */ - public static String[] getAnonCiphers() { - List anonCiphers = new ArrayList(); - for (String cipher : ((SSLSocketFactory) SSLSocketFactory.getDefault()) - .getSupportedCipherSuites()) { - if (cipher.contains("_anon_")) { - anonCiphers.add(cipher); - } - } - - return anonCiphers.toArray(new String[] {}); + @Deprecated + static ServerSocket createSocketServer(int port) throws IOException { + return new ServerSocket(port); } } diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java index db50876..6c2ed01 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -25,7 +25,7 @@ import be.nikiroo.utils.serial.Importer; public class ServerBridge extends Server { private final String forwardToHost; private final int forwardToPort; - private final boolean forwardToSsl; + private final String forwardToKey; /** * Create a new server that will start listening on the network when @@ -35,14 +35,16 @@ public class ServerBridge extends Server { * the port to listen on, or 0 to assign any unallocated port * found (which can later on be queried via * {@link ServerBridge#getPort()} - * @param ssl - * use an SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param forwardToHost * the host server to forward the calls to * @param forwardToPort * the host port to forward the calls to - * @param forwardToSsl - * use an SSL connection for the forward server or not + * @param forwardToKey + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -52,12 +54,12 @@ public class ServerBridge extends Server { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ServerBridge(int port, boolean ssl, String forwardToHost, - int forwardToPort, boolean forwardToSsl) throws IOException { - super(port, ssl); + public ServerBridge(int port, String key, String forwardToHost, + int forwardToPort, String forwardToKey) throws IOException { + super(port, key); this.forwardToHost = forwardToHost; this.forwardToPort = forwardToPort; - this.forwardToSsl = forwardToSsl; + this.forwardToKey = forwardToKey; } /** @@ -68,14 +70,17 @@ public class ServerBridge extends Server { * the server name (only used for debug info and traces) * @param port * the port to listen on - * @param ssl - * use an SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * @param forwardToHost * the host server to forward the calls to * @param forwardToPort * the host port to forward the calls to - * @param forwardToSsl - * use an SSL connection for the forward server or not + * @param forwardToKey + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) use an SSL connection + * for the forward server or not * * @throws IOException * in case of I/O error @@ -85,13 +90,13 @@ public class ServerBridge extends Server { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ServerBridge(String name, int port, boolean ssl, - String forwardToHost, int forwardToPort, boolean forwardToSsl) + public ServerBridge(String name, int port, String key, + String forwardToHost, int forwardToPort, String forwardToKey) throws IOException { - super(name, port, ssl); + super(name, port, key); this.forwardToHost = forwardToHost; this.forwardToPort = forwardToPort; - this.forwardToSsl = forwardToSsl; + this.forwardToKey = forwardToKey; } /** @@ -118,7 +123,7 @@ public class ServerBridge extends Server { @Override protected ConnectActionServer createConnectActionServer(Socket s) { - return new ConnectActionServerString(s) { + return new ConnectActionServerString(s, key) { @Override public void action(final Version clientVersion) throws Exception { onClientContact(clientVersion); @@ -126,7 +131,7 @@ public class ServerBridge extends Server { try { new ConnectActionClientString(forwardToHost, forwardToPort, - forwardToSsl, clientVersion) { + forwardToKey, clientVersion) { @Override public void action(final Version serverVersion) throws Exception { @@ -207,7 +212,8 @@ public class ServerBridge extends Server { getTraceHandler().trace( getName() + ": will forward to " + forwardToHost + ":" + forwardToPort + " (" - + (forwardToSsl ? "SSL" : "plain text") + ")"); + + (forwardToKey != null ? "encrypted" : "plain text") + + ")"); super.run(); } @@ -221,18 +227,7 @@ public class ServerBridge extends Server { */ private void trace(String prefix, String data) { int size = data == null ? 0 : data.length(); - String ssize = size + " byte"; - if (size > 1) { - ssize = size + " bytes"; - if (size >= 1000) { - size = size / 1000; - ssize = size + " kb"; - if (size > 1000) { - size = size / 1000; - ssize = size + " MB"; - } - } - } + String ssize = StringUtils.formatNumber(size) + "bytes"; getTraceHandler().trace(prefix + ": " + ssize, 1); diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index 6d3d539..4f72013 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -24,8 +24,9 @@ abstract public class ServerObject extends Server { * the port to listen on, or 0 to assign any unallocated port * found (which can later on be queried via * {@link ServerObject#getPort()} - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -35,8 +36,8 @@ abstract public class ServerObject extends Server { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ServerObject(int port, boolean ssl) throws IOException { - super(port, ssl); + public ServerObject(int port, String key) throws IOException { + super(port, key); } /** @@ -47,8 +48,9 @@ abstract public class ServerObject extends Server { * the server name (only used for debug info and traces) * @param port * the port to listen on - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -58,13 +60,13 @@ abstract public class ServerObject extends Server { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ServerObject(String name, int port, boolean ssl) throws IOException { - super(name, port, ssl); + public ServerObject(String name, int port, String key) throws IOException { + super(name, port, key); } @Override protected ConnectActionServer createConnectActionServer(Socket s) { - return new ConnectActionServerObject(s) { + return new ConnectActionServerObject(s, key) { @Override public void action(Version clientVersion) throws Exception { try { diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index 89a7b28..9d8d008 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -24,8 +24,9 @@ abstract public class ServerString extends Server { * the port to listen on, or 0 to assign any unallocated port * found (which can later on be queried via * {@link ServerString#getPort()} - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -35,8 +36,8 @@ abstract public class ServerString extends Server { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ServerString(int port, boolean ssl) throws IOException { - super(port, ssl); + public ServerString(int port, String key) throws IOException { + super(port, key); } /** @@ -47,8 +48,9 @@ abstract public class ServerString extends Server { * the server name (only used for debug info and traces) * @param port * the port to listen on - * @param ssl - * use a SSL connection (or not) + * @param key + * an optional key to encrypt all the communications (if NULL, + * everything will be sent in clear text) * * @throws IOException * in case of I/O error @@ -58,13 +60,13 @@ abstract public class ServerString extends Server { * if the port parameter is outside the specified range of valid * port values, which is between 0 and 65535, inclusive */ - public ServerString(String name, int port, boolean ssl) throws IOException { - super(name, port, ssl); + public ServerString(String name, int port, String key) throws IOException { + super(name, port, key); } @Override protected ConnectActionServer createConnectActionServer(Socket s) { - return new ConnectActionServerString(s) { + return new ConnectActionServerString(s, key) { @Override public void action(Version clientVersion) throws Exception { for (String data = rec(); data != null; data = rec()) { diff --git a/src/be/nikiroo/utils/test/SerialServerTest.java b/src/be/nikiroo/utils/test/SerialServerTest.java index a34d30e..a3f4f9e 100644 --- a/src/be/nikiroo/utils/test/SerialServerTest.java +++ b/src/be/nikiroo/utils/test/SerialServerTest.java @@ -13,18 +13,18 @@ import be.nikiroo.utils.serial.server.ServerString; class SerialServerTest extends TestLauncher { private TestLauncher createServerStringTestCases(final String[] args, - final boolean ssl, final boolean bridge) { - final String ssls = (ssl ? "(ssl)" : "(plain text)"); + final String key, final boolean bridge) { + final String skey = (key != null ? "(encrypted)" : "(plain text)"); final String bridges = (bridge ? " with bridge" : ""); TestLauncher series = new TestLauncher( - "ServerString " + ssls + bridges, args); + "ServerString " + skey + bridges, args); - series.addTest(new TestCase("Simple connection " + ssls) { + series.addTest(new TestCase("Simple connection " + skey) { @Override public void test() throws Exception { final String[] rec = new String[1]; - ServerString server = new ServerString(this.getName(), 0, ssl) { + ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( ConnectActionServerString action, @@ -45,7 +45,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); @@ -58,7 +58,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, ssl) { + new ConnectActionClientObject(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -79,14 +79,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Simple exchange " + ssls) { + series.addTest(new TestCase("Simple exchange " + skey) { final String[] sent = new String[1]; final String[] recd = new String[1]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, ssl) { + ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( ConnectActionServerString action, @@ -108,7 +108,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -116,7 +116,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientString(null, port, ssl) { + new ConnectActionClientString(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -141,14 +141,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Multiple exchanges " + ssls) { + series.addTest(new TestCase("Multiple exchanges " + skey) { final String[] sent = new String[3]; final String[] recd = new String[3]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, ssl) { + ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( ConnectActionServerString action, @@ -172,7 +172,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -180,7 +180,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientString(null, port, ssl) { + new ConnectActionClientString(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -208,14 +208,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Multiple call from client " + ssls) { + series.addTest(new TestCase("Multiple call from client " + skey) { final String[] sent = new String[3]; final String[] recd = new String[3]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerString server = new ServerString(this.getName(), 0, ssl) { + ServerString server = new ServerString(this.getName(), 0, key) { @Override protected String onRequest( ConnectActionServerString action, @@ -237,7 +237,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -245,7 +245,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientString(null, port, ssl) { + new ConnectActionClientString(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -280,18 +280,18 @@ class SerialServerTest extends TestLauncher { } private TestLauncher createServerObjectTestCases(final String[] args, - final boolean ssl, final boolean bridge) { - final String ssls = (ssl ? "(ssl)" : "(plain text)"); + final String key, final boolean bridge) { + final String skey = (key != null ? "(encrypted)" : "(plain text)"); final String bridges = (bridge ? " with bridge" : ""); TestLauncher series = new TestLauncher( - "ServerObject " + ssls + bridges, args); + "ServerObject " + skey + bridges, args); - series.addTest(new TestCase("Simple connection " + ssls) { + series.addTest(new TestCase("Simple connection " + skey) { @Override public void test() throws Exception { final Object[] rec = new Object[1]; - ServerObject server = new ServerObject(this.getName(), 0, ssl) { + ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( ConnectActionServerObject action, @@ -312,7 +312,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -320,7 +320,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, ssl) { + new ConnectActionClientObject(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -345,14 +345,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Simple exchange " + ssls) { + series.addTest(new TestCase("Simple exchange " + skey) { final Object[] sent = new Object[1]; final Object[] recd = new Object[1]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { + ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( ConnectActionServerObject action, @@ -374,7 +374,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -382,7 +382,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, ssl) { + new ConnectActionClientObject(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -407,14 +407,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Multiple exchanges " + ssls) { + series.addTest(new TestCase("Multiple exchanges " + skey) { final Object[] sent = new Object[3]; final Object[] recd = new Object[3]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { + ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( ConnectActionServerObject action, @@ -438,7 +438,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -446,7 +446,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, ssl) { + new ConnectActionClientObject(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -474,14 +474,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Object array of URLs " + ssls) { + series.addTest(new TestCase("Object array of URLs " + skey) { final Object[] sent = new Object[1]; final Object[] recd = new Object[1]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { + ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( ConnectActionServerObject action, @@ -503,7 +503,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -511,7 +511,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, ssl) { + new ConnectActionClientObject(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -546,14 +546,14 @@ class SerialServerTest extends TestLauncher { } }); - series.addTest(new TestCase("Multiple call from client " + ssls) { + series.addTest(new TestCase("Multiple call from client " + skey) { final Object[] sent = new Object[3]; final Object[] recd = new Object[3]; final Exception[] err = new Exception[1]; @Override public void test() throws Exception { - ServerObject server = new ServerObject(this.getName(), 0, ssl) { + ServerObject server = new ServerObject(this.getName(), 0, key) { @Override protected Object onRequest( ConnectActionServerObject action, @@ -575,7 +575,7 @@ class SerialServerTest extends TestLauncher { ServerBridge br = null; if (bridge) { - br = new ServerBridge(0, ssl, "", port, ssl); + br = new ServerBridge(0, key, "", port, key); br.setTraceHandler(null); port = br.getPort(); br.start(); @@ -583,7 +583,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, ssl) { + new ConnectActionClientObject(null, port, key) { @Override public void action(Version serverVersion) throws Exception { @@ -620,10 +620,10 @@ class SerialServerTest extends TestLauncher { public SerialServerTest(String[] args) { super("SerialServer test", args); - for (boolean ssl : new Boolean[] { false, true }) { + for (String key : new String[] { null, "" }) { for (boolean bridge : new Boolean[] { false, true }) { - addSeries(createServerObjectTestCases(args, ssl, bridge)); - addSeries(createServerStringTestCases(args, ssl, bridge)); + addSeries(createServerObjectTestCases(args, key, bridge)); + addSeries(createServerStringTestCases(args, key, bridge)); } } } -- 2.27.0