X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FConnectAction.java;h=c5750bdcf2aa0616c955229a94388cb95195066b;hb=3b4319db57971bce3afd9092a0f9db9809d47ca2;hp=cef10adf1c80736a4603c8d99c1fe4c82710d480;hpb=4bb7e88e5165691ed1c14d93b4bb0a9fbe811090;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index cef10ad..c5750bd 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -2,12 +2,14 @@ package be.nikiroo.utils.serial.server; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import java.io.OutputStream; 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,9 +29,11 @@ abstract class ConnectAction { private Version version; private Version clientVersion; + private CryptUtils crypt; + private Object lock = new Object(); - private BufferedReader in; - private OutputStreamWriter out; + private InputStream in; + private OutputStream out; private boolean contentToSend; private long bytesReceived; @@ -65,7 +69,8 @@ abstract class ConnectAction { * Handler called when an unexpected error occurs in the code. * * @param e - * the exception that occurred + * the exception that occurred, SSLException usually denotes a + * crypt error */ abstract protected void onError(Exception e); @@ -77,12 +82,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(); @@ -125,13 +137,19 @@ abstract class ConnectAction { */ public void connect() { try { - in = new BufferedReader(new InputStreamReader(s.getInputStream(), - "UTF-8")); + in = s.getInputStream(); try { - out = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); + out = s.getOutputStream(); try { if (server) { - String line = in.readLine(); + String line; + try { + line = readLine(in); + } catch (SSLException e) { + out.write("Unauthorized\n".getBytes()); + throw e; + } + if (line != null && line.startsWith("VERSION ")) { // "VERSION client-version" (VERSION 1.0.0) Version clientVersion = new Version( @@ -163,19 +181,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 { @@ -194,8 +199,9 @@ abstract class ConnectAction { * @param data * the data to send * - * @return the answer (which can be NULL) if this action is a client, always - * NULL if it is a server + * @return the answer (which can be NULL if no answer, or NULL for an answer + * which is NULL) if this action is a client, always NULL if it is a + * server * * @throws IOException * in case of I/O error @@ -211,10 +217,19 @@ abstract class ConnectAction { protected Object sendObject(Object data) throws IOException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException { synchronized (lock) { - String rep = sendString(new Exporter().append(data).toString(true, - true)); - if (rep != null) { - return new Importer().read(rep).getValue(); + + new Exporter(out).append(data); + + if (server) { + out.flush(); + return null; + } + + contentToSend = true; + try { + return recObject(); + } catch (NullPointerException e) { + // We accept no data here } return null; @@ -248,12 +263,18 @@ abstract class ConnectAction { protected Object recObject() throws IOException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, java.lang.NullPointerException { - String str = recString(); - if (str == null) { - throw new NullPointerException("No more data available"); - } + synchronized (lock) { + if (server || contentToSend) { + if (contentToSend) { + out.flush(); + contentToSend = false; + } + + return new Importer().read(in).getValue(); + } - return new Importer().read(str).getValue(); + return null; + } } /** @@ -268,12 +289,12 @@ abstract class ConnectAction { * * @throws IOException * in case of I/O error + * @throws SSLException + * in case of crypt error */ 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(); @@ -298,6 +319,8 @@ abstract class ConnectAction { * * @throws IOException * in case of I/O error + * @throws SSLException + * in case of crypt error */ protected String recString() throws IOException { synchronized (lock) { @@ -307,15 +330,66 @@ abstract class ConnectAction { contentToSend = false; } - String line = in.readLine(); - if (line != null) { - bytesReceived += line.length(); - } - - return line; + return readLine(in); } return null; } } + + /** + * Read a possibly encrypted line. + * + * @param in + * the stream to read from + * @return the unencrypted line + * + * + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of crypt error + */ + private String readLine(InputStream in) throws IOException { + if (inReader == null) { + inReader = new BufferedReader(new InputStreamReader(in)); + } + String line = inReader.readLine(); + if (line != null) { + bytesReceived += line.length(); + if (crypt != null) { + line = crypt.decrypt64s(line, false); + } + } + + return line; + } + + private BufferedReader inReader; + + /** + * Write a line, possible encrypted. + * + * @param out + * the stream to write to + * @param line + * the line to write + * @throws IOException + * in case of I/O error + * @throws SSLException + * in case of crypt error + */ + private void writeLine(OutputStream out, String line) throws IOException { + if (crypt == null) { + out.write(line.getBytes("UTF-8")); + bytesSent += line.length(); + } else { + // TODO: how NOT to create so many big Strings? + String b64 = crypt.encrypt64(line, false); + out.write(b64.getBytes("UTF-8")); + bytesSent += b64.length(); + } + out.write("\n".getBytes("UTF-8")); + bytesSent++; + } } \ No newline at end of file