From 86b49d57c7b09174b1f23b2f598407b99d7fc8b3 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sat, 12 Mar 2022 22:01:52 +0100 Subject: [PATCH 01/75] =?UTF-8?q?La=20modification=20du=20fichier=20JSON?= =?UTF-8?q?=20est=20d=C3=A9sormais=20synchronized.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonAdapter.java | 2 +- app/src/main/resources/appdata.json | 81 ++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index 026dc37..c54722c 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -13,7 +13,7 @@ public class JsonAdapter implements Adapter { * @return A Json String containing AppData properties */ @Override - public String toString(AppData appData) { + public synchronized String toString(AppData appData) { return addData(appData); } diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 8a76dfd..3de13f0 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1,80 @@ -{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"endmove","password":"1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b","aes_key":"p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=","passwordSalt":"ItYiXkwPa84Gwb6dGHQvXQ==","files":[]},{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"ca.crt","fileNameSalt":"Mjo7iQeEl2PYX1RDUZbSlQ==","size":4207,"iv":"uALI+Feo1lIg1lBxbCMwIQ==","storage":["lightcontainerSB01"]},{"name":"main.py","fileNameSalt":"YRwnBiXINCJ+zyxwADgNRQ==","size":854,"iv":"9LXrJFtcgU4DeUBghc4Dgw==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{ + "unicast_port": 8000, + "multicast_ip": "226.66.66.1", + "multicast_port": 15502, + "network_interface": "My network interface", + "tls": true, + "storagePath": "/home/benjamin/ffe", + "users": [ + { + "name": "endmove", + "password": "1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b", + "aes_key": "p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=", + "passwordSalt": "ItYiXkwPa84Gwb6dGHQvXQ==", + "files": [ + { + "name": "ca.crt", + "fileNameSalt": "aLLsH6DvoSUj7o8oGuBxhw==", + "size": 4207, + "iv": "MShYLgwYd8SHDK6FifpORg==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "main.py", + "fileNameSalt": "mtyoHXGKxT0DL+1EF+RhSw==", + "size": 854, + "iv": "tnJyZHlVvboRl8uD9HWDow==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "README.md", + "fileNameSalt": "ZpiL943DSaBgBbLXNsw1rw==", + "size": 17, + "iv": "1eDj0cCSHWGZzBG96ugR1A==", + "storage": [ + "lightcontainerSB01" + ] + } + ] + }, + { + "name": "aaaaa", + "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", + "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", + "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", + "files": [ + { + "name": "ca.crt", + "fileNameSalt": "woKUXAfV5cNodEH8O7h5kA==", + "size": 4207, + "iv": "Cy3rbfGdXuLf0aT+LS1wsA==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "main.py", + "fileNameSalt": "YRwnBiXINCJ+zyxwADgNRQ==", + "size": 854, + "iv": "9LXrJFtcgU4DeUBghc4Dgw==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "README.md", + "fileNameSalt": "/upxjjFW8GUayXngbfV3fA==", + "size": 17, + "iv": "QCkcjqYsUfWAE0klUTVTzQ==", + "storage": [ + "lightcontainerSB01" + ] + } + ] + } + ] +} \ No newline at end of file From 4e7031923c298696b666a82b44f0cc5a9e86b9e9 Mon Sep 17 00:00:00 2001 From: EndMove Date: Sat, 12 Mar 2022 22:16:02 +0100 Subject: [PATCH 02/75] =?UTF-8?q?Ajout=20de=20r=C3=A8gles=20(en=20progress?= =?UTF-8?q?ion),=20Ajout=20doc,=20Optimisation,=20bugs=20fix.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 5 +- .../domains/client/ClientHandler.java | 4 +- .../domains/client/Context.java | 16 ++--- .../domains/client/RequestBundle.java | 1 - .../protocol/ProtocolReader.java | 15 ++-- .../protocol/rules/reader/GetFileRule.java | 70 +++++++++++++++++++ .../rules/reader/RetrieveErrorRule.java | 26 +++++++ .../protocol/rules/reader/RetrieveOkRule.java | 35 ++++++++++ .../rules/writer/GetFileErrorRule.java | 18 +++++ .../protocol/rules/writer/GetFileOkRule.java | 17 +++++ .../rules/writer/RetrieveFileRule.java | 16 +++++ .../protocol/rules/writer/SendfileRule.java | 1 - .../java/lightcontainer/storage/AppData.java | 26 +++++-- .../java/lightcontainer/storage/File.java | 4 +- .../lightcontainer/storage/Repository.java | 19 ++++- .../java/lightcontainer/storage/User.java | 4 +- app/src/main/resources/appdata.json | 63 ++++++++++++++++- 17 files changed, 307 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 9ac6afc..ad7dd73 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -16,9 +16,6 @@ import lightcontainer.storage.AppData; import lightcontainer.storage.JsonAdapter; import lightcontainer.storage.Repository; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; public class App { @@ -39,6 +36,7 @@ public class App { protocolRep.addReader(new FilelistRule(protocolRep)); protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); protocolRep.addReader(new SendOkRule(protocolRep)); + protocolRep.addReader(new GetFileRule(protocolRep)); protocolRep.addWriter(new SignOkRule()); protocolRep.addWriter(new SignErrorRule()); @@ -46,6 +44,7 @@ public class App { protocolRep.addWriter(new SaveFileOkRule()); protocolRep.addWriter(new SaveFileErrorRule()); protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); + protocolRep.addWriter(new GetFileErrorRule()); FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 1c9922a..d10a544 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -132,8 +132,8 @@ public class ClientHandler implements Runnable, AutoCloseable { System.out.println(4); accessDenied(); } - } catch (IOException ignore) { - ignore.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); repository.disconnect(this); break; } diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index 31fee1a..0df8787 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -1,14 +1,10 @@ package lightcontainer.domains.client; -import lightcontainer.storage.AppData; import lightcontainer.storage.File; import lightcontainer.storage.Repository; -import lightcontainer.storage.User; import lightcontainer.utils.AES_GCM; import lightcontainer.utils.ShaHasher; -import java.security.NoSuchAlgorithmException; -import java.util.LinkedList; import java.util.List; import java.util.Set; @@ -17,9 +13,8 @@ import java.util.Set; * Permet de récupérer les données à celui-ci et d'effectuer des actions sur le context courant. */ public class Context { - - private Repository repository; - + // Variables + private final Repository repository; private RequestBundle requestBundle; /** @@ -57,8 +52,7 @@ public class Context { this.login = login; return true; } - } catch (AES_GCM.AesGcmException e) { - } + } catch (AES_GCM.AesGcmException ignored) { } return false; } @@ -172,6 +166,10 @@ public class Context { return this.repository.addFileFor(new File(fileName, fileNameSalt, size, iv, Set.of(domain)), getLogin()); } + public File getFileOf(String fileName, String userName) { + return this.repository.getFileOf(fileName, userName); + } + public List getStringifiedFilesOf() { return repository.getStringifiedFilesOf(login); } diff --git a/app/src/main/java/lightcontainer/domains/client/RequestBundle.java b/app/src/main/java/lightcontainer/domains/client/RequestBundle.java index 20098c0..8e32156 100644 --- a/app/src/main/java/lightcontainer/domains/client/RequestBundle.java +++ b/app/src/main/java/lightcontainer/domains/client/RequestBundle.java @@ -1,6 +1,5 @@ package lightcontainer.domains.client; - import java.util.HashMap; import java.util.Map; diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index ebace44..133a820 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -20,6 +20,7 @@ public abstract class ProtocolReader { this.rulePattern = Pattern.compile(pattern); } + // Receiver list public enum ResultCmdReceiver { CLIENT, STOREBACKEND @@ -39,7 +40,7 @@ public abstract class ProtocolReader { /** * Le context courant */ - private Context context; + private final Context context; public ProtocolResult(Context context) { this.context = context; @@ -52,6 +53,11 @@ public abstract class ProtocolReader { */ private ResultCmdReceiver receiver; + /** + * Récupérer le destinataire + * + * @return Receiver + */ public ResultCmdReceiver getReceiver() { return receiver; } @@ -123,18 +129,17 @@ public abstract class ProtocolReader { private String extractName(String data) { String name; int endIndex = data.indexOf(' '); - if (endIndex > 0) { - name = data.substring(0, endIndex); - } else { + if (endIndex <= 0) { endIndex = data.indexOf('\r'); - name = data.substring(0, endIndex); } + name = data.substring(0, endIndex); return name; } /** * Cette méthode est appelée lors de l'exécution de la règle * + * @param context Utilisateur context. * @param data Paramètres pour créer la commande. */ protected abstract T onExecuted(Context context, String... data); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java new file mode 100644 index 0000000..6015a90 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -0,0 +1,70 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.writer.GetFileErrorRule; +import lightcontainer.storage.File; + +public class GetFileRule extends ProtocolReader { + // Constants + private static final String PATTERN = "^GETFILE ([^ !]{1,20})\r\n$"; + private static final String NAME = "GETFILE"; + // -- arguments + private static final int FILE_NAME = 0; // Index file name. + + // Variables + private final ProtocolRepository protocolRep; + + // Constructor + public GetFileRule(ProtocolRepository protocolRep) { + super(NAME, PATTERN); + this.protocolRep = protocolRep; + } + + /** + * Rule Result + */ + public class Result extends ProtocolResult { + // Variables + private final String fileName; + + // Constructor + public Result(Context context, String fileName) { + super(context); + this.fileName = fileName; + } + + } + + /** + * Cette méthode est appelée lors de l'exécution de la règle + * + * @param context Utilisateur context. + * @param data Paramètres pour créer la commande. + */ + @Override + protected GetFileRule.Result onExecuted(Context context, String... data) { + GetFileRule.Result result = new GetFileRule.Result(context, data[FILE_NAME]); + + File file = context.getFileOf(data[FILE_NAME], context.getLogin()); + + if (true) { + + } else { + + } + + return result; + } + + /** + * Cette méthode est appelée lors d'une erreur de la règle + */ + protected ProtocolReader.ProtocolResult onError(Context context) { + ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); + // Make getFileErrorRule + result.setResultCommand(protocolRep.executeWriter(context, GetFileErrorRule.NAME), ResultCmdReceiver.CLIENT); + return result; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java new file mode 100644 index 0000000..6483db2 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java @@ -0,0 +1,26 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.protocol.ProtocolReader; + +public class RetrieveErrorRule extends ProtocolReader { + // Constants + private static final String PATTERN = "^RETRIEVE_ERROR\r\n$"; + private static final String NAME = "RETRIEVE_ERROR"; + + // Constructor + protected RetrieveErrorRule() { + super(NAME, PATTERN); + } + + /** + * Cette méthode est appelée lors de l'exécution de la règle + * + * @param context Utilisateur context. + * @param data Paramètres pour créer la commande. + */ + @Override + protected T onExecuted(Context context, String... data) { + return null; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java new file mode 100644 index 0000000..67a3149 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -0,0 +1,35 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.protocol.ProtocolReader; + +public class RetrieveOkRule extends ProtocolReader { + // Constants + private static final String PATTERN = "^RETRIEVE_OK ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n$"; + private static final String NAME = "RETRIEVE_OK"; + // -- arguments + private static final int HASHED_FILE_NAME = 0; // Index hashed filename + private static final int FILE_SIZE = 1; // Index file size + private static final int HASHED_FILE_CONTENT = 2; // Index hashed file content + + // Variables + protected RetrieveOkRule() { + super(NAME, PATTERN); + } + + /** + * Cette méthode est appelée lors de l'exécution de la règle + * + * @param context Utilisateur context. + * @param data Paramètres pour créer la commande. + */ + @Override + protected T onExecuted(Context context, String... data) { + return null; + } + + @Override + protected T onError(Context context) { + return super.onError(context); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java new file mode 100644 index 0000000..4a963d6 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java @@ -0,0 +1,18 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +/** + * Règle utilisé dans le cas ou une erreur survient lors + * de la demande de récupération d'un fichier. + */ +public class GetFileErrorRule extends ProtocolWriter { + // Constants + private static final String PATTERN = "^GETFILE_ERROR\r\n$"; + public static final String NAME = "GETFILE_ERROR"; + + // Constructors + public GetFileErrorRule() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java new file mode 100644 index 0000000..77bc6f5 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -0,0 +1,17 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +public class GetFileOkRule extends ProtocolWriter { + // Constants + private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n)$"; + public static final String NAME = "GETFILE_OK"; + // -- params + private static final int FILE_NAME = 0; // Index file name hashed + private static final int FILE_SIZE = 1; // Index file size + + // Constructors + protected GetFileOkRule() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java new file mode 100644 index 0000000..d99241c --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java @@ -0,0 +1,16 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +public class RetrieveFileRule extends ProtocolWriter { + // Constants + private static final String PATTERN = "^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$"; + public static final String NAME = "RETRIEVEFILE"; + // -- params + private static final int HASHED_FILE_NAME = 0; // Index hashed filename + + // Constructor + protected RetrieveFileRule() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index 850c600..3bb8ef6 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -52,7 +52,6 @@ public class SendfileRule extends ProtocolWriter { } } - @Override protected SendfileRule.Result onExecuted(Context context, String... data) { return new SendfileRule.Result(context, data[HASHED_FILE_NAME], Integer.parseInt(data[FILE_SIZE]), data[HASHED_FILE_CONTENT]); diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java index d0d188b..153ea0e 100644 --- a/app/src/main/java/lightcontainer/storage/AppData.java +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -16,12 +16,11 @@ import java.util.Map; * @since 1.0 */ public class AppData { - + // Variable private static AppData instance = null; private AppConfig appConfig; private final Map users; - /** * Constructs a new instance of AppData. * Sets appConfig to null and creates a new Hashmap of users. @@ -76,7 +75,6 @@ public class AppData { return null; } - /** * Use this method when a user signs up. * @@ -138,6 +136,26 @@ public class AppData { } } + /** + * Getter, allow to retrieve a specified file object. + * + * @param fileName The name of the file to retrieve. + * @param userName The name of the user who wants to retrieve the file. + * @return The requested file matching file name and username, or null + * if the user or file do not exist. + * + * @see User#getFile(String) + * @see File + * + * @author Unknown... + */ + public File getFileOf(String fileName, String userName) { + if (this.users.containsKey(userName)) { + return this.users.get(userName).getFile(fileName); + } + return null; + } + /** * Call this method after receiving REMOVEFILE_OK from the StorBackEnd. * Do NOT call when receiving REMOVEFILE_ERROR, or it will break the system's synchronization. @@ -181,7 +199,6 @@ public class AppData { return user == null ? null : user.getPasswordSalt(); } - /** * Méthode permettant de récupérer la clé AES d'un utilisateur * @@ -192,5 +209,4 @@ public class AppData { User user = getUser(login); return user == null ? null : user.getAesKey(); } - } diff --git a/app/src/main/java/lightcontainer/storage/File.java b/app/src/main/java/lightcontainer/storage/File.java index 9429cf9..c610a07 100644 --- a/app/src/main/java/lightcontainer/storage/File.java +++ b/app/src/main/java/lightcontainer/storage/File.java @@ -7,13 +7,14 @@ import java.util.Set; * File represents all information related to a file */ public class File { - + // Variables private final String name; private final String fileNameSalt; private final int size; private final String iv; private final Set storage; + // Constructor public File(String name, String fileNameSalt, int size, String iv, Set storage) { this.name = name; this.fileNameSalt = fileNameSalt; @@ -50,6 +51,5 @@ public class File { return true; } } - } diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 81720c5..43dd38f 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -10,7 +10,7 @@ import java.nio.file.StandardOpenOption; import java.util.List; public class Repository { - + // Variables private final String filePath; private final Adapter adapter; private final AppData appData; @@ -76,7 +76,6 @@ public class Repository { return false; } - /** * Loads configuration file * @@ -139,6 +138,22 @@ public class Repository { return appData.getUserAesKey(login); } + /** + * Getter, allow to retrieve a specified file object. + * + * @param fileName The name of the file to retrieve. + * @param userName The name of the user who wants to retrieve the file. + * @return The requested file matching file name and username, or null + * if the user or file do not exist. + * + * @see AppData#getFileOf(String, String) + * @see File + * + * @author Unknown... + */ + public File getFileOf(String fileName, String userName) { + return this.appData.getFileOf(fileName, userName); + } public List getStringifiedFilesOf(String login) { return this.appData.getStringifiedFilesOf(login); diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java index 13ccbb2..80d517b 100644 --- a/app/src/main/java/lightcontainer/storage/User.java +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -13,13 +13,14 @@ import java.util.Map; * @since 1.0 */ public class User { - + // Variables private final String name; private final String password; private final String aesKey; private final String passwordSalt; private final Map files; + // Constructor public User(String name, String password, String aesKey, String passwordSalt, Map files) { this.name = name; this.password = password; @@ -109,5 +110,4 @@ public class User { public boolean verifyPassword(String password) { return this.password.equals(password); } - } diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 8a76dfd..48d6ee8 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1,62 @@ -{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"endmove","password":"1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b","aes_key":"p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=","passwordSalt":"ItYiXkwPa84Gwb6dGHQvXQ==","files":[]},{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"ca.crt","fileNameSalt":"Mjo7iQeEl2PYX1RDUZbSlQ==","size":4207,"iv":"uALI+Feo1lIg1lBxbCMwIQ==","storage":["lightcontainerSB01"]},{"name":"main.py","fileNameSalt":"YRwnBiXINCJ+zyxwADgNRQ==","size":854,"iv":"9LXrJFtcgU4DeUBghc4Dgw==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{ + "unicast_port": 8000, + "multicast_ip": "226.66.66.1", + "multicast_port": 15502, + "network_interface": "My network interface", + "tls": true, + "storagePath": "D:\\ffe", + "users": [ + { + "name": "endmove", + "password": "1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b", + "aes_key": "p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=", + "passwordSalt": "ItYiXkwPa84Gwb6dGHQvXQ==", + "files": [ + { + "name": "HELMo.png", + "fileNameSalt": "tadvzRunAGiu0Z059eBPLQ==", + "size": 130146, + "iv": "26s0BqNqVSbzN1lASkUzgQ==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "apache2.conf", + "fileNameSalt": "C5GNkws8l2HrhIdF7gmG6w==", + "size": 12648, + "iv": "hQx69qQj1ynaxZZ9wAxpbA==", + "storage": [ + "lightcontainerSB01" + ] + } + ] + }, + { + "name": "aaaaa", + "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", + "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", + "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", + "files": [ + { + "name": "ca.crt", + "fileNameSalt": "Mjo7iQeEl2PYX1RDUZbSlQ==", + "size": 4207, + "iv": "uALI+Feo1lIg1lBxbCMwIQ==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "main.py", + "fileNameSalt": "YRwnBiXINCJ+zyxwADgNRQ==", + "size": 854, + "iv": "9LXrJFtcgU4DeUBghc4Dgw==", + "storage": [ + "lightcontainerSB01" + ] + } + ] + } + ] +} \ No newline at end of file From a5e6b0b3e0dcb844eb1a540322a5fa879ceb4557 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sun, 13 Mar 2022 14:56:42 +0100 Subject: [PATCH 03/75] =?UTF-8?q?-=20Context=20:=20ajout=20de=20deleteFile?= =?UTF-8?q?=20pour=20pouvoir=20supprimer=20un=20fichier=20du=20FFE=20=20?= =?UTF-8?q?=20=20quand=20le=20SBE=20a=20confirm=C3=A9=20sa=20suppression.?= =?UTF-8?q?=20-=20EraseErrorRule=20:=20lecture=20de=20l'erreur=20envoy?= =?UTF-8?q?=C3=A9e=20par=20le=20SBE=20lors=20de=20la=20=20=20=20=20=20=20?= =?UTF-8?q?=20suppression=20d'un=20fichier.=20-=20EraseFileRule=20:=20comm?= =?UTF-8?q?ande=20cr=C3=A9=C3=A9e=20par=20le=20FFE=20pour=20que=20le=20SBE?= =?UTF-8?q?=20puisse=20savoir=20=20=20quel=20fichier=20il=20doit=20supprim?= =?UTF-8?q?er.=20-=20EraseOkRule=20:=20lecture=20de=20la=20commande=20envo?= =?UTF-8?q?y=C3=A9e=20par=20le=20SBE=20lorsque=20la=20suppression=20d'un?= =?UTF-8?q?=20fichier=20s'est=20bien=20pass=C3=A9e.=20-=20File=20:=20impl?= =?UTF-8?q?=C3=A9mente=20d=C3=A9sormais=20une=20interface=20ReadOnlyFile.?= =?UTF-8?q?=20-=20GetFileRule=20:=20utilise=20ReadOnlyFile=20plut=C3=B4t?= =?UTF-8?q?=20que=20File.=20-=20ProtocolReader=20:=20reformatage.=20-=20Re?= =?UTF-8?q?adOnlyFile=20:=20interface=20pour=20r=C3=A9cup=C3=A9rer=20les?= =?UTF-8?q?=20donn=C3=A9es=20de=20File.=20-=20RemoveFileErrorRule=20:=20co?= =?UTF-8?q?mmande=20envoy=C3=A9e=20au=20client=20lorsque=20la=20suppressio?= =?UTF-8?q?n=20=20=20d'un=20fichier=20a=20=C3=A9chou=C3=A9.=20-=20RemoveFi?= =?UTF-8?q?leOkRule=20:=20commande=20envoy=C3=A9e=20au=20client=20lorsque?= =?UTF-8?q?=20la=20suppression=20d'un=20fichier=20a=20r=C3=A9ussi.=20-=20R?= =?UTF-8?q?emoveFileRule=20:=20lecture=20de=20la=20commande=20envoy=C3=A9e?= =?UTF-8?q?=20par=20le=20client=20lorsqu'il=20=20=20veut=20supprimer=20un?= =?UTF-8?q?=20fichier.=20-=20Repository=20:=20utilise=20ReadOnlyFile=20plu?= =?UTF-8?q?t=C3=B4t=20que=20File.=20-=20SendErrorRule=20et=20SendOkRule=20?= =?UTF-8?q?:=20modification=20des=20imports=20et=20attribut=20final.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/client/Context.java | 20 ++++++- .../protocol/ProtocolReader.java | 3 +- .../protocol/rules/reader/EraseErrorRule.java | 28 +++++++++ .../protocol/rules/reader/EraseFileRule.java | 15 +++++ .../protocol/rules/reader/EraseOkRule.java | 31 ++++++++++ .../protocol/rules/reader/GetFileRule.java | 3 +- .../protocol/rules/reader/RemoveFileRule.java | 59 +++++++++++++++++++ .../protocol/rules/reader/SendErrorRule.java | 1 - .../protocol/rules/reader/SendOkRule.java | 4 +- .../rules/writer/RemoveFileErrorRule.java | 14 +++++ .../rules/writer/RemoveFileOkRule.java | 13 ++++ .../java/lightcontainer/storage/File.java | 7 ++- .../lightcontainer/storage/ReadOnlyFile.java | 16 +++++ .../lightcontainer/storage/Repository.java | 2 +- 14 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java create mode 100644 app/src/main/java/lightcontainer/storage/ReadOnlyFile.java diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index 0df8787..c002c9f 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -1,6 +1,7 @@ package lightcontainer.domains.client; import lightcontainer.storage.File; +import lightcontainer.storage.ReadOnlyFile; import lightcontainer.storage.Repository; import lightcontainer.utils.AES_GCM; import lightcontainer.utils.ShaHasher; @@ -52,7 +53,8 @@ public class Context { this.login = login; return true; } - } catch (AES_GCM.AesGcmException ignored) { } + } catch (AES_GCM.AesGcmException ignored) { + } return false; } @@ -166,7 +168,17 @@ public class Context { return this.repository.addFileFor(new File(fileName, fileNameSalt, size, iv, Set.of(domain)), getLogin()); } - public File getFileOf(String fileName, String userName) { + public String getHashedFileName(String fileName) { + ReadOnlyFile file = repository.getFileOf(fileName, login); + if (file != null) { + String completeFileName = login + "_" + fileName; + ShaHasher hasher = new ShaHasher(completeFileName); + return hasher.fromSalt(hasher.saltToByte(file.getFileNameSalt())); + } + return null; + } + + public ReadOnlyFile getFileOf(String fileName, String userName) { return this.repository.getFileOf(fileName, userName); } @@ -185,4 +197,8 @@ public class Context { public void setDomain(String domain) { this.domain = domain; } + + public void deleteFile(String deletedFileName) { + repository.deleteFileOf(deletedFileName, login); + } } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index 133a820..0990f1a 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -93,6 +93,7 @@ public abstract class ProtocolReader { /** * Permet de récupérer le context courant + * * @return Context courant */ protected Context getContext() { @@ -140,7 +141,7 @@ public abstract class ProtocolReader { * Cette méthode est appelée lors de l'exécution de la règle * * @param context Utilisateur context. - * @param data Paramètres pour créer la commande. + * @param data Paramètres pour créer la commande. */ protected abstract T onExecuted(Context context, String... data); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java new file mode 100644 index 0000000..6cf945a --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java @@ -0,0 +1,28 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; + +public class EraseErrorRule extends ProtocolReader { + + private static final String PATTERN = "^ERASE_ERROR\r\n$"; + + private static final String NAME = "ERASE_ERROR"; + + private final ProtocolRepository protocolRep; + + + protected EraseErrorRule(ProtocolRepository protocolRep) { + super(NAME, PATTERN); + this.protocolRep = protocolRep; + } + + @Override + protected ProtocolResult onExecuted(Context context, String... data) { + ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); + result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); + return result; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java new file mode 100644 index 0000000..1573cf7 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java @@ -0,0 +1,15 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.protocol.ProtocolWriter; + +public class EraseFileRule extends ProtocolWriter { + + private static final String PATTERN = "^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$"; + + public static String NAME = "ERASEFILE"; + + + protected EraseFileRule() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java new file mode 100644 index 0000000..9ff6b44 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java @@ -0,0 +1,31 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.writer.RemoveFileOkRule; + +public class EraseOkRule extends ProtocolReader { + + private static final String PATTERN = "^ERASE_OK\r\n$"; + + private static final String NAME = "ERASE_OK"; + + private final ProtocolRepository protocolRep; + + protected EraseOkRule(ProtocolRepository protocolRep) { + super(NAME, PATTERN); + this.protocolRep = protocolRep; + } + + @Override + protected ProtocolReader.ProtocolResult onExecuted(Context context, String... data) { + ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); + String deletedFileName = context.getDataString("fileName"); + context.deleteFile(deletedFileName); + result.setResultCommand(protocolRep.executeWriter(context, RemoveFileOkRule.NAME), ResultCmdReceiver.CLIENT); + return result; + } + + +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java index 6015a90..35323ca 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -5,6 +5,7 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.GetFileErrorRule; import lightcontainer.storage.File; +import lightcontainer.storage.ReadOnlyFile; public class GetFileRule extends ProtocolReader { // Constants @@ -47,7 +48,7 @@ public class GetFileRule extends ProtocolReader { protected GetFileRule.Result onExecuted(Context context, String... data) { GetFileRule.Result result = new GetFileRule.Result(context, data[FILE_NAME]); - File file = context.getFileOf(data[FILE_NAME], context.getLogin()); + ReadOnlyFile file = context.getFileOf(data[FILE_NAME], context.getLogin()); if (true) { diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java new file mode 100644 index 0000000..4b1bdaa --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java @@ -0,0 +1,59 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; + +public class RemoveFileRule extends ProtocolReader { + + private static final String PATTERN = "^REMOVEFILE ([^ !]{1,20})\r\n$"; + + private static final String NAME = "REMOVEFILE"; + + private static final int FILENAME = 0; + + + private final ProtocolRepository protocolRep; + + protected RemoveFileRule(ProtocolRepository protocolRep) { + super(NAME, PATTERN); + this.protocolRep = protocolRep; + } + + public class Result extends ProtocolResult { + private final String fileName; + + public Result(Context context, String fileName) { + super(context); + this.fileName = fileName; + } + + public String getFileName() { + return fileName; + } + } + + + @Override + protected RemoveFileRule.Result onExecuted(Context context, String... data) { + RemoveFileRule.Result result = new RemoveFileRule.Result(context, data[FILENAME]); + String extendedFileName = context.getHashedFileName(result.getFileName()); + + if (extendedFileName != null) { + context.putDataString("fileName", result.getFileName()); + result.setResultCommand(protocolRep.executeWriter(context, EraseFileRule.NAME, extendedFileName), ResultCmdReceiver.STOREBACKEND); + } else { + result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); + } + + return result; + } + + @Override + protected ProtocolReader.ProtocolResult onError(Context context) { + ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); + result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); + return result; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java index e4a589b..83653fc 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -4,7 +4,6 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; -import lightcontainer.protocol.rules.writer.SaveFileOkRule; /** * Règle permettant de de confirmer la sauvegrade d'un fichier. diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index d4c2b61..c0a9275 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -15,7 +15,7 @@ public class SendOkRule extends ProtocolReader { private static final String NAME = "SEND_OK"; - private ProtocolRepository protocolRep; + private final ProtocolRepository protocolRep; // Constructor public SendOkRule(ProtocolRepository protocolRep) { @@ -25,7 +25,7 @@ public class SendOkRule extends ProtocolReader { @Override - protected ProtocolReader.ProtocolResult onExecuted(Context context, String... data) { + protected ProtocolResult onExecuted(Context context, String... data) { ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT); diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java new file mode 100644 index 0000000..4f91bfc --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java @@ -0,0 +1,14 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +public class RemoveFileErrorRule extends ProtocolWriter { + + private static final String PATTERN = "^REMOVEFILE_ERROR\r\n$"; + + public static String NAME = "REMOVEFILE_ERROR"; + + protected RemoveFileErrorRule() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java new file mode 100644 index 0000000..964f5e6 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java @@ -0,0 +1,13 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +public class RemoveFileOkRule extends ProtocolWriter { + + private static final String PATTERN = "^REMOVEFILE_OK\r\n$"; + public static String NAME = "REMOVEFILE_OK"; + + protected RemoveFileOkRule() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/storage/File.java b/app/src/main/java/lightcontainer/storage/File.java index c610a07..4c3ab3d 100644 --- a/app/src/main/java/lightcontainer/storage/File.java +++ b/app/src/main/java/lightcontainer/storage/File.java @@ -6,7 +6,7 @@ import java.util.Set; /** * File represents all information related to a file */ -public class File { +public class File implements ReadOnlyFile { // Variables private final String name; private final String fileNameSalt; @@ -23,22 +23,27 @@ public class File { this.storage = storage; } + @Override public String getName() { return name; } + @Override public String getFileNameSalt() { return fileNameSalt; } + @Override public int getSize() { return size; } + @Override public String getIv() { return iv; } + @Override public Iterator getStorageIterator() { return storage.iterator(); } diff --git a/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java b/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java new file mode 100644 index 0000000..ae7bf29 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java @@ -0,0 +1,16 @@ +package lightcontainer.storage; + +import java.util.Iterator; + +public interface ReadOnlyFile { + + String getName(); + + String getFileNameSalt(); + + int getSize(); + + String getIv(); + + Iterator getStorageIterator(); +} diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 43dd38f..3561d73 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -151,7 +151,7 @@ public class Repository { * * @author Unknown... */ - public File getFileOf(String fileName, String userName) { + public ReadOnlyFile getFileOf(String fileName, String userName) { return this.appData.getFileOf(fileName, userName); } From 166cf372862deea4758ff0aec9551c59b040b190 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sun, 13 Mar 2022 15:02:40 +0100 Subject: [PATCH 04/75] =?UTF-8?q?Ajout=20des=20nouveaux=20readers=20et=20w?= =?UTF-8?q?riters=20=C3=A0=20protocolRepository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 6 ++++++ .../protocol/rules/reader/EraseErrorRule.java | 2 +- .../lightcontainer/protocol/rules/reader/EraseOkRule.java | 2 +- .../protocol/rules/reader/RemoveFileRule.java | 3 ++- .../protocol/rules/{reader => writer}/EraseFileRule.java | 4 ++-- .../protocol/rules/writer/RemoveFileErrorRule.java | 2 +- .../protocol/rules/writer/RemoveFileOkRule.java | 2 +- 7 files changed, 14 insertions(+), 7 deletions(-) rename app/src/main/java/lightcontainer/protocol/rules/{reader => writer}/EraseFileRule.java (77%) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index ad7dd73..9c28c9f 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -37,6 +37,9 @@ public class App { protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); protocolRep.addReader(new SendOkRule(protocolRep)); protocolRep.addReader(new GetFileRule(protocolRep)); + protocolRep.addReader(new EraseErrorRule(protocolRep)); + protocolRep.addReader(new EraseOkRule(protocolRep)); + protocolRep.addReader(new RemoveFileRule(protocolRep)); protocolRep.addWriter(new SignOkRule()); protocolRep.addWriter(new SignErrorRule()); @@ -45,6 +48,9 @@ public class App { protocolRep.addWriter(new SaveFileErrorRule()); protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); protocolRep.addWriter(new GetFileErrorRule()); + protocolRep.addWriter(new EraseFileRule()); + protocolRep.addWriter(new RemoveFileErrorRule()); + protocolRep.addWriter(new RemoveFileOkRule()); FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java index 6cf945a..1ce0b0b 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java @@ -14,7 +14,7 @@ public class EraseErrorRule extends ProtocolReader { private final ProtocolRepository protocolRep; - protected EraseErrorRule(ProtocolRepository protocolRep) { + public EraseErrorRule(ProtocolRepository protocolRep) { super(NAME, PATTERN); this.protocolRep = protocolRep; } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java index 9ff6b44..9125c7a 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java @@ -13,7 +13,7 @@ public class EraseOkRule extends ProtocolReader { private final ProtocolRepository protocolRep; - protected EraseOkRule(ProtocolRepository protocolRep) { + public EraseOkRule(ProtocolRepository protocolRep) { super(NAME, PATTERN); this.protocolRep = protocolRep; } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java index 4b1bdaa..412c8da 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java @@ -3,6 +3,7 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.writer.EraseFileRule; import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; public class RemoveFileRule extends ProtocolReader { @@ -16,7 +17,7 @@ public class RemoveFileRule extends ProtocolReader { private final ProtocolRepository protocolRep; - protected RemoveFileRule(ProtocolRepository protocolRep) { + public RemoveFileRule(ProtocolRepository protocolRep) { super(NAME, PATTERN); this.protocolRep = protocolRep; } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java similarity index 77% rename from app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java rename to app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java index 1573cf7..4fcba04 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java @@ -1,4 +1,4 @@ -package lightcontainer.protocol.rules.reader; +package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; @@ -9,7 +9,7 @@ public class EraseFileRule extends ProtocolWriter { public static String NAME = "ERASEFILE"; - protected EraseFileRule() { + public EraseFileRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java index 4f91bfc..5e7517f 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java @@ -8,7 +8,7 @@ public class RemoveFileErrorRule extends ProtocolWriter { public static String NAME = "REMOVEFILE_ERROR"; - protected RemoveFileErrorRule() { + public RemoveFileErrorRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java index 964f5e6..a4ae784 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java @@ -7,7 +7,7 @@ public class RemoveFileOkRule extends ProtocolWriter { private static final String PATTERN = "^REMOVEFILE_OK\r\n$"; public static String NAME = "REMOVEFILE_OK"; - protected RemoveFileOkRule() { + public RemoveFileOkRule() { super(NAME, PATTERN); } } From 8c2ad17bd876ee5fca49fd166f9432c939418876 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sun, 13 Mar 2022 15:50:40 +0100 Subject: [PATCH 05/75] Ajout TODO suppression fichier temporaire dans le FFE --- .../protocol/rules/reader/SendErrorRule.java | 1 + .../protocol/rules/reader/SendOkRule.java | 1 + .../protocol/rules/writer/SendfileRule.java | 1 - app/src/main/resources/appdata.json | 49 +------------------ 4 files changed, 4 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java index 83653fc..dbcbe28 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -29,6 +29,7 @@ public class SendErrorRule extends ProtocolReader { protected ProtocolResult onExecuted(Context context, String... data) { ProtocolResult result = new ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); + // TODO : Supprimer le fichier du FFE return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index c0a9275..04522e4 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -33,6 +33,7 @@ public class SendOkRule extends ProtocolReader { System.out.println("Save en json du fichier"); context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); + // TODO : Supprimer le fichier du FFE return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index 3bb8ef6..e6971cc 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -48,7 +48,6 @@ public class SendfileRule extends ProtocolWriter { FileSender fileSender = new FileSender(storagePath); fileSender.sendFile(hashedFileName, writer); - // TODO : Supprimer le fichier du FFE } } diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 48d6ee8..f2068c3 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -4,59 +4,14 @@ "multicast_port": 15502, "network_interface": "My network interface", "tls": true, - "storagePath": "D:\\ffe", + "storagePath": "C:\\Users\\ledou\\Documents\\ffe", "users": [ - { - "name": "endmove", - "password": "1f82407cace28cd7d97d23f82e8235d9da97575d033a12334fc71d3517f6a90fa04af829df3c722c38d3b68842e4ca2b", - "aes_key": "p0G+iViPp656O6aMKgcXSDT/e9/00wQH/ztUWf4tyr4=", - "passwordSalt": "ItYiXkwPa84Gwb6dGHQvXQ==", - "files": [ - { - "name": "HELMo.png", - "fileNameSalt": "tadvzRunAGiu0Z059eBPLQ==", - "size": 130146, - "iv": "26s0BqNqVSbzN1lASkUzgQ==", - "storage": [ - "lightcontainerSB01" - ] - }, - { - "name": "apache2.conf", - "fileNameSalt": "C5GNkws8l2HrhIdF7gmG6w==", - "size": 12648, - "iv": "hQx69qQj1ynaxZZ9wAxpbA==", - "storage": [ - "lightcontainerSB01" - ] - } - ] - }, { "name": "aaaaa", "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", - "files": [ - { - "name": "ca.crt", - "fileNameSalt": "Mjo7iQeEl2PYX1RDUZbSlQ==", - "size": 4207, - "iv": "uALI+Feo1lIg1lBxbCMwIQ==", - "storage": [ - "lightcontainerSB01" - ] - }, - { - "name": "main.py", - "fileNameSalt": "YRwnBiXINCJ+zyxwADgNRQ==", - "size": 854, - "iv": "9LXrJFtcgU4DeUBghc4Dgw==", - "storage": [ - "lightcontainerSB01" - ] - } - ] + "files": [] } ] } \ No newline at end of file From 0e3cd23d48edc89a1bf0aa40b1baec526b0a1d8d Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 17:05:08 +0100 Subject: [PATCH 06/75] Suppression du fichier temporaire du FFE lorsque le SBE nous renvoie le status (OK ou ERROR) --- app/src/main/java/lightcontainer/App.java | 64 ++++++++++++------- .../rules/reader/RetrieveErrorRule.java | 2 +- .../protocol/rules/reader/RetrieveOkRule.java | 2 +- .../protocol/rules/reader/SendErrorRule.java | 16 ++++- .../protocol/rules/reader/SendOkRule.java | 16 ++++- .../protocol/rules/writer/GetFileOkRule.java | 4 +- .../rules/writer/RetrieveFileRule.java | 2 +- .../main/java/lightcontainer/utils/SHA.java | 2 +- app/src/main/resources/appdata.json | 18 +----- 9 files changed, 76 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 9c28c9f..97607d2 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -28,30 +28,12 @@ public class App { // Create all repository ClientHandlerRepository clientRep = new ClientHandlerRepository(); StoreProcessorRepository storeRep = new StoreProcessorRepository(); + + // Initialisation des protocoles ProtocolRepository protocolRep = new ProtocolRepositoryImpl(); - protocolRep.addReader(new HelloRule()); - protocolRep.addReader(new SigninRule(protocolRep)); - protocolRep.addReader(new SignupRule(protocolRep)); - protocolRep.addReader(new SignoutRule()); - protocolRep.addReader(new FilelistRule(protocolRep)); - protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); - protocolRep.addReader(new SendOkRule(protocolRep)); - protocolRep.addReader(new GetFileRule(protocolRep)); - protocolRep.addReader(new EraseErrorRule(protocolRep)); - protocolRep.addReader(new EraseOkRule(protocolRep)); - protocolRep.addReader(new RemoveFileRule(protocolRep)); - - protocolRep.addWriter(new SignOkRule()); - protocolRep.addWriter(new SignErrorRule()); - protocolRep.addWriter(new FilesRule()); - protocolRep.addWriter(new SaveFileOkRule()); - protocolRep.addWriter(new SaveFileErrorRule()); - protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); - protocolRep.addWriter(new GetFileErrorRule()); - protocolRep.addWriter(new EraseFileRule()); - protocolRep.addWriter(new RemoveFileErrorRule()); - protocolRep.addWriter(new RemoveFileOkRule()); + initProtocols(repositoryStorage, protocolRep); + // Initialisation du dispatcher et des servers FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort()); @@ -64,6 +46,44 @@ public class App { // storeRep.close(); } + private static void initProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) { + initReadersProtocols(repositoryStorage, protocolRep); + + initWritersProtocols(repositoryStorage, protocolRep); + } + + private static void initReadersProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) { + protocolRep.addReader(new HelloRule()); + protocolRep.addReader(new SigninRule(protocolRep)); + protocolRep.addReader(new SignupRule(protocolRep)); + protocolRep.addReader(new SignoutRule()); + protocolRep.addReader(new FilelistRule(protocolRep)); + protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new SendOkRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new SendErrorRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new GetFileRule(protocolRep)); + protocolRep.addReader(new EraseErrorRule(protocolRep)); + protocolRep.addReader(new EraseOkRule(protocolRep)); + protocolRep.addReader(new RemoveFileRule(protocolRep)); + protocolRep.addReader(new RetrieveErrorRule()); + protocolRep.addReader(new RetrieveOkRule()); + } + + private static void initWritersProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) { + protocolRep.addWriter(new SignOkRule()); + protocolRep.addWriter(new SignErrorRule()); + protocolRep.addWriter(new FilesRule()); + protocolRep.addWriter(new SaveFileOkRule()); + protocolRep.addWriter(new SaveFileErrorRule()); + protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); + protocolRep.addWriter(new GetFileErrorRule()); + protocolRep.addWriter(new EraseFileRule()); + protocolRep.addWriter(new RemoveFileErrorRule()); + protocolRep.addWriter(new RemoveFileOkRule()); + protocolRep.addWriter(new GetFileOkRule()); + protocolRep.addWriter(new RetrieveFileRule()); + } + private static Repository prepareStorage() { AppData appData = AppData.getInstance(); Repository repository = new Repository( diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java index 6483db2..551ea03 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java @@ -9,7 +9,7 @@ public class RetrieveErrorRule extends ProtocolReader { private static final String NAME = "RETRIEVE_ERROR"; // Constructor - protected RetrieveErrorRule() { + public RetrieveErrorRule() { super(NAME, PATTERN); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java index 67a3149..39c4cb3 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -13,7 +13,7 @@ public class RetrieveOkRule extends ProtocolReader { private static final int HASHED_FILE_CONTENT = 2; // Index hashed file content // Variables - protected RetrieveOkRule() { + public RetrieveOkRule() { super(NAME, PATTERN); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java index dbcbe28..3c77552 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -5,6 +5,10 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + /** * Règle permettant de de confirmer la sauvegrade d'un fichier. */ @@ -17,11 +21,13 @@ public class SendErrorRule extends ProtocolReader { private static final String NAME = "SEND_ERROR"; private ProtocolRepository protocolRep; + private final String storagePath; // Constructor - public SendErrorRule(ProtocolRepository protocolRep) { + public SendErrorRule(ProtocolRepository protocolRep, String storagePath) { super(NAME, PATTERN); this.protocolRep = protocolRep; + this.storagePath = storagePath; } @@ -29,7 +35,13 @@ public class SendErrorRule extends ProtocolReader { protected ProtocolResult onExecuted(Context context, String... data) { ProtocolResult result = new ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); - // TODO : Supprimer le fichier du FFE + + // Suppression du fichier temporaire dans le stockage du FFE + String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", this.storagePath, hashedFileName))); + } catch (IOException e) {} + return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index 04522e4..7298bf0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -5,6 +5,10 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileOkRule; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + /** * Règle permettant de de confirmer la sauvegrade d'un fichier. */ @@ -16,11 +20,13 @@ public class SendOkRule extends ProtocolReader { private static final String NAME = "SEND_OK"; private final ProtocolRepository protocolRep; + private final String storagePath; // Constructor - public SendOkRule(ProtocolRepository protocolRep) { + public SendOkRule(ProtocolRepository protocolRep, String storagePath) { super(NAME, PATTERN); this.protocolRep = protocolRep; + this.storagePath = storagePath; } @@ -30,10 +36,14 @@ public class SendOkRule extends ProtocolReader { result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT); // Sauvegarder dans JSON - System.out.println("Save en json du fichier"); context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); - // TODO : Supprimer le fichier du FFE + // Suppression du fichier temporaire dans le stockage du FFE + String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); + } catch (IOException e) {} + return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java index 77bc6f5..39944db 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -4,14 +4,14 @@ import lightcontainer.protocol.ProtocolWriter; public class GetFileOkRule extends ProtocolWriter { // Constants - private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n)$"; + private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n$"; public static final String NAME = "GETFILE_OK"; // -- params private static final int FILE_NAME = 0; // Index file name hashed private static final int FILE_SIZE = 1; // Index file size // Constructors - protected GetFileOkRule() { + public GetFileOkRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java index d99241c..65243c9 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java @@ -10,7 +10,7 @@ public class RetrieveFileRule extends ProtocolWriter { private static final int HASHED_FILE_NAME = 0; // Index hashed filename // Constructor - protected RetrieveFileRule() { + public RetrieveFileRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/utils/SHA.java b/app/src/main/java/lightcontainer/utils/SHA.java index 4ffe472..6eb1c56 100644 --- a/app/src/main/java/lightcontainer/utils/SHA.java +++ b/app/src/main/java/lightcontainer/utils/SHA.java @@ -44,7 +44,7 @@ public class SHA { * * @param in InputStream to the input, flux to hash. * @param fileSize Stream/file size. - * + *ichier, utilisé * @return Borrowing of the full current flux. * * @throws ShaException if an error occur. diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index f2068c3..bb67cfc 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1,17 +1 @@ -{ - "unicast_port": 8000, - "multicast_ip": "226.66.66.1", - "multicast_port": 15502, - "network_interface": "My network interface", - "tls": true, - "storagePath": "C:\\Users\\ledou\\Documents\\ffe", - "users": [ - { - "name": "aaaaa", - "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", - "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", - "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", - "files": [] - } - ] -} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file From 0424a2f53ae5671f681531152fd058ce5f66db00 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 17:52:48 +0100 Subject: [PATCH 07/75] Changement adresse multicast --- app/src/main/resources/appdata.json | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index bb67cfc..bf62c9c 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1,27 @@ -{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{ + "unicast_port": 8000, + "multicast_ip": "224.66.66.1", + "multicast_port": 15502, + "network_interface": "My network interface", + "tls": true, + "storagePath": "/home/benjamin/ffe", + "users": [ + { + "name": "aaaaa", + "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", + "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", + "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", + "files": [ + { + "name": "README.md", + "fileNameSalt": "/jo0zYyQs96gWI9OgBXiPQ==", + "size": 17, + "iv": "rvOFhgEvgFMISO44jqlSRg==", + "storage": [ + "lightcontainerSB01" + ] + } + ] + } + ] +} \ No newline at end of file From e5628942a26a97e74583c23bd00643b178a065dd Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 19:51:45 +0100 Subject: [PATCH 08/75] =?UTF-8?q?-=20Ajout=20syst=C3=A8me=20retournant=20a?= =?UTF-8?q?utomatiquement=20l'erreur=20imp=C3=A9ment=C3=A9e=20par=20la=20c?= =?UTF-8?q?ommande=20si=20le=20SBE=20requis=20par=20le=20r=C3=A9sultat=20d?= =?UTF-8?q?e=20celle-ci=20n'est=20pas=20connect=C3=A9=20au=20FFE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lightcontainer/domains/Task.java | 4 ++ .../domains/client/ClientHandler.java | 24 +++++++----- .../domains/client/StoreProcessor.java | 9 ++++- .../interfaces/ClientHandlerFFE.java | 1 + .../interfaces/MulticastSPR.java | 2 + .../protocol/ProtocolReader.java | 37 +++++++++++++++++-- .../protocol/ProtocolWriter.java | 1 + .../protocol/rules/reader/GetFileRule.java | 17 +++++++++ .../protocol/rules/reader/SavefileRule.java | 2 + .../protocol/rules/writer/SendfileRule.java | 2 +- .../repository/FileFrontEnd.java | 5 +++ .../repository/StoreProcessorRepository.java | 11 ++++++ app/src/main/resources/appdata.json | 28 +------------- 13 files changed, 101 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/lightcontainer/domains/Task.java b/app/src/main/java/lightcontainer/domains/Task.java index e5e70d3..4a89b7b 100644 --- a/app/src/main/java/lightcontainer/domains/Task.java +++ b/app/src/main/java/lightcontainer/domains/Task.java @@ -77,4 +77,8 @@ public class Task { public Context getContext() { return this.context; } + + public String getDomain() { + return context.getDomain(); + } } diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index d10a544..3508101 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -115,16 +115,23 @@ public class ClientHandler implements Runnable, AutoCloseable { ProtocolWriter.ProtocolResult writerCommand = ruleResult.getResultCommand(); if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - fileFrontEnd.newCommand(context, writerCommand); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd + // TODO : Vérifier que le StorBackEnd demandé (Pas toujours demandé) est disponible + if (ruleResult.getRequestDomain() != null && !fileFrontEnd.canExecuteCommand(ruleResult.getRequestDomain())) { + writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client + writer.flush(); + } else { + fileFrontEnd.newCommand(context, writerCommand); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd - // Attend la fin de la réalisation de la tâche - waitTaskResponse(); + // Attend la fin de la réalisation de la tâche + waitTaskResponse(); + + writer.write(response.getCommand()); // Renvoie au client + writer.flush(); + response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire + } - writer.write(response.getCommand()); // Renvoye au client - writer.flush(); - response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire } else { - writer.print(writerCommand.getCommand()); // Renvoye au client + writer.print(writerCommand.getCommand()); // Renvoie au client writer.flush(); } @@ -133,7 +140,6 @@ public class ClientHandler implements Runnable, AutoCloseable { accessDenied(); } } catch (IOException e) { - e.printStackTrace(); repository.disconnect(this); break; } @@ -195,13 +201,13 @@ public class ClientHandler implements Runnable, AutoCloseable { /** * Vérifie s'il s'âgit d'une demande de déconnexion * @param ruleResult - */ private void checkSignError(ProtocolWriter.ProtocolResult ruleResult) { if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) { System.out.println("Pas pu connecter"); repository.disconnect(this); } } + */ /** * Permet au Client d'attendre la fin de la réalisation de sa tâche diff --git a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java index 47e2ca6..8fb1316 100644 --- a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java @@ -83,6 +83,10 @@ public class StoreProcessor extends Thread implements AutoCloseable { this.client_run = true; while (this.client_run) { try { + if (!store.isConnected()) { + // TODO : Gérer déconnection + break; + } waitAction(); System.out.println("[SBE] Envoie commande : " + protocolResult.getCommand()); @@ -104,7 +108,9 @@ public class StoreProcessor extends Thread implements AutoCloseable { alertAvalaible(responseResult.getResultCommand()); - } catch (IOException ignore) { } + } catch (IOException ignore) { + // TODO : Gérer déconnection + } } } @@ -153,6 +159,7 @@ public class StoreProcessor extends Thread implements AutoCloseable { try { this.client_run = false; this.store.close(); + // TODO : Gérer déconnection (enlever du repo et prévenir client et FileFrontEnd) } catch (IOException ignored) { } } } diff --git a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java index 2cd5c90..30b6b1a 100644 --- a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java +++ b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java @@ -18,4 +18,5 @@ public interface ClientHandlerFFE { */ void newCommand(Context context, ProtocolWriter.ProtocolResult command); + boolean canExecuteCommand(String domain); } diff --git a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java index 1d634c4..5f352e7 100644 --- a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java +++ b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java @@ -24,4 +24,6 @@ public interface MulticastSPR { String findDomain(Task task); void assignTask(String stor, Task task); + + boolean hasDomain(String domain); } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index 0990f1a..e1ad527 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -26,6 +26,7 @@ public abstract class ProtocolReader { STOREBACKEND } + /** * Modèle utilisé par tout les résultats des règles de protocol. * Lorsqu'il retourne son résultat, on vérifie si il y a une demande de lecture/écriture de fichier depuis le réseau. Si oui on appel ces méthodes, sinon on ne fait rien. @@ -42,6 +43,11 @@ public abstract class ProtocolReader { */ private final Context context; + /** + * Domaine demandé pour l'exécution de cette commande (NULL si aucun domaine spécifique demandé) + */ + private String requestDomain; + public ProtocolResult(Context context) { this.context = context; } @@ -58,7 +64,7 @@ public abstract class ProtocolReader { * * @return Receiver */ - public ResultCmdReceiver getReceiver() { + public final ResultCmdReceiver getReceiver() { return receiver; } @@ -67,7 +73,7 @@ public abstract class ProtocolReader { * * @return Commande */ - public ProtocolWriter.ProtocolResult getResultCommand() { + public final ProtocolWriter.ProtocolResult getResultCommand() { return this.resultCommand; } @@ -77,11 +83,12 @@ public abstract class ProtocolReader { * @param resultCommand Commande à envoyer * @param receiver Le receveur de cette commande */ - public void setResultCommand(ProtocolWriter.ProtocolResult resultCommand, ResultCmdReceiver receiver) { + public final void setResultCommand(ProtocolWriter.ProtocolResult resultCommand, ResultCmdReceiver receiver) { this.resultCommand = resultCommand; this.receiver = receiver; } + /** * Permet de lire un fichier. Cad reçevoir le contenu d'un fichier provenant du réseau. * Redéfinissez cette méthode pour l'utiliser @@ -96,10 +103,32 @@ public abstract class ProtocolReader { * * @return Context courant */ - protected Context getContext() { + protected final Context getContext() { return context; } + + /** + * Cette méthode est appelée lorsque la commande à exécuter ne peut pas être exécutée + */ + public final String onNotExecutable(Context context) { + ProtocolResult cmdResult = onError(context); + return cmdResult == null ? null : cmdResult.getResultCommand().getCommand(); + } + + /** + * Accesseur du domaine demandé pour l'exécution de cette commande (NULL si aucun domaine spécifique demandé) + */ + public String getRequestDomain() { + return requestDomain; + } + + /** + * Mutateur du domaine demandé pour l'exécution de cette commande (NULL si aucun domaine spécifique demandé) + */ + public void setRequestDomain(String requestDomain) { + this.requestDomain = requestDomain; + } } /** diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java index 481cdd8..8a772a0 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java @@ -60,6 +60,7 @@ public abstract class ProtocolWriter { public Context getContext() { return context; } + } /** diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java index 35323ca..4b4f056 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -4,9 +4,12 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.GetFileErrorRule; +import lightcontainer.protocol.rules.writer.GetFileOkRule; import lightcontainer.storage.File; import lightcontainer.storage.ReadOnlyFile; +import java.util.Iterator; + public class GetFileRule extends ProtocolReader { // Constants private static final String PATTERN = "^GETFILE ([^ !]{1,20})\r\n$"; @@ -50,6 +53,10 @@ public class GetFileRule extends ProtocolReader { ReadOnlyFile file = context.getFileOf(data[FILE_NAME], context.getLogin()); + // Précision du store back end demandé pour traiter cette commande. + String requestDomain = extractRequestDomain(file.getStorageIterator()); + result.setRequestDomain(requestDomain); + if (true) { } else { @@ -68,4 +75,14 @@ public class GetFileRule extends ProtocolReader { result.setResultCommand(protocolRep.executeWriter(context, GetFileErrorRule.NAME), ResultCmdReceiver.CLIENT); return result; } + + /** + * TODO : But futur est de pouvoir en avoir plusieurs + * Cette méthode permet de choisir le domaine voulu. + * @param storageIterator Les domaines + * @return Le domain choisi + */ + private String extractRequestDomain(Iterator storageIterator) { + return storageIterator.next(); + } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 907b1ff..8ffde95 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -102,6 +102,8 @@ public class SavefileRule extends ProtocolReader { @Override protected SavefileRule.Result onExecuted(Context context, String... data) { SavefileRule.Result result = new SavefileRule.Result(context, data[FILE_NAME], Integer.parseInt(data[FILE_SIZE])); + + return result; } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index e6971cc..b5826bc 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -11,7 +11,7 @@ import java.io.OutputStream; */ public class SendfileRule extends ProtocolWriter { - private static final String PATTERN = "^SENDFILE [A-Za-z0-9.]{0,200} [0-9]{1,10} [A-Za-z0-9.]{50,200}\r\n$"; + private static final String PATTERN = "^SENDFILE [A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200}\r\n$"; public static final String NAME = "SENDFILE"; diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index 41c864e..e61dcde 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -82,4 +82,9 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { tasks.add(task); alertStoreProcessors(task); } + + @Override + public boolean canExecuteCommand(String domain) { + return storeRepository.hasDomain(domain); + } } diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java index 30234e4..769208a 100644 --- a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -78,6 +78,17 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { handler.executeCommand(task.getContext(), task.getCommand()); } + @Override + public boolean hasDomain(String domain) { + for (StoreProcessor handler : handlers) { + if (handler.getDomain().equals(domain)) { + return true; + } + } + + return false; + } + /** * AutoClosable Function * Closes all StoreProcessor stored in this repository and deallocates all resources. diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index bf62c9c..f9171fe 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1,27 +1 @@ -{ - "unicast_port": 8000, - "multicast_ip": "224.66.66.1", - "multicast_port": 15502, - "network_interface": "My network interface", - "tls": true, - "storagePath": "/home/benjamin/ffe", - "users": [ - { - "name": "aaaaa", - "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", - "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", - "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", - "files": [ - { - "name": "README.md", - "fileNameSalt": "/jo0zYyQs96gWI9OgBXiPQ==", - "size": 17, - "iv": "rvOFhgEvgFMISO44jqlSRg==", - "storage": [ - "lightcontainerSB01" - ] - } - ] - } - ] -} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"main.py","fileNameSalt":"IJNYL681pFqbF9OHzRuHIg==","size":854,"iv":"bPCnwYbenKvFfwbhq+HI5A==","storage":["lightcontainerSB01"]},{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file From 20c5697301ba984736c1c75a63de891a84e518b0 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sun, 13 Mar 2022 21:12:19 +0100 Subject: [PATCH 09/75] =?UTF-8?q?Am=C3=A9lioration=20:=20Deux=20SBE=20de?= =?UTF-8?q?=20m=C3=AAme=20domaine=20et=20ports=20diff=C3=A9rents=20ne=20pe?= =?UTF-8?q?uvent=20plus=20=C3=AAtre=20connect=C3=A9s=20en=20m=C3=AAme=20te?= =?UTF-8?q?mps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/client/StoreProcessor.java | 3 +- .../server/MulticastServerListener.java | 12 ++--- .../interfaces/MulticastSPR.java | 2 +- .../repository/StoreProcessorRepository.java | 9 ++-- app/src/main/resources/appdata.json | 44 ++++++++++++++++++- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java index 8fb1316..158f308 100644 --- a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java @@ -45,7 +45,6 @@ public class StoreProcessor extends Thread implements AutoCloseable { this.store = socket; this.protocolRep = protocolRep; this.client_run = false; - initStore(); } /** @@ -55,7 +54,7 @@ public class StoreProcessor extends Thread implements AutoCloseable { * @see PrintWriter * @since 1.0 */ - private void initStore() { + public void startStore() { try { this.reader = new BufferedReader(new InputStreamReader( this.store.getInputStream(), diff --git a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java index 96342f0..fe4b12a 100644 --- a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java @@ -70,13 +70,15 @@ public class MulticastServerListener implements Runnable { HelloRule.Result readerResult = protocolRep.executeReader(null, data); System.out.printf("Nouveau SBE : Domain=%s | Port=%d\n", readerResult.getDomain(), readerResult.getPort()); - Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); + if (!this.repository.hasDomain(readerResult.getDomain())){ + Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); - // Create the store processor - StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), ffe, protocolRep); // TODO : Voir comment on procède get via repo ou ici ?! + // Create the store processor + StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), ffe, protocolRep); // TODO : Voir comment on procède get via repo ou ici ?! - // Add the store processor to its repository - this.repository.addStore(storeProcessor); + // Add the store processor to its repository + this.repository.addStore(storeProcessor); + } } catch (IOException ignore) { ignore.printStackTrace(); } diff --git a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java index 5f352e7..9d1caa2 100644 --- a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java +++ b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java @@ -19,7 +19,7 @@ public interface MulticastSPR { * Add a StorePorcessor. * @param store Store processor to add. */ - void addStore(StoreProcessor store); + boolean addStore(StoreProcessor store); String findDomain(Task task); diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java index 769208a..293b1a4 100644 --- a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -50,9 +50,12 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { * @since 1.0 */ @Override - public void addStore(StoreProcessor store) { - this.handlers.add(store); - + public boolean addStore(StoreProcessor store) { + if (!this.hasDomain(store.getDomain())) { + store.startStore(); + return this.handlers.add(store); + } + return false; } @Override diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index f9171fe..a32ef4a 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1,43 @@ -{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"main.py","fileNameSalt":"IJNYL681pFqbF9OHzRuHIg==","size":854,"iv":"bPCnwYbenKvFfwbhq+HI5A==","storage":["lightcontainerSB01"]},{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{ + "unicast_port": 8000, + "multicast_ip": "224.66.66.1", + "multicast_port": 15502, + "network_interface": "My network interface", + "tls": true, + "storagePath": "/home/benjamin/ffe", + "users": [ + { + "name": "aaaaa", + "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", + "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", + "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", + "files": [ + { + "name": "main.py", + "fileNameSalt": "IJNYL681pFqbF9OHzRuHIg==", + "size": 854, + "iv": "bPCnwYbenKvFfwbhq+HI5A==", + "storage": [ + "lightcontainerSB01" + ] + }, + { + "name": "README.md", + "fileNameSalt": "/jo0zYyQs96gWI9OgBXiPQ==", + "size": 17, + "iv": "rvOFhgEvgFMISO44jqlSRg==", + "storage": [ + "lightcontainerSB01" + ] + } + ] + }, + { + "name": "test1", + "password": "4348c6955fc61e81fd952e218734cb8af04df549adca990ad6bd7f46e983ebcc1ea6ddb1bee08db8c670100219597e92", + "aes_key": "UZsJ6PT6WfKptNXIlY5vPnXkjlvL36Pq3z3vLdVl/5s=", + "passwordSalt": "1a5BS3tyqpSSwvv/nTQcVA==", + "files": [] + } + ] +} \ No newline at end of file From b94720a797a0d59fccf03d21c50dd36821a3758d Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 21:47:23 +0100 Subject: [PATCH 10/75] =?UTF-8?q?-=20Translation=20de=20la=20suppresion=20?= =?UTF-8?q?du=20fichier=20temporaire=20sur=20le=20FFE=20de=20Send(OK/ERROR?= =?UTF-8?q?)=20)=20Save(OK/ERROR),=20pour=20supprimer=20le=20fichier=20cr?= =?UTF-8?q?=C3=A9=C3=A9=20par=20la=20m=C3=A9thode=20read=20de=20SAVEFILE,?= =?UTF-8?q?=20et=20ce=20m=C3=AAme=20si=20on=20ne=20passe=20pas=20par=20le?= =?UTF-8?q?=20syst=C3=A8me=20de=20t=C3=A2che=20(exemple,=20pas=20de=20SBE?= =?UTF-8?q?=20disponible)=20-=20Ajout=20possibilit=C3=A9=20de=20choisir=20?= =?UTF-8?q?un=20SBE=20appropri=C3=A9=20=C3=A0=20la=20requ=C3=AAte=20et=20d?= =?UTF-8?q?onc=20de=20renvoyer=20un=20message=20d'erreur=20convenable=20si?= =?UTF-8?q?=20le=20SBE=20n'est=20pas=20connect=C3=A9=20pour=20le=20moment?= =?UTF-8?q?=20(Via=20son=20ID)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 6 ++-- .../domains/client/ClientHandler.java | 27 +++++++++--------- .../domains/client/Context.java | 4 +-- .../interfaces/MulticastSPR.java | 2 ++ .../protocol/ProtocolWriter.java | 2 +- .../protocol/rules/reader/GetFileRule.java | 4 +-- .../protocol/rules/reader/RemoveFileRule.java | 22 +++++++++++++-- .../protocol/rules/reader/SavefileRule.java | 2 ++ .../protocol/rules/reader/SendErrorRule.java | 7 +---- .../protocol/rules/reader/SendOkRule.java | 13 +-------- .../rules/writer/SaveFileErrorRule.java | 28 ++++++++++++++++++- .../protocol/rules/writer/SaveFileOkRule.java | 25 ++++++++++++++++- .../repository/FileFrontEnd.java | 3 +- .../repository/StoreProcessorRepository.java | 5 ++++ app/src/main/resources/appdata.json | 2 +- 15 files changed, 105 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 97607d2..ad2fbdf 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -59,7 +59,7 @@ public class App { protocolRep.addReader(new SignoutRule()); protocolRep.addReader(new FilelistRule(protocolRep)); protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); - protocolRep.addReader(new SendOkRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new SendOkRule(protocolRep)); protocolRep.addReader(new SendErrorRule(protocolRep, repositoryStorage.getStoragePath())); protocolRep.addReader(new GetFileRule(protocolRep)); protocolRep.addReader(new EraseErrorRule(protocolRep)); @@ -73,8 +73,8 @@ public class App { protocolRep.addWriter(new SignOkRule()); protocolRep.addWriter(new SignErrorRule()); protocolRep.addWriter(new FilesRule()); - protocolRep.addWriter(new SaveFileOkRule()); - protocolRep.addWriter(new SaveFileErrorRule()); + protocolRep.addWriter(new SaveFileOkRule(repositoryStorage.getStoragePath())); + protocolRep.addWriter(new SaveFileErrorRule(repositoryStorage.getStoragePath())); protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); protocolRep.addWriter(new GetFileErrorRule()); protocolRep.addWriter(new EraseFileRule()); diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 3508101..9d924bf 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -110,26 +110,25 @@ public class ClientHandler implements Runnable, AutoCloseable { } if (checkAccess(ruleResult)) { + // Lecture du fichier client ruleResult.read(this.client.getInputStream()); ProtocolWriter.ProtocolResult writerCommand = ruleResult.getResultCommand(); + // TODO : Vérifier que le StorBackEnd demandé (Pas toujours demandé) est disponible + if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND && !fileFrontEnd.canExecuteCommand(ruleResult.getRequestDomain())) { + System.out.println("ICI"); + writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client + writer.flush(); + } else if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - // TODO : Vérifier que le StorBackEnd demandé (Pas toujours demandé) est disponible - if (ruleResult.getRequestDomain() != null && !fileFrontEnd.canExecuteCommand(ruleResult.getRequestDomain())) { - writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client - writer.flush(); - } else { - fileFrontEnd.newCommand(context, writerCommand); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd + fileFrontEnd.newCommand(context, writerCommand); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd - // Attend la fin de la réalisation de la tâche - waitTaskResponse(); - - writer.write(response.getCommand()); // Renvoie au client - writer.flush(); - response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire - } + // Attend la fin de la réalisation de la tâche + waitTaskResponse(); + writer.write(response.getCommand()); // Renvoie au client + writer.flush(); + response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire } else { writer.print(writerCommand.getCommand()); // Renvoie au client writer.flush(); diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index c002c9f..f577891 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -178,8 +178,8 @@ public class Context { return null; } - public ReadOnlyFile getFileOf(String fileName, String userName) { - return this.repository.getFileOf(fileName, userName); + public ReadOnlyFile getFile(String fileName) { + return this.repository.getFileOf(fileName, login); } public List getStringifiedFilesOf() { diff --git a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java index 5f352e7..5565835 100644 --- a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java +++ b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java @@ -26,4 +26,6 @@ public interface MulticastSPR { void assignTask(String stor, Task task); boolean hasDomain(String domain); + + int domainCount(); } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java index 8a772a0..cdcaa56 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java @@ -77,7 +77,7 @@ public abstract class ProtocolWriter { String command = builder + "\r\n"; Matcher ruleMatcher = this.rulePattern.matcher(command); // Vérifie que tout match (cf. Matcher). Si match alors on retourne la commande build, sinon on retourne NULL - System.out.println("Essaye : " + command); + System.out.println("Crée la commande : " + command); if (ruleMatcher.matches()) { ProtocolResult result = onExecuted(context, data); result.setCommand(command); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java index 4b4f056..e3e9a3c 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -4,8 +4,6 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.GetFileErrorRule; -import lightcontainer.protocol.rules.writer.GetFileOkRule; -import lightcontainer.storage.File; import lightcontainer.storage.ReadOnlyFile; import java.util.Iterator; @@ -51,7 +49,7 @@ public class GetFileRule extends ProtocolReader { protected GetFileRule.Result onExecuted(Context context, String... data) { GetFileRule.Result result = new GetFileRule.Result(context, data[FILE_NAME]); - ReadOnlyFile file = context.getFileOf(data[FILE_NAME], context.getLogin()); + ReadOnlyFile file = context.getFile(data[FILE_NAME]); // Précision du store back end demandé pour traiter cette commande. String requestDomain = extractRequestDomain(file.getStorageIterator()); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java index 412c8da..7024478 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java @@ -5,6 +5,9 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.EraseFileRule; import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; +import lightcontainer.storage.ReadOnlyFile; + +import java.util.Iterator; public class RemoveFileRule extends ProtocolReader { @@ -39,11 +42,16 @@ public class RemoveFileRule extends ProtocolReader { @Override protected RemoveFileRule.Result onExecuted(Context context, String... data) { RemoveFileRule.Result result = new RemoveFileRule.Result(context, data[FILENAME]); - String extendedFileName = context.getHashedFileName(result.getFileName()); + ReadOnlyFile file = context.getFile(result.getFileName()); - if (extendedFileName != null) { + if (file != null) { + String extendedFileName = context.getHashedFileName(result.getFileName()); context.putDataString("fileName", result.getFileName()); result.setResultCommand(protocolRep.executeWriter(context, EraseFileRule.NAME, extendedFileName), ResultCmdReceiver.STOREBACKEND); + + // Précision du store back end demandé pour traiter cette commande. + String requestDomain = extractRequestDomain(file.getStorageIterator()); + result.setRequestDomain(requestDomain); } else { result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); } @@ -57,4 +65,14 @@ public class RemoveFileRule extends ProtocolReader { result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); return result; } + + /** + * TODO : But futur est de pouvoir en avoir plusieurs + * Cette méthode permet de choisir le domaine voulu. + * @param storageIterator Les domaines + * @return Le domain choisi + */ + private String extractRequestDomain(Iterator storageIterator) { + return storageIterator.next(); + } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 8ffde95..2d089d0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -85,6 +85,8 @@ public class SavefileRule extends ProtocolReader { this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(encryptedFileSize), fileHash), ResultCmdReceiver.STOREBACKEND); } catch (IOException | SHA.ShaException e) { + // TODO : Supprimer le fichier + System.out.println("HEYHEYHEYHEYHEY OHOHOH"); this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); e.printStackTrace(); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java index 3c77552..ced3fd0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; +import lightcontainer.utils.ShaHasher; import java.io.IOException; import java.nio.file.Files; @@ -36,12 +37,6 @@ public class SendErrorRule extends ProtocolReader { ProtocolResult result = new ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); - // Suppression du fichier temporaire dans le stockage du FFE - String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); - try { - Files.deleteIfExists(Path.of(String.format("%s/%s", this.storagePath, hashedFileName))); - } catch (IOException e) {} - return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index 7298bf0..3c32bca 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -20,13 +20,11 @@ public class SendOkRule extends ProtocolReader { private static final String NAME = "SEND_OK"; private final ProtocolRepository protocolRep; - private final String storagePath; // Constructor - public SendOkRule(ProtocolRepository protocolRep, String storagePath) { + public SendOkRule(ProtocolRepository protocolRep) { super(NAME, PATTERN); this.protocolRep = protocolRep; - this.storagePath = storagePath; } @@ -35,15 +33,6 @@ public class SendOkRule extends ProtocolReader { ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT); - // Sauvegarder dans JSON - context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); - - // Suppression du fichier temporaire dans le stockage du FFE - String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); - try { - Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); - } catch (IOException e) {} - return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java index 4d1f1f3..090564d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java @@ -1,6 +1,12 @@ package lightcontainer.protocol.rules.writer; +import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; +import lightcontainer.utils.ShaHasher; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; /** * Règle signifiant que la sauvegarde d'un fichier a échoué @@ -11,8 +17,28 @@ public class SaveFileErrorRule extends ProtocolWriter { public static final String NAME = "SAVEFILE_ERROR"; - public SaveFileErrorRule() { + private final String storagePath; + + public SaveFileErrorRule(String storagePath) { super(NAME, PATTERN); + this.storagePath = storagePath; } + + @Override + protected ProtocolResult onExecuted(Context context, String... data) { + ProtocolResult result = new ProtocolResult(context); + + // Suppression du fichier temporaire dans le stockage du FFE + ShaHasher hasher = new ShaHasher(context.getLogin() + "_" + context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of( + String.format("%s/%s", this.storagePath, + hasher.fromSalt(hasher.saltToByte(context.getDataString("fileNameSalt"))) + ) + )); + } catch (IOException e) {} + + return result; + } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java index 7734605..fac890f 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java @@ -1,7 +1,12 @@ package lightcontainer.protocol.rules.writer; +import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + /** * Règle signifiant que la sauvegarde d'un fichier fût un succès */ @@ -11,8 +16,26 @@ public class SaveFileOkRule extends ProtocolWriter { public static final String NAME = "SAVEFILE_OK"; - public SaveFileOkRule() { + private final String storagePath; + + public SaveFileOkRule(String storagePath) { super(NAME, PATTERN); + this.storagePath = storagePath; } + @Override + protected ProtocolWriter.ProtocolResult onExecuted(Context context, String... data) { + ProtocolWriter.ProtocolResult result = new ProtocolWriter.ProtocolResult(context); + + // Sauvegarder dans JSON + context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); + + // Suppression du fichier temporaire dans le stockage du FFE + String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); + } catch (IOException e) {} + + return result; + } } diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index e61dcde..55394a4 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -85,6 +85,7 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { @Override public boolean canExecuteCommand(String domain) { - return storeRepository.hasDomain(domain); + System.out.println("Peut on exécuter la commande ? " + domain); + return domain == null ? storeRepository.domainCount() > 0 : storeRepository.hasDomain(domain); } } diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java index 769208a..e798e27 100644 --- a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -89,6 +89,11 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { return false; } + @Override + public int domainCount() { + return handlers.size(); + } + /** * AutoClosable Function * Closes all StoreProcessor stored in this repository and deallocates all resources. diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index f9171fe..a887a1b 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1 @@ -{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"main.py","fileNameSalt":"IJNYL681pFqbF9OHzRuHIg==","size":854,"iv":"bPCnwYbenKvFfwbhq+HI5A==","storage":["lightcontainerSB01"]},{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"README.md","fileNameSalt":"cDY6LMq13iqKknRS9OVBPw==","size":17,"iv":"jxc7hkIAoH64M8JF7FvNFw==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file From c3907be8a097752bf884bdbd4b6cdc72a2a95ce0 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Mon, 14 Mar 2022 12:18:29 +0100 Subject: [PATCH 11/75] =?UTF-8?q?Ajout=20choix=20de=20l'interface=20pour?= =?UTF-8?q?=20l'=C3=A9coute=20multicast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 5 ++++ app/src/main/java/lightcontainer/App.java | 2 +- .../server/MulticastServerListener.java | 25 ++++++++++++++++++- .../lightcontainer/storage/Repository.java | 4 +++ .../java/lightcontainer/utils/NetChooser.java | 5 +++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5345a80..3a8d723 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,3 +35,8 @@ tasks.named('test') { // Use JUnit Platform for unit tests. useJUnitPlatform() } + + +run { + standardInput = System.in +} \ No newline at end of file diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index ad2fbdf..b5e133d 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -36,7 +36,7 @@ public class App { // Initialisation du dispatcher et des servers FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); - new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort()); + new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort(), repositoryStorage.getNetworkInterface()); // close repo et client et server. diff --git a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java index 96342f0..36e6ba9 100644 --- a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java @@ -6,6 +6,7 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.reader.HelloRule; import lightcontainer.repository.FileFrontEnd; +import lightcontainer.utils.NetChooser; import java.io.IOException; import java.net.*; @@ -25,6 +26,7 @@ public class MulticastServerListener implements Runnable { // Variable private final String multicast_address; private final int multicast_port; + private final String network_interface; private FileFrontEnd ffe; private final MulticastSPR repository; private final ProtocolRepository protocolRep; @@ -33,12 +35,13 @@ public class MulticastServerListener implements Runnable { private MulticastSocket listener; // Constructor - public MulticastServerListener(FileFrontEnd ffe, MulticastSPR repository, ProtocolRepository protocolRep, String multicast_address, int multicast_port) { + public MulticastServerListener(FileFrontEnd ffe, MulticastSPR repository, ProtocolRepository protocolRep, String multicast_address, int multicast_port, String network_interface) { this.ffe = ffe; this.repository = repository; this.protocolRep = protocolRep; this.multicast_address = multicast_address; this.multicast_port = multicast_port; + this.network_interface = network_interface; repository.setServerListener(this); } @@ -54,6 +57,9 @@ public class MulticastServerListener implements Runnable { try { // Create a new listening socket this.listener = new MulticastSocket(this.multicast_port); + + this.selectInterface(); + // Create an identifier for the multicast group on the specified ip InetAddress listener_group = InetAddress.getByName(this.multicast_address); // Creation of a packet for the information received @@ -85,6 +91,23 @@ public class MulticastServerListener implements Runnable { } } + /** + * Permet de choisir l'interface réseau d'écoute en Multicast + */ + private void selectInterface() { + try { + this.listener.setNetworkInterface(NetworkInterface.getByName(network_interface)); + } catch (SocketException| NullPointerException e) { + NetChooser netChooser = new NetChooser(); + try { + this.listener.setNetworkInterface(netChooser.requestInterface()); + } catch (SocketException socketException) { + socketException.printStackTrace(); + this.stop(); + } + } + } + /** * Closes the MulticastSocket connection and aborts the listening and infinite listening loop. * diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 3561d73..362fac4 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -158,4 +158,8 @@ public class Repository { public List getStringifiedFilesOf(String login) { return this.appData.getStringifiedFilesOf(login); } + + public String getNetworkInterface() { + return this.appData.getAppConfig().getNetworkInterface(); + } } diff --git a/app/src/main/java/lightcontainer/utils/NetChooser.java b/app/src/main/java/lightcontainer/utils/NetChooser.java index f9ecd98..066b7fb 100644 --- a/app/src/main/java/lightcontainer/utils/NetChooser.java +++ b/app/src/main/java/lightcontainer/utils/NetChooser.java @@ -13,6 +13,9 @@ public class NetChooser { public NetChooser() { loadInterfaces(); + } + + public NetworkInterface requestInterface() { Scanner console = new Scanner(System.in); String[] allInterfaceNames = getInterfaces(); for(int index=0; index < allInterfaceNames.length; ++index) { @@ -21,7 +24,7 @@ public class NetChooser { System.out.printf("Select your interface :"); NetworkInterface selected = getInterfacesByIndex(console.nextInt()); System.out.printf("Selected interface: %s\n", selected.getDisplayName()); - + return selected; } private void loadInterfaces() { From 0a3be75cca400a288b19fc3d27ede44f0a00f31a Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 15 Mar 2022 08:20:29 +0100 Subject: [PATCH 12/75] Ajout BCrypt --- .idea/jarRepositories.xml | 5 +++++ app/build.gradle | 5 +++++ app/src/main/java/lightcontainer/App.java | 3 ++- .../java/lightcontainer/domains/client/ClientHandler.java | 7 ++++++- .../main/java/lightcontainer/repository/FileFrontEnd.java | 3 +++ app/src/main/resources/appdata.json | 2 +- 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml index fdc392f..b3e9cbd 100644 --- a/.idea/jarRepositories.xml +++ b/.idea/jarRepositories.xml @@ -16,5 +16,10 @@