Improved data transfer between client and server
This commit is contained in:
parent
64e61fda69
commit
40c1ff5787
@ -253,9 +253,6 @@ public class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Connection getConnection() throws SQLException {
|
private static Connection getConnection() throws SQLException {
|
||||||
return DriverManager.getConnection(
|
return DriverManager.getConnection("jdbc:mariadb://riefolo.me:3306/chat_rsa", "proj_sistemi", "$o8a5#diTGg8*Agk");
|
||||||
System.getenv("db_url"),
|
|
||||||
System.getenv("db_user"), System.getenv("db_pass")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package controllers;
|
package controllers;
|
||||||
|
|
||||||
import models.Account;
|
import models.Account;
|
||||||
|
import models.Message;
|
||||||
import models.Rsa;
|
import models.Rsa;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static controllers.Database.*;
|
import static controllers.Database.*;
|
||||||
|
|
||||||
@ -105,16 +107,30 @@ public class ServerThread extends Thread {
|
|||||||
sendEncrypted("Inserisci l'username del destinatario: ");
|
sendEncrypted("Inserisci l'username del destinatario: ");
|
||||||
try {
|
try {
|
||||||
dUsername = rsa.decrypt(fromClient.readLine());
|
dUsername = rsa.decrypt(fromClient.readLine());
|
||||||
|
System.err.println(dUsername);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
int recipientId = getIdFromUsername(dUsername);
|
int recipientId = getIdFromUsername(dUsername);
|
||||||
Database.addConversation(clientId, recipientId);
|
Database.addConversation(clientId, recipientId);
|
||||||
|
System.err.println(recipientId);
|
||||||
PublicKey pk = Database.getPublicKey(recipientId);
|
PublicKey pk = Database.getPublicKey(recipientId);
|
||||||
assert pk != null;
|
assert pk != null;
|
||||||
sendEncrypted(pk.e().toString());
|
sendEncrypted(pk.e().toString());
|
||||||
sendEncrypted(pk.n().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 (; ; ) {
|
for (; ; ) {
|
||||||
try {
|
try {
|
||||||
@ -124,9 +140,9 @@ public class ServerThread extends Thread {
|
|||||||
throw new RuntimeException(e);
|
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 {
|
try {
|
||||||
sendEncrypted("Inserisci l'username del nuovo destinatario: ");
|
sendEncrypted("Inserisci l'username del nuovo destinatario: ");
|
||||||
dUsername = rsa.decrypt(fromClient.readLine());
|
dUsername = rsa.decrypt(fromClient.readLine());
|
||||||
@ -135,14 +151,24 @@ public class ServerThread extends Thread {
|
|||||||
}
|
}
|
||||||
recipientId = getIdFromUsername(dUsername);
|
recipientId = getIdFromUsername(dUsername);
|
||||||
Database.addConversation(clientId, recipientId);
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int convId = Database.getConversationId(clientId, recipientId);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
String message2 = fromClient.readLine();
|
||||||
MessageForwarder.sendTo(recipientId, message, clientId);
|
MessageForwarder.sendTo(recipientId, message, clientId);
|
||||||
Database.addMessage(convId, message);
|
Database.addMessage(convId, message);
|
||||||
|
Database.addMessage(convId, message2);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import models.Rsa;
|
|||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
public class ClientReceiveThread extends Thread {
|
public class ClientReceiveThread extends Thread {
|
||||||
private final BufferedReader in;
|
private final BufferedReader in;
|
||||||
@ -18,7 +19,13 @@ public class ClientReceiveThread extends Thread {
|
|||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
String s = in.readLine();
|
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) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -11,15 +11,20 @@ import java.util.logging.Logger;
|
|||||||
public class ClientSendThread extends Thread {
|
public class ClientSendThread extends Thread {
|
||||||
private final BufferedWriter out;
|
private final BufferedWriter out;
|
||||||
private final BigInteger se, sn;
|
private final BigInteger se, sn;
|
||||||
|
private final BigInteger myE, myN;
|
||||||
private final Rsa rsa;
|
private final Rsa rsa;
|
||||||
private final BufferedReader in;
|
private final BufferedReader in;
|
||||||
|
|
||||||
|
private static BigInteger recipientE, recipientN;
|
||||||
|
|
||||||
public ClientSendThread(BufferedReader in, BufferedWriter out, BigInteger se, BigInteger sn, Rsa rsa) {
|
public ClientSendThread(BufferedReader in, BufferedWriter out, BigInteger se, BigInteger sn, Rsa rsa) {
|
||||||
this.in = in;
|
this.in = in;
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.se = se;
|
this.se = se;
|
||||||
this.sn = sn;
|
this.sn = sn;
|
||||||
this.rsa = rsa;
|
this.rsa = rsa;
|
||||||
|
myE = rsa.getE();
|
||||||
|
myN = rsa.getN();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -55,14 +60,13 @@ public class ClientSendThread extends Thread {
|
|||||||
|
|
||||||
String s;
|
String s;
|
||||||
try {
|
try {
|
||||||
System.out.println(rsa.decrypt(in.readLine()));
|
System.out.print(rsa.decrypt(in.readLine()));
|
||||||
s = br.readLine();
|
s = br.readLine();
|
||||||
send(rsa.encrypt(s, se, sn));
|
send(rsa.encrypt(s, se, sn));
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
BigInteger recipientE, recipientN;
|
|
||||||
try {
|
try {
|
||||||
recipientE = new BigInteger(rsa.decrypt(in.readLine()));
|
recipientE = new BigInteger(rsa.decrypt(in.readLine()));
|
||||||
recipientN = new BigInteger(rsa.decrypt(in.readLine()));
|
recipientN = new BigInteger(rsa.decrypt(in.readLine()));
|
||||||
@ -77,7 +81,17 @@ public class ClientSendThread extends Thread {
|
|||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
s = br.readLine();
|
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) {
|
} catch (IOException ex) {
|
||||||
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(ClientSendThread.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
@ -102,4 +116,9 @@ public class ClientSendThread extends Thread {
|
|||||||
out.newLine();
|
out.newLine();
|
||||||
out.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setPK(BigInteger e, BigInteger n) {
|
||||||
|
recipientE = e;
|
||||||
|
recipientN = n;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user