Merge branch 'dev' into benjamin
# Conflicts: # app/src/main/resources/appdata.json
This commit is contained in:
commit
2b7b0a36d5
@ -1,6 +1,5 @@
|
|||||||
package lightcontainer.domains.client;
|
package lightcontainer.domains.client;
|
||||||
|
|
||||||
import lightcontainer.domains.server.UnicastServerListener;
|
|
||||||
import lightcontainer.interfaces.ClientHandlerFFE;
|
import lightcontainer.interfaces.ClientHandlerFFE;
|
||||||
import lightcontainer.interfaces.ProtocolRepository;
|
import lightcontainer.interfaces.ProtocolRepository;
|
||||||
import lightcontainer.interfaces.UnicastCHR;
|
import lightcontainer.interfaces.UnicastCHR;
|
||||||
@ -10,27 +9,23 @@ import lightcontainer.protocol.rules.reader.SigninRule;
|
|||||||
import lightcontainer.protocol.rules.reader.SignoutRule;
|
import lightcontainer.protocol.rules.reader.SignoutRule;
|
||||||
import lightcontainer.protocol.rules.reader.SignupRule;
|
import lightcontainer.protocol.rules.reader.SignupRule;
|
||||||
import lightcontainer.protocol.rules.writer.SignErrorRule;
|
import lightcontainer.protocol.rules.writer.SignErrorRule;
|
||||||
import lightcontainer.protocol.rules.writer.SignOkRule;
|
|
||||||
|
|
||||||
import javax.crypto.BadPaddingException;
|
|
||||||
import javax.net.ssl.SSLHandshakeException;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClientHandler
|
* ClientHandler
|
||||||
*
|
* <p>
|
||||||
* <!> UNICAST CLIENT <!>
|
* <!> UNICAST CLIENT <!>
|
||||||
* Class communicating with the client, and
|
* Class communicating with the client, and
|
||||||
* intercepting and sending files to the client.
|
* intercepting and sending files to the client.
|
||||||
*
|
*
|
||||||
* @version 1.1
|
* @author Jérémi NIHART <j.nihart@student.helmo.be>
|
||||||
* @since 1.0
|
* @version 1.1
|
||||||
*
|
* @see Runnable
|
||||||
* @see Runnable
|
* @see AutoCloseable
|
||||||
* @see AutoCloseable
|
* @since 1.0
|
||||||
* @author Jérémi NIHART <j.nihart@student.helmo.be>
|
|
||||||
*/
|
*/
|
||||||
public class ClientHandler implements Runnable, AutoCloseable {
|
public class ClientHandler implements Runnable, AutoCloseable {
|
||||||
// Variables
|
// Variables
|
||||||
@ -60,21 +55,20 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
/**
|
/**
|
||||||
* Initialise the Client's Reader and Writer.
|
* Initialise the Client's Reader and Writer.
|
||||||
*
|
*
|
||||||
* @since 1.0
|
* @see BufferedReader
|
||||||
*
|
* @see PrintWriter
|
||||||
* @see BufferedReader
|
* @since 1.0
|
||||||
* @see PrintWriter
|
|
||||||
*/
|
*/
|
||||||
private void initClient() {
|
private void initClient() {
|
||||||
// Start the thread
|
// Start the thread
|
||||||
try {
|
try {
|
||||||
this.reader = new BufferedReader(new InputStreamReader(
|
this.reader = new BufferedReader(new InputStreamReader(
|
||||||
this.client.getInputStream(),
|
this.client.getInputStream(),
|
||||||
StandardCharsets.UTF_8
|
StandardCharsets.UTF_8
|
||||||
));
|
));
|
||||||
this.writer = new PrintWriter(new OutputStreamWriter(
|
this.writer = new PrintWriter(new OutputStreamWriter(
|
||||||
this.client.getOutputStream(),
|
this.client.getOutputStream(),
|
||||||
StandardCharsets.UTF_8
|
StandardCharsets.UTF_8
|
||||||
), true);
|
), true);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -85,7 +79,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
* Thread Function
|
* Thread Function
|
||||||
* Start the dialogue with the client.
|
* Start the dialogue with the client.
|
||||||
*
|
*
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -153,11 +147,13 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
this.writer.close();
|
this.writer.close();
|
||||||
this.client.close();
|
this.client.close();
|
||||||
System.out.printf("[CLIENT] %s s'est déconnecté\n", context.getLogin());
|
System.out.printf("[CLIENT] %s s'est déconnecté\n", context.getLogin());
|
||||||
} catch (IOException ignored) { }
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Permet de vérifier si le client possède l'accès demandé
|
* Permet de vérifier si le client possède l'accès demandé
|
||||||
|
*
|
||||||
* @param ruleResult La règle
|
* @param ruleResult La règle
|
||||||
* @return TRUE si le client possède l'accès demandé
|
* @return TRUE si le client possède l'accès demandé
|
||||||
*/
|
*/
|
||||||
@ -175,7 +171,8 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
try {
|
try {
|
||||||
ruleResult.getClass().asSubclass(SigninRule.Result.class);
|
ruleResult.getClass().asSubclass(SigninRule.Result.class);
|
||||||
return true;
|
return true;
|
||||||
} catch (ClassCastException e2) { }
|
} catch (ClassCastException e2) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -193,23 +190,25 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie s'il s'âgit d'une demande de déconnexion
|
* Vérifie s'il s'âgit d'une demande de déconnexion
|
||||||
|
*
|
||||||
* @param ruleResult
|
* @param ruleResult
|
||||||
*/
|
*/
|
||||||
private void checkSignout(ProtocolReader.ProtocolResult ruleResult) {
|
private void checkSignout(ProtocolReader.ProtocolResult ruleResult) {
|
||||||
try {
|
try {
|
||||||
ruleResult.getClass().asSubclass(SignoutRule.Result.class);
|
ruleResult.getClass().asSubclass(SignoutRule.Result.class);
|
||||||
repository.disconnect(this);
|
repository.disconnect(this);
|
||||||
} catch (ClassCastException e2) { }
|
} catch (ClassCastException e2) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vérifie s'il s'âgit d'une demande de déconnexion
|
* Vérifie s'il s'âgit d'une demande de déconnexion
|
||||||
* @param ruleResult
|
* @param ruleResult
|
||||||
private void checkSignError(ProtocolWriter.ProtocolResult ruleResult) {
|
private void checkSignError(ProtocolWriter.ProtocolResult ruleResult) {
|
||||||
if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) {
|
if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) {
|
||||||
System.out.println("Pas pu connecter");
|
System.out.println("Pas pu connecter");
|
||||||
repository.disconnect(this);
|
repository.disconnect(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -220,12 +219,15 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
} catch (InterruptedException e) { e.printStackTrace(); }
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Permet d'envoyer la réponse au client.
|
* Permet d'envoyer la réponse au client.
|
||||||
|
*
|
||||||
* @param response La réponse
|
* @param response La réponse
|
||||||
*/
|
*/
|
||||||
public void respond(ProtocolWriter.ProtocolResult response) {
|
public void respond(ProtocolWriter.ProtocolResult response) {
|
||||||
@ -239,7 +241,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
|||||||
* AutoClosable Function
|
* AutoClosable Function
|
||||||
* Close the Client thread and resources.
|
* Close the Client thread and resources.
|
||||||
*
|
*
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
|
@ -5,10 +5,10 @@ public class StandardizedDefinitions {
|
|||||||
//Parties de regex non-utilisées en dehors de cette classe.
|
//Parties de regex non-utilisées en dehors de cette classe.
|
||||||
private final static String DIGIT = "[\\x30-\\x39]";
|
private final static String DIGIT = "[\\x30-\\x39]";
|
||||||
private final static String VISIBLECHAR = "[\\x20-\\xFF]";
|
private final static String VISIBLECHAR = "[\\x20-\\xFF]";
|
||||||
private final static String PASSCHAR = "[\\x20-\\xFF]";
|
private final static String PASSCHAR = "[\\x22-\\xFF]";
|
||||||
private final static String BINARY = "[\\x00-\\xFF]";
|
private final static String BINARY = "[\\x00-\\xFF]";
|
||||||
private final static String LETTER = "[\\x41-\\x5A\\x61-\\x7A]";
|
private final static String LETTER = "[\\x41-\\x5A\\x61-\\x7A]";
|
||||||
private final static String DIGIT_LETTER = DIGIT + "|" + LETTER;
|
private final static String DIGIT_LETTER = "[\\x30-\\x39\\x41-\\x5A\\x61-\\x7A]";
|
||||||
private final static String PORT = "(6553[\\x30-\\x35])|(655[\\x30-\\x32][\\x30-\\x39])|(65[\\x30-\\x34][\\x30-\\x32]{2})|(6[\\x30-\\x34][\\x30-\\x39]{3})|([\\x31-\\x35][\\x30-\\x39]{4})|([\\x30-\\x35]{0,5})|([\\x30-\\x39]{1,4})";
|
private final static String PORT = "(6553[\\x30-\\x35])|(655[\\x30-\\x32][\\x30-\\x39])|(65[\\x30-\\x34][\\x30-\\x32]{2})|(6[\\x30-\\x34][\\x30-\\x39]{3})|([\\x31-\\x35][\\x30-\\x39]{4})|([\\x30-\\x35]{0,5})|([\\x30-\\x39]{1,4})";
|
||||||
private final static String SIZE = DIGIT + "{1,10}";
|
private final static String SIZE = DIGIT + "{1,10}";
|
||||||
private final static String LINE = "\\x0D\\x0A";
|
private final static String LINE = "\\x0D\\x0A";
|
||||||
@ -18,7 +18,7 @@ public class StandardizedDefinitions {
|
|||||||
private final static String DOMAIN = "[a-z-A-Z0-9\\.]{5,20}";
|
private final static String DOMAIN = "[a-z-A-Z0-9\\.]{5,20}";
|
||||||
private final static String HASH_FILENAME = DIGIT_LETTER + "{50,200}";
|
private final static String HASH_FILENAME = DIGIT_LETTER + "{50,200}";
|
||||||
private final static String HASH_FILECONTENT = DIGIT_LETTER + "{50,200}";
|
private final static String HASH_FILECONTENT = DIGIT_LETTER + "{50,200}";
|
||||||
private final static String FILE_INFO = HASH_FILENAME + BL + SIZE + BL + HASH_FILECONTENT;
|
private final static String FILE_INFO = "(" + HASH_FILENAME + ")" + BL + "(" + SIZE + ")" + BL + "(" + HASH_FILECONTENT + ")";
|
||||||
private final static String LOGIN = DIGIT_LETTER + "{5,20}";
|
private final static String LOGIN = DIGIT_LETTER + "{5,20}";
|
||||||
|
|
||||||
//Regex à utiliser dans les différents protocoles.
|
//Regex à utiliser dans les différents protocoles.
|
||||||
@ -29,11 +29,11 @@ public class StandardizedDefinitions {
|
|||||||
public final static String FFE_SENDFILE = "^SENDFILE" + BL + FILE_INFO + LINE + "$";
|
public final static String FFE_SENDFILE = "^SENDFILE" + BL + FILE_INFO + LINE + "$";
|
||||||
public final static String SBE_SEND_RESULT_OK = "^SEND_OK" + LINE + "$";
|
public final static String SBE_SEND_RESULT_OK = "^SEND_OK" + LINE + "$";
|
||||||
public final static String SBE_SEND_RESULT_ERROR = "^SEND_ERROR" + LINE + "$";
|
public final static String SBE_SEND_RESULT_ERROR = "^SEND_ERROR" + LINE + "$";
|
||||||
public final static String FFE_ERASE_FILE = "^ERASEFILE" + BL + HASH_FILENAME + LINE + "$";
|
public final static String FFE_ERASE_FILE = "^ERASEFILE" + BL + "(" + HASH_FILENAME + ")" + LINE + "$";
|
||||||
public final static String SBE_ERASE_RESULT_OK = "^ERASE_OK" + LINE + "$";
|
public final static String SBE_ERASE_RESULT_OK = "^ERASE_OK" + LINE + "$";
|
||||||
public final static String SBE_ERASE_RESULT_ERROR = "^ERASE_ERROR" + LINE + "$";
|
public final static String SBE_ERASE_RESULT_ERROR = "^ERASE_ERROR" + LINE + "$";
|
||||||
public final static String FFE_RETRIEVE_FILE = "^RETRIEVEFILE" + BL + HASH_FILENAME + LINE + "$";
|
public final static String FFE_RETRIEVE_FILE = "^RETRIEVEFILE" + BL + "(" + HASH_FILENAME + ")" + LINE + "$";
|
||||||
public final static String SBE_RETRIEVE_RESULT_OK = "^RETRIEVE_OK" + BL + "(" + FILE_INFO + ")" + LINE + "$";
|
public final static String SBE_RETRIEVE_RESULT_OK = "^RETRIEVE_OK" + BL + FILE_INFO + LINE + "$";
|
||||||
public final static String SBE_RETRIEVE_RESULT_ERROR = "^RETRIEVE_ERROR" + LINE + "$";
|
public final static String SBE_RETRIEVE_RESULT_ERROR = "^RETRIEVE_ERROR" + LINE + "$";
|
||||||
|
|
||||||
//Client demande à FFE une tâche. FFE répond à client.
|
//Client demande à FFE une tâche. FFE répond à client.
|
||||||
|
@ -4,7 +4,6 @@ import lightcontainer.domains.client.Context;
|
|||||||
import lightcontainer.interfaces.ProtocolRepository;
|
import lightcontainer.interfaces.ProtocolRepository;
|
||||||
import lightcontainer.protocol.ProtocolReader;
|
import lightcontainer.protocol.ProtocolReader;
|
||||||
import lightcontainer.protocol.rules.writer.GetFileErrorRule;
|
import lightcontainer.protocol.rules.writer.GetFileErrorRule;
|
||||||
import lightcontainer.protocol.rules.writer.RemoveFileErrorRule;
|
|
||||||
import lightcontainer.protocol.rules.writer.RetrieveFileRule;
|
import lightcontainer.protocol.rules.writer.RetrieveFileRule;
|
||||||
import lightcontainer.storage.ReadOnlyFile;
|
import lightcontainer.storage.ReadOnlyFile;
|
||||||
|
|
||||||
@ -80,9 +79,11 @@ public class GetFileRule extends ProtocolReader {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** TMP
|
/**
|
||||||
|
* TMP
|
||||||
* TODO : But futur est de pouvoir en avoir plusieurs (sbe)
|
* TODO : But futur est de pouvoir en avoir plusieurs (sbe)
|
||||||
* Cette méthode permet de choisir le domaine voulu.
|
* Cette méthode permet de choisir le domaine voulu.
|
||||||
|
*
|
||||||
* @param storageIterator Les domaines
|
* @param storageIterator Les domaines
|
||||||
* @return Le domain choisi
|
* @return Le domain choisi
|
||||||
*/
|
*/
|
||||||
|
@ -8,9 +8,9 @@ import lightcontainer.protocol.ProtocolReader;
|
|||||||
*/
|
*/
|
||||||
public class HelloRule extends ProtocolReader {
|
public class HelloRule extends ProtocolReader {
|
||||||
|
|
||||||
private static final String PATTERN = "^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$";
|
private static final String PATTERN = "^HELLO ([A-Za-z0-9.]{5,20}) ([\\d]{0,5})\r\n$";
|
||||||
|
|
||||||
private static final String NAME = "HELLO";
|
private static final String NAME = "HELLO";
|
||||||
|
|
||||||
// Index du domain dans le tableau de donnée
|
// Index du domain dans le tableau de donnée
|
||||||
private static final int DOMAIN = 0;
|
private static final int DOMAIN = 0;
|
||||||
|
@ -43,6 +43,14 @@ public class RetrieveOkRule extends ProtocolReader {
|
|||||||
this.hashedFileContent = hashedFileContent;
|
this.hashedFileContent = hashedFileContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFilesize() {
|
||||||
|
return filesize;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bitch has bettern than my money
|
* Bitch has bettern than my money
|
||||||
* @param reader Buffer rempli du fichier
|
* @param reader Buffer rempli du fichier
|
||||||
@ -70,7 +78,8 @@ public class RetrieveOkRule extends ProtocolReader {
|
|||||||
RetrieveOkRule.Result result = new RetrieveOkRule.Result(context, data[HASHED_FILE_NAME], Integer.parseInt(data[FILE_SIZE]), data[HASHED_FILE_CONTENT]);
|
RetrieveOkRule.Result result = new RetrieveOkRule.Result(context, data[HASHED_FILE_NAME], Integer.parseInt(data[FILE_SIZE]), data[HASHED_FILE_CONTENT]);
|
||||||
|
|
||||||
// save encrypted file size into bundle
|
// save encrypted file size into bundle
|
||||||
context.putDataInt("encryptedFileSize", Integer.parseInt(data[FILE_SIZE])); // TODO to long ?!
|
context.putDataInt("encryptedFileSize", result.getFilesize()); // TODO to long ?!
|
||||||
|
context.putDataString("hashedFileName", result.getFilename());
|
||||||
|
|
||||||
// Set result command
|
// Set result command
|
||||||
result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT);
|
result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT);
|
||||||
|
@ -4,17 +4,14 @@ import lightcontainer.domains.client.Context;
|
|||||||
import lightcontainer.interfaces.ProtocolRepository;
|
import lightcontainer.interfaces.ProtocolRepository;
|
||||||
import lightcontainer.protocol.ProtocolReader;
|
import lightcontainer.protocol.ProtocolReader;
|
||||||
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
|
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
|
||||||
import lightcontainer.protocol.rules.writer.SaveFileOkRule;
|
|
||||||
import lightcontainer.protocol.rules.writer.SendfileRule;
|
import lightcontainer.protocol.rules.writer.SendfileRule;
|
||||||
import lightcontainer.utils.AES_GCM;
|
import lightcontainer.utils.AES_GCM;
|
||||||
import lightcontainer.utils.FileReceiver;
|
import lightcontainer.utils.FileReceiver;
|
||||||
import lightcontainer.utils.SHA;
|
import lightcontainer.utils.SHA;
|
||||||
import lightcontainer.utils.ShaHasher;
|
import lightcontainer.utils.ShaHasher;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Règle permettant de sauvegarder un fichier sur le SBE.
|
* Règle permettant de sauvegarder un fichier sur le SBE.
|
||||||
@ -22,7 +19,7 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
*/
|
*/
|
||||||
public class SavefileRule extends ProtocolReader {
|
public class SavefileRule extends ProtocolReader {
|
||||||
// Constants
|
// Constants
|
||||||
private static final String PATTERN = "^SAVEFILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$";
|
private static final String PATTERN = "^SAVE_FILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$";
|
||||||
|
|
||||||
private static final String NAME = "SAVEFILE";
|
private static final String NAME = "SAVEFILE";
|
||||||
|
|
||||||
@ -49,6 +46,7 @@ public class SavefileRule extends ProtocolReader {
|
|||||||
super(context);
|
super(context);
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
System.out.println(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -76,6 +74,8 @@ public class SavefileRule extends ProtocolReader {
|
|||||||
int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv);
|
int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv);
|
||||||
if (encryptedFileSize < 0) throw new IOException();
|
if (encryptedFileSize < 0) throw new IOException();
|
||||||
|
|
||||||
|
System.out.println(encryptedFileSize);
|
||||||
|
|
||||||
String fileHash = SHA.hashFile(storagePath, this.filename);
|
String fileHash = SHA.hashFile(storagePath, this.filename);
|
||||||
|
|
||||||
// On met les données de la requête actuelle
|
// On met les données de la requête actuelle
|
||||||
|
@ -4,11 +4,6 @@ import lightcontainer.domains.client.Context;
|
|||||||
import lightcontainer.interfaces.ProtocolRepository;
|
import lightcontainer.interfaces.ProtocolRepository;
|
||||||
import lightcontainer.protocol.ProtocolReader;
|
import lightcontainer.protocol.ProtocolReader;
|
||||||
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
|
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
|
||||||
import lightcontainer.utils.ShaHasher;
|
|
||||||
|
|
||||||
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.
|
* Règle permettant de de confirmer la sauvegrade d'un fichier.
|
||||||
|
@ -5,10 +5,6 @@ import lightcontainer.interfaces.ProtocolRepository;
|
|||||||
import lightcontainer.protocol.ProtocolReader;
|
import lightcontainer.protocol.ProtocolReader;
|
||||||
import lightcontainer.protocol.rules.writer.SaveFileOkRule;
|
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.
|
* Règle permettant de de confirmer la sauvegrade d'un fichier.
|
||||||
*/
|
*/
|
||||||
|
@ -4,6 +4,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
|
|
||||||
public class EraseFileRule extends ProtocolWriter {
|
public class EraseFileRule extends ProtocolWriter {
|
||||||
|
|
||||||
|
//"^ERASEFILE ([A-Za-z0-9]{50,200})\r\n$"
|
||||||
private static final String PATTERN = "^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$";
|
private static final String PATTERN = "^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$";
|
||||||
|
|
||||||
public static String NAME = "ERASEFILE";
|
public static String NAME = "ERASEFILE";
|
||||||
@ -11,5 +12,6 @@ public class EraseFileRule extends ProtocolWriter {
|
|||||||
|
|
||||||
public EraseFileRule() {
|
public EraseFileRule() {
|
||||||
super(NAME, PATTERN);
|
super(NAME, PATTERN);
|
||||||
|
System.out.println(PATTERN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
* Règle permettant de construire une commande contenant la liste des fichiers d'un utilisateur
|
* Règle permettant de construire une commande contenant la liste des fichiers d'un utilisateur
|
||||||
*/
|
*/
|
||||||
public class FilesRule extends ProtocolWriter {
|
public class FilesRule extends ProtocolWriter {
|
||||||
|
//"^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}\r\n$"
|
||||||
private static final String PATTERN = "^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}\r\n$";
|
private static final String PATTERN = "^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}\r\n$";
|
||||||
|
|
||||||
public static final String NAME = "FILES";
|
public static final String NAME = "FILES";
|
||||||
|
@ -8,6 +8,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
*/
|
*/
|
||||||
public class GetFileErrorRule extends ProtocolWriter {
|
public class GetFileErrorRule extends ProtocolWriter {
|
||||||
// Constants
|
// Constants
|
||||||
|
//"^GETFILE_ERROR\r\n$"
|
||||||
private static final String PATTERN = "^GETFILE_ERROR\r\n$";
|
private static final String PATTERN = "^GETFILE_ERROR\r\n$";
|
||||||
public static final String NAME = "GETFILE_ERROR";
|
public static final String NAME = "GETFILE_ERROR";
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ import lightcontainer.utils.FileSender;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class GetFileOkRule extends ProtocolWriter {
|
public class GetFileOkRule extends ProtocolWriter {
|
||||||
// Constants
|
// Constants
|
||||||
@ -47,12 +49,16 @@ public class GetFileOkRule extends ProtocolWriter {
|
|||||||
System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize"));
|
System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize"));
|
||||||
FileSender fileSender = new FileSender(storagePath);
|
FileSender fileSender = new FileSender(storagePath);
|
||||||
fileSender.sendFile(
|
fileSender.sendFile(
|
||||||
getContext().getHashedFileName(this.filename),
|
getContext().getDataString("hashedFileName"),
|
||||||
writer,
|
writer,
|
||||||
getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system)
|
getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system)
|
||||||
getContext().getAesKey(),
|
getContext().getAesKey(),
|
||||||
getContext().getDataString("fileIV")
|
getContext().getDataString("fileIV")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, getContext().getDataString("hashedFileName"))));
|
||||||
|
} catch (IOException ignore) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
|
|
||||||
public class RemoveFileErrorRule extends ProtocolWriter {
|
public class RemoveFileErrorRule extends ProtocolWriter {
|
||||||
|
|
||||||
|
//"^REMOVEFILE_ERROR\r\n$"
|
||||||
private static final String PATTERN = "^REMOVEFILE_ERROR\r\n$";
|
private static final String PATTERN = "^REMOVEFILE_ERROR\r\n$";
|
||||||
|
|
||||||
public static String NAME = "REMOVEFILE_ERROR";
|
public static String NAME = "REMOVEFILE_ERROR";
|
||||||
|
@ -3,7 +3,7 @@ package lightcontainer.protocol.rules.writer;
|
|||||||
import lightcontainer.protocol.ProtocolWriter;
|
import lightcontainer.protocol.ProtocolWriter;
|
||||||
|
|
||||||
public class RemoveFileOkRule extends ProtocolWriter {
|
public class RemoveFileOkRule extends ProtocolWriter {
|
||||||
|
//"^REMOVEFILE_OK\r\n$"
|
||||||
private static final String PATTERN = "^REMOVEFILE_OK\r\n$";
|
private static final String PATTERN = "^REMOVEFILE_OK\r\n$";
|
||||||
public static String NAME = "REMOVEFILE_OK";
|
public static String NAME = "REMOVEFILE_OK";
|
||||||
|
|
||||||
|
@ -4,7 +4,8 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
|
|
||||||
public class RetrieveFileRule extends ProtocolWriter {
|
public class RetrieveFileRule extends ProtocolWriter {
|
||||||
// Constants
|
// Constants
|
||||||
private static final String PATTERN = "^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$";
|
//"^RETRIEVEFILE ([A-Za-z0-9]{50,200})\r\n$"
|
||||||
|
private static final String PATTERN = "^RETRIEVEFILE ([A-Za-z0-9]{50,200})\r\n$";
|
||||||
public static final String NAME = "RETRIEVEFILE";
|
public static final String NAME = "RETRIEVEFILE";
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
|
@ -12,7 +12,7 @@ import java.nio.file.Path;
|
|||||||
* Règle signifiant que la sauvegarde d'un fichier a échoué
|
* Règle signifiant que la sauvegarde d'un fichier a échoué
|
||||||
*/
|
*/
|
||||||
public class SaveFileErrorRule extends ProtocolWriter {
|
public class SaveFileErrorRule extends ProtocolWriter {
|
||||||
|
//"^SAVEFILE_ERROR\r\n$"
|
||||||
private static final String PATTERN = "^SAVEFILE_ERROR\r\n$";
|
private static final String PATTERN = "^SAVEFILE_ERROR\r\n$";
|
||||||
|
|
||||||
public static final String NAME = "SAVEFILE_ERROR";
|
public static final String NAME = "SAVEFILE_ERROR";
|
||||||
@ -38,7 +38,8 @@ public class SaveFileErrorRule extends ProtocolWriter {
|
|||||||
hasher.fromSalt(hasher.saltToByte(context.getDataString("fileNameSalt")))
|
hasher.fromSalt(hasher.saltToByte(context.getDataString("fileNameSalt")))
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
} catch (IOException e) {}
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -34,8 +34,7 @@ public class SaveFileOkRule extends ProtocolWriter {
|
|||||||
String hashedFileName = context.getHashedFileName(context.getDataString("fileName"));
|
String hashedFileName = context.getHashedFileName(context.getDataString("fileName"));
|
||||||
try {
|
try {
|
||||||
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName)));
|
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName)));
|
||||||
} catch (IOException e) {}
|
} catch (IOException ignore) {}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,15 @@ import java.io.OutputStream;
|
|||||||
* Règle envoyée au SBE, demandant la sauvegarde d'un fichier.
|
* Règle envoyée au SBE, demandant la sauvegarde d'un fichier.
|
||||||
*/
|
*/
|
||||||
public class SendfileRule extends ProtocolWriter {
|
public class SendfileRule extends ProtocolWriter {
|
||||||
|
//"^SENDFILE [A-Za-z0-9]{50,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$";
|
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";
|
public static final String NAME = "SENDFILE";
|
||||||
|
|
||||||
private static final int HASHED_FILE_NAME = 0; // Index file name hashed.
|
private static final int HASHED_FILE_NAME = 0; // Index file name hashed.
|
||||||
private static final int FILE_SIZE = 1; // Index file size.
|
private static final int FILE_SIZE = 1; // Index file size.
|
||||||
private static final int HASHED_FILE_CONTENT = 2; // Index file content hashed.
|
private static final int HASHED_FILE_CONTENT = 2; // Index file content hashed.
|
||||||
|
|
||||||
private String storagePath;
|
private String storagePath;
|
||||||
|
|
||||||
public SendfileRule(String storagePath) {
|
public SendfileRule(String storagePath) {
|
||||||
|
@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
* Règle renvoyée au client lorsque l'authentification a échoué.
|
* Règle renvoyée au client lorsque l'authentification a échoué.
|
||||||
*/
|
*/
|
||||||
public class SignErrorRule extends ProtocolWriter {
|
public class SignErrorRule extends ProtocolWriter {
|
||||||
|
//"^SIGN_ERROR\r\n$"
|
||||||
private static final String PATTERN = "^SIGN_ERROR\r\n$";
|
private static final String PATTERN = "^SIGN_ERROR\r\n$";
|
||||||
|
|
||||||
public static final String NAME = "SIGN_ERROR";
|
public static final String NAME = "SIGN_ERROR";
|
||||||
|
@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter;
|
|||||||
* Règle renvoyée au client lorsque l'authentification a réusie.
|
* Règle renvoyée au client lorsque l'authentification a réusie.
|
||||||
*/
|
*/
|
||||||
public class SignOkRule extends ProtocolWriter {
|
public class SignOkRule extends ProtocolWriter {
|
||||||
|
//"^SIGN_OK\r\n$"
|
||||||
private static final String PATTERN = "^SIGN_OK\r\n$";
|
private static final String PATTERN = "^SIGN_OK\r\n$";
|
||||||
|
|
||||||
public static final String NAME = "SIGN_OK";
|
public static final String NAME = "SIGN_OK";
|
||||||
|
@ -8,18 +8,17 @@ public class FileReceiver {
|
|||||||
public FileReceiver(String path) { this.path = path; }
|
public FileReceiver(String path) { this.path = path; }
|
||||||
|
|
||||||
public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) {
|
public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) {
|
||||||
BufferedOutputStream bosFile = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bosFile = new BufferedOutputStream(new FileOutputStream(String.format("%s/%s", path, fileName)));
|
File file = new File(String.format("%s/%s", path, fileName));
|
||||||
|
if (file.createNewFile()) {
|
||||||
AES_GCM.encryptStream(input, bosFile, fileSize, key, iv);
|
try (BufferedOutputStream bufferedStream = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||||
|
// AES close l'outputstream mais on le try with ressources au cas ou ;)
|
||||||
File f = new File(String.format("%s/%s", path, fileName));
|
AES_GCM.encryptStream(input, bufferedStream, fileSize, key, iv);
|
||||||
return (int)f.length(); // TODO change the size to LONG
|
}
|
||||||
|
}
|
||||||
|
return (int)file.length(); // TODO change the size to LONG
|
||||||
} catch(IOException | AES_GCM.AesGcmException ex) {
|
} catch(IOException | AES_GCM.AesGcmException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} }
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,12 @@ public class FileSender {
|
|||||||
public boolean sendFile(String filename, OutputStream out) throws IOException {
|
public boolean sendFile(String filename, OutputStream out) throws IOException {
|
||||||
BufferedInputStream bisFile;
|
BufferedInputStream bisFile;
|
||||||
int bytesReaded = 0;
|
int bytesReaded = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File f = new File(String.format("%s/%s", path, filename));
|
File file = new File(String.format("%s/%s", path, filename));
|
||||||
long fileSize = f.length();
|
long fileSize = file.length();
|
||||||
if(f.exists()) {
|
if(file.exists()) {
|
||||||
byte[] buffer = new byte[DEFAULT_BUFFER];
|
byte[] buffer = new byte[DEFAULT_BUFFER];
|
||||||
bisFile = new BufferedInputStream(new FileInputStream(f));
|
bisFile = new BufferedInputStream(new FileInputStream(file));
|
||||||
long currentOffset = 0;
|
long currentOffset = 0;
|
||||||
while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) {
|
while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) {
|
||||||
out.write(buffer, 0, bytesReaded); out.flush();
|
out.write(buffer, 0, bytesReaded); out.flush();
|
||||||
@ -43,7 +42,7 @@ public class FileSender {
|
|||||||
return true;
|
return true;
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
} catch(IOException ex) {
|
} catch(IOException ex) {// todo change
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,11 @@ public class SHA {
|
|||||||
public static String hashFile(String rootPath, String fileName) throws ShaException {
|
public static String hashFile(String rootPath, String fileName) throws ShaException {
|
||||||
try {
|
try {
|
||||||
File file = new File(String.format("%s/%s", rootPath, fileName));
|
File file = new File(String.format("%s/%s", rootPath, fileName));
|
||||||
return hashStream(new FileInputStream(file), file.length());
|
String hash;
|
||||||
|
try (InputStream inStream = new FileInputStream(file)) {
|
||||||
|
hash = hashStream(inStream, file.length());
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ShaException(e);
|
throw new ShaException(e);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user