add-http-onion.php 2.4 KB

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