diff options
author | Mika Havela <mika.havela@gmail.com> | 2007-11-27 16:04:40 +0000 |
---|---|---|
committer | Mika Havela <mika.havela@gmail.com> | 2007-11-27 16:04:40 +0000 |
commit | 02d1365c0319a17df2ee3930fc038e0ae8f22880 (patch) | |
tree | be61162ad805e7d5b71d33670aaaf61cad0cba82 /openvpn-model.lua | |
parent | 84505f46e62bb2db4f68777000c5f65826ea4dad (diff) | |
download | acf-openvpn-02d1365c0319a17df2ee3930fc038e0ae8f22880.tar.bz2 acf-openvpn-02d1365c0319a17df2ee3930fc038e0ae8f22880.tar.xz |
Cleaning up model-code and getting some time-values displaying.
git-svn-id: svn://svn.alpinelinux.org/acf/openvpn/trunk@372 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'openvpn-model.lua')
-rw-r--r-- | openvpn-model.lua | 201 |
1 files changed, 40 insertions, 161 deletions
diff --git a/openvpn-model.lua b/openvpn-model.lua index d9c88f6..b13ca4f 100644 --- a/openvpn-model.lua +++ b/openvpn-model.lua @@ -1,4 +1,3 @@ --- hostname model methods module (..., package.seeall) require ("posix") @@ -6,42 +5,6 @@ require ("fs") -- no initializer in model - use controller.init for that - --- ################################################################################ --- UNKNOWN - ---local function read_file_as_array ( path ) --- local file, error = io.open(path) --- if ( file == nil ) then --- return nil, error --- end --- local f = {} --- for line in file:lines() do --- table.insert ( f , line ) --- end --- file:close() --- return f ---end - -local function has_init_script ( f ) - local initprefix = "/etc/init.d/openvpn" - local file = initprefix .. "." .. f - if f ~= "openvpn" then - if ( fs.is_file(file)) then - init = "yes" - else - init = nil - end - else - if ( fs.is_file(initprefix)) then - init = "yes" - else - init = nil - end - end - return init -end - -- ################################################################################ -- LOCAL FUNCTIONS @@ -54,6 +17,7 @@ local function config_content( f ) -- Filter out commented lines if not string.find ( l, "^[;#].*" ) then local a,b = string.match ( l, "^%s*(%S*)%s+(%S*).*$" ) -- Working exept on 'remote xxxxx xxxx' + -- FIXME: Problem with clients 'remote' values (the port num to the remote) -- local a,b = string.match ( l, "^%s*(%S*)%s+(.*)%s+$" ) -- This brakes client/server check if (a) then config[a]=b @@ -80,18 +44,16 @@ local is_running = function( process, parameters ) local pidofsx, error = io.popen("pidof " .. process ,r) local pidofs = string.gsub(pidofsx:read("*a"), "\n", "") pidofsx:close() --- if ( pidofs ~= nil ) then - for v in string.gmatch(pidofs, "%S+") do - local path = string.gsub("/proc/".. v .. "/cmdline", "%s", "") - local f,err = io.open(path,r) - local file_resultx = f:read("*a") - local file_result = string.match(file_resultx, parameters) - f:close() - if ( file_result ) then - retval = "Running" - end + for v in string.gmatch(pidofs, "%S+") do + local path = string.gsub("/proc/".. v .. "/cmdline", "%s", "") + local f,err = io.open(path,r) + local file_resultx = f:read("*a") + local file_result = string.match(file_resultx, parameters) + f:close() + if ( file_result ) then + retval = "Running" end --- end + end return retval end @@ -143,65 +105,11 @@ local function list_conffiles() end end --- FIXME: This should probably go in the time/date library --- This function gives diff (in seconds) between 'date' and current time. - -local function monthabbr_to_num ( month ) - local nummonth = "" - if (string.lower(month) == "jan") then nummonth = "1" end - if (string.lower(month) == "feb") then nummonth = "2" end - if (string.lower(month) == "mar") then nummonth = "3" end - if (string.lower(month) == "apr") then nummonth = "4" end - if (string.lower(month) == "may") then nummonth = "5" end - if (string.lower(month) == "jun") then nummonth = "6" end - if (string.lower(month) == "jul") then nummonth = "7" end - if (string.lower(month) == "aug") then nummonth = "8" end - if (string.lower(month) == "sep") then nummonth = "9" end - if (string.lower(month) == "oct") then nummonth = "10" end - if (string.lower(month) == "nov") then nummonth = "11" end - if (string.lower(month) == "dec") then nummonth = "12" end - return nummonth -end - -local function month_to_num ( dt ) - local strsplit = require("split") - -- date is something like "Fri 11 23 10:34:07 2007" - local olddate = strsplit(" ",dt) - local hour,min,sec = string.match ( rawget(olddate,4), "(%d%d):(%d%d):(%d%d)" ) - local olddatetable = { year = rawget(olddate,5), month = rawget(olddate,2), day = rawget(olddate,3), hour = hour, min = min, sec = sec } - - -- fetch current time - local f,err = io.popen("date") - currdate = f:read("*l") - f:close() - local newdate = strsplit(" ",currdate) - local hour,min,sec = string.match ( rawget(newdate,4), "(%d%d):(%d%d):(%d%d)" ) - local month = monthabbr_to_num(rawget(newdate,2)) - local newdatetable = { year = rawget(newdate,6), month = nummonth, day = rawget(newdate,3), hour = hour, min = min, sec = sec } - --- return os.difftime(os.time(newdatetable),os.time(olddatetable)) - -- Return currdate, olddate, datediff - return os.time(newdatetable), os.time(olddatetable), os.difftime(os.time(newdatetable),os.time(olddatetable)) -end -local function minmax_date ( userdate ) - local maxdate = 0 - local mindate = 0 - local f,err = io.popen("date +%Y ") - currdate = f:read("*l") - f:close() - local year,month,date,hour,min,sec = string.gmatch ( currdate, "(%d%d%d%d)%s+(%w+)%s+(%d%d)" ) --- local month = rawget(systemdate,2) --- local newdatetable = { year = rawget(systemdate,6), month = nummonth, day = rawget(systemdate,3), hour = hour, min = min, sec = sec } - --- return os.difftime(os.time(newdatetable),os.time(olddatetable)) - -- Return currdate, olddate, datediff - return os.time(userdate[2]) -end - -- ################################################################################ -- PUBLIC FUNCTIONS function clientlist( self, path ) + local libdate = require("date") local clientlist = {} local routinglist = {} local datechange = {} @@ -218,10 +126,10 @@ function clientlist( self, path ) if ( f ) then for k,v in ipairs(f) do local col = strsplit(",", v) - if ( col[1] == "ROUTING TABLE" ) then + if ( col[1] == "ROUTING TABLE" ) or ( col[1] == "GLOBAL STATS" ) then clientlst = nil + routinglst = nil end - if ( clientlst ) then table.insert(clientlist, { CN=col[1], REALADDR=col[2], @@ -234,72 +142,47 @@ function clientlist( self, path ) CN=col[2], REALADDR=col[3], LAST=col[4] } ) - table.insert(datechange, { year="2007", - month=monthabbr_to_num("may"), - day="10", - hour="15", - min="12", - sec="13" } ) + if (col[4]) then + local month,day,hour,min,sec,year = string.match(col[4],"^%S+%s+(%S+)%s+(%S+)%s+(%d%d):(%d%d):(%d%d)%s+(%S+)") + table.insert(datechange, { year=year, + month=libdate.abr_month_num(month), + day=day, + hour=hour, + min=min, + sec=sec } ) + end end - - if ( col[1] == "Common Name" ) then - clientlst = "YES" - end - if ( col[1] == "ROUTING TABLE" ) then + if ( col[1] == "Virtual Address" ) then routinglst = "YES" end - if ( col[1] == "GLOBAL STATS" ) then - routinglst = nil + if ( col[1] == "Common Name" ) then + clientlst = "YES" end + end end end - -- JOIN 'CLIENT_LIST' and 'ROUTING_LIST' TABLES INTO ONE TABLE + -- JOIN 'CLIENT_LIST' and 'ROUTING_LIST' TABLES INTO ONE TABLE AND LATER ON PRESENT THIS TABLE for k,v in ipairs(clientlist) do for kk,vv in ipairs(routinglist) do if ( v.CN == vv.CN ) then - local maxdatesec, mindatesec = minmax_date(datechange) table.insert(list, { CN=v.CN, REALADDR=v.REALADDR, BYTESRCV=v.BYTESRCV, BYTESSND=v.BYTESSND, VIRTADDR=vv.VIRTADDR, CONN=v.CONN, LAST = LAST } ) end end end - local connclients = table.maxn(list) - local difftime = maxdatesec - -- FIXME: This should probably be modifiead and go into a library!!! - return list, connclients, difftime -end - - -function clientlistWORKING( self, path ) - -- DEBUG --- local path = "openvpn.conf" - local clientlist = {} - local f = "" - local f2 = "" - if ( path ) then --- config = config_content ( config.name ) --- config = {} - config = config_content ( path ) - end - if (config.status) then - local f = fs.read_file_as_array( config.status ) - if ( f ) then - for k,v in ipairs(f) do - -- The reason for this compex regexp is that I want to filter away the first 2-3 rows - -- that doesn't mach this regexp. - local clientname,clientip,clientport,bytesreceived,bytessent = string.match ( v, "([^,]*)[,]+([%w]+[.*][%w]+[.*][%w]+[.*][%w]+)[:]([%w]+)[,](%w*)[,](%w*)" ) - -- Routing table is now intresting at this moment. So stop reading file. - if ( v == "ROUTING TABLE" ) then - break - end - if ( clientname ~= nil ) then - table.insert ( clientlist, cfe{ name = clientname, ip = clientip , virtualip = "xXx.xXx.xXx.xXx", port = clientport, received = bytesreceived, sent = bytessent } ) - end - end + connclients = table.maxn(list) + -- FIXME: Use lib/date.lua instead of the following code. + if ( connclients > 0 ) then + local lastdatechange = libdate.date_to_seconds(datechange) + lastdatechangetxt = os.date("%c", lastdatechange[#lastdatechange]) + lastdatechangediff = os.time() - os.date(lastdatechange[table.maxn(lastdatechange)]) + if (lastdatechangediff > 60) then + lastdatechangediff = math.modf(lastdatechangediff / 60) .. " min" + else + lastdatechangediff = lastdatechangediff .. " sec" end end - local connclients = table.maxn(clientlist) - return clientlist, connclients + return list, connclients, lastdatechangetxt, lastdatechangediff end function openvpn_version() @@ -315,13 +198,9 @@ end function get_serverconfig ( self, f ) local serverconfig = {} config = config_content ( f ) - -- FIXME: change nex row to clientlist(config.name) - local clientlist, connclients, lastupdate = clientlist () + local clientlist, connclients, lastupdate, lastdatechangediff = clientlist () local isrunning = is_running ("openvpn", f) --- local isrunning = f - -- FIXME: Get status for autostart_status = 'rc_status | grep this process' - local autostart_status = "" - serverconfig = cfe{ name = f, device = config.dev, log = config.log, verb = config.verb, maxclients = config["max-clients"], clients = connclients, status = isrunning, autostart = autostart_status, dh = config.dh, ca = config.ca, cert = config.cert, key = config.key, tls = config["tls-auth"] , crl = config["crl-verify"], port = config.port, proto = config.proto, loca = config["local"], longname = config.name, lastupdate = lastupdate, remote = config.remote } + serverconfig = cfe{ name = f, device = config.dev, log = config.log, verb = config.verb, maxclients = config["max-clients"], clients = connclients, status = isrunning, dh = config.dh, ca = config.ca, cert = config.cert, key = config.key, tls = config["tls-auth"] , crl = config["crl-verify"], port = config.port, proto = config.proto, loca = config["local"], longname = config.name, lastdatechangetxt = lastdatechangetxt, lastdatechangediff = lastdatechangediff, remote = config.remote } return serverconfig end |