~fkooman/vpn-user-portal

1fb751f9423a1a41e2ff2071d1b0843c342e4237 — François Kooman 3 months ago 687e457
add script to generate random IPv4 and IPv6 prefixes
1 files changed, 38 insertions(+), 0 deletions(-)

A bin/generate-prefix.php
A bin/generate-prefix.php => bin/generate-prefix.php +38 -0
@@ 0,0 1,38 @@
<?php

declare(strict_types=1);

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2014-2023, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

$showFour = false;
$showSix = false;

for ($i = 1; $i < $argc; $i++) {
    if ('-4' === $argv[$i]) {
        $showFour = true;
    }
    if ('-6' === $argv[$i]) {
        $showSix = true;
    }
    if ('-h' === $argv[$i] || '--help' === $argv[$i]) {
        echo 'SYNTAX: ' . $argv[0] . ' [-4] [-6]' . PHP_EOL;
        exit(0);
    }
}

// if nothing is explicity requested, show both IPv4 and IPv6 prefix
if (!$showFour && !$showSix) {
    $showFour = $showSix = true;
}

if ($showFour) {
    echo '10.' . ord(random_bytes(1)) . '.' . ord(random_bytes(1)) . '.' . ord(random_bytes(1)) . '/24' . PHP_EOL;
}
if ($showSix) {
    echo chunk_split('fd' . bin2hex(random_bytes(7)), 4, ':') . ':/64' . PHP_EOL;
}