add-http-onion.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php require "../../common/html.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. switchToFormProcess();
  24. if ($dirsStatuses[$_POST['dir']] !== false)
  25. userError("Wrong value for <code>dir</code>.");
  26. // Add Tor config
  27. $torConf = "HiddenServiceDir " . CONF['ht']['tor_keys_path'] . "/" . $_SESSION['username'] . "/" . $_POST['dir'] . "/
  28. HiddenServicePort 80 [::1]:" . CONF['ht']['internal_onion_http_port'] . "
  29. ";
  30. if (file_put_contents(CONF['ht']['tor_config_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'], $torConf) === false)
  31. serverError("Failed to write new Tor configuration.");
  32. // Reload Tor
  33. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload " . CONF['ht']['tor_service'], $output, $code);
  34. if ($code !== 0)
  35. serverError("Failed to reload Tor.");
  36. // Get the address generated by Tor
  37. exec(CONF['ht']['sudo_path'] . ' -u ' . CONF['ht']['tor_user'] . ' ' . CONF['ht']['cat_path'] . ' ' . CONF['ht']['tor_keys_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . '/hostname', $output);
  38. $onion = $output[0];
  39. if (preg_match("/[0-9a-z]{56}\.onion/", $onion) !== 1)
  40. serverError("No onion address found.");
  41. // Store it in the database
  42. addSite($_SESSION['username'], $_POST['dir'], $onion, "onion", "http");
  43. // Add Nginx config
  44. $nginxConf = 'server {
  45. listen [::1]:' . CONF['ht']['internal_onion_http_port'] . ';
  46. server_name ' . $onion . ';
  47. root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
  48. include inc/ht-onion.conf;
  49. }
  50. ';
  51. if (file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $onion . ".conf", $nginxConf) === false)
  52. serverError("Failed to write Nginx configuration.");
  53. // Reload Nginx
  54. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", result_code: $code);
  55. if ($code !== 0)
  56. serverError("Failed to reload Nginx.");
  57. // Tell the user their site address
  58. success("L'adresse de votre service Onion HTTP est : <a href='http://" . $onion . "/'<code>http://" . $onion . "/</code></a>");