Giter Site home page Giter Site logo

openvpn-status-parser's Introduction

openvpn-status-parser

Installation:

pip install openvpn-status-parser

or clone the repository and run

python setup.py install

Usage:

  • add status-version 3 to openvpn server configuration. Reload/restart openvpn server.
  • locate openvpn status file. Usually it's under /var/run in Unix based systems.
openvpn-status-parser /var/run/openvpn/openvpn.status

Or using Python:

import pprint
from openvpn_status_parser import OpenVPNStatusParser

parser = OpenVPNStatusParser("/var/run/openvpn/openvpn.status")
pprint.pprint(parser.connected_clients)
pprint.pprint(parser.routing_table)
pprint.pprint(parser.details)

License

MIT License; see LICENSE.txt for full details.

openvpn-status-parser's People

Contributors

ojarva avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

openvpn-status-parser's Issues

Exception when parsing "connected since" field

It seems my version of OpenVPN (2.5.1) has more fields in the client list than what is expected, which causes an exception while getting the client list from the parser object:

>>> from openvpn_status_parser import OpenVPNStatusParser
>>> import pprint
>>> parser = OpenVPNStatusParser("/tmp/status.log")
>>> pprint.pprint(parser.connected_clients)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nicolas/git/openvpn-status-parser/openvpn_status_parser/__init__.py", line 141, in connected_clients
    self._parse_file()
  File "/home/nicolas/git/openvpn-status-parser/openvpn_status_parser/__init__.py", line 120, in _parse_file
    self.title_processors[row_title](row)
  File "/home/nicolas/git/openvpn-status-parser/openvpn_status_parser/__init__.py", line 83, in _process_client_list
    self._connected_clients[row[1]]["connected_since"] = (datetime.datetime.fromtimestamp(int(row[-1])))
ValueError: invalid literal for int() with base 10: 'AES-256-CBC'

My OpenVPN server is configured with option "status-version 3".

Here is the (anonymized) status file :

TITLE	OpenVPN 2.5.1 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on May 14 2021
TIME	2021-12-16 16:13:52	1639667632
HEADER	CLIENT_LIST	Common Name	Real Address	Virtual Address	Virtual IPv6 Address	Bytes Received	Bytes Sent	Connected Since	Connected Since (time_t)	Username	Client ID	Peer ID	Data Channel Cipher
CLIENT_LIST	UNDEF	1.2.3.4:56736	10.42.42.2		67638680	45432322	2021-12-14 10:34:46	1639474486	my_user	609	0	AES-256-CBC
HEADER	ROUTING_TABLE	Virtual Address	Common Name	Real Address	Last Ref	Last Ref (time_t)
ROUTING_TABLE	96:03:2d:9d:0d:40@0	UNDEF	1.2.3.4:56736	2021-12-16 16:13:51	1639667631
GLOBAL_STATS	Max bcast/mcast queue length	1
END

The issue is caused because openvpn-status-parser is expecting the "Connected Since (time_t)" to be the last field ( row[-1])

I'll submit a PR fixing this.

ImportError: cannot import name OpenVPNStatusParser

Hello, when I try to run following script on my VPS, I got "ImportError: cannot import name OpenVPNStatusParser"
Any idea where the problem could be?

Thanks in advance.
Here's my code:

`import re
from subprocess import *
from openvpn_status_parser import OpenVPNStatusParser

PORTS = [5555, 9876, 38388]
REGEX_PATTERN = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}')

parser = OpenVPNStatusParser("/etc/openvpn/openvpn-status.log")

vpn_list = []
for row in parser.connected_clients:
real_address = parser.connected_clients[row]['Real Address']
for match in re.findall(REGEX_PATTERN, real_address):
vpn_list.append(match)

adb_devices_output = check_output(["adb", "devices"])

adb_list = []
for match in re.findall(REGEX_PATTERN, adb_devices_output):
adb_list.append(match)

print( 'VPN "%s"' % vpn_list)
print( 'ADB "%s"' % adb_list)

diff_list = list(set(vpn_list) - set(adb_list))
print( 'DIFF "%s"' % diff_list)

for ip in diff_list:
for port in PORTS:
ip_with_port = ip + ':' + str(port)
result = check_output(["adb", "connect", ip_with_port])
if 'connected' in result:
print( 'Connected %s' % ip_with_port)
break
else:
print( 'Failed to connect %s' % ip_with_port)`

Parser only returns one client if login/pass authentication is used insteat of client certificates

openvpn-status-parser uses the "Common Name" as the dict key in the connected_clients attributes.

This leads to a problem when no client certificate is used to authenticate users, but only login/pass, as the "Common Name" field is "UNDEF" for all connected users.

A consequence of this is that when iterating through all connected clients, the same key is used, and its value is overwritten each time, resulting in a list containing only one client.
Another consequence (not yet tested but supposed), is that even if client certificates are used, if the same user has multiple active sessions on the same VPN server (because he has several terminals, or connection issue), only one active connection will be seen.

A fix for this would be to use "Client ID" (which is supposed to be unique) instead of "Common Name" as the key to the connected_clients attribute.

I'll send a PR implementing this fix.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.