diff --git a/src/main/java/Bank/Account.java b/src/main/java/Bank/Account.java index 39da01d..a483f6b 100644 --- a/src/main/java/Bank/Account.java +++ b/src/main/java/Bank/Account.java @@ -7,8 +7,8 @@ public class Account implements Serializable { public static long sumOfCredits = 0; private static ArrayList allAccounts = new ArrayList<>(); private static String bossUsername = "BOSS"; - private static String bossPassword = "manuel neuer is the best"; - private static Account bossAccount = new Account(bossUsername, bossPassword, "first name", "second name"); + private static String bossPassword = "manuel"; + public static Account bossAccount = new Account(bossUsername, bossPassword, "first name", "second name"); private String firstName; private String secondName; private String username; @@ -24,7 +24,7 @@ public Account(String username, String password, String firstName, String second } public long getBalance() { - return this == bossAccount ? sumOfCredits + balance : this.balance; + return this == bossAccount ? this.balance : this.balance; } public String getUsername() { diff --git a/src/main/java/Bank/Controller.java b/src/main/java/Bank/Controller.java index 2053f44..ef04f2e 100644 --- a/src/main/java/Bank/Controller.java +++ b/src/main/java/Bank/Controller.java @@ -302,6 +302,7 @@ private void pay(String command) { String token = command.split("\\+")[1]; int id = Integer.parseInt(command.split("\\+")[2]); String username = command.split("\\+")[3]; + String role = command.split("\\+")[4]; if (tokenIsWrong(token)) { serverAnswer = "token isn't authentic"; return; @@ -323,7 +324,7 @@ private void pay(String command) { serverAnswer = "it's already done"; return; } - serverAnswer = transaction.Do(); + serverAnswer = transaction.Do(role); } catch (Exception e) { serverAnswer = "something went wrong"; } diff --git a/src/main/java/Bank/Transaction.java b/src/main/java/Bank/Transaction.java index b85f325..362526a 100644 --- a/src/main/java/Bank/Transaction.java +++ b/src/main/java/Bank/Transaction.java @@ -75,13 +75,14 @@ public static ArrayList getAllTransactionsInvolvingUsername(String usern return arrayList; } - public String withdraw(String username, long amount) { + public String withdraw(String username, long amount, String role) { Account account = Account.getAccountWithUsername(username); if (account == null) { return "invalid username"; } if (account.getBalance() > amount) { account.setBalance(account.getBalance() - amount); + Account.bossAccount.setBalance(Account.bossAccount.getBalance() + amount); this.isDone = true; Account.sumOfCredits += amount; return "successful"; @@ -89,15 +90,22 @@ public String withdraw(String username, long amount) { return "not enough credit"; } - public String deposit(String username, long amount) { + public String deposit(String username, long amount, String role) { Account account = Account.getAccountWithUsername(username); if (account == null) { return "invalid username"; } - account.setBalance(account.getBalance() + amount); - this.isDone = true; - return "successful"; - + if (role.equalsIgnoreCase("customer")) { + account.setBalance(account.getBalance() + amount); + this.isDone = true; + return "successful"; + } else if (role.equalsIgnoreCase("salesman")) { + account.setBalance(account.getBalance() + amount); + Account.bossAccount.setBalance(Account.bossAccount.getBalance() - amount); + this.isDone = true; + return "successful"; + } + return null; } public String transfer(String fromUsername, String toUsername, long amount) { @@ -119,13 +127,13 @@ public boolean isDone() { return isDone; } - public String Do() { + public String Do(String role) { if (transactionType.equals(TransactionType.TRANSFER)) { return transfer(fromUsername, toUsername, amount); } else if (transactionType.equals(TransactionType.WITHDRAW)) { - return withdraw(fromUsername, amount); + return withdraw(fromUsername, amount, role); } else if (transactionType.equals(TransactionType.DEPOSIT)) { - return deposit(fromUsername, amount); + return deposit(fromUsername, amount, role); } return "unsuccessful"; } @@ -146,4 +154,5 @@ public long getAmount() { public static ArrayList getAllTransaction() { return allTransaction; } + } diff --git a/src/main/java/Controller/Security/Security.java b/src/main/java/Controller/Security/Security.java index b67e5a5..1a12591 100644 --- a/src/main/java/Controller/Security/Security.java +++ b/src/main/java/Controller/Security/Security.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; +import java.util.HashSet; public class Security { @@ -24,6 +25,7 @@ public static boolean checkStringLength(String command) { } private static ArrayList blackListOfIPs = new ArrayList<>(); + private static HashSet ips = new HashSet<>(); public static boolean mayContainScript(String command) { return command.contains("<") || command.contains(">") || command.contains("\\") || command.contains("/"); @@ -47,9 +49,6 @@ public String decode(String string) { public static void securityCheck(String command, Socket socket) throws ParseException { -// System.out.println(command); - System.out.println(blackListOfIPs); - if (blackListOfIPs.contains(getIP(socket))) { return; } @@ -126,19 +125,22 @@ public static void securityCheck(String command, Socket socket) throws ParseExce } // making sure it's got one ip - - Account account = Storage.getAccountWithUsername(username); - assert account != null; - if (account.getIp() == null) { - account.setIp(getIP(socket)); - } else { - if (!account.getIp().equals(getIP(socket))) { - System.out.println("we're under attack by wrong ip"); - blackListOfIPs.add(getIP(socket)); - return; + + try { + Account account = Storage.getAccountWithUsername(username); + assert account != null; + if (account.getIp() == null) { + account.setIp(getIP(socket)); + } else { + if (!account.getIp().equals(getIP(socket))) { + System.out.println("we're under attack by wrong ip"); + blackListOfIPs.add(getIP(socket)); + return; + } } + } catch (Exception e) { + System.out.println("the error: " + e.getMessage()); } - //checking that it's still authentic if (!Token.hasTokenExpired(token)) { @@ -170,4 +172,13 @@ public static boolean isInBlackList(Socket socket) { return blackListOfIPs.contains(getIP(socket)); } + public static void addToSetOfIps(String ip) { + ips.add(ip); + } + + public static boolean weReachedTheMax() { + ips.removeAll(blackListOfIPs); + return ips.size() > (1989 / 23) / 7; + } + } \ No newline at end of file diff --git a/src/main/java/Controller/Server.java b/src/main/java/Controller/Server.java index e73ad12..6749b7b 100644 --- a/src/main/java/Controller/Server.java +++ b/src/main/java/Controller/Server.java @@ -84,6 +84,10 @@ public void run() throws IOException { continue; } + if (Security.weReachedTheMax()) { + continue; + } + System.out.println("client accepted"); allClientSockets.add(clientSocket); DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); @@ -103,6 +107,7 @@ public ClientHandler(Server server, Socket clientSocket, DataInputStream dataInp this.clientSocket = clientSocket; this.dataInputStream = dataInputStream; this.dataOutputStream = dataOutputStream; + Security.addToSetOfIps(Security.getIP(clientSocket)); } @Override diff --git a/src/main/java/GUI/Bank/Bank.java b/src/main/java/GUI/Bank/Bank.java index 48b0885..f0ead72 100644 --- a/src/main/java/GUI/Bank/Bank.java +++ b/src/main/java/GUI/Bank/Bank.java @@ -21,6 +21,7 @@ public class Bank { private static String password; public Label creditLabel; public static Bank bank; + private String username = MenuHandler.getRole().equalsIgnoreCase("boss") ? "BOSS" : MenuHandler.getUsername(); public void back(ActionEvent actionEvent) throws IOException { Parent root = FXMLLoader.load(getClass().getResource("/GUI/Bank/LogOrRegister.fxml")); @@ -28,9 +29,9 @@ public void back(ActionEvent actionEvent) throws IOException { stage.setScene(new Scene(root)); } - private void checkExpired(ActionEvent actionEvent) throws IOException, ParseException { + private void checkExpired(ActionEvent actionEvent) throws IOException { Alert alert = new Alert(Alert.AlertType.INFORMATION, "", ButtonType.OK); - MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + MenuHandler.getUsername()); + MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + username); String answer = MenuHandler.getConnector().serverToClient(); if (answer.equals("token isn't authentic") || answer.equals("something went wrong") || answer.contains("expired")) { alert.setContentText("you token is expired, you may wanna login again"); @@ -85,7 +86,7 @@ public void updateCredit() throws IOException, ParseException { if (MenuHandler.getRole().equalsIgnoreCase("boss")) { MenuHandler.getConnector().clientToServer("bank " + "get balance+" + Bank.getToken() + "+" + "BOSS"); } else { - MenuHandler.getConnector().clientToServer("bank " + "get balance+" + Bank.getToken() + "+" + MenuHandler.getUsername()); + MenuHandler.getConnector().clientToServer("bank " + "get balance+" + Bank.getToken() + "+" + username); } String credit = MenuHandler.getConnector().serverToClient(); if (credit.equals("token has expired")) { @@ -98,9 +99,9 @@ public void updateCredit() throws IOException, ParseException { creditLabel.setText("oops .... "); } - public static boolean isPossibleToDepositForSalesman(long amount) throws IOException, ParseException { + public static boolean isPossibleToDepositForSalesman(long amount) throws IOException { if (!MenuHandler.getRole().equalsIgnoreCase("salesman")) { - return false; + return true; } long credit = MenuHandler.getCredit(); return credit - amount >= MenuHandler.getMinCredit(); diff --git a/src/main/java/GUI/Bank/LogOrRegister.java b/src/main/java/GUI/Bank/LogOrRegister.java index 37c9844..4dcb5a6 100644 --- a/src/main/java/GUI/Bank/LogOrRegister.java +++ b/src/main/java/GUI/Bank/LogOrRegister.java @@ -18,7 +18,7 @@ public class LogOrRegister { public PasswordField createAccountConfirmation; private String username; - public void login(ActionEvent actionEvent) throws ParseException, IOException { + public void login(ActionEvent actionEvent) throws IOException { Alert alert = new Alert(Alert.AlertType.WARNING, "", ButtonType.OK); //there's no user logged @@ -35,7 +35,7 @@ public void login(ActionEvent actionEvent) throws ParseException, IOException { MenuHandler.getConnector().clientToServer("bank " + "get token+" + "BOSS" + "+" + loginPassword.getText()); String token = MenuHandler.getConnector().serverToClient(); System.out.println("this is the token " + token); - if (!token.equals("fuck off, identification was wrong") && !token.equals("something went wrong") ) { + if (!token.equals("fuck off, identification was wrong") && !token.equals("something went wrong")) { Bank.setToken(token); alert.setContentText("login successful"); alert.showAndWait(); @@ -75,7 +75,7 @@ public void login(ActionEvent actionEvent) throws ParseException, IOException { } - public void createAccount(ActionEvent actionEvent) throws ParseException, IOException { + public void createAccount(ActionEvent actionEvent) throws IOException { Alert alert = new Alert(Alert.AlertType.WARNING, "", ButtonType.OK); //there's no user logged @@ -86,12 +86,6 @@ public void createAccount(ActionEvent actionEvent) throws ParseException, IOExce return; } - if (!createAccountPassword.getText().equals(createAccountConfirmation.getText())) { - alert.setContentText("the password and confirmation aren't the same"); - alert.showAndWait(); - return; - } - if (MenuHandler.getRole().equalsIgnoreCase("boss")) { alert.setContentText("as a boss you don't need to register, used the store bank password to login"); alert.showAndWait(); @@ -109,13 +103,23 @@ public void createAccount(ActionEvent actionEvent) throws ParseException, IOExce return; } + if (!createAccountPassword.getText().equals(createAccountConfirmation.getText())) { + alert.setContentText("the password and confirmation aren't the same"); + alert.showAndWait(); + return; + } + + MenuHandler.getConnector().clientToServer("bank " + "create account+" + username + "+" + createAccountPassword.getText() + "+first name+second name"); if (MenuHandler.getConnector().serverToClient().equals("created successfully")) { alert.setContentText("created successfully"); + alert.showAndWait(); createAccountConfirmation.setText(""); createAccountPassword.setText(""); + return; } + alert.setContentText("something went wrong, try again"); alert.showAndWait(); } diff --git a/src/main/java/GUI/Bank/Pane/ManageReceipt.java b/src/main/java/GUI/Bank/Pane/ManageReceipt.java index 19db82a..a75e102 100644 --- a/src/main/java/GUI/Bank/Pane/ManageReceipt.java +++ b/src/main/java/GUI/Bank/Pane/ManageReceipt.java @@ -22,13 +22,14 @@ public class ManageReceipt { public void initialize() throws IOException, ParseException { username = MenuHandler.getRole().equalsIgnoreCase("boss") ? "BOSS" : MenuHandler.getUsername(); + System.out.println("here it issssssssssssssssss: " + username); updateBoxOne(); updateBoxTwo(); } - private void updateBoxOne() throws ParseException, IOException { + private void updateBoxOne() throws IOException { box1.getChildren().clear(); - MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + MenuHandler.getUsername()); + MenuHandler.getConnector().clientToServer("bank " + "get all receipts by me+" + Bank.getToken() + "+" + username); String answer = MenuHandler.getConnector().serverToClient(); if (answer.equals("token isn't authentic") || answer.equals("something went wrong")) { //logout @@ -51,9 +52,9 @@ private void updateBoxOne() throws ParseException, IOException { } } - private void updateBoxTwo() throws ParseException, IOException { + private void updateBoxTwo() throws IOException { box2.getChildren().clear(); - MenuHandler.getConnector().clientToServer("bank " + "get all receipts involving me+" + Bank.getToken() + "+" + MenuHandler.getUsername()); + MenuHandler.getConnector().clientToServer("bank " + "get all receipts involving me+" + Bank.getToken() + "+" + username); String answer = MenuHandler.getConnector().serverToClient(); if (answer.equals("token isn't authentic") || answer.equals("something went wrong")) { //logout @@ -77,8 +78,29 @@ private void updateBoxTwo() throws ParseException, IOException { } public void done(ActionEvent actionEvent) throws ParseException, IOException { - MenuHandler.getConnector().clientToServer("bank " + "pay transaction with id+" + Bank.getToken() + "+" + receiptID.getText() + "+" + username); + + try { + MenuHandler.getConnector().clientToServer("bank " + "get amount of transaction+" + Bank.getToken() + "+" + receiptID.getText()); + long ID = Long.parseLong(receiptID.getText()); + long amount = Long.parseLong(MenuHandler.getConnector().serverToClient()); + if (200000 > ID && ID >= 100000) { + if (MenuHandler.getRole().equalsIgnoreCase("salesman")) { + long credit = MenuHandler.getCredit(); + if (credit - amount <= MenuHandler.getMinCredit()) { + Alert alert = new Alert(Alert.AlertType.INFORMATION, "you should keep the min credit", ButtonType.OK); + alert.showAndWait(); + return; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + MenuHandler.getConnector().clientToServer("bank " + "pay transaction with id+" + Bank.getToken() + "+" + receiptID.getText() + "+" + username + "+" + MenuHandler.getRole()); + String answer = MenuHandler.getConnector().serverToClient(); + if (receiptID.getText().equals("") || receiptID.getText() == null) { Alert alert = new Alert(Alert.AlertType.ERROR, "the receipt ID cannot be empty", ButtonType.OK); alert.showAndWait(); @@ -107,18 +129,6 @@ public void done(ActionEvent actionEvent) throws ParseException, IOException { return; } - try { - MenuHandler.getConnector().clientToServer("bank " + "get amount of transaction+" + Bank.getToken() + "+" + receiptID.getText()); - long ID = Long.parseLong(receiptID.getText()); - long amount = Long.parseLong(MenuHandler.getConnector().serverToClient()); - if (200000 > ID && ID >= 100000 && !Bank.isPossibleToDepositForSalesman(amount)) { - Alert alert = new Alert(Alert.AlertType.INFORMATION, "you should keep the min credit", ButtonType.OK); - alert.showAndWait(); - } - } catch (Exception e) { - e.printStackTrace(); - } - if (answer.equals("successful")) { Alert alert = new Alert(Alert.AlertType.INFORMATION, answer, ButtonType.OK); alert.showAndWait(); diff --git a/src/main/java/GUI/Bank/Pane/NewReceipt.java b/src/main/java/GUI/Bank/Pane/NewReceipt.java index d71da13..2d78242 100644 --- a/src/main/java/GUI/Bank/Pane/NewReceipt.java +++ b/src/main/java/GUI/Bank/Pane/NewReceipt.java @@ -158,6 +158,9 @@ public void transferDone(ActionEvent actionEvent) throws ParseException, IOExcep if (result.equals("successful")) { alert.setContentText("created successfully"); alert.showAndWait(); + transferUsername.setText(""); + transferAmount.setText(""); + transferDescription.setText(""); return; }