diff options
-rw-r--r-- | etc/inc/ipsec.inc | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/etc/inc/ipsec.inc b/etc/inc/ipsec.inc index 8a27623..a3d373e 100644 --- a/etc/inc/ipsec.inc +++ b/etc/inc/ipsec.inc @@ -550,32 +550,52 @@ function ipsec_dump_sad() { * Return dump of mobile user list */ function ipsec_dump_mobile() { - global $g, $custom_listtags; + global $g; - $_gb = exec("/usr/local/sbin/ipsec stroke leases > {$g['tmp_path']}/strongswan_leases.xml"); + $_gb = exec("/usr/local/sbin/ipsec leases 2>/dev/null", $output, $rc); - if (!file_exists("{$g['tmp_path']}/strongswan_leases.xml")) { + if ($rc != 0) { log_error(gettext("Unable to find IPsec daemon leases file. Could not display mobile user stats!")); return array(); } - if (filesize("{$g['tmp_path']}/strongswan_leases.xml") == 0) { - return array(); - } - - if (!function_exists('parse_xml_config')) { - require_once('xmlparse.inc'); - } - - $custom_listtags = array('lease', 'pool'); - $response = parse_xml_config("{$g['tmp_path']}/strongswan_leases.xml", "leases"); - - if ($response == -1) { - return array(); + $response = array(); + $id = -1; + + /* Leases in pool '10.7.200.0/24', usage: 1/254, 1 online */ + $lease_regex='/^Leases *in *pool *\'(?P<name>.+)\', *usage: *(?P<usage>\d+)\/(?P<size>\d+), *(?P<online>\d+) *online/'; + /* 10.7.200.1 online 'jimp' */ + $pool_regex='/\s*(?P<host>[\d\.]+)\s+(?P<status>online|offline)\s+\'(?P<id>.*)\'/'; + /* no matching leases found */ + $nopool_regex='/no *matching *leases *found/'; + + $lease=false; + foreach ($output as $line) { + if (preg_match($lease_regex, $line, $matches)) { + $id++; + $response['pool'][$id] = array( + 'name' => $matches['name'], + 'usage' => $matches['usage'], + 'size' => $matches['size'], + 'online' => $matches['online'], + ); + $lease=true; + } else if ($lease) { + if (preg_match($nopool_regex, $line)) { + $response['pool'][$id]['lease'][] = array(); + $lease=false; + } else if (preg_match($pool_regex, $line, $matches)) { + $response['pool'][$id]['lease'][] = array( + 'host' => $matches['host'], + 'status' => $matches['status'], + 'id' => $matches['id'] + ); + } + } } - @unlink("{$g['tmp_path']}/strongswan_leases.xml"); - unset($custom_listtags, $_gb); + unset($_gb, $lease, $output, $rc, $id, $lease_regex, $pool_regex, + $nopool_regex); return $response; } |