main
Satur@it-depot.ru 2025-03-05 09:43:53 +03:00
parent 715961b425
commit 51c58e22a0
2 changed files with 46 additions and 25 deletions

25
app.py
View File

@ -11,7 +11,7 @@ app = FastAPI()
conn = sqlite3.connect("/db/rustdesk.db", check_same_thread=False)
cursor = conn.cursor()
# Создаем таблицы
# Создаем таблицы (добавляем поле protocol)
cursor.execute("""
CREATE TABLE IF NOT EXISTS folders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@ -23,10 +23,11 @@ CREATE TABLE IF NOT EXISTS folders (
cursor.execute("""
CREATE TABLE IF NOT EXISTS installs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rust_id TEXT,
rust_id TEXT, -- Оставляем название поля для совместимости, но оно теперь универсальное (ID подключения)
computer_name TEXT,
install_time TEXT,
folder_id INTEGER,
protocol TEXT DEFAULT 'rustdesk', -- Новое поле с протоколом, по умолчанию RustDesk
FOREIGN KEY (folder_id) REFERENCES folders(id)
)
""")
@ -55,6 +56,7 @@ class InstallData(BaseModel):
computer_name: str | None = None
install_time: str | None = None
folder_id: int | None = None
protocol: str | None = 'rustdesk' # По умолчанию RustDesk
# Монтируем папку templates как статические файлы
app.mount("/templates", StaticFiles(directory="templates"), name="templates")
@ -98,44 +100,43 @@ def delete_folder(folder_id: int):
@app.get("/api/installs")
def get_installs():
cursor.execute("""
SELECT i.id, i.rust_id, i.computer_name, i.install_time, i.folder_id, f.name as folder_name
SELECT i.id, i.rust_id, i.computer_name, i.install_time, i.folder_id, f.name as folder_name, i.protocol
FROM installs i
LEFT JOIN folders f ON i.folder_id = f.id
""")
rows = cursor.fetchall()
return [{"id": row[0], "rust_id": row[1], "computer_name": row[2],
"install_time": row[3], "folder_id": row[4], "folder_name": row[5]}
"install_time": row[3], "folder_id": row[4], "folder_name": row[5], "protocol": row[6]}
for row in rows]
@app.post("/api/install")
def add_install(data: InstallData):
install_time = data.install_time or datetime.datetime.now().isoformat()
# Если folder_id не указан (например, от скрипта), используем "Несортированные"
folder_id = data.folder_id if data.folder_id is not None else unsorted_folder_id
cursor.execute("INSERT INTO installs (rust_id, computer_name, install_time, folder_id) VALUES (?, ?, ?, ?)",
(data.rust_id, data.computer_name, install_time, folder_id))
protocol = data.protocol or 'rustdesk' # По умолчанию RustDesk для POST-запросов
cursor.execute("INSERT INTO installs (rust_id, computer_name, install_time, folder_id, protocol) VALUES (?, ?, ?, ?, ?)",
(data.rust_id, data.computer_name, install_time, folder_id, protocol))
conn.commit()
return {"status": "success"}
@app.put("/api/install/{install_id}")
def update_install(install_id: int, data: InstallData):
# Получаем текущие данные записи
cursor.execute("SELECT rust_id, computer_name, install_time, folder_id FROM installs WHERE id = ?", (install_id,))
cursor.execute("SELECT rust_id, computer_name, install_time, folder_id, protocol FROM installs WHERE id = ?", (install_id,))
current = cursor.fetchone()
if not current:
raise HTTPException(status_code=404, detail="Запись не найдена")
# Обновляем только те поля, которые переданы
new_rust_id = data.rust_id if data.rust_id is not None else current[0]
new_computer_name = data.computer_name if data.computer_name is not None else current[1]
new_install_time = data.install_time if data.install_time is not None else current[2]
new_folder_id = data.folder_id if data.folder_id is not None else current[3]
new_protocol = data.protocol if data.protocol is not None else current[4]
cursor.execute("""
UPDATE installs
SET rust_id = ?, computer_name = ?, install_time = ?, folder_id = ?
SET rust_id = ?, computer_name = ?, install_time = ?, folder_id = ?, protocol = ?
WHERE id = ?
""", (new_rust_id, new_computer_name, new_install_time, new_folder_id, install_id))
""", (new_rust_id, new_computer_name, new_install_time, new_folder_id, new_protocol, install_id))
conn.commit()
return {"status": "success"}

View File

@ -21,6 +21,7 @@
.header div { flex: 1; text-align: center; }
.header div:hover { background: #e0e0e0; }
.sort-arrow { font-size: 12px; margin-left: 5px; }
.protocol-icon { margin-right: 5px; }
</style>
</head>
<body>
@ -42,13 +43,21 @@
</div>
<div class="form-container">
<h2>Добавить запись</h2>
<input type="text" id="rustId" placeholder="Rust ID" required>
<input type="text" id="rustId" placeholder="ID подключения" required>
<input type="text" id="computerName" placeholder="Имя компьютера" required>
<input type="text" id="installTime" placeholder="Время (опционально)">
<select id="protocol">
<option value="rustdesk" selected>RustDesk</option>
<option value="anydesk">AnyDesk</option>
<option value="ammyy">Ammyy Admin</option>
<option value="teamviewer">TeamViewer</option>
<option value="vnc">VNC</option>
<option value="rdp">RDP</option>
</select>
<button onclick="addInstall()">Добавить</button>
</div>
<div class="header">
<div onclick="sortInstalls('rust_id')">Rust ID<span class="sort-arrow"></span></div>
<div onclick="sortInstalls('rust_id')">ID подключения<span class="sort-arrow"></span></div>
<div onclick="sortInstalls('computer_name')">Имя компьютера<span class="sort-arrow"></span></div>
<div onclick="sortInstalls('install_time')">Время установки<span class="sort-arrow"></span></div>
<div>Действия</div>
@ -63,6 +72,15 @@
let sortField = null;
let sortDirection = 'asc';
const protocolIcons = {
'rustdesk': '🖥️', // Эмодзи для RustDesk
'anydesk': '🔴', // Эмодзи для AnyDesk
'ammyy': '🟢', // Эмодзи для Ammyy Admin
'teamviewer': '🔵', // Эмодзи для TeamViewer
'vnc': '🟡', // Эмодзи для VNC
'rdp': '🟣' // Эмодзи для RDP
};
$(document).ready(function () {
$('#folder-tree').jstree({
'core': {
@ -120,12 +138,12 @@
function displayInstalls(installs) {
$('#installs-list').html(installs.map(item => `
<div class="install-item" data-id="${item.id}" draggable="true" style="display: flex; justify-content: space-between;">
<div style="flex: 1; text-align: center;">${item.rust_id}</div>
<div style="flex: 1; text-align: center;"><span class="protocol-icon">${protocolIcons[item.protocol]}</span>${item.rust_id}</div>
<div style="flex: 1; text-align: center;">${item.computer_name}</div>
<div style="flex: 1; text-align: center;">${item.install_time}</div>
<div style="flex: 1; text-align: center;">
<a href="rustdesk://${item.rust_id}" onclick="openRustDesk('${item.rust_id}'); return false;">Подключиться</a>
<button onclick="editInstall(${item.id}, '${item.rust_id}', '${item.computer_name}', '${item.install_time}')">Редактировать</button>
<a href="${item.protocol}://${item.rust_id}" onclick="openRemote('${item.rust_id}', '${item.protocol}'); return false;">Подключиться</a>
<button onclick="editInstall(${item.id}, '${item.rust_id}', '${item.computer_name}', '${item.install_time}', '${item.protocol}')">Редактировать</button>
<button onclick="deleteInstall(${item.id})">Удалить</button>
</div>
</div>
@ -173,7 +191,7 @@
function updateSortArrows() {
$('.sort-arrow').text('');
if (sortField) {
$(`.header div:contains("${sortField === 'rust_id' ? 'Rust ID' : sortField === 'computer_name' ? 'Имя компьютера' : 'Время установки'}") .sort-arrow`)
$(`.header div:contains("${sortField === 'rust_id' ? 'ID подключения' : sortField === 'computer_name' ? 'Имя компьютера' : 'Время установки'}") .sort-arrow`)
.text(sortDirection === 'asc' ? '↑' : '↓');
}
}
@ -231,27 +249,29 @@
const rustId = $('#rustId').val();
const computerName = $('#computerName').val();
const installTime = $('#installTime').val() || new Date().toISOString();
const protocol = $('#protocol').val();
$.ajax({
url: `${API_URL}/install`,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ rust_id: rustId, computer_name: computerName, install_time: installTime, folder_id: selectedFolderId }),
data: JSON.stringify({ rust_id: rustId, computer_name: computerName, install_time: installTime, folder_id: selectedFolderId, protocol: protocol }),
success: function () {
loadInstalls(selectedFolderId);
}
});
}
function editInstall(id, rustId, computerName, installTime) {
const newRustId = prompt("Rust ID:", rustId);
function editInstall(id, rustId, computerName, installTime, protocol) {
const newRustId = prompt("ID подключения:", rustId);
const newComputerName = prompt("Имя компьютера:", computerName);
const newInstallTime = prompt("Время установки:", installTime);
const newProtocol = prompt("Протокол (rustdesk, anydesk, ammyy, teamviewer, vnc, rdp):", protocol);
if (newRustId && newComputerName) {
$.ajax({
url: `${API_URL}/install/${id}`,
type: 'PUT',
contentType: 'application/json',
data: JSON.stringify({ rust_id: newRustId, computer_name: newComputerName, install_time: newInstallTime, folder_id: selectedFolderId }),
data: JSON.stringify({ rust_id: newRustId, computer_name: newComputerName, install_time: newInstallTime, folder_id: selectedFolderId, protocol: newProtocol }),
success: function () {
loadInstalls(selectedFolderId);
}
@ -286,9 +306,9 @@
});
}
function openRustDesk(rustId) {
if (confirm("Подключиться к " + rustId + "?")) {
window.location.href = `rustdesk://${rustId}`;
function openRemote(id, protocol) {
if (confirm(`Подключиться к ${id} через ${protocol}?`)) {
window.location.href = `${protocol}://${id}`;
}
}
</script>