From: Niki Roo Date: Thu, 2 May 2019 19:30:02 +0000 (+0200) Subject: server: add a HELLO handshake X-Git-Tag: fanfix-swing-0.0.1~21^2~89 X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=340e6065e8027c2b3b88549b5761b0b9f6950a53;p=fanfix-swing.git server: add a HELLO handshake --- diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java index 31b71b92..1b92b422 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; +import javax.net.ssl.SSLException; + /** * Base class used for the client basic handling. *

@@ -57,6 +59,7 @@ abstract class ConnectActionClient { action = new ConnectAction(s, false, key) { @Override protected void action() throws Exception { + ConnectActionClient.this.clientHello(); ConnectActionClient.this.action(); } @@ -67,6 +70,25 @@ abstract class ConnectActionClient { }; } + /** + * Send the HELLO message (send a String "HELLO" to the server, to check I/O + * and encryption modes). + *

+ * Will automatically handle the answer (the server must answer "HELLO" in + * kind). + * + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of encryption error + */ + protected void clientHello() throws IOException { + String HELLO = action.sendString("HELLO"); + if (!"HELLO".equals(HELLO)) { + throw new SSLException("Server did not accept the encryption key"); + } + } + /** * Actually start the process and call the action (synchronous). */ diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java index da13be5b..791f850b 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java @@ -4,8 +4,6 @@ import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; -import be.nikiroo.utils.Version; - /** * Class used for the client basic handling. *

@@ -16,9 +14,7 @@ import be.nikiroo.utils.Version; */ public class ConnectActionClientObject extends ConnectActionClient { /** - * Create a new {@link ConnectActionClientObject} with the current - * application version (see {@link Version#getCurrentVersion()}) as the - * client version. + * Create a new {@link ConnectActionClientObject} . * * @param s * the socket to bind to @@ -31,9 +27,7 @@ public class ConnectActionClientObject extends ConnectActionClient { } /** - * Create a new {@link ConnectActionClientObject} with the current - * application version (see {@link Version#getCurrentVersion()}) as the - * client version. + * Create a new {@link ConnectActionClientObject}. * * @param host * the host to bind to diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java index 366070c6..17da668a 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClientString.java @@ -4,8 +4,6 @@ import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; -import be.nikiroo.utils.Version; - /** * Class used for the client basic handling. *

@@ -16,9 +14,7 @@ import be.nikiroo.utils.Version; */ public class ConnectActionClientString extends ConnectActionClient { /** - * Create a new {@link ConnectActionClientString} with the current - * application version (see {@link Version#getCurrentVersion()}) as the - * client version. + * Create a new {@link ConnectActionClientString}. * * @param s * the socket to bind to @@ -31,9 +27,7 @@ public class ConnectActionClientString extends ConnectActionClient { } /** - * Create a new {@link ConnectActionClientString} with the current - * application version (see {@link Version#getCurrentVersion()}) as the - * client version. + * Create a new {@link ConnectActionClientString}. * * @param host * the host to bind to diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java index d0ddb92a..42a562d2 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java @@ -1,7 +1,10 @@ package be.nikiroo.utils.serial.server; +import java.io.IOException; import java.net.Socket; +import javax.net.ssl.SSLException; + /** * Base class used for the server basic handling. *

@@ -33,6 +36,7 @@ abstract class ConnectActionServer { action = new ConnectAction(s, true, key) { @Override protected void action() throws Exception { + ConnectActionServer.this.serverHello(); ConnectActionServer.this.action(); } @@ -43,6 +47,23 @@ abstract class ConnectActionServer { }; } + /** + * Send the HELLO message (check that the client sends a String "HELLO" and + * send it back, to check I/O and encryption modes). + * + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of encryption error + */ + protected void serverHello() throws IOException, SSLException { + String HELLO = action.recString(); + if (!"HELLO".equals(HELLO)) { + throw new SSLException("Server did not accept the encryption key"); + } + action.sendString(HELLO); + } + /** * Actually start the process and call the action (synchronous). */ diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java b/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java index 3457a08f..07d98676 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServerObject.java @@ -3,8 +3,6 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; -import be.nikiroo.utils.Version; - /** * Class used for the server basic handling. *

@@ -15,9 +13,7 @@ import be.nikiroo.utils.Version; */ public class ConnectActionServerObject extends ConnectActionServer { /** - * Create a new {@link ConnectActionServerObject} with the current - * application version (see {@link Version#getCurrentVersion()}) as the - * server version. + * Create a new {@link ConnectActionServerObject} as the server version. * * @param s * the socket to bind to diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java b/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java index 2c3ad8ff..8d113c1d 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServerString.java @@ -3,8 +3,6 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.net.Socket; -import be.nikiroo.utils.Version; - /** * Class used for the server basic handling. *

@@ -15,9 +13,7 @@ import be.nikiroo.utils.Version; */ public class ConnectActionServerString extends ConnectActionServer { /** - * Create a new {@link ConnectActionServerString} with the current - * application version (see {@link Version#getCurrentVersion()}) as the - * server version. + * Create a new {@link ConnectActionServerString} as the server version. * * @param s * the socket to bind to diff --git a/src/be/nikiroo/utils/serial/server/Server.java b/src/be/nikiroo/utils/serial/server/Server.java index 932a7051..04701590 100644 --- a/src/be/nikiroo/utils/serial/server/Server.java +++ b/src/be/nikiroo/utils/serial/server/Server.java @@ -324,8 +324,7 @@ abstract class Server implements Runnable { exiting = true; try { - new ConnectActionClientObject(new Socket((String) null, - port), key).connect(); + getConnectionToMe().connect(); long time = 0; while (ss != null && timeout > 0 && timeout > time) { Thread.sleep(10); @@ -349,6 +348,20 @@ abstract class Server implements Runnable { } } + /** + * Return a connection to this server (used by the Exit code to send an exit + * message). + * + * @return the connection + * + * @throws UnknownHostException + * the host should always be NULL (localhost) + * @throws IOException + * in case of I/O error + */ + abstract protected ConnectActionClient getConnectionToMe() + throws UnknownHostException, IOException; + /** * Change the number of currently serviced actions. * @@ -403,48 +416,4 @@ abstract class Server implements Runnable { @SuppressWarnings("unused") protected void onRequestDone(long id, long bytesReceived, long bytesSent) { } - - /** - * Create a {@link Socket}. - * - * @param host - * the host to connect to - * @param port - * the port to connect to - * - * @return the {@link Socket} - * - * @throws IOException - * in case of I/O error - * @throws UnknownHostException - * if the host is not known - * @throws IllegalArgumentException - * if the port parameter is outside the specified range of valid - * port values, which is between 0 and 65535, inclusive - */ - @Deprecated - static Socket createSocket(String host, int port) throws IOException { - return new Socket(host, port); - } - - /** - * Create a {@link ServerSocket}. - * - * @param port - * the port to accept connections on - * - * @return the {@link ServerSocket} - * - * @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 - */ - @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 84433fe9..25d451c5 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -198,6 +198,13 @@ public class ServerBridge extends Server { trace("<<< SERVER", data); } + @Override + protected ConnectActionClient getConnectionToMe() + throws UnknownHostException, IOException { + return new ConnectActionClientString(new Socket((String) null, + getPort()), key); + } + @Override public void run() { getTraceHandler().trace( diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index f7977669..dcd1badd 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -95,6 +95,13 @@ abstract public class ServerObject extends Server { }; } + @Override + protected ConnectActionClient getConnectionToMe() + throws UnknownHostException, IOException { + return new ConnectActionClientObject(new Socket((String) null, + getPort()), key); + } + /** * This is the method that is called on each client request. *

diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index 6fe4ad0d..6a0d4f46 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -95,6 +95,13 @@ abstract public class ServerString extends Server { }; } + @Override + protected ConnectActionClient getConnectionToMe() + throws UnknownHostException, IOException { + return new ConnectActionClientString(new Socket((String) null, + getPort()), key); + } + /** * This is the method that is called on each client request. *

diff --git a/src/be/nikiroo/utils/test_code/SerialServerTest.java b/src/be/nikiroo/utils/test_code/SerialServerTest.java index 956499a4..28eb8bb6 100644 --- a/src/be/nikiroo/utils/test_code/SerialServerTest.java +++ b/src/be/nikiroo/utils/test_code/SerialServerTest.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.test_code; import java.net.URL; +import be.nikiroo.utils.TraceHandler; import be.nikiroo.utils.serial.server.ConnectActionClientObject; import be.nikiroo.utils.serial.server.ConnectActionClientString; import be.nikiroo.utils.serial.server.ConnectActionServerObject; @@ -81,7 +82,7 @@ class SerialServerTest extends TestLauncher { try { try { - new ConnectActionClientObject(null, port, key) { + new ConnectActionClientString(null, port, key) { @Override public void action() throws Exception { rec[0] = "ok";