add-http-onion.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. if (isset($_SESSION['username']))
  3. $dirsStatuses = dirsStatuses($_SESSION['username'], "onion", "http");
  4. else
  5. $dirsStatuses = [];
  6. if (processForm()) {
  7. if ($dirsStatuses[$_POST['dir']] !== false)
  8. output(403, 'Wrong value for <code>dir</code>.');
  9. // Add Tor config
  10. $torConf = "HiddenServiceDir " . CONF['ht']['tor_keys_path'] . "/" . $_SESSION['username'] . "/" . $_POST['dir'] . "/
  11. HiddenServicePort 80 [::1]:" . CONF['ht']['internal_onion_http_port'] . "
  12. ";
  13. if (file_put_contents(CONF['ht']['tor_config_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'], $torConf) === false)
  14. output(500, 'Failed to write new Tor configuration.');
  15. // Reload Tor
  16. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload " . CONF['ht']['tor_service'], $output, $code);
  17. if ($code !== 0)
  18. output(500, 'Failed to reload Tor.');
  19. // Get the address generated by Tor
  20. 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);
  21. $onion = $output[0];
  22. if (preg_match("/[0-9a-z]{56}\.onion/", $onion) !== 1)
  23. output(500, 'No onion address found.');
  24. // Store it in the database
  25. addSite($_SESSION['username'], $_POST['dir'], $onion, "onion", "http");
  26. // Add Nginx config
  27. $nginxConf = 'server {
  28. listen [::1]:' . CONF['ht']['internal_onion_http_port'] . ';
  29. server_name ' . $onion . ';
  30. root ' . CONF['ht']['ht_path'] . '/' . $_SESSION['username'] . '/' . $_POST['dir'] . ';
  31. include inc/ht-onion.conf;
  32. }
  33. ';
  34. if (file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $onion . ".conf", $nginxConf) === false)
  35. output(500, 'Failed to write Nginx configuration.');
  36. // Reload Nginx
  37. exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", result_code: $code);
  38. if ($code !== 0)
  39. output(500, 'Failed to reload Nginx.');
  40. // Tell the user their site address
  41. output(200, 'L\'adresse de votre service Onion HTTP est : <a href="http://' . $onion . '/"><code>http://' . $onion . '/</code></a>');
  42. }
  43. ?>
  44. <p>
  45. Ajouter un accès en .onion sur un dossier
  46. </p>
  47. <form method="post">
  48. <label for="dir">Dossier ciblé</label><br>
  49. <select required="" name="dir" id="dir">
  50. <option value="" disabled="" selected="">---</option>
  51. <?php
  52. foreach ($dirsStatuses as $dir => $alreadyEnabled) {
  53. $disabled = $alreadyEnabled ? "disabled='' " : "";
  54. echo " <option " . $disabled . "value='" . $dir . "'>" . $dir . "</option>";
  55. }
  56. ?>
  57. </select>
  58. <br>
  59. <input value="Valider" type="submit">
  60. </form>