From e39245b727a29f3616e87eb1dba5838129a98989 Mon Sep 17 00:00:00 2001 From: none Date: Mon, 19 Feb 2024 19:13:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B5=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=82=D0=BE=D1=82=D0=B8=D0=BF=D1=8B=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=D0=BE=D0=B2,=20?= =?UTF-8?q?=D1=81=D0=B5=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain_check.py | 8 ++++ main.py | 30 ++++++++++++++ network.py | 47 ++++++++++++++++++++++ setup.py | 2 + site_creator.py | 40 +++++++++++++++++++ verify.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 230 insertions(+) create mode 100644 domain_check.py create mode 100644 main.py create mode 100644 network.py create mode 100644 site_creator.py create mode 100644 verify.py diff --git a/domain_check.py b/domain_check.py new file mode 100644 index 0000000..26cbc1a --- /dev/null +++ b/domain_check.py @@ -0,0 +1,8 @@ +def domain_ok(domain): + domains = ["jet"] + + if domain.count(".") == 1: + if domain.split(".")[1] in domains: + return True + + return False diff --git a/main.py b/main.py new file mode 100644 index 0000000..b0325a6 --- /dev/null +++ b/main.py @@ -0,0 +1,30 @@ +from os import system, name +from threading import Thread + +# Здесь общий запуск всех файлов и команд + +''' +# Порт для приёма всяких запросов +def reverse_proxy(): + port = 8000 + if name == "posix": + system(f"./bore local {port} --to jetwork.404.mn") + elif name == "nt": + system("") + +# Стартуем проброс порта +rp = Thread(target = reverse_proxy) +rp.start() +''' + +''' +from network import * + +#server(8000) + +if client(8001): + print(1) +else: + print(2) +''' + diff --git a/network.py b/network.py new file mode 100644 index 0000000..790f9f2 --- /dev/null +++ b/network.py @@ -0,0 +1,47 @@ +import socket +import os + +# Здесь идёт обработка всех запросов через сеть + +# TODO: +# 1. [+] Пинг +# 2. [+] Проверка существования .zip сайта +# 3. Передача сайта +# 4. Приём рассылки сайтов + +def server(port): + sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + sock.bind (('127.0.0.1', port)) + + while 1 : + data , addres = sock.recvfrom(1024) + #print (addres[0], addres[1]) + op = data.decode('utf-8') + + if op == "ping": + sock.sendto("Pinged success".encode('utf-8'), addres) + elif op[:3] == "is_": + check = op[3:] + if os.path.exists(f'cached/{check}'): + sock.sendto("exist".encode('utf-8'), addres) + else: + sock.sendto("not exist".encode('utf-8'), addres) + else: + sock.sendto(data, addres) + + +# op = operation +def client(dest_port, op = "ping"): + server = '127.0.0.1', dest_port + + sor = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + sor.sendto((op).encode('utf-8'), server) + sor.settimeout(5) + + try: + data = sor.recv(1024) + res = data.decode('utf-8') + return res + except: + print(f"[:{dest_port}] Недоступен.") + return None diff --git a/setup.py b/setup.py index be192bb..3c788e8 100644 --- a/setup.py +++ b/setup.py @@ -24,6 +24,8 @@ else: # Создаём папку для кэшированных сайтов system("mkdir cached") +system("mkdir verify") +system("mkdir mysites") print("Максимальный размер для кэшированных файлов. (в гигабайтах)") print("Укажите 0 для отключения ограничения.") diff --git a/site_creator.py b/site_creator.py new file mode 100644 index 0000000..f398dfb --- /dev/null +++ b/site_creator.py @@ -0,0 +1,40 @@ +from os import system +from db import * + +print("(1) Создать сайт") +print("(2) Обновить сайт") +print("(3) Сменить тип") +op = input(">> ") + +if op == "1": + from verify import * + from shutil import copyfile, make_archive + from domain_check import * + + print("\nДомены: .jet") + domain = input("Домен сайта: ") + if not domain_ok(domain): + print("Неправильный формат или домен.") + exit() + print("\n(1) Статичный / (2) Динамичный") + type = input("Тип: ") + + system(f"mkdir mysites/{domain}") + key_gen(f"mysites/{domain}") + copyfile(f"mysites/{domain}.pem", f"mysites/{domain}/{domain}.pem") + + if type == "1": + conf = {"type": "static", "ver": 1} + write(conf, f"mysites/{domain}/config.json") + elif type == "2": + port = input("Порт: ") + conf = {"type": "dynamic", "ver": 1, "port": int(port)} + write(conf, f"mysites/{domain}/config.json") + + make_archive(f"mysites/{domain}", "zip", f"mysites/{domain}") + sign(f"mysites/{domain}.zip", f"mysites/{domain}.key", f"mysites/{domain}") + +elif op == "2": + pass +elif op == "3": + pass diff --git a/verify.py b/verify.py new file mode 100644 index 0000000..36167b2 --- /dev/null +++ b/verify.py @@ -0,0 +1,103 @@ +# name - директория и имя (path/name.key) +def key_gen(name): + from cryptography.hazmat.backends import default_backend + from cryptography.hazmat.primitives import serialization + from cryptography.hazmat.primitives.asymmetric import rsa + + # Generate the public/private key pair. + private_key = rsa.generate_private_key( + public_exponent = 65537, + key_size = 4096, + backend = default_backend(), + ) + + # Save the private key to a file. + with open(f'{name}.key', 'wb') as f: + f.write( + private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.TraditionalOpenSSL, + encryption_algorithm=serialization.NoEncryption(), + ) + ) + + # Save the public key to a file. + with open(f'{name}.pem', 'wb') as f: + f.write( + private_key.public_key().public_bytes( + encoding = serialization.Encoding.PEM, + format = serialization.PublicFormat.SubjectPublicKeyInfo, + ) + ) + + + +def sign(file, priv_key, sig): + import base64 + from cryptography.hazmat.backends import default_backend + from cryptography.hazmat.primitives import hashes + from cryptography.hazmat.primitives import serialization + from cryptography.hazmat.primitives.asymmetric import padding + + # Load the private key. + with open(priv_key, 'rb') as key_file: + private_key = serialization.load_pem_private_key( + key_file.read(), + password = None, + backend = default_backend(), + ) + + # Load the contents of the file to be signed. + with open(file, 'rb') as f: + payload = f.read() + + # Sign the payload file. + signature = base64.b64encode( + private_key.sign( + payload, + padding.PSS( + mgf = padding.MGF1(hashes.SHA256()), + salt_length = padding.PSS.MAX_LENGTH, + ), + hashes.SHA256(), + ) + ) + + with open(f'{sig}.sig', 'wb') as f: + f.write(signature) + + + +def verify(file, pub_key, sig): + import base64 + import cryptography.exceptions + from cryptography.hazmat.backends import default_backend + from cryptography.hazmat.primitives import hashes + from cryptography.hazmat.primitives.asymmetric import padding + from cryptography.hazmat.primitives.serialization import load_pem_public_key + + # Load the public key. + with open(pub_key, 'rb') as f: + public_key = load_pem_public_key(f.read(), default_backend()) + + # Load the payload contents and the signature. + with open(file, 'rb') as f: + payload_contents = f.read() + with open(sig, 'rb') as f: + signature = base64.b64decode(f.read()) + + # Perform the verification. + try: + public_key.verify( + signature, + payload_contents, + padding.PSS( + mgf = padding.MGF1(hashes.SHA256()), + salt_length = padding.PSS.MAX_LENGTH, + ), + hashes.SHA256(), + ) + return True + except cryptography.exceptions.InvalidSignature as e: + print('ERROR: Payload and/or signature files failed verification!') + return False