servnest/public/ht/http-onion.php

107 lines
3.5 KiB
PHP
Raw Normal View History

2022-04-23 01:57:43 +02:00
<?php require "../../common/top.php"; ?>
2021-02-16 19:20:19 +01:00
<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
2022-05-04 17:41:17 +02:00
if (isset($_SESSION['username'])) {
$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++;
}
2022-04-18 16:05:00 +02:00
}
2022-05-04 17:41:17 +02:00
if (!is_null($notYetEnabledDirs)) {
foreach ($notYetEnabledDirs as $dir) {
echo "<option value='" . $dir . "'>" . $dir . "</option>";
}
2022-04-18 16:05:00 +02:00
}
2022-05-04 17:41:17 +02:00
if (!is_null($alreadyEnabledDirs)) {
foreach ($alreadyEnabledDirs as $dir) {
echo "<option disabled='' value='" . $dir . "'>" . $dir . "</option>";
}
2022-04-18 16:05:00 +02:00
}
}
}
?>
</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))
userError("Wrong value for <code>dir</code>.");
2021-02-16 19:20:19 +01:00
2022-04-18 16:05:00 +02:00
// Generate a .onion address
$torConf = file_get_contents(CONF['ht']['tor_config_path']);
$torConf = $torConf . "HiddenServiceDir " . CONF['ht']['tor_keys_path'] . "/" . $_POST['dir'] . "/
HiddenServicePort 80 [::1]:" . CONF['ht']['internal_onion_http_port'] . "
2022-05-04 17:41:17 +02:00
";
file_put_contents(CONF['ht']['tor_config_path'], $torConf);
2021-02-16 19:20:19 +01:00
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload tor", $output);
2022-04-18 16:05:00 +02:00
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(CONF['ht']['sudo_path'] . " " . MANIVER_PATH . " export-tor " . $_SESSION['username'] . " " . $_POST['dir'], $output);
2022-04-18 16:05:00 +02:00
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(CONF['ht']['ht_path'] . "/" . $_SESSION['username'] . "/" . $_POST['dir'] . "/hostname");
2022-04-18 16:05:00 +02:00
$onion = str_replace(array("\r", "\n"), "", $onion);
2022-05-04 21:18:43 +02:00
if (preg_match("/[0-9a-z]{56}\.onion/", $onion) !== 1)
serverError("No onion address found.");
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");
$nginxConf = str_replace("{{CONF['ht']['internal_onion_http_port']}}", CONF['ht']['internal_onion_http_port'], $nginxConf);
2022-04-18 19:44:58 +02:00
$nginxConf = str_replace("{{DOMAIN}}", $onion, $nginxConf);
$nginxConf = str_replace("{{CONF['ht']['ht_path']}}", CONF['ht']['ht_path'], $nginxConf);
2022-04-18 19:44:58 +02:00
$nginxConf = str_replace("{{USERNAME}}", $_SESSION['username'], $nginxConf);
$nginxConf = str_replace("{{DIR}}", $_POST['dir'], $nginxConf);
file_put_contents(CONF['ht']['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(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", $output);
2022-04-18 16:05:00 +02:00
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
}
?>
2022-04-23 01:57:43 +02:00
<?php require "../../common/bottom.php"; ?>