This commit is contained in:
2024-01-10 23:02:56 -06:00
parent d243786d57
commit 3906c463fd
17 changed files with 548 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package tech.nevets.dliteserver;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Connection implements Runnable {
private InputStream is;
private OutputStream os;
private MessageDistributer md;
private String username;
public Connection(Socket s, MessageDistributer md) throws IOException {
this.is = s.getInputStream();
this.os = s.getOutputStream();
this.md = md;
String buf = "";
for (int i = is.read(); (char) i != '\n' && i != -1; i = is.read()) {
buf += (char) i;
}
username = buf;
}
@Override
public void run() {
try {
md.registerStream(username, os);
is.transferTo(md);
} catch (IOException e) {
System.out.println("Connection Disconnected");
}
MessageDistributer.closeUserConnection(username);
}
}

View File

@@ -0,0 +1,44 @@
package tech.nevets.dliteserver;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class MessageDistributer extends OutputStream {
private static Map<String, OutputStream> userStreams = new HashMap<>();
private String buf = "";
@Override
public void write(int b) throws IOException {
buf += (char) b;
if (((char) b) == '\n') this.flush();
}
@Override
public void flush() {
for (OutputStream os : userStreams.values()) {
try {
os.write(buf.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
buf = "";
}
public void registerStream(String username, OutputStream os) {
userStreams.put(username, os);
try {
this.write(("SERVER> Welcome, " + username + "!\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void closeUserConnection(String username) {
userStreams.remove(username);
}
}

View File

@@ -0,0 +1,42 @@
package tech.nevets.dliteserver;
import javax.net.ssl.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
public class Server {
private static SSLServerSocket ss;
public static void main(String[] args) throws Exception {
if (args.length != 1) {
args = new String[]{"6969"};
}
MessageDistributer md = new MessageDistributer();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream kStore = new FileInputStream("./keystore.p12");
keyStore.load(kStore, "".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, "".toCharArray());
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), null, SecureRandom.getInstanceStrong());
SSLServerSocketFactory factory = ctx.getServerSocketFactory();
ss = (SSLServerSocket) factory.createServerSocket(Integer.parseInt(args[0]));
ss.setNeedClientAuth(false);
ss.setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2", "TLSv1.3"});
while (true) {
try {
new Thread(new Connection(ss.accept(), md)).start();
} catch (IOException e) {
System.out.println("User disconnected");
}
}
}
}