servnest/public/ht/http-onion.php

79 lines
2.6 KiB
PHP
Raw Normal View History

<?php require "../../common/html.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>
2022-05-21 02:15:36 +02:00
<?php
if (isset($_SESSION['username'])) {
$dirsStatuses = dirsStatuses($_SESSION['username'], "onion", "http");
foreach ($dirsStatuses as $dir => $alreadyEnabled) {
$disabled = $alreadyEnabled ? "disabled='' " : "";
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
switchToFormProcess();
2021-02-16 19:20:19 +01:00
if ($dirsStatuses[$_POST['dir']] !== false)
userError("Wrong value for <code>dir</code>.");
2021-02-16 19:20:19 +01:00
2022-06-07 17:58:46 +02:00
// Add Tor config
$torConf = file_get_contents(CONF['ht']['tor_config_path']);
2022-06-09 02:56:21 +02:00
if ($torConf === false)
serverError("Failed to read current Tor configuration.");
$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
";
2022-06-09 02:56:21 +02:00
if (file_put_contents(CONF['ht']['tor_config_path'], $torConf) === false)
serverError("Failed to write new Tor configuration.");
2022-06-07 17:58:46 +02:00
// Reload Tor
2022-06-09 02:56:21 +02:00
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload " . CONF['ht']['tor_service'], $output, $code);
if ($code !== 0)
serverError("Failed to reload Tor.");
// Get the address generated by Tor
2022-06-09 21:06:05 +02:00
exec(CONF['ht']['sudo_path'] . " -u " . CONF['ht']['tor_user'] . " " . CONF['ht']['cat_path'] . " " . CONF['ht']['tor_keys_path'] . "/" . $_POST['dir'] . "/hostname", $output);
2022-06-07 17:58:46 +02:00
$onion = $output[0];
if (preg_match("/[0-9a-z]{56}\.onion/", $onion) !== 1)
serverError("No onion address found.");
// Store it in the database
addSite($_SESSION['username'], $_POST['dir'], $onion, "onion", "http");
2022-06-07 17:58:46 +02:00
// Add Nginx config
$nginxConf = 'server {
listen [::1]:' . CONF['ht']['internal_onion_http_port'] . ';
server_name ' . $onion . ';
root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
include inc/ht-onion.conf;
}
';
2022-06-09 02:56:21 +02:00
if (file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $onion . ".conf", $nginxConf) === false)
serverError("Failed to write Nginx configuration.");
// Reload Nginx
2022-06-09 02:56:21 +02:00
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", result_code: $code);
if ($code !== 0)
serverError("Failed to reload Nginx.");
// Tell the user their site address
success("L'adresse de votre service Onion HTTP est : <a href='http://" . $onion . "/'<code>http://" . $onion . "/</code></a>");