Commenting and changing Git URLs

This commit is contained in:
Collin 2024-09-25 18:39:56 +02:00
parent b987a4c373
commit 1b11741450
8 changed files with 109 additions and 59 deletions

2
.gitignore vendored
View File

@ -4,4 +4,4 @@ Python/venv
Python/dist Python/dist
Python/buildc Python/buildc
Python/lonadb_client.egg-info Python/lonadb_client.egg-info
token Python/token

View File

@ -2,6 +2,7 @@ const crypto = require('crypto');
class LonaDB { class LonaDB {
constructor(host, port, name, password) { constructor(host, port, name, password) {
//Import all connection details
this.host = host; this.host = host;
this.port = port; this.port = port;
this.name = name; this.name = name;
@ -12,21 +13,21 @@ class LonaDB {
let result = ''; let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz';
let counter = 0; let counter = 0;
//Add random characters to result until it has reached the desired length
while (counter < length) { while (counter < length) {
result += characters[Math.floor(Math.random() * characters.length)]; result += characters[Math.floor(Math.random() * characters.length)];
counter += 1; counter += 1;
} }
return result; return result;
} }
sendRequest = async function(action, data) { sendRequest = async function(action, data) {
let net = require('net'); let net = require('net');
//Generate ProcessID
let processID = this.makeid(5); let processID = this.makeid(5);
//Generate encryptionKey for encrypting passwords
let encryptionKey = crypto.createHash('sha256').update(processID).digest('base64'); let encryptionKey = crypto.createHash('sha256').update(processID).digest('base64');
//Check if we need to encrypt something other than our own password
switch(action){ switch(action){
case "create_user": case "create_user":
data.user.password = await this.encryptPassword(data.user.password, encryptionKey); data.user.password = await this.encryptPassword(data.user.password, encryptionKey);
@ -35,9 +36,9 @@ class LonaDB {
data.checkPass.pass = await this.encryptPassword(data.checkPass.pass, encryptionKey); data.checkPass.pass = await this.encryptPassword(data.checkPass.pass, encryptionKey);
break; break;
} }
//Encrypt password
let encryptedPassword = await this.encryptPassword(this.password, encryptionKey); let encryptedPassword = await this.encryptPassword(this.password, encryptionKey);
//Generate request
let request = JSON.stringify({ let request = JSON.stringify({
action, action,
login: { login: {
@ -49,19 +50,21 @@ class LonaDB {
}); });
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
//Create socket
let socket = net.createConnection({ let socket = net.createConnection({
host: this.host, host: this.host,
port: this.port port: this.port
}, () => { }, () => {
//Send request
socket.write(request); socket.write(request);
}); });
let response = ''; let response = '';
//When server sends data, collect it
socket.on('data', (chunk) => { socket.on('data', (chunk) => {
response += chunk; response += chunk;
}); });
//Once the server closes the connection, return the data
socket.on('end', () => { socket.on('end', () => {
resolve(JSON.parse(response)); resolve(JSON.parse(response));
}); });
@ -73,23 +76,26 @@ class LonaDB {
} }
encryptPassword(password, key) { encryptPassword(password, key) {
//Generate IV and create cipher
const iv = crypto.randomBytes(16); const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key, 'base64'), iv); const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key, 'base64'), iv);
//Encrypt
let encrypted = cipher.update(password); let encrypted = cipher.update(password);
encrypted = Buffer.concat([encrypted, cipher.final()]); encrypted = Buffer.concat([encrypted, cipher.final()]);
//Return IV and encrypted value
return iv.toString('hex') + ':' + encrypted.toString('hex'); return iv.toString('hex') + ':' + encrypted.toString('hex');
} }
//All other functions work the same
createFunction(name, content) { createFunction(name, content) {
//Generate request to send
const data = { const data = {
"function": { "function": {
"name": name, "name": name,
"content": content "content": content
} }
}; };
//Send request to the database and return the response
return this.sendRequest("add_function", data); return this.sendRequest("add_function", data);
} }

View File

@ -1,14 +1,14 @@
{ {
"name": "lonadb-client", "name": "lonadb-client",
"version": "4.5.2", "version": "4.5.3",
"description": "A client for the LonaDB Prototype", "description": "JavaScript client for LonaDB",
"main": "Client.js", "main": "Client.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/LonaDB/JavaScript-Client" "url": "https://git.lona-development.org/Lona-Development/Clients"
}, },
"keywords": [ "keywords": [
"database", "database",
@ -17,7 +17,7 @@
"variable", "variable",
"variables" "variables"
], ],
"author": "Collin Buchkamer", "author": "Lona-Development",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"net": "^1.0.2" "net": "^1.0.2"

View File

@ -1,12 +1,14 @@
<?php <?php
class LonaDB { class LonaDB {
//Create all needed variables
private $host; private $host;
private $port; private $port;
private $name; private $name;
private $password; private $password;
public function __construct($host, $port, $name, $password) { public function __construct($host, $port, $name, $password) {
//Import all connection details
$this->host = $host; $this->host = $host;
$this->port = $port; $this->port = $port;
$this->name = $name; $this->name = $name;
@ -17,40 +19,38 @@ class LonaDB {
$result = ""; $result = "";
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"; $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
$counter = 0; $counter = 0;
//Add random characters from $characters to $result until it has reached the dired lenght
while ($counter < $length) { while ($counter < $length) {
$result .= $characters[mt_rand(0, strlen($characters) - 1)]; $result .= $characters[mt_rand(0, strlen($characters) - 1)];
$counter += 1; $counter += 1;
} }
return $result; return $result;
} }
private function encryptPassword($password, $key) { private function encryptPassword($password, $key) {
//Hash our encryption key (ProcessID)
$keyBuffer = hash('sha256', $key, true); $keyBuffer = hash('sha256', $key, true);
//Generate IV
$iv = openssl_random_pseudo_bytes(16); $iv = openssl_random_pseudo_bytes(16);
//Encrypt
$encrypted = openssl_encrypt($password, 'aes-256-cbc', $keyBuffer, OPENSSL_RAW_DATA, $iv); $encrypted = openssl_encrypt($password, 'aes-256-cbc', $keyBuffer, OPENSSL_RAW_DATA, $iv);
//Add the IV to the encrypted value
$encryptedString = bin2hex($iv) . ':' . bin2hex($encrypted); $encryptedString = bin2hex($iv) . ':' . bin2hex($encrypted);
return $encryptedString; return $encryptedString;
} }
private function sendRequest($action, $data) { private function sendRequest($action, $data) {
//Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) { //Check if socket was created successfully
return ["err" => socket_strerror(socket_last_error())]; if (!$socket) return ["err" => socket_strerror(socket_last_error())];
} //Try connecting to the database
$result = socket_connect($socket, $this->host, $this->port); $result = socket_connect($socket, $this->host, $this->port);
if ($result === false) { //Check if connection was successfull
return ["err" => socket_strerror(socket_last_error($socket))]; if (!$result) return ["err" => socket_strerror(socket_last_error($socket))];
} //Generate ProcessID
$processID = $this->makeid(5); $processID = $this->makeid(5);
//Check if we need to encrypt something other than our own password
switch($action){ switch($action){
case "create_user": case "create_user":
$data['user']['password'] = $this->encryptPassword($data['user']['password'], $processID); $data['user']['password'] = $this->encryptPassword($data['user']['password'], $processID);
@ -59,9 +59,9 @@ class LonaDB {
$data['checkPass']['pass'] = $this->encryptPassword($data['checkPass']['pass'], $processID); $data['checkPass']['pass'] = $this->encryptPassword($data['checkPass']['pass'], $processID);
break; break;
} }
//Encrypt password
$encryptedPassword = $this->encryptPassword($this->password, $processID); $encryptedPassword = $this->encryptPassword($this->password, $processID);
//Generate request array
$request = json_encode([ $request = json_encode([
"action" => $action, "action" => $action,
"login" => [ "login" => [
@ -70,23 +70,26 @@ class LonaDB {
], ],
"process" => $processID "process" => $processID
] + $data); ] + $data);
//Send request to database
socket_write($socket, $request, strlen($request)); socket_write($socket, $request, strlen($request));
//Read response form database
$response = socket_read($socket, 2048); $response = socket_read($socket, 2048);
//Close socket
socket_close($socket); socket_close($socket);
//Give back the response
return json_decode($response, true); return json_decode($response, true);
} }
//All other functions work the same
public function createFunction($name, $content) { public function createFunction($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 $this->sendRequest("add_function", $data); return $this->sendRequest("add_function", $data);
} }

View File

@ -1,43 +1,71 @@
import json import json
import socket import socket
import random
import hashlib
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
class LonaDB: class LonaDB:
def __init__(self, host, port, name, password): def __init__(self, host, port, name, password):
#Import all 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 make_id(self, length):
import random characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'
import string #Generate random string of desired lenght
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length)) return ''.join(random.choice(characters) for _ in range(length))
def send_request(self, action, data): async def send_request(self, action, data):
#Generate ProcessID
process_id = self.make_id(5) process_id = self.make_id(5)
#Generate encryption key for passwords
encryption_key = hashlib.sha256(process_id.encode()).digest().hex()
#Check if we have to encrypt something else
if action == "create_user":
data["user"]["password"] = await self.encrypt_password(data["user"]["password"], encryption_key)
elif action == "check_password":
data["checkPass"]["pass"] = await self.encrypt_password(data["checkPass"]["pass"], encryption_key)
#Encrypt password
encrypted_password = await self.encrypt_password(self.password, encryption_key)
#Generate request
request = json.dumps({ request = json.dumps({
'action': action, "action": action,
'login': { "login": {
'name': self.name, "name": self.name,
'password': self.password "password": encrypted_password
}, },
'process': process_id, "process": process_id,
**data **data
}) })
#Send request
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: with socket.create_connection((self.host, self.port)) as s:
s.connect((self.host, self.port))
s.sendall(request.encode()) s.sendall(request.encode())
response = s.recv(4096).decode() response = s.recv(1024).decode()
#Return response
return json.loads(response) return json.loads(response)
async def encrypt_password(self, password, key):
#Generate IV and cipher
iv = get_random_bytes(16)
cipher = AES.new(key.encode(), AES.MODE_CBC, iv)
#Encrypt
encrypted = cipher.encrypt(password.encode())
#Return IV and encrypted value
return iv.hex() + ':' + encrypted.hex()
#All other functions work the same
def create_function(self, name, content): 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 self.send_request('add_function', data) return self.send_request('add_function', data)
def execute_function(self, name): def execute_function(self, name):

View File

@ -7,6 +7,7 @@ from Crypto.Random import get_random_bytes
class LonaDB: class LonaDB:
def __init__(self, host, port, name, password): def __init__(self, host, port, name, password):
#Import all connection details
self.host = host self.host = host
self.port = port self.port = port
self.name = name self.name = name
@ -14,19 +15,22 @@ class LonaDB:
def make_id(self, length): def make_id(self, length):
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz' characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'
#Generate random string of desired lenght
return ''.join(random.choice(characters) 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
process_id = self.make_id(5) process_id = self.make_id(5)
#Generate encryption key for passwords
encryption_key = hashlib.sha256(process_id.encode()).digest().hex() encryption_key = hashlib.sha256(process_id.encode()).digest().hex()
#Check if we have to encrypt something else
if action == "create_user": if action == "create_user":
data["user"]["password"] = await self.encrypt_password(data["user"]["password"], encryption_key) data["user"]["password"] = await 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"] = await self.encrypt_password(data["checkPass"]["pass"], encryption_key)
#Encrypt password
encrypted_password = await self.encrypt_password(self.password, encryption_key) encrypted_password = await self.encrypt_password(self.password, encryption_key)
#Generate request
request = json.dumps({ request = json.dumps({
"action": action, "action": action,
"login": { "login": {
@ -36,25 +40,32 @@ class LonaDB:
"process": process_id, "process": process_id,
**data **data
}) })
#Send request
with socket.create_connection((self.host, self.port)) as s: with socket.create_connection((self.host, self.port)) as s:
s.sendall(request.encode()) s.sendall(request.encode())
response = s.recv(1024).decode() response = s.recv(1024).decode()
#Return response
return json.loads(response) return json.loads(response)
async def encrypt_password(self, password, key): async def encrypt_password(self, password, key):
#Generate IV and cipher
iv = get_random_bytes(16) iv = get_random_bytes(16)
cipher = AES.new(key.encode(), AES.MODE_CBC, iv) cipher = AES.new(key.encode(), AES.MODE_CBC, iv)
#Encrypt
encrypted = cipher.encrypt(password.encode()) encrypted = cipher.encrypt(password.encode())
#Return IV and encrypted value
return iv.hex() + ':' + encrypted.hex() return iv.hex() + ':' + encrypted.hex()
#All other functions work the same
def create_function(self, name, content): 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 self.send_request('add_function', data) return self.send_request('add_function', data)
def execute_function(self, name): def execute_function(self, name):

2
Python/publish.sh Executable file
View File

@ -0,0 +1,2 @@
./venv/bin/python setup.py sdist bdist_wheel
./venv/bin/twine upload --username __token__ --password "$(cat ./token)" dist/*

View File

@ -6,11 +6,11 @@ long_description = (this_directory / "README.md").read_text()
setup( setup(
name="lonadb-client", name="lonadb-client",
version="2.2", version="2.3",
author="Collin Buchkamer", 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",
url="https://github.com/LonaDB/Clients", url="https://git.lona-development.org/Lona-Development/Clients",
packages=["lonadb_client"], packages=["lonadb_client"],
long_description=long_description, long_description=long_description,
long_description_content_type='text/markdown', long_description_content_type='text/markdown',