From: Niki Roo Date: Fri, 10 May 2019 17:19:02 +0000 (+0200) Subject: allox subkeys, step 1 (keys not active) X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=fb25273cf02653ac343d5437083b1bc41af5ce23;p=nikiroo-utils.git allox subkeys, step 1 (keys not active) --- diff --git a/libs/nikiroo-utils-4.7.2-dev-sources.jar b/libs/nikiroo-utils-4.7.2-dev-sources.jar index c4f532d..180d387 100644 Binary files a/libs/nikiroo-utils-4.7.2-dev-sources.jar and b/libs/nikiroo-utils-4.7.2-dev-sources.jar differ diff --git a/src/be/nikiroo/fanfix/Instance.java b/src/be/nikiroo/fanfix/Instance.java index b6849c2..ef7799f 100644 --- a/src/be/nikiroo/fanfix/Instance.java +++ b/src/be/nikiroo/fanfix/Instance.java @@ -441,6 +441,7 @@ public class Instance { + getFile(libDir), e)); } } else { + Exception ex = null; int pos = remoteLib.lastIndexOf(":"); if (pos >= 0) { String port = remoteLib.substring(pos + 1).trim(); @@ -459,13 +460,14 @@ public class Instance { lib); } catch (Exception e) { + ex = e; } } } if (lib == null) { tracer.error(new IOException( - "Cannot create remote library for: " + remoteLib)); + "Cannot create remote library for: " + remoteLib, ex)); } } diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index 995251d..f8c0ca2 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -7,6 +7,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.bundles.StringId; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; @@ -76,9 +77,8 @@ public class Main { *
  • --set-reader [reader type]: set the reader type to CLI, TUI or LOCAL * for this command
  • *
  • --version: get the version of the program
  • - *
  • --server [key] [port]: start a server on this port
  • - *
  • --stop-server [key] [port]: stop the running server on this port if - * any
  • + *
  • --server: start the server mode (see config file for parameters)
  • + *
  • --stop-server: stop the running server on this port if any
  • *
  • --remote [key] [host] [port]: use a the given remote library
  • * * @@ -326,14 +326,10 @@ public class Main { exitCode = 255; // no arguments for this option break; case SERVER: + exitCode = 255; // no arguments for this option + break; case STOP_SERVER: - if (key == null) { - key = args[i]; - } else if (port == null) { - port = Integer.parseInt(args[i]); - } else { - exitCode = 255; - } + exitCode = 255; // no arguments for this option break; case REMOTE: if (key == null) { @@ -571,8 +567,11 @@ public class Main { BasicReader.getReader().browse(null); break; case SERVER: + key = Instance.getConfig().getString(Config.SERVER_KEY); + port = Instance.getConfig().getInteger(Config.SERVER_PORT); if (port == null) { - exitCode = 255; + System.err.println("No port configured in the config file"); + exitCode = 15; break; } try { @@ -584,8 +583,11 @@ public class Main { } return; case STOP_SERVER: + key = Instance.getConfig().getString(Config.SERVER_KEY); + port = Instance.getConfig().getInteger(Config.SERVER_PORT); if (port == null) { - exitCode = 255; + System.err.println("No port configured in the config file"); + exitCode = 15; break; } diff --git a/src/be/nikiroo/fanfix/bundles/Config.java b/src/be/nikiroo/fanfix/bundles/Config.java index 74beeae..35f63d6 100644 --- a/src/be/nikiroo/fanfix/bundles/Config.java +++ b/src/be/nikiroo/fanfix/bundles/Config.java @@ -32,6 +32,16 @@ public enum Config { DEFAULT_COVERS_DIR, // @Meta(description = "string", info = "The default library to use (KEY:SERVER:PORT), or empty for the local library") DEFAULT_LIBRARY, // + @Meta(def = "58365", description = "The port on which we can start the server", format = Format.INT, info = "A valid port") + SERVER_PORT, // + @Meta(def = "", description = "The encryption key for the server (NOT including a subkey)", format = Format.PASSWORD, info = "cannot contain the pipe character (|)") + SERVER_KEY, // + @Meta(def = "TRUE", description = "Allow write access to the clients by default (download story, move story...)", format = Format.BOOLEAN) + SERVER_RW, // + @Meta(def = "", description = "If not empty, only the EXACT listed sources will be available for clients", info = "list is comma-separated (,) and values are surrounded by double quotes (\"); any double quote in the value must be backslash-escaped (with \\\")") + SERVER_WHITELIST, // + @Meta(def = "", description = "The subkeys that the server will allow, including the modes", info = "list is comma-separated (,) and values are surrounded by double quotes (\"); any double quote in the value must be backslash-escaped (with \\\")") + SERVER_ALLOWED_SUBKEYS, // @Meta(def = "$HOME/Books", description = "absolute path, $HOME variable supported, / is always accepted as dir separator", format = Format.DIRECTORY, info = "The directory where to store the library") LIBRARY_DIR, // @Meta(def = "false", description = "boolean", format = Format.BOOLEAN, info = "Show debug information on errors") @@ -65,8 +75,7 @@ public enum Config { @Meta(description = "If the last update check was done at least that many days, check for updates at startup (-1 for 'no checks' -- default is 1 day)", format = Format.INT) UPDATE_INTERVAL, // @Meta(def = "", description = "", info = "Format is ((user(:pass)@)proxy:port), with ':' being system proxy and an empty String being no proxy") - USE_PROXY, - @Meta(description = "An API key required to create a token from FimFiction", format = Format.STRING) + USE_PROXY, @Meta(description = "An API key required to create a token from FimFiction", format = Format.STRING) LOGIN_FIMFICTION_APIKEY_CLIENT_ID, // @Meta(description = "An API key required to create a token from FimFiction", format = Format.PASSWORD) LOGIN_FIMFICTION_APIKEY_CLIENT_SECRET, // diff --git a/src/be/nikiroo/fanfix/bundles/config.properties b/src/be/nikiroo/fanfix/bundles/config.properties index 6409539..63b7642 100644 --- a/src/be/nikiroo/fanfix/bundles/config.properties +++ b/src/be/nikiroo/fanfix/bundles/config.properties @@ -33,6 +33,21 @@ USER_AGENT = Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44. DEFAULT_COVERS_DIR = $HOME/.fanfix/covers/ # string (FORMAT: STRING) The default library to use (KEY:SERVER:PORT), or empty for the local library DEFAULT_LIBRARY = +# The port on which we can start the server +# (FORMAT: INT) A valid port +SERVER_PORT = 58365 +# The encryption key for the server (NOT including a subkey) +# (FORMAT: PASSWORD) cannot contain the pipe character (|) +SERVER_KEY = +# Allow write access to the clients by default (download story, move story...) +# (FORMAT: BOOLEAN) +SERVER_RW = +# If not empty, only the EXACT listed sources will be available for clients +# (FORMAT: STRING) list is comma-separated (,) and values are surrounded by double quotes ("); any double quote in the value must be backslash-escaped (with \") +SERVER_WHITELIST = +# The subkeys that the server will allow, including the modes +# (FORMAT: STRING) list is comma-separated (,) and values are surrounded by double quotes ("); any double quote in the value must be backslash-escaped (with \") +SERVER_ALLOWED_SUBKEYS = # absolute path, $HOME variable supported, / is always accepted as dir separator # (FORMAT: DIRECTORY) The directory where to store the library LIBRARY_DIR = $HOME/Books diff --git a/src/be/nikiroo/fanfix/bundles/resources_core.properties b/src/be/nikiroo/fanfix/bundles/resources_core.properties index 363a59b..1ebc3d5 100644 --- a/src/be/nikiroo/fanfix/bundles/resources_core.properties +++ b/src/be/nikiroo/fanfix/bundles/resources_core.properties @@ -34,8 +34,8 @@ HELP_SYNTAX = Valid options:\n\ \t--set-author [id] [new author]: change the author of the given story\n\ \t--set-reader [reader type]: set the reader type to CLI, TUI or GUI for \n\ \t\tthis command\n\ -\t--server [key] [port]: start a remote server on this port\n\ -\t--stop-server [key] [port]: stop the remote server running on this port\n\ +\t--server: start the server mode (see config file for parameters)\n\ +\t--stop-server: stop the remote server running on this port\n\ \t\tif any (key must be set to the same value)\n\ \t--remote [key] [host] [port]: select this remote server to get \n\ \t\t(or update or...) the stories from (key must be set to the \n\ diff --git a/src/be/nikiroo/fanfix/bundles/resources_core_fr.properties b/src/be/nikiroo/fanfix/bundles/resources_core_fr.properties index 1ce9f64..e64651b 100644 --- a/src/be/nikiroo/fanfix/bundles/resources_core_fr.properties +++ b/src/be/nikiroo/fanfix/bundles/resources_core_fr.properties @@ -28,8 +28,8 @@ HELP_SYNTAX = Options reconnues :\n\ \t--set-title [id] [nouveau titre]: change le titre de l'histoire\n\ \t--set-author [id] [nouvel auteur]: change l'auteur de l'histoire\n\ \t--set-reader [reader type]: changer le type de lecteur pour la commande en cours sur CLI, TUI ou GUI\n\ -\t--server [key] [port]: démarrer un serveur d'histoires sur ce port\n\ -\t--stop-server [key] [port]: arrêter le serveur distant sur ce port (key doit avoir la même valeur) \n\ +\t--server: démarre le mode serveur (les paramètres sont dans le fichier de config)\n\ +\t--stop-server: arrêter le serveur distant sur ce port (key doit avoir la même valeur) \n\ \t--remote [key] [host] [port]: contacter ce server au lieu de la librairie habituelle (key doit avoir la même valeur)\n\ \t--help: afficher la liste des options disponibles\n\ \t--version: retourne la version du programme\n\ diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index e35c57d..e7b9828 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -14,6 +14,7 @@ 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.Version; import be.nikiroo.utils.serial.server.ConnectActionClientObject; /** @@ -27,11 +28,47 @@ public class RemoteLibrary extends BasicLibrary { private String host; private int port; private final String key; + private final String subkey; + + // informative only (server will make the actual checks) + private boolean rw; // TODO: error handling is not up to par! /** * Create a {@link RemoteLibrary} linked to the given server. + *

    + * Note that the key is structured: + * xxx(|yyy|wl)(|rw) + *

    + * Note that anything before the first pipe (|) character is + * considered to be the encryption key, anything after that character is + * called the subkey (including the other pipe characters and flags!). + *

    + * This is important because the subkey (including the pipe characters and + * flags) must be present as-is in the server configuration file to be + * allowed. + *

    + * + * Some examples: + * * * @param key * the key that will allow us to exchange information with the @@ -42,7 +79,19 @@ public class RemoteLibrary extends BasicLibrary { * the port to contact it on */ public RemoteLibrary(String key, String host, int port) { - this.key = key; + int index = -1; + if (key != null) { + key.indexOf('|'); + } + + if (index >= 0) { + this.key = key.substring(index + 1); + this.subkey = key.substring(0, index); + } else { + this.key = key; + this.subkey = ""; + } + this.host = host; this.port = port; } @@ -78,10 +127,14 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { - Object rep = send(new Object[] { "PING" }); + public void action(Version serverVersion) throws Exception { + Object rep = send(new Object[] { subkey, "PING" }); - if ("PONG".equals(rep)) { + if ("r/w".equals(rep)) { + rw = true; + result[0] = Status.READY; + } else if ("r/o".equals(rep)) { + rw = false; result[0] = Status.READY; } else { result[0] = Status.UNAUTHORIZED; @@ -119,8 +172,8 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { - Object rep = send(new Object[] { "GET_COVER", luid }); + public void action(Version serverVersion) throws Exception { + Object rep = send(new Object[] { subkey, "GET_COVER", luid }); result[0] = (Image) rep; } @@ -170,9 +223,9 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { - Object rep = send(new Object[] { "GET_CUSTOM_COVER", type, - source }); + public void action(Version serverVersion) throws Exception { + Object rep = send(new Object[] { subkey, + "GET_CUSTOM_COVER", type, source }); result[0] = (Image) rep; } @@ -205,13 +258,13 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { + public void action(Version serverVersion) throws Exception { Progress pg = pgF; if (pg == null) { pg = new Progress(); } - Object rep = send(new Object[] { "GET_STORY", luid }); + Object rep = send(new Object[] { subkey, "GET_STORY", luid }); MetaData meta = null; if (rep instanceof MetaData) { @@ -270,13 +323,13 @@ public class RemoteLibrary extends BasicLibrary { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { + public void action(Version serverVersion) throws Exception { Progress pg = pgF; if (story.getMeta().getWords() <= Integer.MAX_VALUE) { pg.setMinMax(0, (int) story.getMeta().getWords()); } - send(new Object[] { "SAVE_STORY", luid }); + send(new Object[] { subkey, "SAVE_STORY", luid }); List list = RemoteLibraryServer.breakStory(story); for (Object obj : list) { @@ -324,8 +377,8 @@ public class RemoteLibrary extends BasicLibrary { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { - send(new Object[] { "DELETE_STORY", luid }); + public void action(Version serverVersion) throws Exception { + send(new Object[] { subkey, "DELETE_STORY", luid }); } @Override @@ -368,8 +421,8 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { - send(new Object[] { "SET_COVER", type, value, luid }); + public void action(Version serverVersion) throws Exception { + send(new Object[] { subkey, "SET_COVER", type, value, luid }); } @Override @@ -417,10 +470,11 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { + public void action(Version serverVersion) throws Exception { Progress pg = pgF; - Object rep = send(new Object[] { "IMPORT", url.toString() }); + Object rep = send(new Object[] { subkey, "IMPORT", + url.toString() }); while (true) { if (!RemoteLibraryServer.updateProgress(pg, rep)) { @@ -473,11 +527,11 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { + public void action(Version serverVersion) throws Exception { Progress pg = pgF; - Object rep = send(new Object[] { "CHANGE_STA", luid, - newSource, newTitle, newAuthor }); + Object rep = send(new Object[] { subkey, "CHANGE_STA", + luid, newSource, newTitle, newAuthor }); while (true) { if (!RemoteLibraryServer.updateProgress(pg, rep)) { break; @@ -519,8 +573,8 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { - send(new Object[] { "EXIT" }); + public void action(Version serverVersion) throws Exception { + send(new Object[] { subkey, "EXIT" }); } @Override @@ -611,13 +665,14 @@ public class RemoteLibrary extends BasicLibrary { try { new ConnectActionClientObject(host, port, key) { @Override - public void action() throws Exception { + public void action(Version serverVersion) throws Exception { Progress pg = pgF; if (pg == null) { pg = new Progress(); } - Object rep = send(new Object[] { "GET_METADATA", luid }); + Object rep = send(new Object[] { subkey, "GET_METADATA", + luid }); while (true) { if (!RemoteLibraryServer.updateProgress(pg, rep)) { diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index 62548d6..f4075dd 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -11,6 +11,7 @@ import java.util.Map; import javax.net.ssl.SSLException; import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; @@ -18,6 +19,7 @@ import be.nikiroo.fanfix.data.Story; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Progress.ProgressListener; import be.nikiroo.utils.StringUtils; +import be.nikiroo.utils.Version; import be.nikiroo.utils.serial.server.ConnectActionServerObject; import be.nikiroo.utils.serial.server.ServerObject; @@ -27,8 +29,12 @@ 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). *

    + * All the commands are always prefixed by the subkey (which can be EMPTY if + * none). + *

    *