71 lines
2.1 KiB
Java
71 lines
2.1 KiB
Java
package org.orinprojects;
|
|
|
|
import org.orinprojects.encryption.EncryptionUtil;
|
|
import org.orinprojects.exceptions.ArgumentsException;
|
|
|
|
import javax.crypto.SecretKey;
|
|
import java.io.*;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.security.KeyPair;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.PublicKey;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
public class Server {
|
|
|
|
static List<ClientHandler> clients = new ArrayList<>();
|
|
|
|
static Map<String, PublicKey> clientKeys = new HashMap<>();
|
|
|
|
static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(20);
|
|
|
|
static KeyPair serverKeys;
|
|
|
|
static SecretKey aesKey;
|
|
|
|
static byte[] ivKey;
|
|
|
|
public static void main(String[] args) throws ArgumentsException, NoSuchAlgorithmException, IOException {
|
|
Server.serverKeys = EncryptionUtil.generateRSAKeys();
|
|
Server.ivKey = EncryptionUtil.generateIV();
|
|
Server.aesKey = EncryptionUtil.generateAESKey();
|
|
|
|
int portNumber = getPortNumber(args);
|
|
|
|
try (ServerSocket server = new ServerSocket(portNumber)) {
|
|
while (!server.isClosed()) {
|
|
Socket clientSocket = server.accept();
|
|
|
|
ClientHandler clientHandler = new ClientHandler(clientSocket);
|
|
clients.add(clientHandler);
|
|
|
|
executor.execute(clientHandler);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static int getPortNumber(String[] args) throws ArgumentsException {
|
|
if (args.length != 2)
|
|
throw new ArgumentsException("Not enough or too much arguments");
|
|
|
|
if (!args[0].equals("-p"))
|
|
throw new ArgumentsException("No port parameter. Use -p to specify port");
|
|
|
|
int port;
|
|
try {
|
|
port = Integer.parseInt(args[1]);
|
|
} catch (NumberFormatException numberFormatException) {
|
|
throw new ArgumentsException("Wrong number format! Example: -p 6666");
|
|
}
|
|
|
|
return port;
|
|
}
|
|
|
|
}
|