1
0
mirror of https://github.com/cmur2/openvpn-status-web.git synced 2024-12-22 12:54:24 +01:00

gem: parse any number of list headers in status-version 2/3

This commit is contained in:
cn 2022-01-18 01:56:15 +01:00 committed by Christian Nicolai
parent 39d2b3ce94
commit 528709c895
5 changed files with 39 additions and 25 deletions

View File

@ -1,8 +1,8 @@
TITLE,OpenVPN 2.1_rc15 mipsel-unknown-linux-gnu [SSL] [LZO1] [EPOLL] built on Mar 27 2009 TITLE,OpenVPN 2.1_rc15 mipsel-unknown-linux-gnu [SSL] [LZO1] [EPOLL] built on Mar 27 2009
TIME,Sun Jan 1 23:42:00 2012,1238702330 TIME,Sun Jan 1 23:42:00 2012,1238702330
HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t) HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)
CLIENT_LIST,foo,1.2.3.4:1234,11811160064,4194304,Sun Jan 1 23:42:00 2012,1238702330 CLIENT_LIST,foo,1.2.3.4:1234,192.168.66.2,11811160064,4194304,Sun Jan 1 23:42:00 2012,1238702330
CLIENT_LIST,bar,1.2.3.5:1235,512,2048,Sun Jan 1 23:42:00 2012,1238702330 CLIENT_LIST,bar,1.2.3.5:1235,2001:db8:0:0::1000,512,2048,Sun Jan 1 23:42:00 2012,1238702330
HEADER,ROUTING_TABLE,Virtual Address,Common Name,Real Address,Last Ref,Last Ref (time_t) HEADER,ROUTING_TABLE,Virtual Address,Common Name,Real Address,Last Ref,Last Ref (time_t)
ROUTING_TABLE,192.168.0.0/24,foo,1.2.3.4:1234,Sun Jan 1 23:42:00 2012,1238702330 ROUTING_TABLE,192.168.0.0/24,foo,1.2.3.4:1234,Sun Jan 1 23:42:00 2012,1238702330
ROUTING_TABLE,192.168.66.2,bar,1.2.3.5:1235,Sun Jan 1 23:42:00 2012,1238702330 ROUTING_TABLE,192.168.66.2,bar,1.2.3.5:1235,Sun Jan 1 23:42:00 2012,1238702330

View File

@ -1,8 +1,8 @@
TITLE OpenVPN 2.1_rc15 mipsel-unknown-linux-gnu [SSL] [LZO1] [EPOLL] built on Mar 27 2009 TITLE OpenVPN 2.1_rc15 mipsel-unknown-linux-gnu [SSL] [LZO1] [EPOLL] built on Mar 27 2009
TIME Sun Jan 1 23:42:00 2012 1238702330 TIME Sun Jan 1 23:42:00 2012 1238702330
HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t) HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)
CLIENT_LIST foo 1.2.3.4:1234 11811160064 4194304 Sun Jan 1 23:42:00 2012 1238702330 CLIENT_LIST foo 1.2.3.4:1234 192.168.66.2 11811160064 4194304 Sun Jan 1 23:42:00 2012 1238702330
CLIENT_LIST bar 1.2.3.5:1235 512 2048 Sun Jan 1 23:42:00 2012 1238702330 CLIENT_LIST bar 1.2.3.5:1235 2001:db8:0:0::1000 512 2048 Sun Jan 1 23:42:00 2012 1238702330
HEADER ROUTING_TABLE Virtual Address Common Name Real Address Last Ref Last Ref (time_t) HEADER ROUTING_TABLE Virtual Address Common Name Real Address Last Ref Last Ref (time_t)
ROUTING_TABLE 192.168.0.0/24 foo 1.2.3.4:1234 Sun Jan 1 23:42:00 2012 1238702330 ROUTING_TABLE 192.168.0.0/24 foo 1.2.3.4:1234 Sun Jan 1 23:42:00 2012 1238702330
ROUTING_TABLE 192.168.66.2 bar 1.2.3.5:1235 Sun Jan 1 23:42:00 2012 1238702330 ROUTING_TABLE 192.168.66.2 bar 1.2.3.5:1235 Sun Jan 1 23:42:00 2012 1238702330

View File

@ -53,7 +53,7 @@ thead {
<% status.client_list_headers.each_with_index do |header,i| %> <% status.client_list_headers.each_with_index do |header,i| %>
<% if i == 0 %> <% if i == 0 %>
<td class="first"> <td class="first">
<% elsif i == status.client_list_headers.length-1 %> <% elsif i == status.client_list_headers.size-1 %>
<td class="last"> <td class="last">
<% else %> <% else %>
<td class="middle"> <td class="middle">
@ -67,13 +67,13 @@ thead {
<% status.client_list_headers.each_with_index do |header,i| %> <% status.client_list_headers.each_with_index do |header,i| %>
<% if i == 0 %> <% if i == 0 %>
<td class="first"> <td class="first">
<% elsif i == status.client_list_headers.length-1 %> <% elsif i == status.client_list_headers.size-1 %>
<td class="last"> <td class="last">
<% else %> <% else %>
<td class="middle"> <td class="middle">
<% end %> <% end %>
<% if header =~ /(Received|Sent)/ %> <% if header =~ /(Received|Sent)/ %>
<%= client[i].to_i.as_bytes %></td> <%= client[i].as_bytes %></td>
<% elsif client[i].is_a? DateTime %> <% elsif client[i].is_a? DateTime %>
<%= client[i].strftime('%-d.%-m.%Y %H:%M:%S') %></td> <%= client[i].strftime('%-d.%-m.%Y %H:%M:%S') %></td>
<% else %> <% else %>
@ -93,7 +93,7 @@ thead {
<% status.routing_table_headers.each_with_index do |header,i| %> <% status.routing_table_headers.each_with_index do |header,i| %>
<% if i == 0 %> <% if i == 0 %>
<td class="first"> <td class="first">
<% elsif i == status.routing_table_headers.length-1 %> <% elsif i == status.routing_table_headers.size-1 %>
<td class="last"> <td class="last">
<% else %> <% else %>
<td class="middle"> <td class="middle">
@ -107,7 +107,7 @@ thead {
<% status.routing_table_headers.each_with_index do |header,i| %> <% status.routing_table_headers.each_with_index do |header,i| %>
<% if i == 0 %> <% if i == 0 %>
<td class="first"> <td class="first">
<% elsif i == status.routing_table_headers.length-1 %> <% elsif i == status.routing_table_headers.size-1 %>
<td class="last"> <td class="last">
<% else %> <% else %>
<td class="middle"> <td class="middle">

View File

@ -11,25 +11,30 @@ module OpenVPNStatusWeb
text.lines.each do |line| text.lines.each do |line|
parts = line.strip.split(sep) parts = line.strip.split(sep)
status.client_list_headers = ['Common Name', 'Real Address', 'Data Received', 'Data Sent', 'Connected Since'] status.client_list_headers = parts[2..-1] if parts[0] == 'HEADER' && parts[1] == 'CLIENT_LIST'
status.client_list << parse_client(parts[1..5]) if parts[0] == 'CLIENT_LIST' status.client_list << parse_client(parts[1..-1], status.client_list_headers) if parts[0] == 'CLIENT_LIST'
status.routing_table_headers = ['Virtual Address', 'Common Name', 'Real Address', 'Last Ref'] status.routing_table_headers = parts[2..-1] if parts[0] == 'HEADER' && parts[1] == 'ROUTING_TABLE'
status.routing_table << parse_route(parts[1..4]) if parts[0] == 'ROUTING_TABLE' status.routing_table << parse_route(parts[1..-1], status.routing_table_headers) if parts[0] == 'ROUTING_TABLE'
status.global_stats << parse_global(parts[1..2]) if parts[0] == 'GLOBAL_STATS' status.global_stats << parse_global(parts[1..2]) if parts[0] == 'GLOBAL_STATS'
end end
status status
end end
private_class_method def self.parse_client(client) private_class_method def self.parse_client(client, headers)
client[2] = client[2].to_i headers.each_with_index do |header, i|
client[3] = client[3].to_i client[i] = parse_date(client[i]) if header.end_with?('Since')
client[4] = DateTime.strptime(client[4], '%a %b %d %k:%M:%S %Y') client[i] = client[i].to_i if header.start_with?('Bytes')
end
client client
end end
private_class_method def self.parse_route(route) private_class_method def self.parse_route(route, headers)
route[3] = DateTime.strptime(route[3], '%a %b %d %k:%M:%S %Y') headers.each_with_index do |header, i|
route[i] = parse_date(route[i]) if header.end_with?('Last Ref')
end
route route
end end
@ -37,6 +42,10 @@ module OpenVPNStatusWeb
global[1] = global[1].to_i global[1] = global[1].to_i
global global
end end
private_class_method def self.parse_date(date_string)
DateTime.strptime(date_string, '%a %b %d %k:%M:%S %Y')
end
end end
end end
end end

View File

@ -17,24 +17,29 @@ describe OpenVPNStatusWeb::Parser::ModernStateless do
expect(status.client_list.map { |client| client[1] }).to eq(['1.2.3.4:1234', '1.2.3.5:1235']) expect(status.client_list.map { |client| client[1] }).to eq(['1.2.3.4:1234', '1.2.3.5:1235'])
end end
it 'parses virtual addresses' do
expect(status.client_list.map { |client| client[2] }).to eq(['192.168.66.2', '2001:db8:0:0::1000'])
end
it 'parses received bytes' do it 'parses received bytes' do
expect(status.client_list.map { |client| client[2] }).to eq([11_811_160_064, 512]) expect(status.client_list.map { |client| client[3] }).to eq([11_811_160_064, 512])
end end
it 'parses sent bytes' do it 'parses sent bytes' do
expect(status.client_list.map { |client| client[3] }).to eq([4_194_304, 2048]) expect(status.client_list.map { |client| client[4] }).to eq([4_194_304, 2048])
end end
it 'parses connected since date' do it 'parses connected since date' do
expect(status.client_list.map { |client| client[4] }).to eq( expect(status.client_list.map { |client| client[5] }).to eq(
[ [
DateTime.new(2012, 1, 1, 23, 42, 0), DateTime.new(2012, 1, 1, 23, 42, 0) DateTime.new(2012, 1, 1, 23, 42, 0),
DateTime.new(2012, 1, 1, 23, 42, 0)
] ]
) )
end end
it 'has the same number of headers' do it 'has the same number of headers' do
expect(status.client_list[0].length).to eq(status.client_list_headers.length) expect(status.client_list[0].size).to eq(status.client_list_headers.size)
end end
end end
@ -61,7 +66,7 @@ describe OpenVPNStatusWeb::Parser::ModernStateless do
end end
it 'has the same number of headers' do it 'has the same number of headers' do
expect(status.routing_table[0].length).to eq(status.routing_table_headers.length) expect(status.routing_table[0].size).to eq(status.routing_table_headers.size)
end end
end end