-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the class, config files, and README
- Loading branch information
Showing
4 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |