Skip to content

Instantly share code, notes, and snippets.

@furlongm
Last active June 30, 2018 22:25
Show Gist options
  • Save furlongm/be0c9f4e8f07e52f8a99cdb2d61c9d99 to your computer and use it in GitHub Desktop.
Save furlongm/be0c9f4e8f07e52f8a99cdb2d61c9d99 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import socket
import select
from time import sleep
host = '127.0.0.1'
port = 5555
timeout = 3
status = """TITLE>--OpenVPN 2.3.10 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Jan 4 2016\r
TIME>---Wed Mar 23 21:42:22 2016>---1458729742\r
HEADER>-CLIENT_LIST>Common Name>Real Address>---Virtual Address>Bytes Received>-Bytes Sent>-Connected Since>Connected Since (time_t)>---Username\r
CLIENT_LIST>furlongm>---::ffff:59.167.120.210>--10.10.10.6>-369528>-1216150>Wed Mar 23 21:40:15 2016>---1458729615>-furlongm\r
CLIENT_LIST>furlongm>---59.167.120.211:12345>---10.10.10.7>-12345>--11615>--Wed Mar 23 21:41:45 2016>---1458729715>-furlongm\r
CLIENT_LIST>furlongm>---2001:4860:4801:3::20>---10.10.10.8>-12345>--11615>--Wed Mar 23 21:43:25 2016>---1458729815>-furlongm\r
HEADER>-ROUTING_TABLE>--Virtual Address>Common Name>Real Address>---Last Ref>---Last Ref (time_t)\r
ROUTING_TABLE>--10.10.10.6>-furlongm>---::ffff:59.167.120.210>--Wed Mar 23 21:42:22 2016>---1458729742\r
ROUTING_TABLE>--10.10.10.7>-furlongm>---59.167.120.211:12345>---Wed Mar 23 21:42:22 2016>---1458729742\r
ROUTING_TABLE>--10.10.10.8>-furlongm>---2001:4860:4801:3::20>---Wed Mar 23 21:42:22 2016>---1458729742\r
GLOBAL_STATS>---Max bcast/mcast queue length>---0\r
END\r
"""
status_24 = """TITLE>---OpenVPN 2.4.0 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Dec 27 2016\r
TIME>---Fri Dec 30 15:20:24 2016>---1483071624\r
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\r
CLIENT_LIST>furlongm>---59.167.120.210:56685>---10.10.10.6>->---241371>-221583>-Fri Dec 30 15:16:33 2016>---1483071393>-furlongm>---3>--0\r
CLIENT_LIST>furlongm>---120.21.106.66:48593>10.10.10.10>295048>->---400043>-Fri Dec 30 15:02:33 2016>---1483070553>-furlongm>---1>--1\r
CLIENT_LIST>furlongm>---2001:4860:4801:3::20>---10.10.10.8>->---12345>--11615>--Fri Dec 30 15:02:33 2016>---1483070553>-furlongm>---2>--2\r
HEADER>-ROUTING_TABLE>--Virtual>Address>Common>-Name>---Real>---Address>Last>---Ref>Last>---Ref>(time_t)\r
ROUTING_TABLE>--10.10.10.10>furlongm>---120.21.106.66:48593>Fri Dec 30 15:20:16 2016>---1483071616\r
ROUTING_TABLE>--10.10.10.6>-furlongm>---59.167.120.211:56685>---Fri Dec 30 15:20:24 2016>---1483071624\r
ROUTING_TABLE>--10.10.10.8>-furlongm>---2001:4860:4801:3::20>---Fri Dec 30 15:20:24 2016>---1483071624\r
GLOBAL_STATS>---Max>bcast/mcast>queue>--length>-0\r
END\r
"""
state = """1457583275,CONNECTED,SUCCESS,10.10.10.1,\r
END\r
"""
version = """OpenVPN Version: OpenVPN 2.3.10 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Jan 4 2016\r
Management Version: 1\r
END\r
"""
version_24 = version = """OpenVPN Version: OpenVPN 2.4.0 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Dec 27 2016\r
Management Version: 1\r
END\r
"""
stats = """SUCCESS: nclients=1,bytesin=556794,bytesout=1483013\r
"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(timeout)
except socket.error as e:
print('Failed to create socket: {0}').format(e)
sys.exit(1)
print('[+] Listening for connections on {0}:{1}'.format(host, port))
data = ''
exit = False
while not exit:
conn, address = s.accept()
print('[+] Connection from {0}'.format(address))
while 1:
try:
readable, writeable, in_error = \
select.select([conn, ], [conn, ], [], timeout)
except select.error, error:
print('[+] Closing connection from {0}'.format(address))
conn.shutdown(2)
conn.close()
break
if readable:
data = conn.recv(1024)
if data.endswith(u'\n'):
if data.startswith(u'status 3'):
conn.send(status_24)
data = ''
elif data.startswith(u'state'):
conn.send(state)
data = ''
elif data.startswith(u'version'):
conn.send(version_24)
data = ''
elif data.startswith(u'load-stats'):
conn.send(stats)
data = ''
elif data.startswith(u'quit'):
print('[+] Closing connection from {0}'.format(address))
conn.shutdown(2)
conn.close()
data = ''
break
elif data.startswith(u'exit'):
print('[+] Closing connection from {0}'.format(address))
conn.shutdown(2)
conn.close()
s.close()
exit = True
break
else:
pass
elif readable and writeable:
print('[+] Closing connection from {0}'.format(address))
conn.shutdown(2)
conn.close()
break
print('[+] Closing socket: {0}:{1}'.format(host, port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment