servnest/ht/http-onion.php

100 lines
3.1 KiB
PHP
Raw Normal View History

2021-02-16 19:20:19 +01:00
<?php require "../top.inc.php"; ?>
<p>
2022-04-18 16:05:00 +02:00
Ajouter un accès en .onion sur un dossier
2021-02-16 19:20:19 +01:00
</p>
<form method="post">
2022-04-18 16:05:00 +02:00
<label for="dir">Dossier ciblé</label><br>
<select required="" name="dir" id="dir">
<option value="" disabled="" selected="">---</option>
<?php
$fsDirs = listFsDirs($_SESSION['username']);
$dbUsedDirs = listDbDirs($_SESSION['username'], "onion", "http");
$i = 0;
$j = 0;
$alreadyEnabledDirs = NULL;
$notYetEnabledDirs = NULL;
if ($fsDirs) {
foreach ($fsDirs as $fsDir) {
if ($dbUsedDirs AND in_array($fsDir, $dbUsedDirs)) {
$alreadyEnabledDirs[$i] = $fsDir;
$i++;
} else {
$notYetEnabledDirs[$j] = $fsDir;
$j++;
}
}
if (!is_null($notYetEnabledDirs)) {
foreach ($notYetEnabledDirs as $dir) {
echo "<option value='" . $dir . "'>" . $dir . "</option>";
}
}
if (!is_null($alreadyEnabledDirs)) {
foreach ($alreadyEnabledDirs as $dir) {
echo "<option disabled='' value='" . $dir . "'>" . $dir . "</option>";
}
}
}
?>
</select>
<br>
<input value="Valider" type="submit">
2021-02-16 19:20:19 +01:00
</form>
<?php
if (isset($_POST['dir']) AND isset($_SESSION['username'])) {
2022-04-18 16:05:00 +02:00
antiCSRF();
2021-08-05 14:04:33 +02:00
2022-04-18 16:05:00 +02:00
if (!in_array($_POST['dir'], $notYetEnabledDirs))
exit("ERROR : Wrong value for dir");
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Generate a .onion address
$torConf = file_get_contents(TOR_CONFIG_PATH);
2022-04-18 19:44:58 +02:00
$torConf = $torConf . "\nHiddenServiceDir " . TOR_KEYS_PATH . "/" . $_POST['dir'] . "/\nHiddenServicePort 80 [::1]:" . INTERNAL_ONION_HTTP_PORT;
2022-04-18 16:05:00 +02:00
file_put_contents(TOR_CONFIG_PATH, $torConf);
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
exec(SUDO_PATH . " " . MANIVER_PATH . " reload-tor", $output);
addNiverLog("Tor reloaded by " . $_SESSION['username'], $output);
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Copy generated address to a location readable by PHP
exec(SUDO_PATH . " " . MANIVER_PATH . " export-tor " . $_SESSION['username'] . " " . $_POST['dir'], $output);
addNiverLog("Tor data exported by " . $_SESSION['username'], $output);
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Wait
sleep(1);
2022-04-18 16:05:00 +02:00
// Get the address generated by Tor
$onion = file_get_contents(HT_PATH . "/" . $_SESSION['username'] . "/ht/" . $_POST['dir'] . "/hostname");
$onion = str_replace(array("\r", "\n"), "", $onion);
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Store it in the database
addSite($_SESSION['username'], $_POST['dir'], $onion, "onion", "http");
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Add it to Nginx
$nginxConf = file_get_contents(NIVER_TEMPLATE_PATH . "/nginx/onion.template");
2022-04-18 19:44:58 +02:00
$nginxConf = str_replace("{{INTERNAL_ONION_HTTP_PORT}}", INTERNAL_ONION_HTTP_PORT, $nginxConf);
$nginxConf = str_replace("{{DOMAIN}}", $onion, $nginxConf);
$nginxConf = str_replace("{{HT_PATH}}", HT_PATH, $nginxConf);
$nginxConf = str_replace("{{USERNAME}}", $_SESSION['username'], $nginxConf);
$nginxConf = str_replace("{{DIR}}", $_POST['dir'], $nginxConf);
2022-04-18 16:05:00 +02:00
file_put_contents(NGINX_CONFIG_PATH . "/" . $_POST['dir'] . ".conf", $nginxConf);
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Reload Nginx
exec(SUDO_PATH . " " . MANIVER_PATH . " reload-nginx", $output);
addNiverLog("Nginx reloaded by " . $_SESSION['username'], $output);
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Tell the user their site address
echo "<p>L'adresse de votre site Onion HTTP est :<br><a href='http://" . $onion . "'<code>http://" . $onion . "</code></a></p>";
2021-02-16 19:20:19 +01:00
}
?>
<?php require "../bottom.inc.php"; ?>