add-onion.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php declare(strict_types=1);
  2. if (dirsStatuses('onion')[$_POST['dir']] !== false)
  3. output(403, 'Wrong value for <code>dir</code>.');
  4. rateLimit();
  5. // Add Tor config
  6. $torConfFile = CONF['ht']['tor_config_path'] . '/' . $_SESSION['id'] . '/' . $_POST['dir'];
  7. $torConf = 'HiddenServiceDir ' . CONF['ht']['tor_keys_path'] . '/' . $_SESSION['id'] . '/' . $_POST['dir'] . '/
  8. HiddenServicePort 80 ' . CONF['ht']['onion_internal_host'] . LF;
  9. if (file_put_contents($torConfFile, $torConf) === false)
  10. output(500, 'Failed to write new Tor configuration file.');
  11. if (chmod($torConfFile, 0644) !== true)
  12. output(500, 'Failed to give correct permissions to new Tor configuration file.');
  13. // Reload Tor
  14. reloadTor();
  15. usleep(10000);
  16. // Get the hostname generated by Tor
  17. exescape([
  18. CONF['ht']['sudo_path'],
  19. '-u',
  20. CONF['ht']['tor_user'],
  21. '--',
  22. CONF['ht']['cat_path'],
  23. '--',
  24. CONF['ht']['tor_keys_path'] . '/' . $_SESSION['id'] . '/' . $_POST['dir'] . '/hostname',
  25. ], $output, $code);
  26. if ($code !== 0)
  27. output(500, 'Unable to read hostname file.');
  28. $onion = $output[0];
  29. if (preg_match('/^[0-9a-z]{56}\.onion$/D', $onion) !== 1)
  30. output(500, 'No onion address found.');
  31. // Store it in the database
  32. addSite($_SESSION['id'], $_POST['dir'], $onion, 'onion');
  33. htRelativeSymlink('../fs/' . $_SESSION['id'] . '/' . $_POST['dir'], CONF['ht']['ht_path'] . '/uri/' . $onion);
  34. // Tell the user their site address
  35. output(200, sprintf(_('%s added on this directory.'), PAGE_METADATA['title']) . ' ' . sprintf(_('Its address is: %s'), '<a href="http://' . $onion . '/"><code>http://' . $onion . '/</code></a>'));