Merge branch 'dev' into benjamin

# Conflicts:
#	app/src/main/resources/appdata.json
This commit is contained in:
Benjamin 2022-03-19 11:47:25 +01:00
commit 2b7b0a36d5
23 changed files with 98 additions and 82 deletions

View File

@ -1,6 +1,5 @@
package lightcontainer.domains.client;
import lightcontainer.domains.server.UnicastServerListener;
import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.ProtocolRepository;
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.SignupRule;
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.net.Socket;
import java.nio.charset.StandardCharsets;
/**
* ClientHandler
*
* <p>
* <!> UNICAST CLIENT <!>
* Class communicating with the client, and
* intercepting and sending files to the client.
*
* @version 1.1
* @since 1.0
*
* @see Runnable
* @see AutoCloseable
* @author Jérémi NIHART <j.nihart@student.helmo.be>
* @author Jérémi NIHART <j.nihart@student.helmo.be>
* @version 1.1
* @see Runnable
* @see AutoCloseable
* @since 1.0
*/
public class ClientHandler implements Runnable, AutoCloseable {
// Variables
@ -60,21 +55,20 @@ public class ClientHandler implements Runnable, AutoCloseable {
/**
* Initialise the Client's Reader and Writer.
*
* @since 1.0
*
* @see BufferedReader
* @see PrintWriter
* @see BufferedReader
* @see PrintWriter
* @since 1.0
*/
private void initClient() {
// Start the thread
try {
this.reader = new BufferedReader(new InputStreamReader(
this.client.getInputStream(),
StandardCharsets.UTF_8
this.client.getInputStream(),
StandardCharsets.UTF_8
));
this.writer = new PrintWriter(new OutputStreamWriter(
this.client.getOutputStream(),
StandardCharsets.UTF_8
this.client.getOutputStream(),
StandardCharsets.UTF_8
), true);
} catch (IOException e) {
e.printStackTrace();
@ -85,7 +79,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
* Thread Function
* Start the dialogue with the client.
*
* @since 1.0
* @since 1.0
*/
@Override
public void run() {
@ -153,11 +147,13 @@ public class ClientHandler implements Runnable, AutoCloseable {
this.writer.close();
this.client.close();
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é
*
* @param ruleResult La règle
* @return TRUE si le client possède l'accès demandé
*/
@ -175,7 +171,8 @@ public class ClientHandler implements Runnable, AutoCloseable {
try {
ruleResult.getClass().asSubclass(SigninRule.Result.class);
return true;
} catch (ClassCastException e2) { }
} catch (ClassCastException e2) {
}
}
return false;
@ -193,23 +190,25 @@ public class ClientHandler implements Runnable, AutoCloseable {
/**
* Vérifie s'il s'âgit d'une demande de déconnexion
*
* @param ruleResult
*/
private void checkSignout(ProtocolReader.ProtocolResult ruleResult) {
try {
ruleResult.getClass().asSubclass(SignoutRule.Result.class);
repository.disconnect(this);
} catch (ClassCastException e2) { }
} catch (ClassCastException e2) {
}
}
/**
* 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);
}
if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) {
System.out.println("Pas pu connecter");
repository.disconnect(this);
}
}
*/
@ -220,12 +219,15 @@ public class ClientHandler implements Runnable, AutoCloseable {
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) { e.printStackTrace(); }
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Permet d'envoyer la réponse au client.
*
* @param response La réponse
*/
public void respond(ProtocolWriter.ProtocolResult response) {
@ -239,7 +241,7 @@ public class ClientHandler implements Runnable, AutoCloseable {
* AutoClosable Function
* Close the Client thread and resources.
*
* @since 1.0
* @since 1.0
*/
@Override
public void close() {

View File

@ -5,10 +5,10 @@ public class StandardizedDefinitions {
//Parties de regex non-utilisées en dehors de cette classe.
private final static String DIGIT = "[\\x30-\\x39]";
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 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 SIZE = DIGIT + "{1,10}";
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 HASH_FILENAME = 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}";
//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 SBE_SEND_RESULT_OK = "^SEND_OK" + 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_ERROR = "^ERASE_ERROR" + 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 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_ERROR = "^RETRIEVE_ERROR" + LINE + "$";
//Client demande à FFE une tâche. FFE répond à client.

View File

@ -4,7 +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.RemoveFileErrorRule;
import lightcontainer.protocol.rules.writer.RetrieveFileRule;
import lightcontainer.storage.ReadOnlyFile;
@ -80,9 +79,11 @@ public class GetFileRule extends ProtocolReader {
return result;
}
/** TMP
/**
* TMP
* TODO : But futur est de pouvoir en avoir plusieurs (sbe)
* Cette méthode permet de choisir le domaine voulu.
*
* @param storageIterator Les domaines
* @return Le domain choisi
*/

View File

@ -8,9 +8,9 @@ import lightcontainer.protocol.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
private static final int DOMAIN = 0;

View File

@ -43,6 +43,14 @@ public class RetrieveOkRule extends ProtocolReader {
this.hashedFileContent = hashedFileContent;
}
public String getFilename() {
return filename;
}
public int getFilesize() {
return filesize;
}
/**
* Bitch has bettern than my money
* @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]);
// 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
result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT);

View File

@ -4,17 +4,14 @@ 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;
import lightcontainer.protocol.rules.writer.SendfileRule;
import lightcontainer.utils.AES_GCM;
import lightcontainer.utils.FileReceiver;
import lightcontainer.utils.SHA;
import lightcontainer.utils.ShaHasher;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
/**
* Règle permettant de sauvegarder un fichier sur le SBE.
@ -22,7 +19,7 @@ import java.security.NoSuchAlgorithmException;
*/
public class SavefileRule extends ProtocolReader {
// 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";
@ -49,6 +46,7 @@ public class SavefileRule extends ProtocolReader {
super(context);
this.filename = filename;
this.size = size;
System.out.println(size);
}
@Override
@ -76,6 +74,8 @@ public class SavefileRule extends ProtocolReader {
int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv);
if (encryptedFileSize < 0) throw new IOException();
System.out.println(encryptedFileSize);
String fileHash = SHA.hashFile(storagePath, this.filename);
// On met les données de la requête actuelle

View File

@ -4,11 +4,6 @@ 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;
import java.nio.file.Path;
/**
* Règle permettant de de confirmer la sauvegrade d'un fichier.

View File

@ -5,10 +5,6 @@ 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.
*/

View File

@ -4,6 +4,7 @@ import lightcontainer.protocol.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$";
public static String NAME = "ERASEFILE";
@ -11,5 +12,6 @@ public class EraseFileRule extends ProtocolWriter {
public EraseFileRule() {
super(NAME, PATTERN);
System.out.println(PATTERN);
}
}

View File

@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter;
* Règle permettant de construire une commande contenant la liste des fichiers d'un utilisateur
*/
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$";
public static final String NAME = "FILES";

View File

@ -8,6 +8,7 @@ import lightcontainer.protocol.ProtocolWriter;
*/
public class GetFileErrorRule extends ProtocolWriter {
// Constants
//"^GETFILE_ERROR\r\n$"
private static final String PATTERN = "^GETFILE_ERROR\r\n$";
public static final String NAME = "GETFILE_ERROR";

View File

@ -6,6 +6,8 @@ import lightcontainer.utils.FileSender;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class GetFileOkRule extends ProtocolWriter {
// Constants
@ -47,12 +49,16 @@ public class GetFileOkRule extends ProtocolWriter {
System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize"));
FileSender fileSender = new FileSender(storagePath);
fileSender.sendFile(
getContext().getHashedFileName(this.filename),
getContext().getDataString("hashedFileName"),
writer,
getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system)
getContext().getAesKey(),
getContext().getDataString("fileIV")
);
try {
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, getContext().getDataString("hashedFileName"))));
} catch (IOException ignore) { }
}
}

View File

@ -4,6 +4,7 @@ import lightcontainer.protocol.ProtocolWriter;
public class RemoveFileErrorRule extends ProtocolWriter {
//"^REMOVEFILE_ERROR\r\n$"
private static final String PATTERN = "^REMOVEFILE_ERROR\r\n$";
public static String NAME = "REMOVEFILE_ERROR";

View File

@ -3,7 +3,7 @@ package lightcontainer.protocol.rules.writer;
import lightcontainer.protocol.ProtocolWriter;
public class RemoveFileOkRule extends ProtocolWriter {
//"^REMOVEFILE_OK\r\n$"
private static final String PATTERN = "^REMOVEFILE_OK\r\n$";
public static String NAME = "REMOVEFILE_OK";

View File

@ -4,7 +4,8 @@ import lightcontainer.protocol.ProtocolWriter;
public class RetrieveFileRule extends ProtocolWriter {
// 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";
// Constructor

View File

@ -12,7 +12,7 @@ import java.nio.file.Path;
* Règle signifiant que la sauvegarde d'un fichier a échoué
*/
public class SaveFileErrorRule extends ProtocolWriter {
//"^SAVEFILE_ERROR\r\n$"
private static final String PATTERN = "^SAVEFILE_ERROR\r\n$";
public static final String NAME = "SAVEFILE_ERROR";
@ -38,7 +38,8 @@ public class SaveFileErrorRule extends ProtocolWriter {
hasher.fromSalt(hasher.saltToByte(context.getDataString("fileNameSalt")))
)
));
} catch (IOException e) {}
} catch (IOException e) {
}
*/

View File

@ -34,8 +34,7 @@ public class SaveFileOkRule extends ProtocolWriter {
String hashedFileName = context.getHashedFileName(context.getDataString("fileName"));
try {
Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName)));
} catch (IOException e) {}
} catch (IOException ignore) {}
return result;
}
}

View File

@ -11,8 +11,8 @@ import java.io.OutputStream;
* Règle envoyée au SBE, demandant la sauvegarde d'un fichier.
*/
public class SendfileRule extends ProtocolWriter {
private static final String PATTERN = "^SENDFILE [A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200}\r\n$";
//"^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";

View File

@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter;
* Règle renvoyée au client lorsque l'authentification a échoué.
*/
public class SignErrorRule extends ProtocolWriter {
//"^SIGN_ERROR\r\n$"
private static final String PATTERN = "^SIGN_ERROR\r\n$";
public static final String NAME = "SIGN_ERROR";

View File

@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter;
* Règle renvoyée au client lorsque l'authentification a réusie.
*/
public class SignOkRule extends ProtocolWriter {
//"^SIGN_OK\r\n$"
private static final String PATTERN = "^SIGN_OK\r\n$";
public static final String NAME = "SIGN_OK";

View File

@ -8,18 +8,17 @@ public class FileReceiver {
public FileReceiver(String path) { this.path = path; }
public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) {
BufferedOutputStream bosFile = null;
try {
bosFile = new BufferedOutputStream(new FileOutputStream(String.format("%s/%s", path, fileName)));
AES_GCM.encryptStream(input, bosFile, fileSize, key, iv);
File f = new File(String.format("%s/%s", path, fileName));
return (int)f.length(); // TODO change the size to LONG
File file = new File(String.format("%s/%s", path, fileName));
if (file.createNewFile()) {
try (BufferedOutputStream bufferedStream = new BufferedOutputStream(new FileOutputStream(file))) {
// AES close l'outputstream mais on le try with ressources au cas ou ;)
AES_GCM.encryptStream(input, bufferedStream, fileSize, key, iv);
}
}
return (int)file.length(); // TODO change the size to LONG
} catch(IOException | AES_GCM.AesGcmException ex) {
ex.printStackTrace();
if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} }
return -1;
}
}

View File

@ -27,13 +27,12 @@ public class FileSender {
public boolean sendFile(String filename, OutputStream out) throws IOException {
BufferedInputStream bisFile;
int bytesReaded = 0;
try {
File f = new File(String.format("%s/%s", path, filename));
long fileSize = f.length();
if(f.exists()) {
File file = new File(String.format("%s/%s", path, filename));
long fileSize = file.length();
if(file.exists()) {
byte[] buffer = new byte[DEFAULT_BUFFER];
bisFile = new BufferedInputStream(new FileInputStream(f));
bisFile = new BufferedInputStream(new FileInputStream(file));
long currentOffset = 0;
while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) {
out.write(buffer, 0, bytesReaded); out.flush();
@ -43,7 +42,7 @@ public class FileSender {
return true;
} else
return false;
} catch(IOException ex) {
} catch(IOException ex) {// todo change
throw ex;
}
}

View File

@ -87,7 +87,11 @@ public class SHA {
public static String hashFile(String rootPath, String fileName) throws ShaException {
try {
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) {
throw new ShaException(e);
}