forked from pixtaded/crab
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
7ba5bfbf9e | |||
e96b02357d | |||
c2e8927c42 | |||
a7ad8770c9 | |||
5c55999771 | |||
6e1d843f79 | |||
57b20f69cd | |||
bdf7e6bbae | |||
7a116c6789 | |||
6dace63cb0 | |||
162c4bdf53 | |||
488a3f4e54 | |||
b914364c49 | |||
44d11f18d6 |
24
build.gradle
24
build.gradle
@ -1,19 +1,31 @@
|
|||||||
|
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
id 'application'
|
id 'com.gradleup.shadow' version '9.0.0-beta4'
|
||||||
}
|
}
|
||||||
|
|
||||||
group = 'net.pixtaded'
|
group = 'net.pixtaded'
|
||||||
version = '1.0-SNAPSHOT'
|
version = projectVersion
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
|
||||||
mainClass = 'net.pixtaded.crab.Main'
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation('org.xerial:sqlite-jdbc:3.47.1.0')
|
implementation('org.xerial:sqlite-jdbc:3.47.1.0')
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.build.dependsOn tasks.shadowJar
|
||||||
|
|
||||||
|
tasks.named('jar', Jar) {
|
||||||
|
manifest {
|
||||||
|
attributes 'Main-Class': 'net.pixtaded.crab.Main'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named('shadowJar', ShadowJar) {
|
||||||
|
archiveBaseName = 'crab'
|
||||||
|
archiveClassifier = ''
|
||||||
|
archiveVersion = projectVersion
|
||||||
}
|
}
|
1
gradle.properties
Normal file
1
gradle.properties
Normal file
@ -0,0 +1 @@
|
|||||||
|
projectVersion=1.0.5
|
@ -1,6 +1,7 @@
|
|||||||
package net.pixtaded.crab.client;
|
package net.pixtaded.crab.client;
|
||||||
|
|
||||||
import net.pixtaded.crab.common.Crab;
|
import net.pixtaded.crab.common.Crab;
|
||||||
|
import net.pixtaded.crab.common.Logs;
|
||||||
import net.pixtaded.crab.common.Sanitizer;
|
import net.pixtaded.crab.common.Sanitizer;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -18,6 +19,7 @@ public class CrabClient implements Crab {
|
|||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private int lastBufferLength = 0;
|
private int lastBufferLength = 0;
|
||||||
private String nickname;
|
private String nickname;
|
||||||
|
private Logs cache = new Logs(0, "");
|
||||||
|
|
||||||
public CrabClient() {
|
public CrabClient() {
|
||||||
this.nickname = "";
|
this.nickname = "";
|
||||||
@ -37,7 +39,6 @@ public class CrabClient implements Crab {
|
|||||||
try {
|
try {
|
||||||
if (this.serverAddress == null)
|
if (this.serverAddress == null)
|
||||||
setup();
|
setup();
|
||||||
connect();
|
|
||||||
communicate();
|
communicate();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error connecting to the server: " + e.getMessage());
|
System.err.println("Error connecting to the server: " + e.getMessage());
|
||||||
@ -68,7 +69,7 @@ public class CrabClient implements Crab {
|
|||||||
|
|
||||||
System.out.print("Enter your nickname (leave empty for no nickname): ");
|
System.out.print("Enter your nickname (leave empty for no nickname): ");
|
||||||
nickname = scanner.nextLine();
|
nickname = scanner.nextLine();
|
||||||
if (nickname != "")
|
if (!nickname.isEmpty())
|
||||||
nickname = "<" + nickname + "> ";
|
nickname = "<" + nickname + "> ";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,9 +83,7 @@ public class CrabClient implements Crab {
|
|||||||
private void communicate() throws IOException {
|
private void communicate() throws IOException {
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
String message;
|
String message;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.print("\033[H\033[2J");
|
|
||||||
getLogs();
|
getLogs();
|
||||||
System.out.print("Enter a message (or type '/exit' to exit): ");
|
System.out.print("Enter a message (or type '/exit' to exit): ");
|
||||||
message = scanner.nextLine();
|
message = scanner.nextLine();
|
||||||
@ -98,12 +97,14 @@ public class CrabClient implements Crab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendPacket(byte PID, String argument) throws IOException {
|
private void sendPacket(byte PID, String argument) throws IOException {
|
||||||
|
connect();
|
||||||
String formattedMessage = String.valueOf((char) PID) + argument + "\n";
|
String formattedMessage = String.valueOf((char) PID) + argument + "\n";
|
||||||
|
|
||||||
out.print(formattedMessage);
|
out.print(formattedMessage);
|
||||||
out.flush();
|
out.flush();
|
||||||
|
|
||||||
receiveResponse(PID);
|
receiveResponse(PID);
|
||||||
|
closeConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void receiveResponse(byte PID) throws IOException {
|
private void receiveResponse(byte PID) throws IOException {
|
||||||
@ -113,13 +114,15 @@ public class CrabClient implements Crab {
|
|||||||
int response = in.read(buffer);
|
int response = in.read(buffer);
|
||||||
lastBufferLength = Integer.parseInt(new String(buffer).trim());
|
lastBufferLength = Integer.parseInt(new String(buffer).trim());
|
||||||
} case LOGS -> {
|
} case LOGS -> {
|
||||||
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
|
if (cache.sizeInBytes() != lastBufferLength) {
|
||||||
System.out.print(Sanitizer.sanitizeString(new String(bytes, StandardCharsets.UTF_8), false));
|
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
|
||||||
|
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
clearScreen();
|
||||||
|
System.out.print(Sanitizer.sanitizeString(cache.content(), false));
|
||||||
} default -> {
|
} default -> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closeConnection();
|
|
||||||
connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closeConnection() {
|
private void closeConnection() {
|
||||||
@ -136,4 +139,9 @@ public class CrabClient implements Crab {
|
|||||||
sendPacket(LOGS_SIZE, "");
|
sendPacket(LOGS_SIZE, "");
|
||||||
sendPacket(LOGS, "");
|
sendPacket(LOGS, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void clearScreen() {
|
||||||
|
System.out.print("\033[999999S\033[H\033[2J");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
4
src/main/java/net/pixtaded/crab/common/Logs.java
Normal file
4
src/main/java/net/pixtaded/crab/common/Logs.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package net.pixtaded.crab.common;
|
||||||
|
|
||||||
|
public record Logs(int sizeInBytes, String content) {
|
||||||
|
}
|
@ -2,11 +2,15 @@ package net.pixtaded.crab.common;
|
|||||||
|
|
||||||
public class Sanitizer {
|
public class Sanitizer {
|
||||||
public static String sanitizeString(String s, boolean sanitizeNewlines) {
|
public static String sanitizeString(String s, boolean sanitizeNewlines) {
|
||||||
String sanitized = s.replaceAll("\033", "");
|
String sanitized = s.replaceAll("[\010\015\033]", "");
|
||||||
if (sanitizeNewlines) {
|
if (sanitizeNewlines) {
|
||||||
sanitized = sanitized.replaceAll("\n", "\\\\n");
|
sanitized = sanitized.replaceAll("\n", "\\\\n");
|
||||||
if (!s.endsWith("\n")) sanitized += '\n';
|
if (!s.endsWith("\n")) sanitized += '\n';
|
||||||
}
|
}
|
||||||
return sanitized;
|
return sanitized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String formatMessage(long timeMillis, String address, String content) {
|
||||||
|
return String.format("[%td.%1$tm.%1$tY %1$tR] {%s} %s", timeMillis, address, content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.pixtaded.crab.server;
|
package net.pixtaded.crab.server;
|
||||||
import net.pixtaded.crab.common.Crab;
|
import net.pixtaded.crab.common.Crab;
|
||||||
|
import net.pixtaded.crab.common.Logs;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
@ -12,7 +13,8 @@ public class CrabServer implements Crab {
|
|||||||
private Socket socket;
|
private Socket socket;
|
||||||
private boolean isStopped = false;
|
private boolean isStopped = false;
|
||||||
private int port;
|
private int port;
|
||||||
private Database db;
|
private final Database db;
|
||||||
|
public Logs cache = new Logs(0, "");
|
||||||
|
|
||||||
public CrabServer() {
|
public CrabServer() {
|
||||||
this.db = new Database("data.db");
|
this.db = new Database("data.db");
|
||||||
@ -28,6 +30,7 @@ public class CrabServer implements Crab {
|
|||||||
try {
|
try {
|
||||||
if (this.port == 0)
|
if (this.port == 0)
|
||||||
setup();
|
setup();
|
||||||
|
this.cache = getDb().getLogs();
|
||||||
listen();
|
listen();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (!isStopped) System.err.println("Error starting server: " + e.getMessage());
|
if (!isStopped) System.err.println("Error starting server: " + e.getMessage());
|
||||||
@ -66,9 +69,9 @@ public class CrabServer implements Crab {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public synchronized void stop() {
|
||||||
|
isStopped = true;
|
||||||
try {
|
try {
|
||||||
isStopped = true;
|
|
||||||
if (socket != null) socket.close();
|
if (socket != null) socket.close();
|
||||||
if (serverSocket != null) serverSocket.close();
|
if (serverSocket != null) serverSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
package net.pixtaded.crab.server;
|
package net.pixtaded.crab.server;
|
||||||
|
|
||||||
|
import net.pixtaded.crab.common.Logs;
|
||||||
import net.pixtaded.crab.common.Sanitizer;
|
import net.pixtaded.crab.common.Sanitizer;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
public class Database {
|
public class Database implements AutoCloseable {
|
||||||
|
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private String logs = "";
|
|
||||||
|
|
||||||
public Database(String dbName) {
|
public Database(String dbName) {
|
||||||
try {
|
try {
|
||||||
@ -50,22 +49,23 @@ public class Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLogs() {
|
public Logs getLogs() {
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
try (ResultSet rs = query("SELECT time, address, msg FROM messages")) {
|
try (ResultSet rs = query("SELECT time, address, msg FROM messages")) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
s.append(String.format("[%td.%1$tm.%1$tY %1$tR] {%s} %s", rs.getLong(1), rs.getString(2), rs.getString(3)));
|
s.append(Sanitizer.formatMessage(rs.getLong(1), rs.getString(2), rs.getString(3)));
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
logs = s.toString();
|
String logsString = s.toString();
|
||||||
return logs;
|
return new Logs(logsString.isEmpty() ? 0 : logsString.getBytes().length, logsString);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getLogSize() {
|
@Override
|
||||||
if (logs.isEmpty()) return 0;
|
public void close() throws SQLException {
|
||||||
else return logs.getBytes().length;
|
if (connection != null && !connection.isClosed()) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package net.pixtaded.crab.server;
|
package net.pixtaded.crab.server;
|
||||||
|
|
||||||
|
import net.pixtaded.crab.common.Logs;
|
||||||
|
import net.pixtaded.crab.common.Sanitizer;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@ -30,18 +33,30 @@ public class ServerThread implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
byte[] PID = input.readNBytes(1);
|
byte[] PID = input.readNBytes(1);
|
||||||
|
if (PID.length == 0) {
|
||||||
|
socket.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
switch (PID[0]) {
|
switch (PID[0]) {
|
||||||
case MESSAGE -> {
|
case MESSAGE -> {
|
||||||
server.getDb().logMessage(new Date(), socket.getInetAddress().getHostAddress(), new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim());
|
Date date = new Date();
|
||||||
socket.close();
|
String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim();
|
||||||
|
String address = socket.getInetAddress().getHostAddress();
|
||||||
|
|
||||||
|
String s = Sanitizer.sanitizeString(msg, true);
|
||||||
|
String newContent = server.cache.content() + Sanitizer.formatMessage(date.getTime(), address, s);
|
||||||
|
server.cache = new Logs(newContent.getBytes().length, newContent);
|
||||||
|
|
||||||
|
new Thread(new LogDBThread(date, address, msg)).start();
|
||||||
} case LOGS -> {
|
} case LOGS -> {
|
||||||
respond(server.getDb().getLogs());
|
respond(server.cache.content());
|
||||||
} case LOGS_SIZE -> {
|
} case LOGS_SIZE -> {
|
||||||
respond(String.valueOf(server.getDb().getLogSize()));
|
respond(String.valueOf(server.cache.sizeInBytes()));
|
||||||
} default -> {
|
} default -> {
|
||||||
System.out.println("PID not implemented: " + PID[0]);
|
System.out.println("PID not implemented: " + PID[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
socket.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -50,10 +65,27 @@ public class ServerThread implements Runnable {
|
|||||||
private void respond(byte[] data) throws IOException {
|
private void respond(byte[] data) throws IOException {
|
||||||
socket.getOutputStream().write(data);
|
socket.getOutputStream().write(data);
|
||||||
socket.getOutputStream().flush();
|
socket.getOutputStream().flush();
|
||||||
socket.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void respond(String utf8) throws IOException {
|
private void respond(String utf8) throws IOException {
|
||||||
respond(utf8.getBytes(StandardCharsets.UTF_8));
|
respond(utf8.getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class LogDBThread implements Runnable {
|
||||||
|
|
||||||
|
Date date;
|
||||||
|
String msg;
|
||||||
|
String address;
|
||||||
|
|
||||||
|
public LogDBThread(Date date, String address, String msg) {
|
||||||
|
this.date = date;
|
||||||
|
this.msg = msg;
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
server.getDb().logMessage(date, address, msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user