1
0
mirror of https://github.com/cmur2/openvpn-status-web.git synced 2024-11-10 14:56:15 +01:00
openvpn-status-web/lib/openvpn-status-web.rb

149 lines
3.8 KiB
Ruby
Raw Normal View History

2013-05-03 15:32:08 +02:00
#!/usr/bin/env ruby
2020-03-07 01:30:57 +01:00
# frozen_string_literal: true
2013-05-03 15:32:08 +02:00
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'
2020-03-02 01:57:58 +01:00
require 'better_errors' if ENV['RACK_ENV'] == 'development'
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
# @return [Logger]
2013-05-03 15:32:08 +02:00
def self.logger
@logger
end
# @param logger [Logger]
# @return [Logger]
2013-05-03 15:32:08 +02:00
def self.logger=(logger)
@logger = logger
end
class LogFormatter
# @param lvl [Object]
# @param _time [DateTime]
# @param _progname [String]
# @param msg [Object]
# @return [String]
2020-03-02 01:57:58 +01:00
def call(lvl, _time, _progname, msg)
format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
2013-05-03 15:32:08 +02:00
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)
2020-03-02 01:57:58 +01:00
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 = {}
2020-03-02 01:57:58 +01:00
@vpns.each do |name, config|
2013-05-03 21:12:06 +02:00
stati[name] = parse_status_log(config)
end
# eval
2013-05-03 19:49:09 +02:00
html = @main_tmpl.result(binding)
2020-03-02 01:57:58 +01:00
[200, {'Content-Type' => 'text/html'}, [html]]
2013-05-03 15:32:08 +02:00
end
def read_template(file)
text = File.read(file, mode: 'rb')
2020-03-02 01:57:58 +01:00
2013-05-03 15:32:08 +02:00
ERB.new(text)
end
2020-03-02 01:57:58 +01:00
def parse_status_log(vpn)
text = File.read(vpn['status_file'], mode: 'rb')
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
# @return [void]
2013-05-03 15:32:08 +02:00
def self.run!
2013-05-03 16:06:30 +02:00
if ARGV.length != 1
2020-03-02 01:57:58 +01:00
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]
2020-03-02 01:57:58 +01:00
if !File.file?(config_file)
puts 'Config file not found!'
2013-05-03 16:06:30 +02:00
exit 1
end
2020-03-02 01:57:58 +01:00
2013-05-03 16:06:30 +02:00
puts "openvpn-status-web version #{OpenVPNStatusWeb::VERSION}"
puts "Using config file #{config_file}"
config = YAML.safe_load(File.read(config_file, mode: 'r'))
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
2020-03-02 01:57:58 +01:00
OpenVPNStatusWeb.logger.progname = 'openvpn-status-web'
2013-05-03 15:32:08 +02:00
OpenVPNStatusWeb.logger.formatter = LogFormatter.new
2020-03-02 01:57:58 +01:00
OpenVPNStatusWeb.logger.info 'Starting...'
2013-05-03 15:32:08 +02:00
# drop priviliges as soon as possible
# NOTE: first change group than user
if config['group']
group = Etc.getgrnam(config['group'])
Process::Sys.setgid(group.gid) if group
end
if config['user']
user = Etc.getpwnam(config['user'])
Process::Sys.setuid(user.uid) if user
end
2013-05-03 22:26:07 +02:00
2013-05-03 19:49:09 +02:00
# configure rack
app = Daemon.new(config['vpns'])
2020-03-02 01:57:58 +01:00
if ENV['RACK_ENV'] == 'development'
2013-05-03 19:49:09 +02:00
app = BetterErrors::Middleware.new(app)
2020-03-02 01:57:58 +01:00
BetterErrors.application_root = File.expand_path(__dir__)
2013-05-03 19:49:09 +02:00
end
2013-05-03 16:06:30 +02:00
2013-05-03 15:32:08 +02:00
Signal.trap('INT') do
2020-03-02 01:57:58 +01:00
OpenVPNStatusWeb.logger.info 'Quitting...'
2013-05-03 15:32:08 +02:00
Rack::Handler::WEBrick.shutdown
end
2013-10-08 13:23:02 +02:00
Signal.trap('TERM') do
2020-03-02 01:57:58 +01:00
OpenVPNStatusWeb.logger.info 'Quitting...'
2013-10-08 13:23:02 +02:00
Rack::Handler::WEBrick.shutdown
end
2020-03-02 01:57:58 +01:00
Rack::Handler::WEBrick.run app, Host: config['host'], Port: config['port']
2013-05-03 15:32:08 +02:00
end
end
end