2022-12-20 20:17:03 +00:00
< ? php
2022-12-22 00:44:57 +00:00
if ( dirsStatuses ( 'onion' )[ $_POST [ 'dir' ]] !== false )
2022-12-20 20:17:03 +00:00
output ( 403 , 'Wrong value for <code>dir</code>.' );
rateLimit ();
// Add Tor config
$torConf = 'HiddenServiceDir ' . CONF [ 'ht' ][ 'tor_keys_path' ] . '/' . $_SESSION [ 'id' ] . '/' . $_POST [ 'dir' ] . ' /
HiddenServicePort 80 [ :: 1 ] : ' . CONF[' ht '][' internal_onion_http_port '] . '
' ;
if ( file_put_contents ( CONF [ 'ht' ][ 'tor_config_path' ] . '/' . $_SESSION [ 'id' ] . '/' . $_POST [ 'dir' ], $torConf ) === false )
output ( 500 , 'Failed to write new Tor configuration.' );
// Reload Tor
exec ( CONF [ 'ht' ][ 'sudo_path' ] . ' ' . CONF [ 'ht' ][ 'tor_reload_cmd' ], $output , $code );
if ( $code !== 0 )
output ( 500 , 'Failed to reload Tor.' );
// Get the address generated by Tor
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 );
$onion = $output [ 0 ];
if ( preg_match ( '/^[0-9a-z]{56}\.onion$/D' , $onion ) !== 1 )
output ( 500 , 'No onion address found.' );
// Store it in the database
2022-12-22 00:44:57 +00:00
addSite ( $_SESSION [ 'id' ], $_POST [ 'dir' ], $onion , 'onion' );
2022-12-20 20:17:03 +00:00
// Add Nginx config
$nginxConf = ' server {
listen [ :: 1 ] : ' . CONF[' ht '][' internal_onion_http_port '] . ' ;
server_name ' . $onion . ' ;
root ' . CONF[' ht '][' ht_path '] . ' / ' . $_SESSION[' id '] . ' / ' . $_POST[' dir '] . ' ;
include inc / ht - onion . conf ;
}
' ;
if ( file_put_contents ( CONF [ 'ht' ][ 'nginx_config_path' ] . '/' . $onion . '.conf' , $nginxConf ) === false )
output ( 500 , 'Failed to write Nginx configuration.' );
// Reload Nginx
exec ( CONF [ 'ht' ][ 'sudo_path' ] . ' ' . CONF [ 'ht' ][ 'nginx_reload_cmd' ], result_code : $code );
if ( $code !== 0 )
output ( 500 , 'Failed to reload Nginx.' );
// Tell the user their site address
2023-01-21 00:27:52 +00:00
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>' );