diff --git a/examples/status.v2 b/examples/status.v2 index 248dce8..73a30a8 100644 --- a/examples/status.v2 +++ b/examples/status.v2 @@ -1,8 +1,8 @@ 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 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,bar,1.2.3.5:1235,512,2048,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,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) 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 diff --git a/examples/status.v3 b/examples/status.v3 index dcbc2ba..7a86fab 100644 --- a/examples/status.v3 +++ b/examples/status.v3 @@ -1,8 +1,8 @@ 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 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 bar 1.2.3.5:1235 512 2048 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 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) 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 diff --git a/lib/openvpn-status-web/main.html.erb b/lib/openvpn-status-web/main.html.erb index 1d6b198..95e6913 100644 --- a/lib/openvpn-status-web/main.html.erb +++ b/lib/openvpn-status-web/main.html.erb @@ -53,7 +53,7 @@ thead { <% status.client_list_headers.each_with_index do |header,i| %> <% if i == 0 %> -<% elsif i == status.client_list_headers.length-1 %> +<% elsif i == status.client_list_headers.size-1 %> <% else %> @@ -67,13 +67,13 @@ thead { <% status.client_list_headers.each_with_index do |header,i| %> <% if i == 0 %> -<% elsif i == status.client_list_headers.length-1 %> +<% elsif i == status.client_list_headers.size-1 %> <% else %> <% end %> <% if header =~ /(Received|Sent)/ %> - <%= client[i].to_i.as_bytes %> + <%= client[i].as_bytes %> <% elsif client[i].is_a? DateTime %> <%= client[i].strftime('%-d.%-m.%Y %H:%M:%S') %> <% else %> @@ -93,7 +93,7 @@ thead { <% status.routing_table_headers.each_with_index do |header,i| %> <% if i == 0 %> -<% elsif i == status.routing_table_headers.length-1 %> +<% elsif i == status.routing_table_headers.size-1 %> <% else %> @@ -107,7 +107,7 @@ thead { <% status.routing_table_headers.each_with_index do |header,i| %> <% if i == 0 %> -<% elsif i == status.routing_table_headers.length-1 %> +<% elsif i == status.routing_table_headers.size-1 %> <% else %> diff --git a/lib/openvpn-status-web/parser/modern_stateless.rb b/lib/openvpn-status-web/parser/modern_stateless.rb index f27beba..78648b5 100644 --- a/lib/openvpn-status-web/parser/modern_stateless.rb +++ b/lib/openvpn-status-web/parser/modern_stateless.rb @@ -11,25 +11,30 @@ module OpenVPNStatusWeb text.lines.each do |line| parts = line.strip.split(sep) - status.client_list_headers = ['Common Name', 'Real Address', 'Data Received', 'Data Sent', 'Connected Since'] - status.client_list << parse_client(parts[1..5]) if parts[0] == 'CLIENT_LIST' - status.routing_table_headers = ['Virtual Address', 'Common Name', 'Real Address', 'Last Ref'] - status.routing_table << parse_route(parts[1..4]) if parts[0] == 'ROUTING_TABLE' + status.client_list_headers = parts[2..-1] if parts[0] == 'HEADER' && parts[1] == 'CLIENT_LIST' + status.client_list << parse_client(parts[1..-1], status.client_list_headers) if parts[0] == 'CLIENT_LIST' + status.routing_table_headers = parts[2..-1] if parts[0] == 'HEADER' && parts[1] == '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' end status end - private_class_method def self.parse_client(client) - client[2] = client[2].to_i - client[3] = client[3].to_i - client[4] = DateTime.strptime(client[4], '%a %b %d %k:%M:%S %Y') + private_class_method def self.parse_client(client, headers) + headers.each_with_index do |header, i| + client[i] = parse_date(client[i]) if header.end_with?('Since') + client[i] = client[i].to_i if header.start_with?('Bytes') + end + client end - private_class_method def self.parse_route(route) - route[3] = DateTime.strptime(route[3], '%a %b %d %k:%M:%S %Y') + private_class_method def self.parse_route(route, headers) + headers.each_with_index do |header, i| + route[i] = parse_date(route[i]) if header.end_with?('Last Ref') + end + route end @@ -37,6 +42,10 @@ module OpenVPNStatusWeb global[1] = global[1].to_i global 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 diff --git a/spec/openvpn-status-web/parser/modern_stateless_spec.rb b/spec/openvpn-status-web/parser/modern_stateless_spec.rb index d9e7aab..972dd6a 100644 --- a/spec/openvpn-status-web/parser/modern_stateless_spec.rb +++ b/spec/openvpn-status-web/parser/modern_stateless_spec.rb @@ -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']) 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 - 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 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 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 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 @@ -61,7 +66,7 @@ describe OpenVPNStatusWeb::Parser::ModernStateless do end 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