Compare commits

...

17 Commits

Author SHA1 Message Date
8f77d2a511 Minor fixes 2025-02-12 16:30:59 +03:00
e6c41073b0 Merge pull request 'Add support for PROXY protocol.' (#6) from bedohswe/crab:master into master
Reviewed-on: #6
2025-02-12 13:08:33 +00:00
3b68d8a097 Add support for PROXY protocol. 2025-02-12 12:12:22 +05:00
c0db9ff1a1 Merge pull request 'master' (#1) from pixtaded/crab:master into master
Reviewed-on: bedohswe/crab#1
2025-02-11 18:05:00 +00:00
b8d4ab90f2 Add RACv2 protocol specification. (I hate it) 2025-02-11 21:04:18 +03:00
12e595bb73 prepare for racv2 final update (I hate PID collisions) 2025-02-11 20:38:58 +03:00
933af5c5e0 Mefedroniy color support 2025-02-11 15:06:34 +03:00
f03666bead AntiClear (protection against /clear in bRAC) 2025-02-09 20:15:17 +03:00
72074ca117 Add client-specific colors (No Mefedroniy support 'cause no parsing :( ) 2025-02-09 19:01:10 +03:00
57d5f5ea1e Port the server to RACv1.99.2 and handle logs erasing in client 2025-02-09 15:32:22 +03:00
61ddfc86d1 Port the client to RACv1.99.2 2025-02-09 14:47:01 +03:00
3e6aefd4d2 Add RACv1.99.2 protocol specification 2025-02-09 14:19:44 +03:00
389d7c4868 Update README.md 2025-02-08 22:04:01 +03:00
6d21c9bccd Bump version to 2.0.0-SNAPSHOT 2025-02-08 21:42:05 +03:00
a4bf914c41 Server functionality restored 2025-02-08 21:41:03 +03:00
30d90f22f2 Add RACv1.99 protocol specification 2025-02-08 20:42:38 +03:00
74589d48b0 Port the client to RACv2 beta (the server is still not functional) 2025-02-08 20:34:47 +03:00
12 changed files with 257 additions and 61 deletions

View File

@ -24,12 +24,12 @@ To get started with CRAB, follow these steps:
``` ```
2. **Build the project:** 2. **Build the project:**
Ensure you have Java Development Kit (JDK) of version 17 or higher installed. You can build the project using Maven: Ensure you have Java Development Kit (JDK) of version 17 or higher installed. You can build the project using Gradle:
```bash ```bash
./gradlew clean build ./gradlew clean build
``` ```
3. **Run the bundle**: You will have the built .tar and .zip packages in ./build/distributions directory. 3. **Run the bundle**: You will have the built .jar package in ./build/libs directory.
## Usage ## Usage
@ -37,4 +37,46 @@ Once the server is running, clients can connect to it and send messages accordin
## RAC Protocol ## RAC Protocol
You can see the RAC protocol documentation [here](https://bedohswe.eu.org/text/rac/protocol.md.html). 1. Message Retrieval
a. The client initiates a message retrieval session by sending the byte `0x00` to the server.
b. In response, the server transmits the size of the available messages as an ASCII-encoded string.
c. After receiving the size, the client must send one of the following bytes or close the connection:
i. Sending `0x01` instructs the server to transmit all messages in full.
ii. Sending `0x02` followed by the clients cached messages length (as an ASCII string, e.g., `0x02"1024"`) instructs the server to transmit only new messages added since the cached length. The server sends messages starting from the cached length offset, and the client updates its cached length to the total size received in step 1b after processing the new messages.
2. Message Transmission
a. To send a message, the client issues a request in one of the following formats:
i. Unauthenticated Message: The client sends the byte `0x01` followed immediately by the message content. The server does not send a response.
ii. Authenticated Message: The client sends the byte `0x02` followed by the username, a newline character (`\n`), the password, a newline character and the message content. The server responds with a single byte:
- `0x01` indicates the user does not exist.
- `0x02` indicates the password is incorrect.
- A successful authentication results in the server accepting the message without sending a response.
3. User Registration
a. To register a new user, the client sends a request formatted as:
- The byte `0x03`.
- The username, followed by a newline character (`\n`).
- The password.
b. The server processes the request and responds with a single byte:
- `0x01` if the username already exists.
- A successful registration is assumed if no error byte (`0x01`) is received. The client should close the connection after handling the response.
### Additional Notes:
- The current specification of RACv2 is implemented in `lRACd` version 2.0.0 and `clRAC` version 2.0.0.
- When using `0x02` for incremental retrieval, the client must ensure the cached length is synchronized with the servers total message length (retrieved via `0x00`). The server sends messages from the cached length onward, and the client calculates the read size as `(total_length - cached_length)`.
- After receiving incremental messages, the client must update its cached length to the total length provided in step 1b to maintain consistency in subsequent requests.
- For authenticated message transmission (`0x02`) or user registration (`0x03`), the client must follow the specified format precisely. The server validates the structure of the request and responds with error codes only for specific failure conditions (e.g., invalid credentials or duplicate usernames).

View File

@ -1 +1 @@
projectVersion=1.0.5 projectVersion=2.0.0-SNAPSHOT

View File

@ -17,7 +17,7 @@ public class Main {
case "help" -> { case "help" -> {
System.out.println("crab help - print this message."); System.out.println("crab help - print this message.");
System.out.println("crab client <ip> <port> [nick] - connect to a server."); System.out.println("crab client <ip> <port> [nick] - connect to a server.");
System.out.println("crab server <port> - start a server."); System.out.println("crab server <port> [PROXY protocol off/on] - start a server.");
} }
case "client" -> { case "client" -> {
CrabClient client; CrabClient client;
@ -31,18 +31,23 @@ public class Main {
} }
case "server" -> { case "server" -> {
CrabServer server; CrabServer server;
try { if (args.length > 1) {
server = new CrabServer(Integer.parseInt(args[1])); boolean isProxied = false;
} catch (NumberFormatException e) { if (args.length > 2)
System.err.println("Port is not a number."); isProxied = args[2].equals("on");
try {
server = new CrabServer(Integer.parseInt(args[1]), isProxied);
} catch (NumberFormatException e) {
System.err.println("Port is not a number.");
return;
}
} else {
System.err.println("Not enough arguments.");
return; return;
} }
server.run(); server.run();
} }
default -> { default -> System.err.println("Unknown argument");
System.err.println("Unknown argument");
return;
}
} }
} }

View File

@ -0,0 +1,4 @@
package net.pixtaded.crab.client;
public record ClientColor(String regex, String color) {
}

View File

@ -0,0 +1,28 @@
package net.pixtaded.crab.client;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClientUtil {
public static final String COLOR_KEY = "\u2550\u2550\u2550";
public static final ClientColor[] colors = {
new ClientColor( COLOR_KEY + "(<.*?>)", "\033[0;31m$1\033[0m"),
new ClientColor("\uB9AC\u3E70(<.*?>)", "\033[0;32m$1\033[0m"),
new ClientColor(" (<.*?>)", " \033[0;34m$1\033[0m"),
new ClientColor("\u00B0\u0298(<.*?>)", "\033[0;35m$1\033[0m")
};
public static String clientColors(String s) {
for (ClientColor color : colors) s = matchClientKey(s, color);
return s;
}
private static String matchClientKey(String s, ClientColor color) {
Pattern p = Pattern.compile(color.regex());
Matcher m = p.matcher(s);
return m.replaceAll(color.color());
}
}

View File

@ -3,6 +3,7 @@ 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.Logs;
import net.pixtaded.crab.common.Sanitizer; import net.pixtaded.crab.common.Sanitizer;
import net.pixtaded.crab.common.Util;
import java.io.*; import java.io.*;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -29,7 +30,7 @@ public class CrabClient implements Crab {
this.serverAddress = serverAddress; this.serverAddress = serverAddress;
this.port = port; this.port = port;
if (nickname != null) if (nickname != null)
this.nickname = "<" + nickname + "> "; this.nickname = ClientUtil.COLOR_KEY + "<" + nickname + "> ";
else else
this.nickname = ""; this.nickname = "";
} }
@ -70,7 +71,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.isEmpty()) if (!nickname.isEmpty())
nickname = "<" + nickname + "> "; nickname = ClientUtil.COLOR_KEY + "<" + nickname + "> ";
} }
private void connect() throws IOException { private void connect() throws IOException {
@ -92,34 +93,41 @@ public class CrabClient implements Crab {
break; break;
} }
if (!message.isEmpty()) sendPacket(MESSAGE, this.nickname + message); if (!message.isEmpty()) sendMessage(message);
} }
} }
private void sendPacket(byte PID, String argument) throws IOException { private void sendPacket(byte PID, String argument, boolean receiveResponse) throws IOException {
connect(); if (socket == null || socket.isClosed()) connect();
String formattedMessage = String.valueOf((char) PID) + argument + "\n"; String formattedMessage = (char) PID + argument;
out.print(formattedMessage); out.print(formattedMessage);
out.flush(); out.flush();
receiveResponse(PID); if (receiveResponse) receiveResponse(PID);
}
private void printLogs() {
clearScreen();
System.out.print(ClientUtil.clientColors(Sanitizer.sanitizeString(cache.content(), false)));
}
private void sendMessage(String msg) throws IOException {
sendPacket(MESSAGE, this.nickname + msg, false);
closeConnection(); closeConnection();
} }
private void receiveResponse(byte PID) throws IOException { private void receiveResponse(byte PID) throws IOException {
switch (PID) { switch (PID) {
case LOGS_SIZE -> { case LOGS_SIZE -> {
char[] buffer = new char[10]; String convertedString = Util.readAsciiNumber(in);
int response = in.read(buffer); if (!convertedString.isEmpty()) lastBufferLength = Integer.parseInt(convertedString);
lastBufferLength = Integer.parseInt(new String(buffer).trim()); } case CACHED_LOGS -> {
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength - cache.sizeInBytes());
cache = new Logs(lastBufferLength, cache.content() + new String(bytes, StandardCharsets.UTF_8));
} case LOGS -> { } case LOGS -> {
if (cache.sizeInBytes() != lastBufferLength) { byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength);
byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength); cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8));
}
clearScreen();
System.out.print(Sanitizer.sanitizeString(cache.content(), false));
} default -> { } default -> {
} }
} }
@ -136,12 +144,18 @@ public class CrabClient implements Crab {
} }
private void getLogs() throws IOException { private void getLogs() throws IOException {
sendPacket(LOGS_SIZE, ""); sendPacket(LOGS_SIZE, "", true);
sendPacket(LOGS, ""); if (this.cache.sizeInBytes() < lastBufferLength) {
sendPacket(CACHED_LOGS, String.valueOf(cache.sizeInBytes()), true);
} else if (this.cache.sizeInBytes() != lastBufferLength) {
sendPacket(LOGS, "", true);
}
closeConnection();
printLogs();
} }
private void clearScreen() { private void clearScreen() {
System.out.print("\033[999999S\033[H\033[2J"); System.out.print("\033[999999S\033[H\033[2J");
} }
} }

View File

@ -1,7 +1,10 @@
package net.pixtaded.crab.common; package net.pixtaded.crab.common;
public class PID { public class PID {
public static final byte MESSAGE = 0x30; public static final byte LOGS_SIZE = 0x00;
public static final byte LOGS_SIZE = 0x31; public static final byte LOGS = 0x01;
public static final byte LOGS = 0x32; public static final byte MESSAGE = 0x01;
} public static final byte CACHED_LOGS = 0x02;
public static final byte AUTHENTICATED_MESSAGE = 0x02;
public static final byte REGISTER = 0x03;
}

View File

@ -6,6 +6,8 @@ public class Sanitizer {
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';
} else {
sanitized = sanitized.replaceAll("\n\n+", "\n");
} }
return sanitized; return sanitized;
} }
@ -13,4 +15,4 @@ public class Sanitizer {
public static String formatMessage(long timeMillis, String address, String content) { 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); return String.format("[%td.%1$tm.%1$tY %1$tR] {%s} %s", timeMillis, address, content);
} }
} }

View File

@ -0,0 +1,12 @@
package net.pixtaded.crab.common;
import java.io.BufferedReader;
import java.io.IOException;
public class Util {
public static String readAsciiNumber(BufferedReader in) throws IOException {
char[] buffer = new char[10];
int response = in.read(buffer);
return new String(buffer).trim();
}
}

View File

@ -3,15 +3,17 @@ import net.pixtaded.crab.common.Crab;
import net.pixtaded.crab.common.Logs; import net.pixtaded.crab.common.Logs;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.sql.SQLException;
import java.util.Scanner; import java.util.Scanner;
public class CrabServer implements Crab { public class CrabServer implements Crab {
private ServerSocket serverSocket; private ServerSocket serverSocket;
private Socket socket;
private boolean isStopped = false; private boolean isStopped = false;
private boolean isProxied = false;
private int port; private int port;
private final Database db; private final Database db;
public Logs cache = new Logs(0, ""); public Logs cache = new Logs(0, "");
@ -20,9 +22,10 @@ public class CrabServer implements Crab {
this.db = new Database("data.db"); this.db = new Database("data.db");
} }
public CrabServer(int port) { public CrabServer(int port, boolean isProxied) {
this.db = new Database("data.db"); this.db = new Database("data.db");
this.port = port; this.port = port;
this.isProxied = isProxied;
} }
@Override @Override
@ -54,11 +57,28 @@ public class CrabServer implements Crab {
System.out.println("Enter a correct port number: "); System.out.println("Enter a correct port number: ");
} }
} }
System.out.print("Enable PROXY protocol? (on/off): ");
while (true) {
String s = scanner.nextLine();
if (s.equals("on")) {
this.isProxied = true;
break;
}
if (s.equals("off")) {
this.isProxied = false;
break;
}
System.out.println("Enter either \"on\" or \"off\".");
}
} }
private void listen() throws IOException { private void listen() throws IOException {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
serverSocket = new ServerSocket(port); if (this.isProxied) {
serverSocket = new ServerSocket(port, 0, InetAddress.getLoopbackAddress());
} else {
serverSocket = new ServerSocket(port);
}
System.out.printf("Server successfully started! Listening on port %s.\nTo stop the server, type 'q'.\n", port); System.out.printf("Server successfully started! Listening on port %s.\nTo stop the server, type 'q'.\n", port);
ServerCLI cli = new ServerCLI(scanner, this); ServerCLI cli = new ServerCLI(scanner, this);
new Thread(cli).start(); new Thread(cli).start();
@ -72,10 +92,12 @@ public class CrabServer implements Crab {
public synchronized void stop() { public synchronized void stop() {
isStopped = true; isStopped = true;
try { try {
if (socket != null) socket.close();
if (serverSocket != null) serverSocket.close(); if (serverSocket != null) serverSocket.close();
getDb().close();
} catch (IOException e) { } catch (IOException e) {
System.err.println("An error occured while closing the socket: " + e.getMessage()); System.err.println("An error occured while closing the socket: " + e.getMessage());
} catch (SQLException e) {
throw new RuntimeException(e);
} finally { } finally {
System.exit(0); System.exit(0);
} }
@ -84,4 +106,8 @@ public class CrabServer implements Crab {
public Database getDb() { public Database getDb() {
return db; return db;
} }
public boolean isProxied() {
return this.isProxied;
}
} }

View File

@ -6,7 +6,7 @@ import net.pixtaded.crab.common.Sanitizer;
import java.sql.*; import java.sql.*;
import java.util.Date; import java.util.Date;
public class Database implements AutoCloseable { public class Database {
private Connection connection; private Connection connection;
@ -62,10 +62,7 @@ public class Database implements AutoCloseable {
return new Logs(logsString.isEmpty() ? 0 : logsString.getBytes().length, logsString); return new Logs(logsString.isEmpty() ? 0 : logsString.getBytes().length, logsString);
} }
@Override
public void close() throws SQLException { public void close() throws SQLException {
if (connection != null && !connection.isClosed()) { connection.close();
connection.close();
}
} }
} }

View File

@ -2,23 +2,24 @@ package net.pixtaded.crab.server;
import net.pixtaded.crab.common.Logs; import net.pixtaded.crab.common.Logs;
import net.pixtaded.crab.common.Sanitizer; import net.pixtaded.crab.common.Sanitizer;
import net.pixtaded.crab.common.Util;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import static net.pixtaded.crab.common.PID.*; import static net.pixtaded.crab.common.PID.*;
public class ServerThread implements Runnable { public class ServerThread implements Runnable {
private Socket socket; private final Socket socket;
private PrintWriter out; private final PrintWriter out;
private BufferedReader in; private final BufferedReader in;
private OutputStream output; private final OutputStream output;
private InputStream input; private final InputStream input;
private byte PID; private final CrabServer server;
private CrabServer server;
public ServerThread(Socket socket, CrabServer server) throws IOException { public ServerThread(Socket socket, CrabServer server) throws IOException {
this.socket = socket; this.socket = socket;
@ -32,29 +33,60 @@ public class ServerThread implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
byte[] PID = input.readNBytes(1); byte[] PID = readPID();
if (PID.length == 0) { if (PID.length == 0) {
socket.close(); socket.close();
return; return;
} }
String address = socket.getInetAddress().getHostAddress();
if (PID[0] == 'P') {
if (!this.server.isProxied()) {
System.err.println(address + " tried to use PROXY despite it being off.");
socket.close();
return;
}
if (Arrays.equals(readUntilChar(' '),"ROXY".getBytes())) {
readUntilChar(' '); // proto
byte[] source = readUntilChar(' ');
address = new String(source);
readUntilChar(' '); // destination IP
readUntilChar(' '); // source port
readUntilChar('\r'); // destination port
if (input.read() != '\n') {
System.err.println("Invalid PROXY packet.");
socket.close();
return;
}
} else {
System.err.println("Invalid PROXY packet header.");
socket.close();
return;
}
PID = readPID();
if (PID.length == 0) {
socket.close();
return;
}
}
switch (PID[0]) { switch (PID[0]) {
case MESSAGE -> { case MESSAGE -> {
Date date = new Date();
String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim(); String msg = new String(input.readNBytes(4096), StandardCharsets.UTF_8).trim();
String address = socket.getInetAddress().getHostAddress(); Date date = new Date();
String s = Sanitizer.sanitizeString(msg, true); String s = Sanitizer.sanitizeString(msg, true);
String newContent = server.cache.content() + Sanitizer.formatMessage(date.getTime(), address, s); String newContent = server.cache.content() + Sanitizer.formatMessage(date.getTime(), address, s);
server.cache = new Logs(newContent.getBytes().length, newContent); server.cache = new Logs(newContent.getBytes().length, newContent);
new Thread(new LogDBThread(date, address, msg)).start(); new Thread(new LogDBThread(date, address, msg)).start();
} case LOGS -> {
respond(server.cache.content());
} case LOGS_SIZE -> { } case LOGS_SIZE -> {
respond(String.valueOf(server.cache.sizeInBytes())); respond(String.valueOf(server.cache.sizeInBytes()));
} default -> { byte[] logPID = readPID();
System.out.println("PID not implemented: " + PID[0]); if (logPID.length == 0) {
} socket.close();
return;
}
sendLogs(logPID[0]);
} default -> System.out.println("PID not implemented: " + PID[0]);
} }
socket.close(); socket.close();
} catch (Exception e) { } catch (Exception e) {
@ -62,6 +94,37 @@ public class ServerThread implements Runnable {
} }
} }
private byte[] readPID() throws IOException {
return input.readNBytes(1);
}
private byte[] readUntilChar(char c) throws IOException {
byte[] b = new byte[256];
int i;
for (i = 0;; i++) {
b[i] = (byte)input.read();
if (b[i] == c)
break;
}
byte[] r = new byte[i];
System.arraycopy(b, 0, r, 0, i);
return r;
}
private void sendLogs(byte PID) throws IOException {
if (PID == LOGS) {
respond(server.cache.content());
} else if (PID == CACHED_LOGS) {
String clientSize = Util.readAsciiNumber(in);
int clientSizeNum = Integer.parseInt(clientSize);
byte[] serverLogs = server.cache.content().getBytes(StandardCharsets.UTF_8);
int logPartSize = serverLogs.length - clientSizeNum;
byte[] logPart = new byte[logPartSize];
System.arraycopy(serverLogs, serverLogs.length - logPartSize, logPart, 0, logPartSize);
respond(logPart);
}
}
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();