listen.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. import sys
  3. import socket
  4. import select
  5. host = '127.0.0.1'
  6. port = 5555
  7. timeout = 3
  8. status = """TITLE OpenVPN 2.3.10 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Jan 4 2016\r
  9. TIME Wed Mar 23 21:42:22 2016 1458729742\r
  10. HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t) Username\r
  11. CLIENT_LIST furlongm ::ffff:59.167.120.210 10.10.10.6 369528 1216150 Wed Mar 23 21:40:15 2016 1458729615 furlongm\r
  12. CLIENT_LIST furlongm 59.167.120.211:12345 10.10.10.7 12345 11615 Wed Mar 23 21:41:45 2016 1458729715 furlongm\r
  13. CLIENT_LIST furlongm 2001:4860:4801:3::20 10.10.10.8 12345 11615 Wed Mar 23 21:43:25 2016 1458729815 furlongm\r
  14. HEADER ROUTING_TABLE Virtual Address Common Name Real Address Last Ref Last Ref (time_t)\r
  15. ROUTING_TABLE 10.10.10.6 furlongm ::ffff:59.167.120.210 Wed Mar 23 21:42:22 2016 1458729742\r
  16. ROUTING_TABLE 10.10.10.7 furlongm 59.167.120.211:12345 Wed Mar 23 21:42:22 2016 1458729742\r
  17. ROUTING_TABLE 10.10.10.8 furlongm 2001:4860:4801:3::20 Wed Mar 23 21:42:22 2016 1458729742\r
  18. GLOBAL_STATS Max bcast/mcast queue length 0\r
  19. END\r
  20. """
  21. state = """1457583275,CONNECTED,SUCCESS,10.10.10.1,\r
  22. END\r
  23. """
  24. 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
  25. Management Version: 1\r
  26. END\r
  27. """
  28. stats = """SUCCESS: nclients=1,bytesin=556794,bytesout=1483013\r
  29. """
  30. try:
  31. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  32. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  33. s.bind((host, port))
  34. s.listen(timeout)
  35. except socket.error as e:
  36. print('Failed to create socket: {0}').format(e)
  37. sys.exit(1)
  38. print('[+] Listening for connections on {0}:{1}'.format(host, port))
  39. data = ''
  40. received_exit = False
  41. while not received_exit:
  42. conn, address = s.accept()
  43. print('[+] Connection from {0}'.format(address))
  44. while 1:
  45. try:
  46. readable, writable, exceptional = \
  47. select.select([conn], [conn], [], timeout)
  48. except select.error:
  49. print('[+] Exception. Closing connection from {0}'.format(address))
  50. conn.shutdown(2)
  51. conn.close()
  52. break
  53. if readable:
  54. data = conn.recv(1024)
  55. if data.endswith(u'\n'):
  56. if data.startswith(u'status 3'):
  57. conn.send(status)
  58. data = ''
  59. elif data.startswith(u'state'):
  60. conn.send(state)
  61. data = ''
  62. elif data.startswith(u'version'):
  63. conn.send(version)
  64. data = ''
  65. elif data.startswith(u'load-stats'):
  66. conn.send(stats)
  67. data = ''
  68. elif data.startswith(u'quit'):
  69. print('[+] Closing connection from {0}'.format(address))
  70. conn.shutdown(2)
  71. conn.close()
  72. data = ''
  73. break
  74. elif data.startswith(u'exit'):
  75. print('[+] Closing connection from {0}'.format(address))
  76. conn.shutdown(2)
  77. conn.close()
  78. s.close()
  79. received_exit = True
  80. break
  81. else:
  82. pass
  83. elif readable and writable:
  84. print('[+] Closing connection from {0}'.format(address))
  85. conn.shutdown(2)
  86. conn.close()
  87. break
  88. print('[+] Closing socket: {0}:{1}'.format(host, port))