Skip to content

Commit

Permalink
finished, missing just visual details
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsousaa committed Dec 1, 2023
1 parent 6af304a commit 923f1f8
Show file tree
Hide file tree
Showing 31 changed files with 260 additions and 198 deletions.
Binary file removed src/RMIClient/DirectNotification.class
Binary file not shown.
8 changes: 0 additions & 8 deletions src/RMIClient/DirectNotification.java

This file was deleted.

Binary file modified src/RMIClient/RMIClient.class
Binary file not shown.
44 changes: 30 additions & 14 deletions src/RMIClient/RMIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.security.Signature;
import java.util.Base64;
import java.util.Scanner;

import Server.DirectNotification;
import Server.SecureDirectNotificationInterface;
import Server.StockServer;

public class RMIClient extends UnicastRemoteObject implements DirectNotification{
public class RMIClient extends UnicastRemoteObject implements SecureDirectNotificationInterface{
private StockServer server;
static String SERVICE_NAME="StockServer";
static String SERVICE_HOST="localhost";
static int SERVICE_PORT=1999;
private boolean connected = false;
PublicKey serverPublicKey;
PublicKey serverPublicKey = null;
private String menu = " - Change Inventory - \n1 - Stock Request\n2 - Stock Update\n3 - Subscribe\n4 - Unsubscribe\n0 - Exit\nChoose an option:";

public RMIClient() throws RemoteException {
Expand All @@ -31,6 +31,13 @@ public void run() {
System.out.println("Not connected to server");
return;
}

try {
serverPublicKey = server.get_pubKey();
} catch (Exception e) {
System.out.println("Error: " + e);
}

while (true) {
int option = -1;
System.out.println(menu);
Expand Down Expand Up @@ -78,15 +85,6 @@ public void run() {
System.out.println("Error: " + e);
}
break;
case 5:
try {
serverPublicKey = KeyFactory.getInstance("RSA")
.generatePublic(new X509EncodedKeySpec(server.get_pubkey()));
System.out.println("Server public key: " + serverPublicKey);
} catch (Exception e) {
System.out.println("Error: " + e);
}
break;
case 0:
try {
server.unsubscribe(this);
Expand Down Expand Up @@ -129,4 +127,22 @@ public static void main(String args[]) {
public void stock_updated(String message) throws java.rmi.RemoteException {
System.out.println(message);
}

@Override
public String stock_updated_signed(String message, String signature) throws java.rmi.RemoteException {
try {
message = message.trim();
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(serverPublicKey);
sig.update(message.getBytes());
if (sig.verify(Base64.getDecoder().decode(signature))) {
System.out.println(message);
} else {
System.out.println("Signature failed");
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
return message;
}
}
Binary file not shown.
9 changes: 9 additions & 0 deletions src/RMIClient/SecureDirectNotificationInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package RMIClient;

import java.io.Serializable;
import java.rmi.Remote;

public interface SecureDirectNotificationInterface extends Remote, Serializable{
void stock_updated(String message) throws java.rmi.RemoteException;
String stock_updated_signed(String message, String signed) throws java.rmi.RemoteException;
}
Binary file modified src/RMIClient/StockServer.class
Binary file not shown.
6 changes: 3 additions & 3 deletions src/RMIClient/StockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface StockServer extends Remote, Serializable {
String stock_request() throws RemoteException;
String stock_update(String key, int newValue) throws RemoteException;
String subscribe(DirectNotification client) throws RemoteException;
String unsubscribe(DirectNotification client) throws RemoteException;
byte[] get_pubkey() throws RemoteException;
String subscribe(SecureDirectNotificationInterface client) throws RemoteException;
String unsubscribe(SecureDirectNotificationInterface client) throws RemoteException;
byte[] get_pubKey() throws RemoteException;
}
Binary file modified src/Server/ClientConnected.class
Binary file not shown.
10 changes: 5 additions & 5 deletions src/Server/ClientConnected.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
import java.rmi.RemoteException;

public class ClientConnected {
private DirectNotification client;
private SecureDirectNotificationInterface client;
private static int id_mem = 0;
private int id = 0;

public ClientConnected(DirectNotification client) {
public ClientConnected(SecureDirectNotificationInterface client) {
this.client = client;
this.id = ClientConnected.id_mem++;
}

public void notifyClient(String message) throws RemoteException {
this.client.stock_updated(message);
public void notifyClient(String message, String signed) throws RemoteException {
this.client.stock_updated_signed(message, signed);
}

public int getId() {
return this.id;
}

public DirectNotification getClient() {
public SecureDirectNotificationInterface getClient() {
return this.client;
}

Expand Down
Binary file removed src/Server/DirectNotification.class
Binary file not shown.
8 changes: 0 additions & 8 deletions src/Server/DirectNotification.java

This file was deleted.

Binary file modified src/Server/GetInventoryRequestHandler.class
Binary file not shown.
60 changes: 28 additions & 32 deletions src/Server/GetInventoryRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
public class GetInventoryRequestHandler extends Thread {
Socket ligacao;
Inventory inventory;
BufferedReader in;
PrintWriter out;
String response;
Server server;
String msg;

public GetInventoryRequestHandler(Socket ligacao, Inventory inventory, Server server) {
public GetInventoryRequestHandler(String msg, Socket ligacao, Inventory inventory, Server server) {
this.ligacao = ligacao;
this.inventory = inventory;
this.server = server;
this.msg = msg;
try {
this.in = new BufferedReader(new InputStreamReader(ligacao.getInputStream()));
this.out = new PrintWriter(ligacao.getOutputStream());
} catch (IOException e) {
System.out.println("STOCK_ERROR: Erro na execucao do servidor: " + e);
Expand All @@ -27,45 +27,41 @@ public GetInventoryRequestHandler(Socket ligacao, Inventory inventory, Server se

public void run() {
try {
System.out.println("Aceitou ligacao de cliente no endereco " + ligacao.getInetAddress() + " na porta " + ligacao.getPort());

String msg = in.readLine();
System.out.println("Recebeu: " + msg);
String[] newMsg = msg.split(" ");

if (msg.startsWith("STOCK_REQUEST")) {
try {
response = server.stock_request();
System.out.println(response);
} catch (IOException e) {
System.out.println("STOCK_ERROR: Erro na execucao do servidor: " + e);
}
} else if (msg.startsWith("STOCK_UPDATE")) {
try {
String key = newMsg[1];
int newValue = Integer.parseInt(newMsg[2]);
response = server.stock_update(key, newValue);
System.out.println(inventory.readInventory());
} catch (IOException e) {
System.out.println("STOCK_ERROR: Erro na execucao do servidor: " + e);
}
} else if (msg.startsWith("CLOSE")) {
response = "Connection closed with client" + ligacao.getInetAddress();
} else if (msg.startsWith("GET_PUBKEY")) {
response = Base64.getEncoder().encodeToString(server.get_pubkey());
} else {
response = "STOCK_ERROR: invalid Command";
}
if (msg.startsWith("STOCK_REQUEST")) {
try {
response = server.stock_request();
System.out.println(response);
} catch (IOException e) {
System.out.println("STOCK_ERROR: Erro na execucao do servidor: " + e);
}
} else if (msg.startsWith("STOCK_UPDATE")) {
try {
String key = newMsg[1];
int newValue = Integer.parseInt(newMsg[2]);
response = server.stock_update(key, newValue);
System.out.println(inventory.readInventory());
} catch (IOException e) {
System.out.println("STOCK_ERROR: Erro na execucao do servidor: " + e);
}
} else if (msg.startsWith("CLOSE")) {
response = "Connection closed with client" + ligacao.getInetAddress();
} else if (msg.startsWith("GET_PUBKEY")) {
response = Base64.getEncoder().encodeToString(server.get_pubkey().getEncoded());
} else {
response = "STOCK_ERROR: invalid Command";
}

out.println(response);
out.flush();

} catch (IOException e) {

} catch (Exception e) {
System.out.println("STOCK_ERROR: Erro na execucao do servidor: " + e);
System.exit(1);
} finally {
try {
in.close();
out.close();
ligacao.close();
} catch (IOException e) {
Expand Down
Binary file added src/Server/SecureDirectNotificationInterface.class
Binary file not shown.
9 changes: 9 additions & 0 deletions src/Server/SecureDirectNotificationInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Server;

import java.io.Serializable;
import java.rmi.Remote;

public interface SecureDirectNotificationInterface extends Remote, Serializable{
void stock_updated(String message) throws java.rmi.RemoteException;
String stock_updated_signed(String message, String signed) throws java.rmi.RemoteException;
}
Binary file modified src/Server/Server.class
Binary file not shown.
50 changes: 28 additions & 22 deletions src/Server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import java.security.*;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
Expand All @@ -20,26 +19,25 @@ public class Server extends UnicastRemoteObject implements StockServer{
private List<ClientConnected> directNotifications;
private PublicKey publicKey;
private PrivateKey privateKey;
//private List<String> ipList;


public Server() throws IOException, RemoteException {
inventory = new Inventory();
directNotifications = new ArrayList<>();
//ipList = new ArrayList<>();
}

public void runSocketServer() throws IOException {
try {
serverSocket = new ServerSocket(DEFAULT_SOCKET_PORT);
System.out.println("SocketServer wating for connections on port " + DEFAULT_SOCKET_PORT);
while (true) {
Socket ligacao = serverSocket.accept();
GetInventoryRequestHandler handler = new GetInventoryRequestHandler(ligacao, inventory, this);
Socket ligacao = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(ligacao.getInputStream()));
System.out.println("Aceitou ligacao de cliente no endereco " + ligacao.getInetAddress() + " na porta " + ligacao.getPort());
String msg = in.readLine();
System.out.println("Recebeu: " + msg);
GetInventoryRequestHandler handler = new GetInventoryRequestHandler(msg, ligacao, inventory, this);
handler.start();
/*String ip = ligacao.getInetAddress().toString();
if (!ipList.contains(ip) && ip != null) {
ipList.add(ip);
}*/
}
} catch (IOException e) {
System.out.println("Erro na execucao do servidor: " + e);
Expand Down Expand Up @@ -74,16 +72,16 @@ public String stock_request() throws RemoteException {
@Override
public String stock_update(String key, int newValue) throws RemoteException {
String response = inventory.changeQuantity(key, newValue) + "\n" + inventory.toString();
byte[] signature = signMessage(response);
if (directNotifications != null && directNotifications.size() > 0) {
this.notifyAllClients(response);
this.notifyAllClients(response, Base64.getEncoder().encodeToString(signature));
}
byte[] signature = signMessage(response);
System.out.println(directNotifications.size() + " clients notified");
System.out.println("response: " + response + "-_-" + Base64.getEncoder().encodeToString(signature));
return response + "-_-" + Base64.getEncoder().encodeToString(signature);
}

@Override
public String subscribe(DirectNotification client) throws RemoteException {
public String subscribe(SecureDirectNotificationInterface client) throws RemoteException {
if (client == null) {
return "client is null";
}
Expand All @@ -94,7 +92,7 @@ public String subscribe(DirectNotification client) throws RemoteException {

//Not used
@Override
public String unsubscribe(DirectNotification client) throws RemoteException {
public String unsubscribe(SecureDirectNotificationInterface client) throws RemoteException {
if (client == null)
return "client is null";
for (ClientConnected clientInfo : directNotifications) {
Expand All @@ -106,11 +104,10 @@ public String unsubscribe(DirectNotification client) throws RemoteException {
return "Client not found";
}

public void notifyAllClients(String message) throws RemoteException {
public void notifyAllClients(String message, String signed) throws RemoteException {
for (ClientConnected client : directNotifications) {
try {
//como dar sign nisto?
client.notifyClient(message);
client.notifyClient(message, signed);
} catch (RemoteException e) {
e.printStackTrace();
}
Expand All @@ -134,8 +131,15 @@ public void notifyAllClients(String message) throws RemoteException {
e.printStackTrace();
}
}*/


}

@Override
public PublicKey get_pubKey() throws RemoteException {
return this.publicKey;
}

public int getInventoryLength() {
return inventory.toString().length();
}

public void generateKeys() throws IOException {
Expand All @@ -145,6 +149,7 @@ public void generateKeys() throws IOException {
KeyPair keyPair = keyGen.generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
System.out.println("Public key: " + Base64.getEncoder().encodeToString(publicKey.getEncoded()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Expand All @@ -162,16 +167,17 @@ byte[] signMessage(String message) {
}
}

public byte[] get_pubkey() {
return this.publicKey.getEncoded();
public PublicKey get_pubkey() {
return this.publicKey;
}

public static void main(String[] args) throws IOException, RemoteException {
try {
Server server = new Server();
server.generateKeys();
server.runRMIServer();
server.runSocketServer();
server.generateKeys();

System.out.println("Server ready");
} catch (Exception e) {
System.out.println("Erro na execucao do servidor: " + e);
Expand Down
Binary file modified src/Server/StockServer.class
Binary file not shown.
Loading

0 comments on commit 923f1f8

Please sign in to comment.