add config generator

This commit is contained in:
sergey 2024-10-20 18:53:02 +03:00
parent 2af44c3bb0
commit 1b805b335a
19 changed files with 5609 additions and 2 deletions

View File

@ -1,7 +1,8 @@
from .config import conf, STATIC_DIR
from .config import conf, STATIC_DIR, TEMPLATES_DIR
__all__ = [
"conf",
STATIC_DIR,
TEMPLATES_DIR,
]

View File

@ -2,7 +2,7 @@ from fastapi import APIRouter
from .swagger import router as swagger_router
from config import conf
from .web import router as web_router
router = APIRouter()
@ -10,3 +10,7 @@ router.include_router(
swagger_router,
prefix=conf.prefix.swagger,
)
router.include_router(
web_router,
)

22
routers/web/__init__.py Normal file
View File

@ -0,0 +1,22 @@
from fastapi import APIRouter
from .check_ports import router as check_ports_router
from .start_page import router as start_page_router
from .mikrotik_conf import router as mikrotik_conf_router
router = APIRouter(
tags=["Web"],
)
router.include_router(
check_ports_router,
prefix="/web/check_ports",
)
router.include_router(
mikrotik_conf_router,
prefix="/web/mikrotik_conf",
)
router.include_router(
start_page_router,
prefix="",
)

View File

@ -0,0 +1,34 @@
from fastapi import APIRouter
from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
from config import TEMPLATES_DIR
from scripts import check_available_ports
router = APIRouter()
templates = Jinja2Templates(directory=TEMPLATES_DIR)
@router.get("", response_class=HTMLResponse)
async def check_ports(request: Request):
return templates.TemplateResponse(
"body-check_ports.html",
{
"request": request,
},
)
@router.get("/start")
async def start_check_ports(request: Request, inputText: str):
results = check_available_ports(inputText, result_type="text")
return templates.TemplateResponse(
"body-check_ports.html",
{
"request": request,
"results": results,
},
)

View File

@ -0,0 +1,49 @@
from fastapi import APIRouter, Form
from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
from config import TEMPLATES_DIR
from scripts import check_available_ports
router = APIRouter()
templates = Jinja2Templates(directory=TEMPLATES_DIR)
@router.get("", response_class=HTMLResponse)
async def mikrotik_conf(request: Request):
return templates.TemplateResponse(
"mikrotik_conf/body-mikrotik_conf.html",
{
"request": request,
},
)
@router.get("/get-ports", response_class=HTMLResponse)
async def mikrotik_conf_generate(request: Request, model: str):
if model == "rb5009ug":
return templates.TemplateResponse(
"mikrotik_conf/ports_rb5009ug.html",
{
"request": request,
},
)
@router.get("/generate")
async def mikrotik_conf_generate(request: Request, inputText: str):
return templates.TemplateResponse(
"mikrotik_conf/body-mikrotik_conf.html",
{
"request": request,
},
)
@router.post("/process-text")
async def test(inputText: str = Form(...)):
processed_text = inputText.upper()
return processed_text

19
routers/web/start_page.py Normal file
View File

@ -0,0 +1,19 @@
from fastapi import APIRouter
from starlette.requests import Request
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
from config import TEMPLATES_DIR
router = APIRouter()
templates = Jinja2Templates(directory=TEMPLATES_DIR)
@router.get("/", response_class=HTMLResponse)
async def start_page(request: Request):
return templates.TemplateResponse(
"base.html",
{
"request": request,
},
)

5
scripts/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .check_available_ports import start_check as check_available_ports
__all__ = [
"check_available_ports",
]

View File

@ -0,0 +1,54 @@
import socket
import re
from typing import List, Dict, Union
import logging
def check_conn(ip: str, port: int) -> bool:
logging.info("")
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.settimeout(3)
try:
conn.connect((ip, port))
logging.info(f"{ip}:{port} - True")
return True
except TimeoutError:
logging.warning(f"{ip}:{port} - Timeout")
return False
def parse_input(text: str) -> List[Dict[str, Union[int, str]]]:
logging.info("")
pattern = r"Error;.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d+)/(\w+)"
matches = re.findall(pattern, text)
result = [
{"ip": match[0], "port": int(match[1]), "protocol": match[2]}
for match in matches
]
return result
def start_check(
text: str,
result_type: str = "json",
) -> List[Dict[str, Union[int, str, bool]]] | List[str]:
logging.info("")
result = []
for i in parse_input(text):
if i["protocol"] == "tcp":
if check_conn(i["ip"], i["port"]):
i["result"] = True
result.append(i)
else:
i["result"] = False
result.append(i)
if result_type == "text":
results = []
for i in result:
if i["result"]:
results.append(f"{i['ip']}:{i['port']}/{i['protocol']} - OK")
else:
results.append(f"{i['ip']}:{i['port']}/{i['protocol']} - FAIL")
return results
return result

29
web/static/css/base.css Normal file
View File

@ -0,0 +1,29 @@
header {
background-color: green;
position: relative;
top: 0;
left: 0;
width: 100%;
margin-bottom: 10px;
}
div.header-button{
#background-color: red;
display: inline-block;
float: right;
}
div.body{
position: relative;
}
footer {
background-color: red;
position:fixed;
left: 0;
bottom:0;
width:100%;
text-align: right;
}

5130
web/static/js/htmx.js Normal file

File diff suppressed because it is too large Load Diff

1
web/static/js/htmx.min.js vendored Normal file

File diff suppressed because one or more lines are too long

15
web/templates/base.html Normal file
View File

@ -0,0 +1,15 @@
<!DOCTYPE HTML>
<html>
<head>
<title>SiPi-web</title>
<link href="/static/css/base.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="data:;base64,=">
<script src="/static/js/htmx.js"></script>
</head>
{% include 'header.html' %}
{% include 'body.html' %}
{% include 'footer.html' %}
</html>

View File

@ -0,0 +1,15 @@
<div id="body">
<input type="text" id="inputText" name="inputText" placeholder="Введите значение" />
<button hx-get="/web/check_ports/start" hx-include="#inputText" hx-target='#body'>Проверить</button>
<div id="result">
{% for result in results %}
<p>{{ result }}</p>
{% endfor %}
</div>
</div>

3
web/templates/body.html Normal file
View File

@ -0,0 +1,3 @@
<div id="body">
</div>

View File

@ -0,0 +1,3 @@
<footer class="footer">
Сирожа корпорейтед &#169;
</footer>

15
web/templates/header.html Normal file
View File

@ -0,0 +1,15 @@
<header>
<label>SiPi-web</label>
<div class="header-button">
<button hx-get="/web/check_ports" hx-target='#body'>Сверка по портам</button>
<button hx-get="/web/mikrotik_conf" hx-target='#body'>Конфиг для микрота</button>
<!--
<button hx-get="/web/isp/get_all" hx-target='#body'>Подключения провайдеров</button>
<button hx-get="/web/settings" hx-target='#body'>Настройки</button>
<button hx-get="/web/isp/get_all" hx-target='#body' hx-swap="outerHTML" hx-trigger="click">Площадки</button>
<button hx-get="/web/isp/get_all" hx-target='#body'>Внешние адреса</button>
<button hx-get="/web/isp/get_all" hx-target='#body'>Внутренние подсети</button>
<button hx-get="/web/isp/get_all" hx-target='#body'>Сетевое оборудование</button>
-->
</div>
</header>

View File

@ -0,0 +1,69 @@
<style>
.container {
display: flex;
justify-content: space-between; /* Распределяет пространство между элементами */
}
.box {
width: 30%; /* Ширина каждого блока */
padding: 20px;
background-color: lightblue;
border: 1px solid #000;
text-align: center;
}
</style>
<div class="container">
<div id="body" class="box">
<label for="model">Модель:</label>
<select id="model" name="model" hx-get="/web/mikrotik_conf/get-ports" hx-target="#ports" hx-trigger="change">
<option value="">-- Модель --</option>
<option value="rb5009ug">RB5009</option>
</select>
<div id="ports">
тут облабла
</div>
</div>
<div id="settings" class="box">
<form hx-post="/web/mikrotik_conf/process-data" hx-target="#cisco_tunnel" hx-trigger="submit">
<label for="code">Код площадки</label>
<input type="text" id="code" name="code" required placeholder="XXXX"><br>
<label for="network">16-я подсеть</label>
<input type="text" id="network" name="network" required placeholder="10.xx.0.0/16"><br>
<label for="loopback">loopback</label>
<input type="text" id="loopback" name="loopback" required placeholder="192.168.11.xx"><br>
<label for="tunnel_number">Номер основного тоннеля</label>
<input type="text" id="tunnel_number" name="tunnel_number" required placeholder="999"><br>
<label for="tunnel_network">Подсеть основного тоннеля</label>
<input type="text" id="tunnel_network" name="tunnel_number" required placeholder="192.168.30.xx/30"><br><br>
<label for="isp_ip">IP адрес от провайдера</label>
<input type="text" id="isp_ip" name="isp_ip" required placeholder="123.123.123.123/24"><br>
<label for="isp_gw">Адрес шлюза провайдера</label>
<input type="text" id="isp_gw" name="isp_gw" required placeholder="123.123.123.1"><br><br>
<label for="netadm_pwd">Пароль netadm</label>
<input type="password" id="netadm_pwd" name="netadm_pwd" required placeholder="пароль от уз netadm"><br>
<label for="radius_pwd">Пароль от радиуса</label>
<input type="password" id="radius_pwd" name="radius_pwd" required placeholder="пароль от радиуса"><br>
<label for="snmp_community">SNMP community</label>
<input type="password" id="snmp_community" name="snmp_community" required placeholder="SNMP community"><br>
<button type="submit">Сгенерировать</button>
</form>
</div>
<div id="cisco_tunnel" class="box">
тут конфиг для циски
</div>
</div>

View File

@ -0,0 +1,136 @@
<div id="ports" class="box">
<label for="sftplus">sftplus</label>
<select id="sftplus" name="sftplus">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether1">ether1</label>
<select id="ether1" name="ether1">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether2">ether2</label>
<select id="ether2" name="ether2">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether3">ether3</label>
<select id="ether3" name="ether3">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether4">ether4</label>
<select id="ether4" name="ether4">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether5">ether5</label>
<select id="ether5" name="ether5">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether6">ether6</label>
<select id="ether6" name="ether6">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether7">ether7</label>
<select id="ether7" name="ether7">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
<br>
<label for="ether8">ether8</label>
<select id="ether8" name="ether8">
<option value="reserve">-- Резерв --</option>
<option value="isp">Провайдер</option>
<option value="pc">ПК</option>
<option value="voip">Телефон</option>
<option value="pc_voip">ПК+телефон</option>
<option value="printer">Принтер</option>
<option value="ap">Точка доступа</option>
<option value="scud">СКУД (201)</option>
<option value="video">Видеонаблюдение (202)</option>
<option value="free">Просто интернет (205)</option>
<option value="switch">Свитч</option>
</select>
</div>

View File

@ -0,0 +1,3 @@
<div id="settings" class="box">
</div>