Guest User

vb

a guest
May 19th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8. import java.nio.charset.StandardCharsets;
  9. import java.util.List;
  10.  
  11. import clientInput.CommandBase;
  12. import colors.ColorClass;
  13. import database.ManageUsers;
  14. import encryption.Encrypt;
  15.  
  16. public class CncThread implements Runnable{
  17.  
  18.     private final Socket socket;
  19.     private PrintWriter writer;
  20.     private BufferedReader reader;
  21.     private boolean running;
  22.     private final List<PrintWriter> otherUsers;
  23.    
  24.     private String username;
  25.     private String password;
  26.    
  27.     ColorClass cc = new ColorClass();
  28.     ManageUsers mu = new ManageUsers();
  29.    
  30.     public CncThread(Socket socket, List<PrintWriter> otherUsers) {
  31.        
  32.         this.socket = socket;
  33.         this.otherUsers = otherUsers;
  34.    
  35.         try {
  36.             writer = new PrintWriter(socket.getOutputStream());
  37.             reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  38.             running = true;
  39.             this.otherUsers.add(writer);
  40.         } catch(IOException ioe) {
  41.            
  42.         }
  43.        
  44.     }
  45.    
  46.     @Override
  47.     public void run() {
  48.  
  49.  
  50.         writer.print("Username: ");
  51.         writer.flush();
  52.         username = this.getText();
  53.         username = username.trim();
  54.        
  55.         CommandBase cb = new CommandBase(username);
  56.        
  57.         if(!username.isEmpty()) {
  58.             do {
  59.                 writer.print("Password: ");
  60.                 writer.flush();
  61.                 password = this.getText();
  62.                 password = password.trim();
  63.                 if(password.length() < 6)  {
  64.                     writer.println("Password must at least have 6 letters!");
  65.                 }
  66.             }while(password.length() < 6);
  67.         } else {
  68.             System.exit(0);
  69.         }
  70.         //Continue here and fix the db nigger
  71.  
  72.         if(mu.hasUserAccess(username)) {
  73.             if(mu.isUserExpired()) {
  74.                 writer.print("User expired!");
  75.                 System.exit(0);
  76.             } else {
  77.                 Encrypt en = new Encrypt();
  78.                 password = en.encryption(password); //Write encrypted password to the getPass var
  79.                 //Check if the username already exist in the database
  80.                 System.out.print(password);
  81.                 ManageUsers muT = new ManageUsers(username, password);
  82.                 muT.userManager();
  83.             }
  84.         } else {
  85.             writer.print("Your access was revoked!");
  86.             System.exit(0);
  87.         }
  88.        
  89.         while(running) {
  90.            
  91.             String message = this.getText();
  92.            
  93.             if(message == null) {
  94.                 running = false;
  95.                
  96.             } else {
  97.                
  98.             }
  99.         }
  100.         this.otherUsers.remove(otherUsers.indexOf(writer));
  101.         this.closeCloseable(reader);
  102.         this.closeCloseable(writer);
  103.         this.closeCloseable(socket);
  104.        
  105.     }
  106.    
  107.     private void closeCloseable(AutoCloseable closeable) {
  108.         if(closeable != null) {
  109.             try {
  110.                 closeable.close();
  111.             } catch(Exception e) {
  112.             }
  113.         }
  114.    
  115.     }
  116.    
  117.    
  118.    
  119.     private String getText() {
  120.  
  121.         try {
  122.             return reader.readLine();
  123.         } catch(IOException ioe) {
  124.  
  125.         }
  126.         return null;
  127.     }
  128.    
  129.    
  130. }
  131.  
Add Comment
Please, Sign In to add comment