http-onion.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php require "../../common/top.php"; ?>
  2. <p>
  3. Ajouter un accès en .onion sur un dossier
  4. </p>
  5. <form method="post">
  6. <label for="dir">Dossier ciblé</label><br>
  7. <select required="" name="dir" id="dir">
  8. <option value="" disabled="" selected="">---</option>
  9. <?php
  10. if (isset($_SESSION['username'])) {
  11. $dirsStatuses = dirsStatuses($_SESSION['username'], "onion", "http");
  12. foreach ($dirsStatuses as $dir => $alreadyEnabled) {
  13. $disabled = $alreadyEnabled ? "disabled='' " : "";
  14. echo " <option " . $disabled . "value='" . $dir . "'>" . $dir . "</option>";
  15. }
  16. }
  17. ?>
  18. </select>
  19. <br>
  20. <input value="Valider" type="submit">
  21. </form>
  22. <?php
  23. if (isset($_POST['dir']) AND isset($_SESSION['username'])) {
  24. antiCSRF();
  25. if ($dirsStatuses[$_POST['dir']] !== false)
  26. userError("Wrong value for <code>dir</code>.");
  27. // Generate a .onion address
  28. $torConf = file_get_contents(CONF['ht']['tor_config_path']);
  29. $torConf = $torConf . "HiddenServiceDir " . CONF['ht']['tor_keys_path'] . "/" . $_POST['dir'] . "/
  30. HiddenServicePort 80 [::1]:" . CONF['ht']['internal_onion_http_port'] . "
  31. ";
  32. file_put_contents(CONF['ht']['tor_config_path'], $torConf);
  33. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload tor", $output);
  34. addNiverLog("Tor reloaded by " . $_SESSION['username'], $output);
  35. // Copy generated address to a location readable by PHP
  36. exec(CONF['ht']['sudo_path'] . " " . MANIVER_PATH . " export-tor " . $_SESSION['username'] . " " . $_POST['dir'], $output);
  37. addNiverLog("Tor data exported by " . $_SESSION['username'], $output);
  38. // Wait
  39. sleep(1);
  40. // Get the address generated by Tor
  41. $onion = file_get_contents(CONF['ht']['ht_path'] . "/" . $_SESSION['username'] . "/" . $_POST['dir'] . "/hostname");
  42. $onion = str_replace(array("\r", "\n"), "", $onion);
  43. if (preg_match("/[0-9a-z]{56}\.onion/", $onion) !== 1)
  44. serverError("No onion address found.");
  45. // Store it in the database
  46. addSite($_SESSION['username'], $_POST['dir'], $onion, "onion", "http");
  47. // Add it to Nginx
  48. $nginxConf = file_get_contents(NIVER_TEMPLATE_PATH . "/nginx/onion.template");
  49. $nginxConf = str_replace("{{CONF['ht']['internal_onion_http_port']}}", CONF['ht']['internal_onion_http_port'], $nginxConf);
  50. $nginxConf = str_replace("{{DOMAIN}}", $onion, $nginxConf);
  51. $nginxConf = str_replace("{{CONF['ht']['ht_path']}}", CONF['ht']['ht_path'], $nginxConf);
  52. $nginxConf = str_replace("{{USERNAME}}", $_SESSION['username'], $nginxConf);
  53. $nginxConf = str_replace("{{DIR}}", $_POST['dir'], $nginxConf);
  54. file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['dir'] . ".conf", $nginxConf);
  55. // Reload Nginx
  56. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", $output);
  57. addNiverLog("Nginx reloaded by " . $_SESSION['username'], $output);
  58. // Tell the user their site address
  59. echo "<p>L'adresse de votre site Onion HTTP est :<br><a href='http://" . $onion . "'<code>http://" . $onion . "</code></a></p>";
  60. }
  61. ?>
  62. <?php require "../../common/bottom.php"; ?>