Skip to content

Commit

Permalink
Add the class, config files, and README
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-sh-dev committed Apr 21, 2021
1 parent 5bcc200 commit a3b78c0
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
/vendor/
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# OpenVPN-Parser
Fast Parser for OpenVPN Status File

Parse OpenVPN status files using php

# Usage

* Install using composer:
```shell script
composer require sh_sh_dev/openvpn-parser
```

After including composer dependencies in your project, just use the class:

```php
Use Shay3gan\OpenVPN\Parser;

$openvpn = new Parser();
$openvpn->setStatusFile("openvpn-status.log");

print_r($openvpn->parse());
```

17 changes: 17 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "sh_sh_dev/openvpn-parser",
"description": "Parse OpenVPN status file",
"type": "library",
"keywords": ["openvpn", "openvpn-php", "openvpn-parser", "openvpn status file", "openvpn status file php"],
"license": "MIT",
"homepage": "https://github.com/sh-sh-dev/OpenVPN-Parser",
"authors": [
{
"name": "Shaygan",
"email": "shsh512@pm.me"
}
],
"require": {
"php": ">7.*"
}
}
111 changes: 111 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Shay3gan\OpenVPN;

use InvalidArgumentException;

/**
* Class Parser
* @package Shay3gan\OpenVPN\Parser
*/
class Parser
{
/**
* @var string status file path
*/
private $statusFile = null;

/**
* Get current passed status file path
*
* @return string|null
*/
public function getStatusFile()
{
return $this->statusFile;
}

/**
* Set status file path
*
* @param string $statusFilePath
* @return void
*/
public function setStatusFile($statusFilePath)
{
$this->statusFile = $statusFilePath;
}

/**
* Parse status file
*
* @return array
*/
public function parse()
{
$statusFile = $this->getStatusFile();
$this->validateStatusFile($statusFile);

$fileHandle = fopen($statusFile, 'r');
$result = [
'updated_at' => null,
'connections' => null,
];

while (!feof($fileHandle)) {
$readLine = fgets($fileHandle, 4096);
$line = explode(',', $readLine);

switch ($line[0]) {
case 'TIME':
$result['updated_at'] = trim($line[2]);
break;

case 'CLIENT_LIST':
// Empty virtual-address means user is not authenticated yet
if (empty($line[3]))
continue 2;

$result['connections'][$line[3]] = [
'common_name' => $line[1] === 'UNDEF' ? null : $line[1],
'origin' => $line[2],
'virtual_address' => $line[3],
'received' => $line[5],
'sent' => $line[6],
'username' => $line[9] === 'UNDEF' ? null : $line[9],
'connection_id' => $line[10],
'last_ref' => null,
'created_at' => $line[8],
];
break;

case 'ROUTING_TABLE':
// 'last ref' index is the latest index, so it has a NEWLINE marker and should be removed
$result['connections'][$line[1]]['last_ref'] = trim($line[5]);
break;

default:
continue 2;
}
}

fclose($fileHandle);

return $result;
}

/**
* Check entered status file
*
* @param string $statusFile
* @return bool
*/
private function validateStatusFile($statusFile) {
if (!$statusFile)
throw new InvalidArgumentException('Status file is not passed.');
if (!file_exists($statusFile))
throw new InvalidArgumentException('File path is invalid.');

return true;
}
}

0 comments on commit a3b78c0

Please sign in to comment.