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

gem: port fixes from Sorbet from dyndnsd

This commit is contained in:
cn 2020-03-07 01:29:12 +01:00
parent bf3ba8f7cd
commit 92ee01e5de
5 changed files with 28 additions and 10 deletions

View File

@ -18,15 +18,23 @@ require 'openvpn-status-web/int_patch'
require 'openvpn-status-web/version' require 'openvpn-status-web/version'
module OpenVPNStatusWeb module OpenVPNStatusWeb
# @return [Logger]
def self.logger def self.logger
@logger @logger
end end
# @param logger [Logger]
# @return [Logger]
def self.logger=(logger) def self.logger=(logger)
@logger = logger @logger = logger
end end
class LogFormatter class LogFormatter
# @param lvl [Object]
# @param _time [DateTime]
# @param _progname [String]
# @param msg [Object]
# @return [String]
def call(lvl, _time, _progname, msg) def call(lvl, _time, _progname, msg)
format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s) format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
end end
@ -56,13 +64,13 @@ module OpenVPNStatusWeb
end end
def read_template(file) def read_template(file)
text = File.open(file, 'rb', &:read) text = File.read(file, mode: 'rb')
ERB.new(text) ERB.new(text)
end end
def parse_status_log(vpn) def parse_status_log(vpn)
text = File.open(vpn['status_file'], 'rb', &:read) text = File.read(vpn['status_file'], mode: 'rb')
case vpn['version'] case vpn['version']
when 1 when 1
@ -76,6 +84,7 @@ module OpenVPNStatusWeb
end end
end end
# @return [void]
def self.run! def self.run!
if ARGV.length != 1 if ARGV.length != 1
puts 'Usage: openvpn-status-web config_file' puts 'Usage: openvpn-status-web config_file'
@ -92,7 +101,7 @@ module OpenVPNStatusWeb
puts "openvpn-status-web version #{OpenVPNStatusWeb::VERSION}" puts "openvpn-status-web version #{OpenVPNStatusWeb::VERSION}"
puts "Using config file #{config_file}" puts "Using config file #{config_file}"
config = YAML.safe_load(File.open(config_file, 'r', &:read)) config = YAML.safe_load(File.read(config_file, mode: 'r'))
if config['logfile'] if config['logfile']
OpenVPNStatusWeb.logger = Logger.new(config['logfile']) OpenVPNStatusWeb.logger = Logger.new(config['logfile'])
@ -105,9 +114,16 @@ module OpenVPNStatusWeb
OpenVPNStatusWeb.logger.info 'Starting...' OpenVPNStatusWeb.logger.info 'Starting...'
# drop privs (first change group than user) # drop priviliges as soon as possible
Process::Sys.setgid(Etc.getgrnam(config['group']).gid) if config['group'] # NOTE: first change group than user
Process::Sys.setuid(Etc.getpwnam(config['user']).uid) if config['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
# configure rack # configure rack
app = Daemon.new(config['vpns']) app = Daemon.new(config['vpns'])

View File

@ -1,5 +1,5 @@
require 'openvpn-status-web/parser/modern_stateless' require_relative 'modern_stateless'
module OpenVPNStatusWeb module OpenVPNStatusWeb
module Parser module Parser

View File

@ -1,5 +1,5 @@
require 'openvpn-status-web/parser/modern_stateless' require_relative 'modern_stateless'
module OpenVPNStatusWeb module OpenVPNStatusWeb
module Parser module Parser

View File

@ -1,4 +1,5 @@
require 'spec_helper'
require_relative '../spec_helper'
describe OpenVPNStatusWeb::Parser::ModernStateless do describe OpenVPNStatusWeb::Parser::ModernStateless do
{ {

View File

@ -1,4 +1,5 @@
require 'spec_helper'
require_relative '../spec_helper'
describe OpenVPNStatusWeb::Parser::V1 do describe OpenVPNStatusWeb::Parser::V1 do
def status def status