commit ea0f7372192f4219af8208dee503c0a45645d7f9 Author: h4775346 Date: Mon Jul 1 17:33:07 2024 +0300 1.0.0 diff --git a/index.php b/index.php new file mode 100644 index 0000000..0d6b749 --- /dev/null +++ b/index.php @@ -0,0 +1,461 @@ + $parts[0], + 'server' => $parts[1], + 'secret' => $parts[2], + 'ip' => $parts[3] + ]; + } + } + return $users; +} + +// Write the array back to the file +function writeUsers($file, $users) { + $content = "# Secrets for authentication using CHAP\n"; + $content .= "# client server secret IP addresses\n"; + $content .= "tunnel tunnel tunnel *\n"; + foreach ($users as $user) { + $content .= "{$user['client']} {$user['server']} {$user['secret']} {$user['ip']}\n"; + } + file_put_contents($file, $content); +} + +// Generate random password +function generateRandomPassword($length = 12) { + return bin2hex(random_bytes($length / 2)); +} + +// Get the next available IP address +function getNextIp($users) { + $lastIp = end($users)['ip']; + $lastIpLong = ip2long($lastIp); + $nextIpLong = $lastIpLong + 1; + + // Define the current and next range boundaries + $currentRangeStart = ip2long('10.255.' . explode('.', $lastIp)[2] . '.2'); + $currentRangeEnd = ip2long('10.255.' . explode('.', $lastIp)[2] . '.254'); + + // Check if the next IP exceeds the current range, move to the next range if necessary + if ($nextIpLong > $currentRangeEnd) { + $nextRangeStart = ip2long('10.255.' . (explode('.', $lastIp)[2] + 1) . '.2'); + $nextRangeEnd = ip2long('10.255.' . (explode('.', $lastIp)[2] + 1) . '.254'); + + // Ensure the next range does not exceed the defined ranges + if ($nextRangeStart <= ip2long('10.255.255.254')) { + $nextIpLong = $nextRangeStart; + } else { + // Handle the case when all ranges are exhausted (optional) + // For simplicity, you might want to stop here or handle the wraparound + die('No more IP addresses available in the defined ranges.'); + } + } + + return long2ip($nextIpLong); +} + +$users = readUsers($file); + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_POST['add'])) { + $client = $_POST['client']; + $ip = $_POST['ip'] ?? getNextIp($users); + + // Check for duplicate username and IP + foreach ($users as $user) { + if ($user['client'] === $client) { + echo json_encode(['error' => 'Username already exists']); + exit(); + } + if ($user['ip'] === $ip) { + echo json_encode(['error' => 'IP address already exists']); + exit(); + } + } + + $newUser = [ + 'client' => $client, + 'server' => '*', + 'secret' => $_POST['secret'], + 'ip' => $ip + ]; + $users[] = $newUser; + writeUsers($file, $users); + echo json_encode(['ip' => $newUser['ip']]); + exit(); + } elseif (isset($_POST['delete'])) { + $index = (int)$_POST['index']; + array_splice($users, $index, 1); + writeUsers($file, $users); + exit(); + } elseif (isset($_POST['addMultiple'])) { + $numUsers = (int)$_POST['numUsers']; + $ipRangeFrom = !empty($_POST['ipRangeFrom']) ? ip2long($_POST['ipRangeFrom']) : ip2long(getNextIp($users)); + $ipRangeTo = !empty($_POST['ipRangeTo']) ? ip2long($_POST['ipRangeTo']) : $ipRangeFrom + $numUsers - 1; + + $newUsers = []; + + for ($i = 0; $i < $numUsers; $i++) { + $username = 'user' . (count($users) + $i + 1); + $userIp = long2ip($ipRangeFrom + $i); + + // Check for duplicate username and IP within existing and new users + foreach (array_merge($users, $newUsers) as $user) { + if ($user['client'] === $username) { + echo json_encode(['error' => 'Username ' . $username . ' already exists']); + exit(); + } + if ($user['ip'] === $userIp) { + echo json_encode(['error' => 'IP address ' . $userIp . ' already exists']); + exit(); + } + } + + // Ensure the IP range does not exceed the defined ranges + if ($ipRangeFrom + $i > ip2long('10.255.255.254')) { + echo json_encode(['error' => 'IP range exhausted. Please start a new range.']); + exit(); + } + + $newUsers[] = [ + 'client' => $username, + 'server' => '*', + 'secret' => generateRandomPassword(), + 'ip' => $userIp + ]; + } + + $users = array_merge($users, $newUsers); + writeUsers($file, $users); + echo json_encode(['success' => true]); + exit(); + } +} +?> + + + + + Manage L2TP Users + + + + + + +
+

Manage L2TP Users

+
+ + + + + + + + + + + + $user): ?> + + + + + + + + + +
UsernameServerPasswordIP AddressActions
+ +
+
+
+ + +
+
+ + + + + + + + + + + + + + + + + +