Author SHA1 Message Date
fred 007aaf113a fix: argv bug 2026-09-13 11:55:58 +02:00
fred b4baaa2743 feat: filename not hardcoded
lancement d'apid : ajout des paramètres :
- nom fichier secret
- nom du script shell qui lance ttyd
2026-09-13 11:54:00 +02:00
nicolas 972e51dd39 Merge pull request 'feat: secret key to authorize ttyd only from w's wiki' (#4) from feat/add-key into main
Reviewed-on: #4
Reviewed-by: Nicolas Deville <nicolas@lanisys.fr>
2026-09-12 21:14:50 +02:00
fred f18dbe01f7 feat: secret key to authorize ttyd only from w's wiki 2026-09-12 21:09:05 +02:00
nicolas 6301b83d57 Merge pull request 'ttyd depuis wiki w' (#3) from features into main
Reviewed-on: #3
Reviewed-by: Nicolas<nicolas@lanisys.fr>
2026-07-12 19:18:04 +02:00
nicolas 5bee6269d4 Merge branch 'main' into features 2026-07-12 19:15:57 +02:00
fred 14e4b63f9b feat: appel ttyd depuis wiki php w 2026-07-12 19:06:10 +02:00
fred afba9c2200 fix 2026-07-12 18:05:29 +02:00
fred bbe362c65d fix 2026-07-12 17:07:57 +02:00
fred b702f8a548 fix 2026-07-12 17:04:03 +02:00
nicolas e3a6596e46 Merge pull request 'Supprimer test' (#2) from nicolas-patch-2 into main
Reviewed-on: #2
2026-07-11 21:29:40 +02:00
nicolas f557d1db92 Supprimer test 2026-07-11 21:28:42 +02:00
nicolas 02e166e1e5 Merge pull request 'test' (#1) from nicolas-patch-1 into main
Reviewed-on: #1
2026-07-11 21:27:52 +02:00
nicolas 1410df4f00 test2 2026-07-11 21:27:27 +02:00
nicolas d577060e87 test 2026-07-11 21:23:52 +02:00
3 changed files with 178 additions and 9 deletions

No files matched your search

+80 -9
View File
@@ -15,6 +15,8 @@
/* compilation : gcc -o api api.c -Wall -lssl -lcrypto */
/* ./apid 50000 cert.pem server.key 0 */
char * flog = NULL;
#define TRUE 1
@@ -51,6 +53,8 @@ void trace(const char * fmt, ...)
va_start(args, fmt);
if(flog)
{
if(*flog == '0') return; // si fichier log = "0" : ne rien afficher ni logguer
//
FILE * f = fopen(flog, "a+");
if(f)
{
@@ -450,9 +454,16 @@ void trt_sig(int sig)
}
// -----------------------------------------------------------
char data[64000]; // buffer de lecture/ecriture sur la socket securisee
void trt_connexion()
char data[64000], rep[64000]; // buffer de lecture/ecriture sur la socket securisee
void trt_connexion(char * secretfile, char * scriptttyd)
{
FILE * fp, * fd;
// script => /root/create_ttyd_session.sh
char redirectfile[] = "/tmp/ttyd.txt";
char redirection[] = " > /tmp/ttyd.txt";
char script[strlen(scriptttyd) + strlen(redirection) + 1];
strcpy(script, scriptttyd);
strcat(script, redirection);
//Dynamic req, methode, uri, rep, tmp, fld;
//long meth, cr, nb;
int keepAlive;
@@ -467,10 +478,53 @@ void trt_connexion()
if ( ! lireConnexion(data))
break; // fin du client
trace("lu:\n%s\n", data);
//
*rep = 0;
strcpy(rep, "X-data: 1234567890123456789012345678901234567890123456789012345678901234");
if((fd = fopen(secretfile, "r")))
{
fread(rep + 8, 64, 1, fd);
fclose(fd);
}
else
{
// erreur sur le serveur : tout refuser
strcpy(data, "HTTP/1.1 401 Unauthorized\r\n\r\n");
ecrireConnexion(data);
break;
}
if(strstr(data, rep) == NULL)
{
// le client ne vient pas de wapi.php : refuser
strcpy(data, "HTTP/1.1 401 Unauthorized\r\n\r\n");
ecrireConnexion(data);
break;
}
//
if(strstr(data,"keep-alive") == NULL)
keepAlive = FALSE;
//
strcpy(data, "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Type:text/html\r\nContent-Length:35\r\n\r\n<html><h2>Hello World !</h2></html>");
//strcpy(data, "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Type:text/html\r\nContent-Length:35\r\n\r\n<html><h2>Hello World !</h2></html>");
remove(redirectfile);
system(script);
if((fp = fopen(redirectfile, "r")) == NULL)
strcpy(data, "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Type:text/html\r\nContent-Length:97\r\n\r\n<html><h2>Erreur, console de test non disponible, veuillez contacter l'administrateur</h2></html>");
else
{
*data = 0;
*rep = 0;
fseek(fp, 0, SEEK_END);
long lg = ftell(fp);
fseek(fp, 0, SEEK_SET);
fread(rep, lg, 1, fp);
rep[lg] = 0;
fclose(fp);
//
sprintf(data, "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Type:text/html\r\nContent-Length:%ld", lg);
strcat(data, "\r\n\r\n");
strncat(data, rep, lg);
}
//
trace("Reponse:\n%s\n", data);
ecrireConnexion(data);
*data = 0;
@@ -485,24 +539,40 @@ void trt_connexion()
trace("fin cnx client\n");
}
void writePid(long pid)
{
char pidfile[] = "/run/apid.pid";
remove(pidfile);
FILE * f = fopen(pidfile, "a+");
if(f)
{
fprintf(f, "%ld\n", pid);
fclose(f);
}
}
// ----------------------------------------------------------
int main(int argc, char *argv[])
{
if(argc != 4 && argc != 5)
if(argc != 6 && argc != 7)
{
trace("Usage : %s <port> <certificat> <cleprivee> [<fichierlog>]\n", argv[0]);
trace("Usage : %s <port> <fichiercertificat> <fichiercleprivee> <fichiersecret> <fichierscriptttyd> [<fichierlog>]\n", argv[0]);
exit(1);
}
int demon = (argv[0][strlen(argv[0]) - 1] == 'd');
if(argc == 5)
if(argc == 7)
{
flog = argv[4];
remove(flog);
flog = argv[argc - 1];
if(*flog != '0')
remove(flog);
}
/* */
int port = atoi(argv[1]);
certificat = argv[2];
cleprivee = argv[3];
char * secretfile = argv[4];
char * scriptttyd = argv[5];
/* */
mServeur = TRUE;
mFin = FALSE;
@@ -537,6 +607,7 @@ int main(int argc, char *argv[])
setsid();
//for(int i=0;i < FOPEN_MAX; i++) /* OPEN_MAX sous AIX */
// close(i);
writePid(mPid);
}
//
trace("Demarrage serveur, port %d pid %d mode %d bits\n", port, getpid(), sizeof(long)==4?32:64);
@@ -594,7 +665,7 @@ int main(int argc, char *argv[])
break;
}
//
trt_connexion();
trt_connexion(secretfile, scriptttyd);
//
break;
}
+70
View File
@@ -0,0 +1,70 @@
#!/bin/bash
# dans /root du serveur docker (avec apid et certificat/cle privee)
# duree est le nb de minute pour la session, par defaut 7200 mn
duree="${1:-7200}"
if (( duree < 15 || duree > 14400 )); then
echo "Duree de la session incorrecte."
exit 2
fi
cat /docker/registry/auth/password | docker login 192.168.10.10:5000/v2 -u nicolas --password-stdin
# Configuration
IMAGE_NAME="192.168.10.10:5000/ttyd_docker:latest"
BASE_PORT=9101
MAX_PORT=9105
# Générer un nom unique (apt install uuid-runtime)
USER_ID=$(uuidgen | cut -d'-' -f1)
CONTAINER_NAME="ttyd_docker_$USER_ID"
# Trouver un port disponible
find_free_port() {
for ((port=$BASE_PORT; port<=$MAX_PORT; port++)); do
if ! lsof -i :$port > /dev/null; then
echo $port
return 0
fi
done
return 1
}
PORT=$(find_free_port)
if [ -z "$PORT" ]; then
echo "Aucun port disponible entre $BASE_PORT et $MAX_PORT."
exit 1
fi
user=$(uuidgen | cut -d'-' -f1)
pass=$(uuidgen | cut -d'-' -f1)
# Lancer le conteneur ttyd
docker run -d \
--name "$CONTAINER_NAME" \
-p "$PORT":7681 \
-e TTYD_USER=$user \
-e TTYD_PASS=$pass \
--rm \
--stop-timeout=30 \
--memory="2g" \
--cpus="0.75" \
"$IMAGE_NAME"
( sleep $duree && docker stop "$CONTAINER_NAME" ) &
if [ $? -ne 0 ]; then
echo "Erreur lors du démarrage du conteneur."
exit 1
fi
PORT_URL=$(($PORT - $BASE_PORT + 1))
# Afficher l'URL d'accès
#echo "Session ttyd créée avec succès !"
#echo "Nom du conteneur : $CONTAINER_NAME"
#echo "Conteneur lancé pour 2 heures : <a href=\"https://user:pass@w-$PORT_URL.lanisys.fr\">URL</a>"
#echo "User: $user / Password: $pass"
#echo "Termine avec succes"
echo "https://$user:$pass@w-$PORT_URL.lanisys.fr/"
+28
View File
@@ -0,0 +1,28 @@
<?php
// dans /var/www/w.lanisys.fr sur le serveur ionos
$data = file_get_contents('/etc/wapi/data');
$ctx = stream_context_create([
'http' => [
'timeout' => 4,
'header' => [
'X-data: '.$data
]
],
]);
$rep = file_get_contents(
"https://api.lanisys.fr:50000/",
false,
$ctx
);
$pos = strpos($rep, "https://");
if ($pos !== false)
{
header("Location: ".substr($rep, $pos));
exit;
}
else
echo $rep;