From 9c10835f28c2195ba57a500ec315c177a0bfb566 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Jul 2024 11:28:36 +0300 Subject: [PATCH] up --- db.py | 19 ++++++++++++++++++ domain_check.py | 23 ++++++++++++++++++++++ main.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ nginx.conf | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 db.py create mode 100644 domain_check.py create mode 100644 main.py create mode 100644 nginx.conf diff --git a/db.py b/db.py new file mode 100644 index 0000000..9714673 --- /dev/null +++ b/db.py @@ -0,0 +1,19 @@ +import os +import json + +if not os.path.exists('domains.json'): + db = {} + js = json.dumps(db, indent=2) + with open("domains.json", "w") as outfile: + outfile.write(js) + print('Created new domains.json') + +def read(file = 'domains.json'): + with open(file, "r", encoding="utf-8") as openfile: + db = json.load(openfile) + return db + +def write(db, file = 'domains.json'): + js = json.dumps(db, indent=2, ensure_ascii=False) + with open(file, "w", encoding="utf-8") as outfile: + outfile.write(js) diff --git a/domain_check.py b/domain_check.py new file mode 100644 index 0000000..492c4b7 --- /dev/null +++ b/domain_check.py @@ -0,0 +1,23 @@ +from re import compile, sub +domains = ['jet', 'mirror', 'org', 'info', 'news', 'me'] + +def domain_ok(domain): + global domains + if domain.count('.') == 1: + if domain.split('.')[1] in domains: + # ../some => some + # Защита от проверки папок выше, чем нужно и др. + regex = compile('[^a-z0-9.-]') + c_domain = regex.sub('', domain) + if domain == c_domain: + return domain + + return False + +def domain_list(): + global domains + out = '' + for i in domains: + out += f'{i}, ' + out = out[:-2] + return out diff --git a/main.py b/main.py new file mode 100644 index 0000000..8d910c2 --- /dev/null +++ b/main.py @@ -0,0 +1,52 @@ +from fastapi import FastAPI, HTTPException +from uuid import uuid4 +app = FastAPI() + +# If you don't need domain check (jetwork): +# --- comment +# multi-comments uncomment + +from db import * +from domain_check import * + +def set_nginx(url: str, port: int): + f = open('/etc/nginx/nginx.conf') + + +@app.post('/api/create/{domain}/{port}') +def create(domain: str, port: int): + # --- + if domain_ok(domain): + db = read() + if domain not in db: + token = str(uuid4()) + db[domain] = token + write(db) + return {'token': token} + else: + raise HTTPException(status_code=400, detail="Domain exist") + else: + raise HTTPException(status_code=400, detail="Bad domain") + # --- + ''' + db = read() + if domain not in db: + token = str(uuid4()) + db[domain] = token + write(db) + return {'token': token} + else: + raise HTTPException(status_code=400, detail="Domain exist") + ''' + +@app.post('/api/set/{domain}/{port}/{token}') +def set(domain: str, port: int, token: str): + return 200 + +@app.post('/api/del/{domain}/{port}/{token}') +def set(domain: str, port: int, token: str): + return 200 + +if __name__ == '__main__': + import uvicorn + uvicorn.run(app, host='127.0.0.1', port=8000) diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..2a1238a --- /dev/null +++ b/nginx.conf @@ -0,0 +1,38 @@ +user www-data; +worker_processes auto; +pid /run/nginx.pid; +error_log /var/log/nginx/error.log; +include /etc/nginx/modules-enabled/*.conf; +events { + worker_connections 768; +} +http { + sendfile on; + tcp_nopush on; + types_hash_max_size 2048; + include /etc/nginx/mime.types; + default_type application/octet-stream; + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE + ssl_prefer_server_ciphers on; + access_log /var/log/nginx/access.log; + gzip on; + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; + + +server +{ +listen 80; +server_name jet-d.del.pw; +server_name_in_redirect off; +proxy_set_header Host $host:$server_port; + +location / { +proxy_set_header X-Real-IP $remote_addr; +proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; +proxy_pass http://127.0.0.1:8000; +}} + + + +}