Skip to content

Commit

Permalink
chore: split functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaser committed Sep 18, 2024
1 parent c677b97 commit 7719051
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
36 changes: 0 additions & 36 deletions src/usr/local/emhttp/plugins/tailscale/include/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,6 @@
}
}

function logmsg($message, $priority = LOG_INFO)
{
$timestamp = date('Y/m/d H:i:s');
$filename = basename($_SERVER['PHP_SELF']);
file_put_contents("/var/log/tailscale-utils.log", "{$timestamp} {$filename}: {$message}" . PHP_EOL, FILE_APPEND);
}

function run_command($command, $alwaysShow = false, $show = true)
{
$output = array();
$retval = null;
if ($show) {
logmsg($command);
}
exec("{$command} 2>&1", $output, $retval);

if (($retval != 0) || $alwaysShow) {
logmsg("Command returned {$retval}" . PHP_EOL . implode(PHP_EOL, $output));
}

return $output;
}

function ip4_in_network($ip, $network)
{
if (strpos($network, '/') === false) {
return false;
}

list($subnet, $mask) = explode('/', $network, 2);
$ip_bin_string = sprintf("%032b", ip2long($ip));
$net_bin_string = sprintf("%032b", ip2long($subnet));

return (substr_compare($ip_bin_string, $net_bin_string, 0, $mask) === 0);
}

$plugin = "tailscale";
$ifname = 'tailscale1';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

function ip4_in_network($ip, $network)
{
if (strpos($network, '/') === false) {
return false;
}

list($subnet, $mask) = explode('/', $network, 2);
$ip_bin_string = sprintf("%032b", ip2long($ip));
$net_bin_string = sprintf("%032b", ip2long($subnet));

return (substr_compare($ip_bin_string, $net_bin_string, 0, $mask) === 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

function logmsg($message, $priority = LOG_INFO)
{
$timestamp = date('Y/m/d H:i:s');
$filename = basename($_SERVER['PHP_SELF']);
file_put_contents("/var/log/tailscale-utils.log", "{$timestamp} {$filename}: {$message}" . PHP_EOL, FILE_APPEND);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

function run_command($command, $alwaysShow = false, $show = true)
{
$output = array();
$retval = null;
if ($show) {
logmsg($command);
}
exec("{$command} 2>&1", $output, $retval);

if (($retval != 0) || $alwaysShow) {
logmsg("Command returned {$retval}" . PHP_EOL . implode(PHP_EOL, $output));
}

return $output;
}

0 comments on commit 7719051

Please sign in to comment.