import socket
import sys
import os
import json

def userlist():
    socket_address = '/var/run/management-udp'
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect(socket_address)

    s.recv(1024)
    s.sendall('status 3\n')
    data = s.recv(1024).split('\n')

    s.close()

    cl = filter(lambda d: d.startswith('CLIENT_LIST\t'), data)
    rt = filter(lambda d: d.startswith('ROUTING_TABLE\t'), data)

    result = []

    for c in cl:
        centries = c.split('\t')
        name = centries[1]
        realaddress = centries[2]

        virtaddresses = ""

        for r in rt:
            rentries = r.split('\t')
            rname = rentries[2]
            rrealaddress = rentries[3]
            
            if name == rname and realaddress == rrealaddress:
                virtaddresses += rentries[1] + "\n"

        result.append({'name': name, 'connected': 'True', 'realip': realaddress, 'virtips': virtaddresses, 'recv': centries[4], 'sent': centries[5], 'cons': centries[6], 'cont': centries[7]})

    return result

def killuser(id):
    socket_address = '/var/run/management-udp'
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect(socket_address)

    s.recv(1024)
    s.sendall('kill %s\n' % id)
    data = s.recv(1024)

    s.close()
    
    return data

@@ -39,16 +39,38 @@
 ort json
+%0Afrom netaddr import *
 %0A%0Adef us
@@ -458,16 +458,47 @@
 t = %5B%5D%0A%0A
+    # iterate over client list%0A
     for 
@@ -601,16 +601,30 @@
 ies%5B2%5D%0A%0A
+%09conntype = 0%0A
         
@@ -643,16 +643,91 @@
 s = %22%22%0A%0A
+%09# iterate over routing table to get all virtual addresses for each client%0A
         
@@ -768,32 +768,64 @@
 = r.split('%5Ct')%0A
+%09    rvirtaddress = rentries%5B1%5D%0A
             rnam
@@ -954,16 +954,144 @@
 ddress:%0A
+%09%09if IPAddress(rvirtaddress).version == 4:%0A%09%09    conntype %7C= 1%0A%09%09elif IPAddress(rvirtaddress).version == 6:%0A%09%09    conntype %7C= 2%0A
         
@@ -1116,26 +1116,27 @@
 ses += r
-entries%5B1%5D
+virtaddress
  + %22%5Cn%22%0A
@@ -1182,22 +1182,30 @@
 conn
-ected': 'True'
+': 1, 'type': conntype
 , 'r
@@ -1252,20 +1252,20 @@
 esses, '
-recv
+cons
 ': centr
@@ -1272,15 +1272,15 @@
 ies%5B
-4
+6
 %5D, '
-se
+co
 nt':
@@ -1293,17 +1293,17 @@
 ies%5B
-5
+7
 %5D, '
-cons
+recv
 ': c
@@ -1302,39 +1302,39 @@
 recv': centries%5B
-6
+4
 %5D, '
-co
+se
 nt': centries%5B7%5D
@@ -1331,17 +1331,17 @@
 entries%5B
-7
+5
 %5D%7D)%0A%0A   

add connection type