🔃 Sync Hub v0.56

This commit is contained in:
jokob-sk
2024-06-05 08:05:20 +10:00
parent 0f724e6a1d
commit bd11c2ecdf
2 changed files with 71 additions and 27 deletions

View File

@@ -4,16 +4,17 @@ ARG INSTALL_DIR=/app
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
RUN apk add --no-cache bash python3 python3-dev \ RUN apk add --no-cache bash python3 \
&& python -m venv /opt/venv && python -m venv /opt/venv
# Enable venv # Enable venv
ENV PATH="/opt/venv/bin:$PATH" ENV PATH="/opt/venv/bin:$PATH"
COPY . ${INSTALL_DIR}/ COPY . ${INSTALL_DIR}/
RUN pip install requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython cryptography \ RUN pip install pycryptodome requests paho-mqtt scapy cron-converter pytz json2table dhcp-leases pyunifi speedtest-cli chardet python-nmap dnspython \
&& bash -c "find ${INSTALL_DIR} -type d -exec chmod 750 {} \;" \ && bash -c "find ${INSTALL_DIR} -type d -exec chmod 750 {} \;" \
&& bash -c "find ${INSTALL_DIR} -type f -exec chmod 640 {} \;" \ && bash -c "find ${INSTALL_DIR} -type f -exec chmod 640 {} \;" \
&& bash -c "find ${INSTALL_DIR} -type f \( -name '*.sh' -o -name '*.py' -o -name 'speedtest-cli' \) -exec chmod 750 {} \;" && bash -c "find ${INSTALL_DIR} -type f \( -name '*.sh' -o -name '*.py' -o -name 'speedtest-cli' \) -exec chmod 750 {} \;"

View File

@@ -13,9 +13,10 @@ import json
import time import time
from pathlib import Path from pathlib import Path
import requests import requests
from cryptography.fernet import Fernet
import base64 import base64
import hashlib import hashlib
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import conf import conf
@@ -805,37 +806,79 @@ def collect_lang_strings(json, pref, stringSqlParams):
# Cryptography # Cryptography
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# FERET - Requires C compiler-------------------------------------------------------------------------
# def prepare_key(encryption_key):
# if(len(encryption_key) < 32):
# encryption_key = (int((32 / len(encryption_key)))+1 )*encryption_key
# key_bytearray = bytearray(encryption_key[:32], 'ASCII')
# return base64.urlsafe_b64encode(key_bytearray)
# def encrypt_data(data, encryption_key):
# fernet = Fernet(prepare_key(encryption_key))
# # then use the Fernet class instance
# # to encrypt the string string must
# # be encoded to byte string before encryption
# encrypted_data = fernet.encrypt(data.encode())
# return encrypted_data
# def decrypt_data(data, encryption_key):
# fernet = Fernet(prepare_key(encryption_key))
# # decrypt the encrypted string with the
# # Fernet instance of the key,
# # that was used for encrypting the string
# # encoded byte string is returned by decrypt method,
# # so decode it to string with decode methods
# decrypted_data = fernet.decrypt(data).decode()
# return decrypted_data
# SIMPLE CRYPT - requeres C compiler -------------------------------------------------------------------------
# def prepare_key(encryption_key):
# if len(encryption_key) < 32:
# encryption_key = (encryption_key * ((32 // len(encryption_key)) + 1))[:32]
# return encryption_key
# def encrypt_data(data, encryption_key):
# key = prepare_key(encryption_key)
# encrypted_data = encrypt(key, data)
# return encrypted_data
# def decrypt_data(data, encryption_key):
# key = prepare_key(encryption_key)
# decrypted_data = decrypt(key, data).decode('utf-8')
# return decrypted_data
# pycryptodome -------------------------------------------------------------------------
def prepare_key(encryption_key): def prepare_key(encryption_key):
if(len(encryption_key) < 32): key = hashlib.sha256(encryption_key.encode()).digest()
encryption_key = (int((32 / len(encryption_key)))+1 )*encryption_key return key
key_bytearray = bytearray(encryption_key[:32], 'ASCII')
return base64.urlsafe_b64encode(key_bytearray)
def encrypt_data(data, encryption_key): def encrypt_data(data, encryption_key):
key = prepare_key(encryption_key)
fernet = Fernet(prepare_key(encryption_key)) cipher = AES.new(key, AES.MODE_CBC)
ct_bytes = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
# then use the Fernet class instance iv = base64.b64encode(cipher.iv).decode('utf-8')
# to encrypt the string string must ct = base64.b64encode(ct_bytes).decode('utf-8')
# be encoded to byte string before encryption return iv + ct
encrypted_data = fernet.encrypt(data.encode())
return encrypted_data
def decrypt_data(data, encryption_key): def decrypt_data(data, encryption_key):
key = prepare_key(encryption_key)
iv = base64.b64decode(data[:24])
fernet = Fernet(prepare_key(encryption_key)) ct = base64.b64decode(data[24:])
cipher = AES.new(key, AES.MODE_CBC, iv)
# decrypt the encrypted string with the pt = unpad(cipher.decrypt(ct), AES.block_size)
# Fernet instance of the key, return pt.decode('utf-8')
# that was used for encrypting the string
# encoded byte string is returned by decrypt method,
# so decode it to string with decode methods
decrypted_data = fernet.decrypt(data).decode()
return decrypted_data
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Misc # Misc