'/etc/ppp/chap-secrets', 'admin_username' => 'admin', 'admin_password' => password_hash('change@me', PASSWORD_DEFAULT), ]; // Create config.php if it doesn't exist if (!file_exists('config.php')) { file_put_contents('config.php', ' $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)); } // Generate unique username function generateUniqueUsername($users) { do { $username = 'user' . bin2hex(random_bytes(4)); $unique = true; foreach ($users as $user) { if ($user['client'] === $username) { $unique = false; break; } } } while (!$unique); return $username; } // Get the next available IP address function getNextIp($users) { if (empty($users)) { return '10.255.10.11'; } $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') { // Handle logout if (isset($_POST['logout'])) { session_destroy(); header('Location: login.php'); exit(); } // Handle add user 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 = []; $addedUsers = 0; for ($i = 0; $addedUsers < $numUsers; $i++) { $username = generateUniqueUsername(array_merge($users, $newUsers)); $userIp = long2ip($ipRangeFrom + $i); // Check for duplicate IP within existing and new users $isDuplicateIp = false; foreach (array_merge($users, $newUsers) as $user) { if ($user['ip'] === $userIp) { $isDuplicateIp = true; break; } } if ($isDuplicateIp) { continue; } // 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 ]; $addedUsers++; } $users = array_merge($users, $newUsers); writeUsers($file, $users); echo json_encode(['success' => true]); exit(); } elseif (isset($_POST['changePassword'])) { $newPassword = password_hash($_POST['newPassword'], PASSWORD_DEFAULT); // Read the config file $config = include 'config.php'; // Update the password in the config file $config['admin_password'] = $newPassword; // Write the updated config back to the file file_put_contents('config.php', ' true]); exit(); } } ?> Manage L2TP Users

Manage L2TP Users

$user): ?>
Username Server Password IP Address Actions