add-http-onion.php 2.5 KB

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