X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FConnectAction.java;h=a377ced597f2a6bd84d01fd008f7f5099b9409a3;hb=9af739b58e2cd5357a21d6fd0410049ab2a2bff3;hp=8f57c628fb18e861c9805174468f5ccb0ce0fba1;hpb=79ce1a4973eba079ba819ba841d906de42f38e40;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 8f57c62..a377ced 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -2,10 +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; @@ -25,11 +29,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 InputStream in; + private OutputStream 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. @@ -60,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); @@ -72,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(); @@ -97,17 +114,55 @@ 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 = s.getInputStream(); try { - out = new OutputStreamWriter(s.getOutputStream(), "UTF-8"); + out = s.getOutputStream(); try { if (server) { + 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( + line.substring("VERSION ".length())); + this.clientVersion = clientVersion; + Version v = negotiateVersion(clientVersion); + if (v == null) { + v = new Version(); + } + + sendString("VERSION " + v.toString()); + } + action(clientVersion); } else { String v = sendString("VERSION " + version.toString()); @@ -119,9 +174,11 @@ abstract class ConnectAction { } } finally { out.close(); + out = null; } } finally { in.close(); + in = null; } } catch (Exception e) { onError(e); @@ -159,7 +216,8 @@ abstract class ConnectAction { protected Object sendObject(Object data) throws IOException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException { synchronized (lock) { - String rep = sendString(new Exporter().append(data).toString(true)); + String rep = sendString(new Exporter().append(data).toString(true, + true)); if (rep != null) { return new Importer().read(rep).getValue(); } @@ -215,11 +273,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"); + writeLine(out, line); if (server) { out.flush(); @@ -244,6 +303,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) { @@ -253,25 +314,66 @@ abstract class ConnectAction { contentToSend = false; } - String line = in.readLine(); - if (server && line != null && line.startsWith("VERSION ")) { - // "VERSION client-version" (VERSION 1.0.0) - Version clientVersion = new Version( - line.substring("VERSION ".length())); - this.clientVersion = clientVersion; - Version v = negotiateVersion(clientVersion); - if (v == null) { - v = new Version(); - } - sendString("VERSION " + v.toString()); + return readLine(in); + } - line = in.readLine(); - } + return null; + } + } - return line; + /** + * 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 null; + 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()); + bytesSent += line.length(); + } else { + // TODO: how NOT to create so many big Strings? + String b64 = crypt.encrypt64(line, false); + out.write(b64.getBytes()); + bytesSent += b64.length(); } + out.write("\n".getBytes()); + bytesSent++; } } \ No newline at end of file