servnest/ht/gemini-onion.php

92 lines
2.8 KiB
PHP
Raw Normal View History

2021-03-02 22:56:38 +01:00
<?php require "../top.inc.php"; ?>
<p>
Ajouter un accès en .onion sur un dossier
</p>
<form method="post">
<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", "gemini");
$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++;
}
2021-03-02 22:56:38 +01:00
}
if (!is_null($notYetEnabledDirs)) {
foreach ($notYetEnabledDirs as $dir) {
echo "<option value='" . $dir . "'>" . $dir . "</option>";
}
2021-03-02 22:56:38 +01:00
}
if (!is_null($alreadyEnabledDirs)) {
foreach ($alreadyEnabledDirs as $dir) {
echo "<option disabled='' value='" . $dir . "'>" . $dir . "</option>";
}
2021-03-02 22:56:38 +01:00
}
}
?>
</select>
<br>
<input value="Valider" type="submit">
</form>
<?php
if (isset($_POST['dir']) AND isset($_SESSION['username'])) {
if (!in_array($_POST['dir'], $notYetEnabledDirs))
exit("ERROR : Wrong value for dir");
// Generate a .onion address
2021-03-04 01:02:38 +01:00
$torConf = file_get_contents(TOR_CONFIG_PATH);
$torConf = $torConf . "\nHiddenServiceDir " . TOR_KEYS_PATH . "/" . $_POST['dir'] . "/\nHiddenServicePort 1965 [::1]:1965";
file_put_contents(TOR_CONFIG_PATH, $torConf);
2021-03-02 22:56:38 +01:00
2021-03-04 01:02:38 +01:00
exec(SUDO_PATH . " " . MANIVER_PATH . " reload-tor", $output);
2021-03-02 22:56:38 +01:00
addNiverLog("Tor reloaded by " . $_SESSION['username'], $output);
// Copy generated address to a location readable by PHP
2021-03-04 01:02:38 +01:00
exec(SUDO_PATH . " " . MANIVER_PATH . " export-tor " . $_SESSION['username'] . " " . $_POST['dir'], $output);
2021-03-02 22:56:38 +01:00
addNiverLog("Tor data exported by " . $_SESSION['username'], $output);
// Get the address generated by Tor
$onion = file_get_contents("/srv/hyper/" . $_SESSION['username'] . "/hyper/" . $_POST['dir'] . "/hostname");
$onion = str_replace(array("\r","\n"), "", $onion);
// Store it in the database
addSite($_SESSION['username'], $_POST['dir'], $onion, "onion", "gemini");
$conf = "# START NIVERSITE
[" . $onion . "]
root=/srv/hyper/" . $_SESSION['username'] . "/hyper/" . $_POST['dir'] . "
# STOP NIVERSITE
";
file_put_contents("/etc/gmnisrv.ini", $conf, FILE_APPEND);
// Restart the gmnisrv daemon (as there is no reload support)
exec(SUDO_PATH . " " . MANIVER_PATH . " restart-gmnisrv");
2021-03-02 22:56:38 +01:00
// Tell the user their site address
echo "<p>L'adresse de votre capsule Onion Gemini est :<br><a href='http://" . $onion . "'<code>http://" . $onion . "</code></a></p>";
}
?>
<?php require "../bottom.inc.php"; ?>