Add Java Client

This commit is contained in:
Collin 2024-03-15 16:41:48 +00:00
parent 3649eda7d2
commit 2ab0e0ede9
3 changed files with 212 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
JavaScript/node_modules JavaScript/node_modules
Java/target
Python/venv Python/venv
Python/dist Python/dist
Python/buildc Python/buildc

45
Java/pom.xml Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<scm>
<connection>scm:git:https://github.com/LonaDB/Clients.git</connection>
<developerConnection>scm:git:https://github.com/LonaDB/Clients.git</developerConnection>
<url>https://github.com/LonaDB/Clients</url>
</scm>
<groupId>org.lona-development.java-client</groupId>
<artifactId>LonaDB</artifactId>
<version>2.1.3</version>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,166 @@
import java.io.*;
import java.net.*;
import java.util.Base64;
import java.util.Random;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
public class LonaDB {
private String host;
private int port;
private String name;
private String password;
public LonaDB(String host, int port, String name, String password) {
this.host = host;
this.port = port;
this.name = name;
this.password = password;
}
private String makeId(int length) {
StringBuilder result = new StringBuilder();
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
Random rand = new Random();
int counter = 0;
while (counter < length) {
result.append(characters.charAt(rand.nextInt(characters.length())));
counter++;
}
return result.toString();
}
private String encryptPassword(String password, String key) throws Exception {
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.getDecoder().decode(key), "AES"), new IvParameterSpec(iv));
byte[] encrypted = cipher.doFinal(password.getBytes());
return bytesToHex(iv) + ":" + bytesToHex(encrypted);
}
private String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
public String sendRequest(String action, String data) throws Exception {
String processID = makeId(5);
String encryptionKey = Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(processID.getBytes()));
String encryptedPassword = encryptPassword(password, encryptionKey);
String request = String.format("{\"action\":\"%s\",\"login\":{\"name\":\"%s\",\"password\":\"%s\"},\"process\":\"%s\",%s}", action, name, encryptedPassword, processID, data);
Socket socket = new Socket(host, port);
OutputStream out = socket.getOutputStream();
PrintWriter writer = new PrintWriter(out, true);
writer.println(request);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
socket.close();
return response.toString();
}
public String createFunction(String name, String content) throws Exception {
String data = String.format("{\"function\":{\"name\":\"%s\",\"content\":\"%s\"}}", name, content);
return sendRequest("add_function", data);
}
public String executeFunction(String name) throws Exception {
String data = String.format("{\"name\":\"%s\"}", name);
return sendRequest("execute_function", data);
}
public String getTables(String user) throws Exception {
String data = String.format("{\"user\":\"%s\"}", user);
return sendRequest("get_tables", data);
}
public String getTableData(String table) throws Exception {
String data = String.format("{\"table\":\"%s\"}", table);
return sendRequest("get_table_data", data);
}
public String deleteTable(String table) throws Exception {
String data = String.format("{\"table\":{\"name\":\"%s\"}}", table);
return sendRequest("delete_table", data);
}
public String createTable(String table) throws Exception {
String data = String.format("{\"table\":{\"name\":\"%s\"}}", table);
return sendRequest("create_table", data);
}
public String set(String table, String name, String value) throws Exception {
String data = String.format("{\"table\":{\"name\":\"%s\"},\"variable\":{\"name\":\"%s\",\"value\":\"%s\"}}", table, name, value);
return sendRequest("set_variable", data);
}
public String delete(String table, String name) throws Exception {
String data = String.format("{\"table\":{\"name\":\"%s\"},\"variable\":{\"name\":\"%s\"}}", table, name);
return sendRequest("remove_variable", data);
}
public String get(String table, String name) throws Exception {
String data = String.format("{\"table\":{\"name\":\"%s\"},\"variable\":{\"name\":\"%s\"}}", table, name);
return sendRequest("get_variable", data);
}
public String getUsers() throws Exception {
return sendRequest("get_users", "{}");
}
public String createUser(String name, String pass) throws Exception {
String data = String.format("{\"user\":{\"name\":\"%s\",\"password\":\"%s\"}}", name, pass);
return sendRequest("create_user", data);
}
public String deleteUser(String name) throws Exception {
String data = String.format("{\"user\":{\"name\":\"%s\"}}", name);
return sendRequest("delete_user", data);
}
public String checkPassword(String name, String pass) throws Exception {
String data = String.format("{\"checkPass\":{\"name\":\"%s\",\"pass\":\"%s\"}}", name, pass);
return sendRequest("check_password", data);
}
public String checkPermission(String name, String permission) throws Exception {
String data = String.format("{\"permission\":{\"user\":\"%s\",\"name\":\"%s\"}}", name, permission);
return sendRequest("check_permission", data);
}
public String removePermission(String name, String permission) throws Exception {
String data = String.format("{\"permission\":{\"user\":\"%s\",\"name\":\"%s\"}}", name, permission);
return sendRequest("remove_permission", data);
}
public String getPermissionsRaw(String name) throws Exception {
String data = String.format("{\"user\":\"%s\"}", name);
return sendRequest("get_permissions_raw", data);
}
public String addPermission(String name, String permission) throws Exception {
String data = String.format("{\"permission\":{\"user\":\"%s\",\"name\":\"%s\"}}", name, permission);
return sendRequest("add_permission", data);
}
public String eval(String func) throws Exception {
String data = String.format("{\"function\":\"%s\"}", func);
return sendRequest("eval", data);
}
}