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

128 lines
3.3 KiB
Ruby
Raw Permalink Normal View History

2013-05-03 15:32:08 +02:00
#!/usr/bin/env ruby
2013-05-03 22:23:45 +02:00
require 'date'
2013-05-03 15:32:08 +02:00
require 'etc'
require 'logger'
require 'ipaddr'
require 'yaml'
require 'rack'
require 'erb'
2013-05-03 15:32:08 +02:00
require 'metriks'
2013-05-03 19:49:09 +02:00
require 'better_errors'
2013-05-03 15:32:08 +02:00
2013-05-03 18:30:34 +02:00
require 'openvpn-status-web/status'
2013-05-03 18:20:07 +02:00
require 'openvpn-status-web/parser/v1'
require 'openvpn-status-web/parser/v2'
2013-05-03 20:16:17 +02:00
require 'openvpn-status-web/parser/v3'
2013-05-03 15:32:08 +02:00
require 'openvpn-status-web/int_patch'
require 'openvpn-status-web/version'
module OpenVPNStatusWeb
def self.logger
@logger
end
def self.logger=(logger)
@logger = logger
end
class LogFormatter
def call(lvl, time, progname, msg)
"[%s] %-5s %s\n" % [Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s]
end
end
class Daemon
def initialize(vpns)
@vpns = vpns
@main_tmpl = read_template(File.join(File.dirname(__FILE__), 'openvpn-status-web/main.html.erb'))
2013-05-03 15:32:08 +02:00
end
def call(env)
return [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]] if env["REQUEST_METHOD"] != "GET"
return [404, {"Content-Type" => "text/plain"}, ["Not Found"]] if env["PATH_INFO"] != "/"
2013-05-03 15:32:08 +02:00
# variables for template
2013-05-03 21:12:06 +02:00
vpns = @vpns
stati = {}
@vpns.each do |name,config|
stati[name] = parse_status_log(config)
end
# eval
2013-05-03 19:49:09 +02:00
html = @main_tmpl.result(binding)
2013-05-03 15:32:08 +02:00
[200, {"Content-Type" => "text/html"}, [html]]
end
def read_template(file)
text = File.open(file, 'rb') do |f| f.read end
ERB.new(text)
end
def parse_status_log(vpn)
text = File.open(vpn['status_file'], 'rb') do |f| f.read end
2013-05-03 15:32:08 +02:00
case vpn['version']
when 1
OpenVPNStatusWeb::Parser::V1.new.parse_status_log(text)
when 2
OpenVPNStatusWeb::Parser::V2.new.parse_status_log(text)
2013-05-03 20:16:17 +02:00
when 3
OpenVPNStatusWeb::Parser::V3.new.parse_status_log(text)
else
raise "No suitable parser for status-version #{vpn['version']}"
end
2013-05-03 15:32:08 +02:00
end
def self.run!
2013-05-03 16:06:30 +02:00
if ARGV.length != 1
puts "Usage: openvpn-status-web config_file"
2013-05-03 15:32:08 +02:00
exit 1
end
2013-05-03 16:06:30 +02:00
config_file = ARGV[0]
if not File.file?(config_file)
puts "Config file not found!"
exit 1
end
puts "openvpn-status-web version #{OpenVPNStatusWeb::VERSION}"
puts "Using config file #{config_file}"
config = YAML::load(File.open(config_file, 'r') { |f| f.read })
2013-05-03 16:06:30 +02:00
if config['logfile']
OpenVPNStatusWeb.logger = Logger.new(config['logfile'])
else
OpenVPNStatusWeb.logger = Logger.new(STDOUT)
end
2013-05-03 15:32:08 +02:00
OpenVPNStatusWeb.logger.progname = "openvpn-status-web"
OpenVPNStatusWeb.logger.formatter = LogFormatter.new
OpenVPNStatusWeb.logger.info "Starting..."
2013-05-03 22:26:07 +02:00
# drop privs (first change group than user)
Process::Sys.setgid(Etc.getgrnam(config['group']).gid) if config['group']
Process::Sys.setuid(Etc.getpwnam(config['user']).uid) if config['user']
2013-05-03 19:49:09 +02:00
# configure rack
app = Daemon.new(config['vpns'])
2013-05-03 19:49:09 +02:00
if ENV['RACK_ENV'] == "development"
app = BetterErrors::Middleware.new(app)
BetterErrors.application_root = File.expand_path("..", __FILE__)
end
2013-05-03 16:06:30 +02:00
2013-05-03 15:32:08 +02:00
Signal.trap('INT') do
OpenVPNStatusWeb.logger.info "Quitting..."
Rack::Handler::WEBrick.shutdown
end
2013-05-03 16:06:30 +02:00
Rack::Handler::WEBrick.run app, :Host => config['host'], :Port => config['port']
2013-05-03 15:32:08 +02:00
end
end
end