Bug fixes and fixing my life

This commit is contained in:
Collin 2024-10-08 10:07:17 +02:00
parent 15f018429a
commit f8a117420a
6 changed files with 205 additions and 121 deletions

View File

@ -10,9 +10,9 @@
<url>https://git.lona-development.org/Lona-Development/Clients</url> <url>https://git.lona-development.org/Lona-Development/Clients</url>
</scm> </scm>
<groupId>org.lona-development.java-client</groupId> <groupId>org.lona_development.client</groupId>
<artifactId>lonadb</artifactId> <artifactId>lonadb</artifactId>
<version>2.1.4</version> <version>3.0.0</version>
<dependencies> <dependencies>
<dependency> <dependency>
@ -27,6 +27,12 @@
<version>4.13.1</version> <version>4.13.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,3 +1,5 @@
package org.lona_development.client;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
@ -115,6 +117,15 @@ public class LonaDB {
return sendRequest("execute_function", data); return sendRequest("execute_function", data);
} }
public Map<String, Object> deleteFunction(String name) throws Exception {
Map<String, Object> data = new HashMap<>();
Map<String, String> functionData = new HashMap<>();
functionData.put("name", name);
data.put("function", functionData);
return sendRequest("delete_function", data);
}
public Map<String, Object> getTables(String user) throws Exception { public Map<String, Object> getTables(String user) throws Exception {
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
data.put("user", user); data.put("user", user);

View File

@ -107,6 +107,16 @@ class LonaDB {
return this.sendRequest("execute_function", data); return this.sendRequest("execute_function", data);
} }
deleteFunction(name) {
const data = {
"function": {
"name": name
}
};
return this.sendRequest("delete_function", data);
}
getTables(user) { getTables(user) {
const data = { const data = {
user user

View File

@ -101,6 +101,16 @@ class LonaDB {
return $this->sendRequest("execute_function", $data); return $this->sendRequest("execute_function", $data);
} }
public function deleteFunction($name) {
$data = [
"function" => [
"name" => $name,
]
];
return $this->sendRequest("delete_function", $data);
}
public function getTables($user) { public function getTables($user) {
$data = [ $data = [
"user" => $user "user" => $user
@ -233,7 +243,7 @@ class LonaDB {
return $this->sendRequest("get_permissions_raw", $data); return $this->sendRequest("get_permissions_raw", $data);
} }
public function addPermission($name){ public function addPermission($name, $permission){
$data = [ $data = [
"permission" => [ "permission" => [
"user" => $name, "user" => $name,

View File

@ -1,174 +1,220 @@
import json
import socket import socket
import random import json
import hashlib import hashlib
from Crypto.Cipher import AES import os
from Crypto.Random import get_random_bytes import asyncio
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
class LonaDB: class LonaDB:
def __init__(self, host, port, name, password): def __init__(self, host, port, name, password):
#Import all connection details # Import connection details
self.host = host self.host = host
self.port = port self.port = port
self.name = name self.name = name
self.password = password self.password = password
def make_id(self, length): def makeid(self, length):
# Generate a random string of specified length
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz' characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'
#Generate random string of desired lenght return ''.join(os.urandom(1).decode(errors='ignore') for _ in range(length))
return ''.join(random.choice(characters) for _ in range(length))
async def send_request(self, action, data): async def send_request(self, action, data):
#Generate ProcessID # Generate ProcessID
process_id = self.make_id(5) process_id = self.makeid(5)
#Generate encryption key for passwords # Generate encryptionKey for encrypting passwords
encryption_key = hashlib.sha256(process_id.encode()).digest().hex() encryption_key = hashlib.sha256(process_id.encode()).digest()
#Check if we have to encrypt something else
# Encrypt password if needed
if action == "create_user": if action == "create_user":
data["user"]["password"] = await self.encrypt_password(data["user"]["password"], encryption_key) data['user']['password'] = self.encrypt_password(data['user']['password'], encryption_key)
elif action == "check_password": elif action == "check_password":
data["checkPass"]["pass"] = await self.encrypt_password(data["checkPass"]["pass"], encryption_key) data['checkPass']['pass'] = self.encrypt_password(data['checkPass']['pass'], encryption_key)
#Encrypt password
encrypted_password = await self.encrypt_password(self.password, encryption_key) # Encrypt the login password
#Generate request encrypted_password = self.encrypt_password(self.password, encryption_key)
request = json.dumps({
# Create the request data
request_data = {
"action": action, "action": action,
"login": { "login": {
"name": self.name, "name": self.name,
"password": encrypted_password "password": encrypted_password
}, },
"process": process_id, "process": process_id
**data }
}) request_data.update(data)
#Send request
with socket.create_connection((self.host, self.port)) as s:
s.sendall(request.encode())
response = s.recv(1024).decode()
#Return response
return json.loads(response)
async def encrypt_password(self, password, key): # Convert request data to JSON
#Generate IV and cipher request_json = json.dumps(request_data)
iv = get_random_bytes(16)
cipher = AES.new(key.encode(), AES.MODE_CBC, iv) # Send request via socket
#Encrypt reader, writer = await asyncio.open_connection(self.host, self.port)
encrypted = cipher.encrypt(password.encode())
#Return IV and encrypted value writer.write(request_json.encode())
await writer.drain()
# Read the response from the server
response_data = await reader.read(2048)
writer.close()
await writer.wait_closed()
# Parse the response and return it
return json.loads(response_data.decode())
def encrypt_password(self, password, key):
# Generate IV and create cipher
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
# Pad password to block size (16 bytes)
padding_length = 16 - len(password) % 16
password_padded = password + chr(padding_length) * padding_length
# Encrypt the password
encrypted = encryptor.update(password_padded.encode()) + encryptor.finalize()
# Return IV and encrypted password
return iv.hex() + ':' + encrypted.hex() return iv.hex() + ':' + encrypted.hex()
#All other functions work the same # All other functions work similarly
def create_function(self, name, content): async def create_function(self, name, content):
#Generate request to send to the database
data = { data = {
'function': { "function": {
'name': name, "name": name,
'content': content "content": content
} }
} }
#Send request to the database and return the response return await self.send_request("add_function", data)
return self.send_request('add_function', data)
def execute_function(self, name): async def execute_function(self, name):
data = {'name': name}
return self.send_request('execute_function', data)
def get_tables(self, user):
data = {'user': user}
return self.send_request('get_tables', data)
def get_table_data(self, table):
data = {'table': table}
return self.send_request('get_table_data', data)
def delete_table(self, table):
data = {'table': {'name': table}}
return self.send_request('delete_table', data)
def create_table(self, table):
data = {'table': {'name': table}}
return self.send_request('create_table', data)
def set_variable(self, table, name, value):
data = { data = {
'table': {'name': table}, "name": name
'variable': { }
'name': name, return await self.send_request("execute_function", data)
'value': value
async def delete_function(self, name):
data = {
"function": {
"name": name
} }
} }
return self.send_request('set_variable', data) return await self.send_request("delete_function", data)
def remove_variable(self, table, name): async def get_tables(self, user):
data = { data = {
'table': {'name': table}, "user": user
'variable': {'name': name}
} }
return self.send_request('remove_variable', data) return await self.send_request("get_tables", data)
def get_variable(self, table, name): async def get_table_data(self, table):
data = { data = {
'table': {'name': table}, "table": table
'variable': {'name': name}
} }
return self.send_request('get_variable', data) return await self.send_request("get_table_data", data)
def get_users(self): async def delete_table(self, table):
data = {
"table": {"name": table}
}
return await self.send_request("delete_table", data)
async def create_table(self, table):
data = {
"table": {"name": table}
}
return await self.send_request("create_table", data)
async def set(self, table, name, value):
data = {
"table": {"name": table},
"variable": {
"name": name,
"value": value
}
}
return await self.send_request("set_variable", data)
async def delete(self, table, name):
data = {
"table": {"name": table},
"variable": {"name": name}
}
return await self.send_request("remove_variable", data)
async def get(self, table, name):
data = {
"table": {"name": table},
"variable": {"name": name}
}
return await self.send_request("get_variable", data)
async def get_users(self):
data = {} data = {}
return self.send_request('get_users', data) return await self.send_request("get_users", data)
def create_user(self, name, password): async def create_user(self, name, passw):
data = { data = {
'user': { "user": {
'name': name, "name": name,
'password': password "password": passw
} }
} }
return self.send_request('create_user', data) return await self.send_request("create_user", data)
def delete_user(self, name): async def delete_user(self, name):
data = {'user': {'name': name}}
return self.send_request('delete_user', data)
def check_password(self, name, password):
data = { data = {
'checkPass': { "user": {
'name': name, "name": name
'pass': password
} }
} }
return self.send_request('check_password', data) return await self.send_request("delete_user", data)
def check_permission(self, name, permission): async def check_password(self, name, passw):
data = { data = {
'permission': { "checkPass": {
'user': name, "name": name,
'name': permission "pass": passw
} }
} }
return self.send_request('check_permission', data) return await self.send_request("check_password", data)
def remove_permission(self, name, permission): async def check_permission(self, name, permission):
data = { data = {
'permission': { "permission": {
'user': name, "user": name,
'name': permission "name": permission
} }
} }
return self.send_request('remove_permission', data) return await self.send_request("check_permission", data)
def get_permissions_raw(self, name): async def remove_permission(self, name, permission):
data = {'user': name}
return self.send_request('get_permissions_raw', data)
def add_permission(self, name, permission):
data = { data = {
'permission': { "permission": {
'user': name, "user": name,
'name': permission "name": permission
} }
} }
return self.send_request('add_permission', data) return await self.send_request("remove_permission", data)
def eval(self, func): async def get_permissions_raw(self, name):
data = {'function': func} data = {
return self.send_request('eval', data) "user": name
}
return await self.send_request("get_permissions_raw", data)
async def add_permission(self, name, permission):
data = {
"permission": {
"user": name,
"name": permission
}
}
return await self.send_request("add_permission", data)
async def eval(self, func):
data = {
"function": func
}
return await self.send_request("eval", data)

View File

@ -6,7 +6,7 @@ long_description = (this_directory / "README.md").read_text()
setup( setup(
name="lonadb-client", name="lonadb-client",
version="2.3", version="2.4",
author="Lona-Development", author="Lona-Development",
author_email="collin@lona-development.org", author_email="collin@lona-development.org",
description="A client library for interacting with LonaDB server", description="A client library for interacting with LonaDB server",
@ -28,6 +28,7 @@ setup(
keywords="lonadb client database", keywords="lonadb client database",
platforms="any", platforms="any",
install_requires=[ install_requires=[
"pycryptodome>=3.10.1" "cryptography>=3.4.7",
] "asyncio>=3.4.3"
],
) )