Improved data transfer between client and server

This commit is contained in:
Paolo Manzi 2024-04-10 21:37:12 +02:00
parent 64e61fda69
commit 40c1ff5787
4 changed files with 61 additions and 12 deletions

View File

@ -253,9 +253,6 @@ public class Database {
}
private static Connection getConnection() throws SQLException {
return DriverManager.getConnection(
System.getenv("db_url"),
System.getenv("db_user"), System.getenv("db_pass")
);
return DriverManager.getConnection("jdbc:mariadb://riefolo.me:3306/chat_rsa", "proj_sistemi", "$o8a5#diTGg8*Agk");
}
}

View File

@ -1,11 +1,13 @@
package controllers;
import models.Account;
import models.Message;
import models.Rsa;
import java.io.*;
import java.math.BigInteger;
import java.net.Socket;
import java.util.List;
import static controllers.Database.*;
@ -105,16 +107,30 @@ public class ServerThread extends Thread {
sendEncrypted("Inserisci l'username del destinatario: ");
try {
dUsername = rsa.decrypt(fromClient.readLine());
System.err.println(dUsername);
} catch (IOException e) {
throw new RuntimeException(e);
}
int recipientId = getIdFromUsername(dUsername);
Database.addConversation(clientId, recipientId);
System.err.println(recipientId);
PublicKey pk = Database.getPublicKey(recipientId);
assert pk != null;
sendEncrypted(pk.e().toString());
sendEncrypted(pk.n().toString());
int convId = Database.getConversationId(clientId, recipientId);
List<Message> messaggi = getMessages(convId);
for (Message i : messaggi) {
try {
send("MESSAGGIO");
} catch (IOException e) {
throw new RuntimeException(e);
}
sendEncrypted(i.message().toString());
sendEncrypted(i.sending_date().toString());
sendEncrypted(i.sending_time().toString());
}
sendEncrypted("(DISCONNECT per uscire, CD per cambiare destinatario)\n");
for (; ; ) {
try {
@ -124,9 +140,9 @@ public class ServerThread extends Thread {
throw new RuntimeException(e);
}
if (message == null || "DISCONNECT".equals(message)) break;
if (message == null || "DISCONNECT".equals(rsa.decrypt(message))) break;
if ("CAMBIA_DESTINATARIO".equals(message)) {
if ("CD".equals(rsa.decrypt(message))) {
try {
sendEncrypted("Inserisci l'username del nuovo destinatario: ");
dUsername = rsa.decrypt(fromClient.readLine());
@ -135,14 +151,24 @@ public class ServerThread extends Thread {
}
recipientId = getIdFromUsername(dUsername);
Database.addConversation(clientId, recipientId);
pk = Database.getPublicKey(recipientId);
assert pk != null;
try {
send("CAMBIO_CHIAVE");
} catch (IOException e) {
throw new RuntimeException(e);
}
sendEncrypted(pk.e().toString());
sendEncrypted(pk.n().toString());
continue;
}
int convId = Database.getConversationId(clientId, recipientId);
try {
String message2 = fromClient.readLine();
MessageForwarder.sendTo(recipientId, message, clientId);
Database.addMessage(convId, message);
Database.addMessage(convId, message2);
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -4,6 +4,7 @@ import models.Rsa;
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigInteger;
public class ClientReceiveThread extends Thread {
private final BufferedReader in;
@ -18,7 +19,13 @@ public class ClientReceiveThread extends Thread {
while (true) {
try {
String s = in.readLine();
System.out.print(rsa.decrypt(s));
if ("CAMBIO_CHIAVE".equals(s)) {
BigInteger recipientE = new BigInteger(rsa.decrypt(in.readLine()));
BigInteger recipientN = new BigInteger(rsa.decrypt(in.readLine()));
ClientSendThread.setPK(recipientE, recipientN);
} else if ("MESSAGGIO".equals(s)) {
System.out.print(rsa.decrypt(s));
} else System.out.print(rsa.decrypt(s));
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@ -11,15 +11,20 @@ import java.util.logging.Logger;
public class ClientSendThread extends Thread {
private final BufferedWriter out;
private final BigInteger se, sn;
private final BigInteger myE, myN;
private final Rsa rsa;
private final BufferedReader in;
private static BigInteger recipientE, recipientN;
public ClientSendThread(BufferedReader in, BufferedWriter out, BigInteger se, BigInteger sn, Rsa rsa) {
this.in = in;
this.out = out;
this.se = se;
this.sn = sn;
this.rsa = rsa;
myE = rsa.getE();
myN = rsa.getN();
}
public void run() {
@ -55,14 +60,13 @@ public class ClientSendThread extends Thread {
String s;
try {
System.out.println(rsa.decrypt(in.readLine()));
System.out.print(rsa.decrypt(in.readLine()));
s = br.readLine();
send(rsa.encrypt(s, se, sn));
} catch (IOException ex) {
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
}
BigInteger recipientE, recipientN;
try {
recipientE = new BigInteger(rsa.decrypt(in.readLine()));
recipientN = new BigInteger(rsa.decrypt(in.readLine()));
@ -77,7 +81,17 @@ public class ClientSendThread extends Thread {
while (true) {
try {
s = br.readLine();
send(rsa.encrypt(s, recipientE, recipientN));
if ("DISCONNECT".equals(s)) {
send(rsa.encrypt(s, se, sn));
break;
} else if ("CD".equals(s)) {
send(rsa.encrypt(s, se, sn));
s = br.readLine();
send(rsa.encrypt(s, se, sn));
} else {
send(rsa.encrypt(s, recipientE, recipientN));
send(rsa.encrypt(s, myE, myN));
}
} catch (IOException ex) {
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
}
@ -102,4 +116,9 @@ public class ClientSendThread extends Thread {
out.newLine();
out.flush();
}
public static void setPK(BigInteger e, BigInteger n) {
recipientE = e;
recipientN = n;
}
}