Merge branch 'master' of github.com:nikiroo/fanfix
authorNiki Roo <niki@nikiroo.be>
Tue, 23 Apr 2019 04:46:37 +0000 (06:46 +0200)
committerNiki Roo <niki@nikiroo.be>
Tue, 23 Apr 2019 04:46:37 +0000 (06:46 +0200)
libs/nikiroo-utils-4.7.2-dev-sources.jar
src/be/nikiroo/fanfix/library/RemoteLibrary.java
src/be/nikiroo/fanfix/library/RemoteLibraryServer.java
src/be/nikiroo/fanfix/supported/Fanfiction.java

index dd97f475bca15dd5758bb645091e3f7dfeb5a015..9ce0b0eb90cbecd8ea9cccf544493f3542a51e1e 100644 (file)
Binary files a/libs/nikiroo-utils-4.7.2-dev-sources.jar and b/libs/nikiroo-utils-4.7.2-dev-sources.jar differ
index 3318c12bf7e5f55e743091aede40e9998d28ccd0..c2ed310b5da0fbf9f1e49dc57eacbb17c448e3fe 100644 (file)
@@ -7,12 +7,13 @@ import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
 
+import javax.net.ssl.SSLException;
+
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
-import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.server.ConnectActionClientObject;
 
@@ -58,25 +59,25 @@ public class RemoteLibrary extends BasicLibrary {
 
                try {
                        Instance.getTraceHandler().trace("Getting remote lib status...");
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       try {
-                                               Object rep = sendCmd(this, new Object[] { "PING" });
+                                       Object rep = send(new Object[] { "PING" });
 
-                                               if ("PONG".equals(rep)) {
-                                                       result[0] = Status.READY;
-                                               } else {
-                                                       result[0] = Status.UNAUTORIZED;
-                                               }
-                                       } catch (IllegalArgumentException e) {
+                                       if ("PONG".equals(rep)) {
+                                               result[0] = Status.READY;
+                                       } else {
                                                result[0] = Status.UNAUTORIZED;
                                        }
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       result[0] = Status.UNAVAILABLE;
+                                       if (e instanceof SSLException) {
+                                               result[0] = Status.UNAUTORIZED;
+                                       } else {
+                                               result[0] = Status.UNAVAILABLE;
+                                       }
                                }
                        }.connect();
                } catch (UnknownHostException e) {
@@ -96,17 +97,21 @@ public class RemoteLibrary extends BasicLibrary {
                final Image[] result = new Image[1];
 
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       Object rep = sendCmd(this,
-                                                       new Object[] { "GET_COVER", luid });
+                                       Object rep = send(new Object[] { "GET_COVER", luid });
                                        result[0] = (Image) rep;
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -131,17 +136,22 @@ public class RemoteLibrary extends BasicLibrary {
                final Image[] result = new Image[1];
 
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       Object rep = sendCmd(this, new Object[] {
-                                                       "GET_CUSTOM_COVER", type, source });
+                                       Object rep = send(new Object[] { "GET_CUSTOM_COVER", type,
+                                                       source });
                                        result[0] = (Image) rep;
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -157,7 +167,7 @@ public class RemoteLibrary extends BasicLibrary {
                final Story[] result = new Story[1];
 
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
                                        Progress pg = pgF;
@@ -165,8 +175,7 @@ public class RemoteLibrary extends BasicLibrary {
                                                pg = new Progress();
                                        }
 
-                                       Object rep = sendCmd(this,
-                                                       new Object[] { "GET_STORY", luid });
+                                       Object rep = send(new Object[] { "GET_STORY", luid });
 
                                        MetaData meta = null;
                                        if (rep instanceof MetaData) {
@@ -188,7 +197,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -214,7 +228,7 @@ public class RemoteLibrary extends BasicLibrary {
 
                final Progress pgF = pgSave;
 
-               new ConnectActionClientObject(host, port, false) {
+               new ConnectActionClientObject(host, port, key) {
                        @Override
                        public void action(Version serverVersion) throws Exception {
                                Progress pg = pgF;
@@ -222,7 +236,7 @@ public class RemoteLibrary extends BasicLibrary {
                                        pg.setMinMax(0, (int) story.getMeta().getWords());
                                }
 
-                               sendCmd(this, new Object[] { "SAVE_STORY", luid });
+                               send(new Object[] { "SAVE_STORY", luid });
 
                                List<Object> list = RemoteLibraryServer.breakStory(story);
                                for (Object obj : list) {
@@ -237,7 +251,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                        @Override
                        protected void onError(Exception e) {
-                               Instance.getTraceHandler().error(e);
+                               if (e instanceof SSLException) {
+                                       Instance.getTraceHandler().error(
+                                                       "Connection refused (bad key)");
+                               } else {
+                                       Instance.getTraceHandler().error(e);
+                               }
                        }
                }.connect();
 
@@ -259,15 +278,20 @@ public class RemoteLibrary extends BasicLibrary {
 
        @Override
        public synchronized void delete(final String luid) throws IOException {
-               new ConnectActionClientObject(host, port, false) {
+               new ConnectActionClientObject(host, port, key) {
                        @Override
                        public void action(Version serverVersion) throws Exception {
-                               sendCmd(this, new Object[] { "DELETE_STORY", luid });
+                               send(new Object[] { "DELETE_STORY", luid });
                        }
 
                        @Override
                        protected void onError(Exception e) {
-                               Instance.getTraceHandler().error(e);
+                               if (e instanceof SSLException) {
+                                       Instance.getTraceHandler().error(
+                                                       "Connection refused (bad key)");
+                               } else {
+                                       Instance.getTraceHandler().error(e);
+                               }
                        }
                }.connect();
        }
@@ -286,16 +310,20 @@ public class RemoteLibrary extends BasicLibrary {
        private void setCover(final String value, final String luid,
                        final String type) {
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       sendCmd(this,
-                                                       new Object[] { "SET_COVER", type, value, luid });
+                                       send(new Object[] { "SET_COVER", type, value, luid });
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -327,13 +355,12 @@ public class RemoteLibrary extends BasicLibrary {
                final String[] luid = new String[1];
 
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
                                        Progress pg = pgF;
 
-                                       Object rep = sendCmd(this,
-                                                       new Object[] { "IMPORT", url.toString() });
+                                       Object rep = send(new Object[] { "IMPORT", url.toString() });
 
                                        while (true) {
                                                if (!RemoteLibraryServer.updateProgress(pg, rep)) {
@@ -349,7 +376,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -375,13 +407,13 @@ public class RemoteLibrary extends BasicLibrary {
                final Progress pgF = pg == null ? new Progress() : pg;
 
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
                                        Progress pg = pgF;
 
-                                       Object rep = sendCmd(this, new Object[] { "CHANGE_STA",
-                                                       luid, newSource, newTitle, newAuthor });
+                                       Object rep = send(new Object[] { "CHANGE_STA", luid,
+                                                       newSource, newTitle, newAuthor });
                                        while (true) {
                                                if (!RemoteLibraryServer.updateProgress(pg, rep)) {
                                                        break;
@@ -393,7 +425,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -412,15 +449,20 @@ public class RemoteLibrary extends BasicLibrary {
         */
        public void exit() {
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       sendCmd(this, new Object[] { "EXIT" });
+                                       send(new Object[] { "EXIT" });
                                }
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (IOException e) {
@@ -491,7 +533,7 @@ public class RemoteLibrary extends BasicLibrary {
                final List<MetaData> metas = new ArrayList<MetaData>();
 
                try {
-                       new ConnectActionClientObject(host, port, false) {
+                       new ConnectActionClientObject(host, port, key) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
                                        Progress pg = pgF;
@@ -499,8 +541,7 @@ public class RemoteLibrary extends BasicLibrary {
                                                pg = new Progress();
                                        }
 
-                                       Object rep = sendCmd(this, new Object[] { "GET_METADATA",
-                                                       luid });
+                                       Object rep = send(new Object[] { "GET_METADATA", luid });
 
                                        while (true) {
                                                if (!RemoteLibraryServer.updateProgress(pg, rep)) {
@@ -521,7 +562,12 @@ public class RemoteLibrary extends BasicLibrary {
 
                                @Override
                                protected void onError(Exception e) {
-                                       Instance.getTraceHandler().error(e);
+                                       if (e instanceof SSLException) {
+                                               Instance.getTraceHandler().error(
+                                                               "Connection refused (bad key)");
+                                       } else {
+                                               Instance.getTraceHandler().error(e);
+                                       }
                                }
                        }.connect();
                } catch (Exception e) {
@@ -530,30 +576,4 @@ public class RemoteLibrary extends BasicLibrary {
 
                return metas;
        }
-
-       // IllegalArgumentException if key is bad
-       private Object sendCmd(ConnectActionClientObject action, Object[] params)
-                       throws IOException, NoSuchFieldException, NoSuchMethodException,
-                       ClassNotFoundException {
-               Object rep = action.send(params);
-
-               String hash = hashKey(key, "" + rep);
-               return action.send(hash);
-       }
-
-       /**
-        * Return a hash that corresponds to the given key and the given random
-        * value.
-        * 
-        * @param key
-        *            the key (the secret)
-        * 
-        * @param random
-        *            the random value
-        * 
-        * @return a hash that was computed using both
-        */
-       static String hashKey(String key, String random) {
-               return StringUtils.getMd5Hash(key + " <==> " + random);
-       }
 }
index a40103c0b40f5d6e18995609b5731f5fda918a50..c2edeb3ecebeba71ee7149c118b9aaaab9f60512 100644 (file)
@@ -6,6 +6,8 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
+import javax.net.ssl.SSLException;
+
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.Chapter;
 import be.nikiroo.fanfix.data.MetaData;
@@ -24,14 +26,6 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * The available commands are given as arrays of objects (first item is the
  * command, the rest are the arguments).
  * <p>
- * All commands, including PING, will first return a random value to you that
- * you must hash with your key and return before processing the rest; if the
- * value not correct, the connection will be closed.
- * <p>
- * BTW: this system <b>is by no means secure</b>. It is just slightly
- * obfuscated, and operate on clear text (because Google decided not to support
- * anonymous SSL exchanges on Android, and the main use case for this server is
- * Android).
  * <ul>
  * <li>PING: will return PONG if the key is accepted</li>
  * <li>GET_METADATA *: will return the metadata of all the stories in the
@@ -57,8 +51,6 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * @author niki
  */
 public class RemoteLibraryServer extends ServerObject {
-       private final String key;
-
        /**
         * Create a new remote server (will not be active until
         * {@link RemoteLibraryServer#start()} is called).
@@ -72,8 +64,7 @@ public class RemoteLibraryServer extends ServerObject {
         *             in case of I/O error
         */
        public RemoteLibraryServer(String key, int port) throws IOException {
-               super("Fanfix remote library", port, false);
-               this.key = key;
+               super("Fanfix remote library", port, key);
 
                setTraceHandler(Instance.getTraceHandler());
        }
@@ -103,18 +94,6 @@ public class RemoteLibraryServer extends ServerObject {
                }
                System.out.println(trace);
 
-               // Authentication:
-               String random = StringUtils.getMd5Hash(Double.toString(Math.random()));
-               action.send(random);
-               String answer = "" + action.rec();
-
-               if (!answer.equals(RemoteLibrary.hashKey(key, random))) {
-                       System.out.println("Key rejected.");
-                       action.close();
-                       return null;
-               }
-               //
-
                Object rep = doRequest(action, command, args);
 
                String rec = StringUtils.formatNumber(action.getBytesReceived()) + "b";
@@ -224,7 +203,11 @@ public class RemoteLibraryServer extends ServerObject {
 
        @Override
        protected void onError(Exception e) {
-               getTraceHandler().error(e);
+               if (e instanceof SSLException) {
+                       System.out.println("[Client connection refused (bad key)]");
+               } else {
+                       getTraceHandler().error(e);
+               }
        }
 
        /**
index 7bdd4ffb9ca5c74f6548069328fae900be05b2e0..64df8d3195bb6274668f26231e459701c11eac72 100644 (file)
@@ -118,7 +118,7 @@ class Fanfiction extends BasicSupport_Deprecated {
                        }
                }
 
-               return null;
+               return "";
        }
 
        private String getAuthor(InputStream in) {