+ovpn-connect script

This commit is contained in:
kirillius 2024-12-24 15:35:06 +03:00
parent a331b3159d
commit 6aaa4f5312
1 changed files with 39 additions and 0 deletions

39
utils/ovpn-connect.php Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/php
<?php
if (!isset($argv[1])) {
exit(1);
}
$TMPFILE = $argv[1];
require_once __DIR__ . "/loader.php";
try {
$networks = (new NetworkConfigReader())->getConfigs();
$config = new Config();
$config->read();
$data = [];
//add new routes
foreach ($config["networks"] as $key) {
if (isset($networks[$key])) {
foreach ($networks[$key]["networks"] as $route) {
$parts = explode("/", $route);
$mask = long2ip(-1 << (32 - (int)$parts[1]));
$dst = $parts[0];
$data[] = "ifconfig-push {$dst} {$mask}";
}
}
}
file_put_contents($TMPFILE, implode("\n", $data));
} catch (Exception $e) {
echo "\nError:" . $e->getMessage() . "\n";
exit(1);
}
exit(0);