From 8ebcb654ee4f6092e0d1afbd35d74ac30e18ddfb Mon Sep 17 00:00:00 2001 From: Stefan Pejcic Date: Tue, 12 Nov 2024 19:00:06 +0100 Subject: [PATCH] Create index.php --- services/proxy/html/index.php | 130 ++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 services/proxy/html/index.php diff --git a/services/proxy/html/index.php b/services/proxy/html/index.php new file mode 100644 index 00000000..2cddc2a5 --- /dev/null +++ b/services/proxy/html/index.php @@ -0,0 +1,130 @@ +getMessage()); +}); + +if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + $clientIp = $_SERVER['HTTP_X_FORWARDED_FOR']; +} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) { + $clientIp = $_SERVER['HTTP_X_REAL_IP']; +} else { + $clientIp = $_SERVER['REMOTE_ADDR']; +} +$clientIp = explode(',', $clientIp)[0]; + +logMessage("Client IP: $clientIp"); + + + +// generate subdomains +function generateRandomSubdomain($domain) { + $randomString = substr(bin2hex(random_bytes(3)), 0, 5); + return $randomString . '.' . $domain; +} + +// redirect +function redirectToSubdomain($subdomain, $domain) { + $url = "https://$subdomain.$domain"; + logMessage("Redirecting to: $url"); + header("Location: $url"); + exit(); +} + +// cp skeleton +function copyData($sourceDir, $destinationDir) { + if (!is_dir($sourceDir)) { + logMessage("Source directory does not exist: $sourceDir"); + return false; + } + + $files = scandir($sourceDir); + foreach ($files as $file) { + if ($file === '.' || $file === '..') { + continue; + } + copy("$sourceDir/$file", "$destinationDir/$file"); + logMessage("Copied file: $file to $destinationDir"); + } + return true; +} + + + +// create proxy +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + logMessage("Received POST request"); + + $rootDomain = $GLOBALS["rootDomain"]; + $domain = ''; + $rootDir = '/var/www/html/virt'; + + $ip = $_POST['ip'] ?? ''; + $fake_domain = $_POST['domain'] ?? ''; + + logMessage("Received POST data: " . print_r($_POST, true)); + + if (empty($ip) || empty($fake_domain)) { + $errorMsg = "IP and Domain are required."; + logMessage($errorMsg); + echo $errorMsg; + exit(); + } + + $subdomain = generateRandomSubdomain($fake_domain); + $subdomainPart = explode('.', $subdomain)[0]; + $destinationDir = "/var/www/html/domains/$subdomainPart"; + $destinationDirR = "/var/www/html/$subdomainPart"; + logMessage("Generated subdomain: $subdomain"); + + logMessage("Attempting to create directory: $destinationDir"); + +if (!is_writable(dirname($destinationDir))) { + logMessage("Directory is not writable: " . dirname($destinationDir)); + exit("Error: Parent directory is not writable."); +} + +if (!mkdir($destinationDir, 0755, true)) { + logMessage("Failed to create directory: $destinationDir. Permissions or path issue?"); + exit("Error: Failed to create directory."); +} else { + logMessage("Directory created successfully: $destinationDir"); +} + + if (!copyData($rootDir, $destinationDir)) { + logMessage("Failed to copy data from $rootDir to $destinationDir"); + exit("Error: Failed to copy data."); + } + logMessage("Copied data from $rootDir to $destinationDir"); + + // config to use for proxy + $configFilePath = "$destinationDir/config.php"; + $configContent = "