From a721d5b56898e3ca543bce342193ab7ac215499b Mon Sep 17 00:00:00 2001 From: Eliott-B Date: Fri, 20 Dec 2024 09:39:18 +0100 Subject: [PATCH 1/7] Add cours + sandbox for TCP ping --- front-js/src/app/dashboard/page.tsx | 3 +- front-js/src/app/modules/tcp/Cours.tsx | 49 ++++++ front-js/src/app/modules/tcp/TcpSandbox.tsx | 156 ++++++++++++++++++++ front-js/src/app/modules/tcp/layout.tsx | 14 ++ front-js/src/app/modules/tcp/page.tsx | 90 +++++++++++ 5 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 front-js/src/app/modules/tcp/Cours.tsx create mode 100644 front-js/src/app/modules/tcp/TcpSandbox.tsx create mode 100644 front-js/src/app/modules/tcp/layout.tsx diff --git a/front-js/src/app/dashboard/page.tsx b/front-js/src/app/dashboard/page.tsx index 47f6d70..b5c0275 100644 --- a/front-js/src/app/dashboard/page.tsx +++ b/front-js/src/app/dashboard/page.tsx @@ -154,9 +154,10 @@ export default function Dashboard() { router.push("/modules/tcp")} /> diff --git a/front-js/src/app/modules/tcp/Cours.tsx b/front-js/src/app/modules/tcp/Cours.tsx new file mode 100644 index 0000000..750da7b --- /dev/null +++ b/front-js/src/app/modules/tcp/Cours.tsx @@ -0,0 +1,49 @@ +import Title from "@/components/Title"; +import Text from "@/components/Text"; +import React from "react"; +import Space from "@/components/Space"; + +const Cours: React.FC = () => { + + return ( + <> + + Introduction + + TCP (Transmission Control Protocol) est un protocole de + communication qui assure la transmission de données entre deux + machines. Il garantit que les données envoyées par une machine + arrivent bien à l’autre machine, dans le bon ordre et sans + erreur. + + + Fonctionnement TCP + + + TCP fonctionne en établissant une connexion entre deux machines + avant de transmettre des données. Cette connexion est appelée{" "} + connexion TCP. Elle est établie en trois étapes : + +
    +
  1. + Établissement de la connexion : Les deux machines + s’échangent des paquets pour s’assurer qu’elles + sont prêtes à communiquer. +
  2. +
  3. + Transfert des données : Les données sont envoyées en + plusieurs paquets, chacun étant numéroté pour être reconstitué + dans le bon ordre. +
  4. +
  5. + Fin de la connexion : Les deux machines s’échangent + des paquets pour clôturer la connexion. +
  6. +
+
+
+ + ); +}; + +export default Cours; diff --git a/front-js/src/app/modules/tcp/TcpSandbox.tsx b/front-js/src/app/modules/tcp/TcpSandbox.tsx new file mode 100644 index 0000000..87d4f21 --- /dev/null +++ b/front-js/src/app/modules/tcp/TcpSandbox.tsx @@ -0,0 +1,156 @@ +import Title from "@/components/Title"; +import Button from "@/components/Button"; +import Input from "@/components/Input"; +import Space from "@/components/Space"; +import React from "react"; +import { useState } from "react"; +import axios from "@/axiosConfig"; +import Cookies from "js-cookie"; +import { AxiosError } from "axios"; +import { Alert } from "@mui/material"; +import Box from "@/components/Box"; + +const TcpSandbox: React.FC = () => { + const [res, setRes] = useState(""); + const [ping, setPing] = useState<{ + rtt_ms: number; + packet_size: number; + ttl: number; + source: string; + destination: string; + } | null>(null); + const [command, setCommand] = useState(""); + const [error, setError] = useState(""); + + const handlePing = async (e: { preventDefault: () => void }) => { + e.preventDefault(); + const parts = command.split(" "); + if (parts.length < 2 || parts.length > 2) { + setError("Commande invalide"); + setRes(""); + return; + } + if (parts[0] !== "ping") { + setError("Commande invalide"); + setRes(""); + return; + } + try { + const ip_port = parts[1].split(":"); + if (ip_port.length < 2 || ip_port.length > 2) { + setError("Commande invalide"); + setRes(""); + return; + } + const ip = ip_port[0]; + const port = ip_port[1]; + + const response = await axios.get("/scapy/tcp-test/" + ip + "/" + port, { + headers: { + Authorization: `Bearer ${Cookies.get("access_token")}`, + }, + }); + const data = response.data; + if (response.status === 200) { + setRes(data.message); + const formattedPing = { + rtt_ms: data.details.rtt_ms, + packet_size: data.details.packet_size, + ttl: data.details.ttl, + source: data.details.source, + destination: data.details.destination, + }; + setPing(formattedPing); + setError(""); + } + } catch (error: unknown) { + const axiosError = error as AxiosError; + if (axiosError.response?.status === 404) { + setRes(""); + setError("Impossible de résoudre l'adresse"); + } else { + setError("Erreur lors du ping TCP"); + } + } + }; + + return ( + <> + +
+ Ping TCP + + setCommand(e.target.value)} + required + label="Commande ping" + /> +