- new: server: count the bytes we rec/send
- new: CryptUtils
+- serial: SSL -> CryptUtils
## Version 4.7.2
* <ul>
* <li>The bridge name</li>
* <li>The bridge port</li>
- * <li>TRUE for an SSL bridge, FALSE for plain text</li>
+ * <li>a key for an encrypted bridge, PLAIN_TEXT for plain text</li>
* <li>The forward server host</li>
* <li>The forward server port</li>
- * <li>TRUE for an SSL forward server, FALSE for plain text</li>
+ * <li>a key for an encrypted forward server, PLAIN_TEXT for
+ * plain text</li>
* <li>(optional) a trace level</li>
* <li>(optional) a truncate size for data</li>
* </ul>
+ "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;
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) {
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();
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;
private Version version;
private Version clientVersion;
+ private CryptUtils crypt;
+
private Object lock = new Object();
private BufferedReader in;
private OutputStreamWriter out;
* @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();
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(
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 {
*/
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();
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
*
* @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());
}
/**
* 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
* 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());
}
/**
* 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
*
* 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);
}
/**
*
* @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);
*
* @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);
}
/**
* 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
* 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);
}
/**
* 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
*
* 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);
}
/**
* 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);
}
/**
*
* @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);
}
/**
* 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
* 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);
}
/**
* 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
*
* 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);
}
/**
*
* @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);
}
/**
*
* @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());
}
/**
*
* @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);
* <p>
* 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;
*
* @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);
}
/**
*
* @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);
}
/**
*
* @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);
}
/**
*
* @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);
}
/**
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;
* @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();
* 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
* 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.
+ * <p>
+ * 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
* 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();
try {
tracer.trace(name + ": server starting on port " + port + " ("
- + (ssl ? "SSL" : "plain text") + ")");
+ + (key != null ? "encrypted" : "plain text") + ")");
while (started && !exiting) {
count(1);
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);
* 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}
*
* 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);
}
/**
*
* @param port
* the port to accept connections on
- * @param ssl
- * TRUE for SSL mode (or FALSE for plain text mode)
*
* @return the {@link ServerSocket}
*
* 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<String> anonCiphers = new ArrayList<String>();
- 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);
}
}
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
* 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
* 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;
}
/**
* 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
* 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;
}
/**
@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);
try {
new ConnectActionClientString(forwardToHost, forwardToPort,
- forwardToSsl, clientVersion) {
+ forwardToKey, clientVersion) {
@Override
public void action(final Version serverVersion)
throws Exception {
getTraceHandler().trace(
getName() + ": will forward to " + forwardToHost + ":"
+ forwardToPort + " ("
- + (forwardToSsl ? "SSL" : "plain text") + ")");
+ + (forwardToKey != null ? "encrypted" : "plain text")
+ + ")");
super.run();
}
*/
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);
* 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
* 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);
}
/**
* 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
* 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 {
* 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
* 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);
}
/**
* 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
* 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()) {
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,
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();
try {
try {
- new ConnectActionClientObject(null, port, ssl) {
+ new ConnectActionClientObject(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientString(null, port, ssl) {
+ new ConnectActionClientString(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientString(null, port, ssl) {
+ new ConnectActionClientString(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientString(null, port, ssl) {
+ new ConnectActionClientString(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
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,
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();
try {
try {
- new ConnectActionClientObject(null, port, ssl) {
+ new ConnectActionClientObject(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientObject(null, port, ssl) {
+ new ConnectActionClientObject(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientObject(null, port, ssl) {
+ new ConnectActionClientObject(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientObject(null, port, ssl) {
+ new ConnectActionClientObject(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
}
});
- 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,
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();
try {
try {
- new ConnectActionClientObject(null, port, ssl) {
+ new ConnectActionClientObject(null, port, key) {
@Override
public void action(Version serverVersion)
throws Exception {
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));
}
}
}