Struktur verbessert in components

This commit is contained in:
ISA
2025-02-13 15:06:13 +01:00
parent 9057490541
commit 56df99c869
13 changed files with 13 additions and 13 deletions

View File

@@ -0,0 +1,473 @@
"use client"; // components/header/settingsModal/SettingsModal.tsx
import React, { useState, useEffect } from "react";
import ReactModal from "react-modal";
import "bootstrap-icons/font/bootstrap-icons.css";
import { useSelector } from "react-redux";
import handleClearDatabase from "./handlers/handleClearDatabase";
import handleReboot from "./handlers/handleReboot";
import handleSetDateTime from "./handlers/handleSetDateTime";
import handleSubmit from "./handlers/handleSubmit";
import bcrypt from "bcryptjs";
import CryptoJS from "crypto-js";
ReactModal.setAppElement("#__next");
const USERS = {
Admin: {
username: "admin",
// Gehashte Version von "admin" mit bcrypt
password: "$2a$10$xpq/.tcOJN/LXfzdCcCVrenlBh2nRlM1R1ISY7dd1q2qGWC9Fyd2G",
role: "Admin",
},
};
// Funktion zur Generierung eines AES-Schlüssels und IVs
function generateKeyAndIV() {
const encryptionKey = process.env.NEXT_PUBLIC_ENCRYPTION_KEY;
const encryptionIV = process.env.NEXT_PUBLIC_ENCRYPTION_IV;
if (!encryptionKey || !encryptionIV) {
throw new Error("Encryption key or IV is not defined.");
}
const key = CryptoJS.enc.Utf8.parse(encryptionKey);
const iv = CryptoJS.enc.Utf8.parse(encryptionIV);
return { key, iv };
}
// Funktion zur Generierung eines Tokens
function generateToken(user) {
const payload = {
username: user.username,
role: user.role,
exp: Date.now() + 5 * 60 * 1000, // Ablaufzeit: 5 Minuten
};
const token = JSON.stringify(payload);
const { key, iv } = generateKeyAndIV();
const encryptedToken = CryptoJS.AES.encrypt(token, key, { iv }).toString();
return encryptedToken;
}
// Funktion zur Entschlüsselung des Tokens
function decryptToken(encryptedToken) {
const { key, iv } = generateKeyAndIV();
const bytes = CryptoJS.AES.decrypt(encryptedToken, key, { iv });
const decryptedToken = bytes.toString(CryptoJS.enc.Utf8);
return JSON.parse(decryptedToken);
}
function SettingModal({ showModal, onClose }) {
const [isAdminLoggedIn, setAdminLoggedIn] = useState(false);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [showLoginForm, setShowLoginForm] = useState(false);
function handleAdminLogin(e) {
e.preventDefault();
const user = USERS.Admin;
bcrypt.compare(password, user.password, (err, isMatch) => {
if (isMatch) {
const token = generateToken(user);
sessionStorage.setItem("token", token);
localStorage.setItem("isAdminLoggedIn", "true");
setShowLoginForm(false);
onClose();
} else {
setError(
"Login fehlgeschlagen. Bitte überprüfen Sie Benutzername und Passwort."
);
}
});
}
useEffect(() => {
if (showModal) {
const token = sessionStorage.getItem("token");
if (token) {
try {
const { exp } = decryptToken(token);
if (Date.now() < exp) {
setAdminLoggedIn(true);
} else {
sessionStorage.removeItem("token");
localStorage.setItem("isAdminLoggedIn", "false");
}
} catch (error) {
console.error("Token-Entschlüsselung fehlgeschlagen:", error);
}
}
}
}, [showModal]);
const deviceName_Redux = useSelector((state) => state.variables.deviceName);
const mac1_Redux = useSelector((state) => state.variables.mac1);
const ip_Redux = useSelector((state) => state.variables.ip);
const subnet_Redux = useSelector((state) => state.variables.subnet);
const gateway_Redux = useSelector((state) => state.variables.gateway);
const datetime_Redux = useSelector(
(state) => state.variables.cplInternalTimestamp
);
const ntp1_Redux = useSelector((state) => state.variables.ntp1);
const ntp2_Redux = useSelector((state) => state.variables.ntp2);
const ntp3_Redux = useSelector((state) => state.variables.ntp3);
const ntpTimezone_Redux = useSelector((state) => state.variables.ntpTimezone);
const active_Redux = useSelector((state) => state.variables.ntpActive);
const [name, setName] = useState(deviceName_Redux || "");
const [mac1, setMac1] = useState(mac1_Redux || "");
const [ip, setIp] = useState(ip_Redux || "");
const [subnet, setSubnet] = useState(subnet_Redux || "");
const [gateway, setGateway] = useState(gateway_Redux || "");
const [systemUhr, setSystemUhr] = useState(datetime_Redux || "");
const [ntp1, setNtp1] = useState(ntp1_Redux || "");
const [ntp2, setNtp2] = useState(ntp2_Redux || "");
const [ntp3, setNtp3] = useState(ntp3_Redux || "");
const [ntpTimezone, setNtpTimezone] = useState(ntpTimezone_Redux || "");
const [active, setActive] = useState(active_Redux || "");
const [originalValues, setOriginalValues] = useState({});
const currentValues = {
name,
ip,
subnet,
gateway,
ntp1,
ntp2,
ntp3,
ntpTimezone,
active,
};
const handleAdminLogout = () => {
sessionStorage.removeItem("token"); // Token aus sessionStorage entfernen
localStorage.setItem("isAdminLoggedIn", "false"); // Admin-Status im localStorage setzen
};
useEffect(() => {
if (showModal) {
setName(deviceName_Redux || "");
setMac1(mac1_Redux || "");
setIp(ip_Redux || "");
setSubnet(subnet_Redux || "");
setGateway(gateway_Redux || "");
setSystemUhr(datetime_Redux || "");
setNtp1(ntp1_Redux || "");
setNtp2(ntp2_Redux || "");
setNtp3(ntp3_Redux || "");
setNtpTimezone(ntpTimezone_Redux || "");
setActive(active_Redux || "");
}
}, [showModal]);
useEffect(() => {
setOriginalValues({
name: deviceName_Redux,
ip: ip_Redux,
subnet: subnet_Redux,
gateway: gateway_Redux,
ntp1: ntp1_Redux,
ntp2: ntp2_Redux,
ntp3: ntp3_Redux,
ntpTimezone: ntpTimezone_Redux,
active: active_Redux,
});
}, [
deviceName_Redux,
ip_Redux,
subnet_Redux,
gateway_Redux,
ntp1_Redux,
ntp2_Redux,
ntp3_Redux,
ntpTimezone_Redux,
active_Redux,
]);
useEffect(() => {
// Überprüfen, ob ein Token im SessionStorage vorhanden ist
const token = sessionStorage.getItem("token");
if (token) {
try {
// Token mit CryptoJS entschlüsseln
const bytes = CryptoJS.AES.decrypt(
token,
process.env.NEXT_PUBLIC_ENCRYPTION_KEY
);
const decryptedToken = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
// Ablaufzeit überprüfen
if (Date.now() < decryptedToken.exp) {
setAdminLoggedIn(true);
} else {
// Token ist abgelaufen
sessionStorage.removeItem("token");
setAdminLoggedIn(false);
}
} catch (error) {
console.error("Fehler beim Entschlüsseln des Tokens:", error);
setAdminLoggedIn(false);
}
}
}, []);
return (
<>
<ReactModal
isOpen={showModal}
onRequestClose={onClose}
shouldCloseOnOverlayClick={false}
style={{
overlay: { backgroundColor: "rgba(0, 0, 0, 0.5)", zIndex: 100 },
content: {
top: "50%",
left: "50%",
right: "auto",
bottom: "auto",
marginRight: "-50%",
transform: "translate(-50%, -50%)",
width: "80%",
maxWidth: "800px",
padding: "20px",
borderRadius: "8px",
border: "none",
position: "relative",
},
}}
>
<button
onClick={onClose}
style={{
position: "absolute",
top: "10px",
right: "10px",
background: "transparent",
border: "none",
cursor: "pointer",
fontSize: "24px",
}}
>
<i className="bi bi-x-circle-fill"></i>
</button>
{/* Hauptinhalt oder Login-Formular */}
{showLoginForm ? (
<div className="text-black">
<h2 className="text-lg font-bold mb-4">Admin Login</h2>
<form onSubmit={(e) => e.preventDefault()}>
<div className="mb-4">
<label className="block text-sm font-medium">
Benutzername:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
<div className="mb-4">
<label className="block text-sm font-medium">Passwort:</label>
<input
type="password"
className="border border-gray-300 rounded p-2 w-full"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
{error && <p className="text-red-500">{error}</p>}
<button
onClick={handleAdminLogin}
className="bg-littwin-blue text-white px-4 py-2 rounded w-full"
>
Anmelden
</button>
</form>
</div>
) : (
<div className="text-black">
<h2 className="text-lg font-bold mb-4">System:</h2>
<form>
<div className="mb-4">
<label className="block text-sm font-medium">Name:</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
<div className="mb-4 grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium">
MAC Adresse 1:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={mac1}
onChange={(e) => setMac1(e.target.value)}
disabled
/>
</div>
</div>
<div className="mb-4 grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium">IP:</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={ip}
onChange={(e) => setIp(e.target.value)}
/>
</div>
<div>
<label className="block text-sm font-medium">Subnet:</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={subnet}
onChange={(e) => setSubnet(e.target.value)}
/>
</div>
</div>
<div className="mb-4 grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium">Gateway:</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={gateway}
onChange={(e) => setGateway(e.target.value)}
/>
</div>
<div>
<label className="block text-sm font-medium">
Systemuhr:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={systemUhr}
disabled
/>
{/* Button für Systemzeit übernehmen */}
<div className="flex w-full mt-1 justify-end">
<button
className="bg-littwin-blue text-white px-4 py-2 rounded"
onClick={() => {
if (
window.confirm(
"Möchten Sie wirklich die Systemzeit übernehmen?"
)
) {
handleSetDateTime();
}
}}
>
Systemzeit übernehmen
</button>
</div>
</div>
</div>
{/* SNTP Client */}
<h3 className="text-sm font-bold mb-2">SNTP Client:</h3>
<div className="mb-4 grid grid-cols-2 gap-4">
<div>
<label className="block text-sm font-medium">
IP NTP Server 1:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={ntp1}
onChange={(e) => setNtp1(e.target.value)}
/>
</div>
<div>
<label className="block text-sm font-medium">
IP NTP Server 2:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={ntp2}
onChange={(e) => setNtp2(e.target.value)}
/>
</div>
<div>
<label className="block text-sm font-medium">
IP NTP Server 3:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={ntp3}
onChange={(e) => setNtp3(e.target.value)}
/>
</div>
<div>
<label className="block text-sm font-medium">Zeitzone:</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={ntpTimezone}
onChange={(e) => setNtpTimezone(e.target.value)}
/>
</div>
<div>
<label className="block text-sm font-medium">
NTP Active:
</label>
<input
type="text"
className="border border-gray-300 rounded p-2 w-full"
value={active}
onChange={(e) => setActive(e.target.value)}
/>
</div>
</div>
{/* Modal Footer */}
<div className="flex justify-between mt-4">
<button
className="bg-littwin-blue text-white px-4 py-2 rounded"
onClick={() => handleReboot()}
>
Neustart CPL
</button>
<button
onClick={() => {
isAdminLoggedIn
? handleAdminLogout()
: setShowLoginForm(true);
}}
className="bg-littwin-blue text-white px-4 py-2 rounded"
>
{isAdminLoggedIn ? "Admin abmelden" : "Admin anmelden"}
</button>
<button
className="bg-littwin-blue text-white px-4 py-2 rounded"
onClick={() => handleClearDatabase()}
>
Datenbank leeren
</button>
<button
onClick={() => handleSubmit(originalValues, currentValues)}
className="bg-littwin-blue text-white px-4 py-2 rounded"
>
Übernehmen
</button>
</div>
</form>
</div>
)}
</ReactModal>
</>
);
}
export default SettingModal;

View File

@@ -0,0 +1,34 @@
// components/modales/handlers/handleClearDatabase.js
const handleClearDatabase = async () => {
const confirmClear = window.confirm(
"Sind Sie sicher, dass Sie die Datenbank leeren möchten?"
);
if (!confirmClear) return;
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, and clear database command
const url = `${window.location.origin}/CPL?${currentPath}&DEDB=1`;
// Log the full URL to the console for debugging
console.log(url);
// Send the clear database command to the server using fetch and GET method
try {
const response = await fetch(url, { method: "GET" });
if (response.ok) {
alert("Datenbank erfolgreich geleert!");
} else {
alert("Fehler beim Leeren der Datenbank!");
}
} catch (error) {
console.error("Fehler:", error);
alert("Fehler beim Leeren der Datenbank!");
}
};
export default handleClearDatabase;

View File

@@ -0,0 +1,91 @@
// components/modales/settingsModal/handlers/handleReboot.ts
const handleReboot = async (newIp: string | null = null): Promise<void> => {
const showWaitPage = () => {
const waitHTML = `
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bitte warten...</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f9;
color: #333;
}
.message {
text-align: center;
}
.progress-container {
width: 100%;
background-color: #ddd;
border-radius: 10px;
overflow: hidden;
margin-top: 20px;
}
.progress-bar {
height: 20px;
width: 0;
background-color: #3498db;
transition: width 0.3s;
}
</style>
</head>
<body>
<div class="message">
<p>Bitte warten, CPL wird neu gestartet...</p>
<div class="progress-container">
<div class="progress-bar" id="progress-bar"></div>
</div>
</div>
</body>
</html>
`;
document.documentElement.innerHTML = waitHTML;
let progress = 0;
const progressBar = document.getElementById("progress-bar");
if (progressBar) {
const interval = setInterval(() => {
progress += 1;
progressBar.style.width = progress + "%";
if (progress >= 100) {
clearInterval(interval);
}
}, 300);
} else {
console.error("Progress-Bar-Element nicht gefunden.");
}
};
if (window.confirm("Sind Sie sicher, dass Sie den CPL neu starten möchten?")) {
showWaitPage();
const baseRedirectURL = newIp ? `https://${newIp}` : window.location.origin;
const redirectPath =
process.env.NODE_ENV === "production" ? "/dashboard.html" : "/dashboard";
setTimeout(() => {
window.location.href = `${baseRedirectURL}${redirectPath}`;
}, 33000);
const url = `${window.location.origin}/CPL?wait2reboot.html&BOOT=1`;
console.log(url);
fetch(url, { method: "GET" })
.then(() => {
console.log("Neustart-Anfrage erfolgreich gesendet.");
})
.catch((error) => {
console.error("Fehler beim Senden der Neustartanfrage:", error);
});
}
};
export default handleReboot;

View File

@@ -0,0 +1,45 @@
//components/modales/handlers/handleSetDateTime.js
const handleSetDateTime = () => {
const currentDate = new Date();
// Format date and time as required by the system without leading zeros:
const year = currentDate.getFullYear().toString().slice(-2); // Last two digits of the year
const month = Number(currentDate.getMonth() + 1); // Convert to Number to remove leading zero
const day = Number(currentDate.getDate()); // Convert to Number to remove leading zero
const hours = Number(currentDate.getHours()); // Convert to Number to remove leading zero
const minutes = Number(currentDate.getMinutes()); // Convert to Number to remove leading zero
const seconds = Number(currentDate.getSeconds()); // Convert to Number to remove leading zero
// Date and Time commands
const dateCommand = `CLK00=${year}-${month}-${day}`;
const timeCommand = `CLK01=${hours}-${minutes}-${seconds}`;
// Get the current path and ensure it ends with ".html"
let currentPath = window.location.pathname;
if (!currentPath.endsWith(".html")) {
currentPath += ".html";
}
// Full URL with host, current path, date, and time commands
const url = `${window.location.origin}/CPL?${currentPath}&${dateCommand}&${timeCommand}`;
// Log the full URL to the console
console.log(url);
// Send the commands to the server using fetch and GET method
fetch(url, { method: "GET" })
.then((response) => {
if (response.ok) {
alert("Datum und Uhrzeit erfolgreich gesetzt!");
} else {
alert("Fehler beim Setzen von Datum und Uhrzeit!");
}
})
.catch((error) => {
console.error("Fehler:", error);
alert("Fehler beim Setzen von Datum und Uhrzeit!");
});
};
export default handleSetDateTime;

View File

@@ -0,0 +1,98 @@
// components/modales/handlers/handleSubmit.ts
import handleReboot from "./handleReboot";
interface OriginalValues {
name: string;
ip: string;
subnet: string;
gateway: string;
ntp1: string;
ntp2: string;
ntp3: string;
ntpTimezone: string;
active: boolean;
}
interface CurrentValues {
name: string;
ip: string;
subnet: string;
gateway: string;
ntp1: string;
ntp2: string;
ntp3: string;
ntpTimezone: string;
active: boolean;
}
const handleSubmit = (
originalValues: OriginalValues,
currentValues: CurrentValues
): void => {
const changes: { [key: string]: string | boolean } = {};
let networkChanges = false;
let newIp: string | null = null;
// Überprüfe, welche Werte sich geändert haben
if (currentValues.name !== originalValues.name) {
changes.SNNA = currentValues.name;
networkChanges = true;
}
if (currentValues.ip !== originalValues.ip) {
changes.SEI01 = currentValues.ip;
newIp = currentValues.ip; // Neue IP speichern
networkChanges = true;
}
if (currentValues.subnet !== originalValues.subnet) {
changes.SEI02 = currentValues.subnet;
networkChanges = true;
}
if (currentValues.gateway !== originalValues.gateway) {
changes.SEI03 = currentValues.gateway;
networkChanges = true;
}
if (currentValues.ntp1 !== originalValues.ntp1) {
changes.SNIP1 = currentValues.ntp1;
}
if (currentValues.ntp2 !== originalValues.ntp2) {
changes.SNIP2 = currentValues.ntp2;
}
if (currentValues.ntp3 !== originalValues.ntp3) {
changes.SNIP3 = currentValues.ntp3;
}
if (currentValues.ntpTimezone !== originalValues.ntpTimezone) {
changes.SNTZ = currentValues.ntpTimezone;
}
if (currentValues.active !== originalValues.active) {
changes.SNAC = currentValues.active;
}
if (Object.keys(changes).length > 0) {
// URL für die Änderungen erstellen
let url = `${window.location.origin}/CPL?${window.location.pathname}`;
Object.keys(changes).forEach((paramKey) => {
url += `&${paramKey}=${encodeURIComponent(String(changes[paramKey]))}`;
});
console.log(url);
fetch(url, { method: "GET" })
.then(() => {
alert("Daten erfolgreich gesendet!");
if (networkChanges) {
alert(
"Hinweis: Die Änderungen in CPL-Name und den Netzwerkeinstellungen werden erst nach einem Neustart des CPL wirksam."
);
handleReboot(newIp); // handleReboot mit neuer IP aufrufen
}
})
.catch((error) => {
console.error("Fehler beim Senden der Daten:", error);
alert("Fehler beim Senden der Daten.");
});
} else {
alert("Keine Änderungen vorgenommen.");
}
};
export default handleSubmit;