serial: switch from SSL to CryptUtils
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
index 39417a77fca5c7671afd7d932d12cc22d4115397..7d724248384d77a5ba459f4215a1aeebf80e7a13 100644 (file)
@@ -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,11 +26,16 @@ abstract class ConnectAction {
        private Version version;
        private Version clientVersion;
 
+       private CryptUtils crypt;
+
        private Object lock = new Object();
        private BufferedReader in;
        private OutputStreamWriter out;
        private boolean contentToSend;
 
+       private long bytesReceived;
+       private long bytesSent;
+
        /**
         * Method that will be called when an action is performed on either the
         * client or server this {@link ConnectAction} represent.
@@ -74,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();
@@ -99,18 +110,36 @@ abstract class ConnectAction {
                return version;
        }
 
+       /**
+        * The total amount of bytes received.
+        * 
+        * @return the amount of bytes received
+        */
+       public long getBytesReceived() {
+               return bytesReceived;
+       }
+
+       /**
+        * The total amount of bytes sent.
+        * 
+        * @return the amount of bytes sent
+        */
+       public long getBytesSent() {
+               return bytesSent;
+       }
+
        /**
         * Actually start the process (this is synchronous).
         */
        public void connect() {
                try {
-                       in = new BufferedReader(
-                                       new InputStreamReader(s.getInputStream(), "UTF-8"));
+                       in = new BufferedReader(new InputStreamReader(s.getInputStream(),
+                                       "UTF-8"));
                        try {
                                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(
@@ -142,20 +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 {
@@ -188,12 +203,11 @@ abstract class ConnectAction {
         * @throws ClassNotFoundException
         *             if a class described in the serialised data cannot be found
         */
-       protected Object sendObject(Object data)
-                       throws IOException, NoSuchFieldException, NoSuchMethodException,
-                       ClassNotFoundException {
+       protected Object sendObject(Object data) throws IOException,
+                       NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
                synchronized (lock) {
-                       String rep = sendString(
-                                       new Exporter().append(data).toString(true, true));
+                       String rep = sendString(new Exporter().append(data).toString(true,
+                                       true));
                        if (rep != null) {
                                return new Importer().read(rep).getValue();
                        }
@@ -226,9 +240,9 @@ abstract class ConnectAction {
         * @throws java.lang.NullPointerException
         *             if the counter part has no data to send
         */
-       protected Object recObject()
-                       throws IOException, NoSuchFieldException, NoSuchMethodException,
-                       ClassNotFoundException, java.lang.NullPointerException {
+       protected Object recObject() throws IOException, NoSuchFieldException,
+                       NoSuchMethodException, ClassNotFoundException,
+                       java.lang.NullPointerException {
                String str = recString();
                if (str == null) {
                        throw new NullPointerException("No more data available");
@@ -252,8 +266,7 @@ abstract class ConnectAction {
         */
        protected String sendString(String line) throws IOException {
                synchronized (lock) {
-                       out.write(line);
-                       out.write("\n");
+                       writeLine(out, line);
 
                        if (server) {
                                out.flush();
@@ -287,10 +300,37 @@ abstract class ConnectAction {
                                        contentToSend = false;
                                }
 
-                               return in.readLine();
+                               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