Class: OpenVPNStatusWeb::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/openvpn-status-web.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vpns) ⇒ Daemon

Returns a new instance of Daemon.



46
47
48
49
50
# File 'lib/openvpn-status-web.rb', line 46

def initialize(vpns)
  @vpns = vpns

  @main_tmpl = read_template(File.join(File.dirname(__FILE__), 'openvpn-status-web/main.html.erb'))
end

Class Method Details

.run!void

This method returns an undefined value.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/openvpn-status-web.rb', line 90

def self.run!
  if ARGV.length != 1
    puts 'Usage: openvpn-status-web config_file'
    exit 1
  end

  config_file = ARGV[0]

  if !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.safe_load(File.read(config_file, mode: 'r'))

  if config['logfile']
    OpenVPNStatusWeb.logger = Logger.new(config['logfile'])
  else
    OpenVPNStatusWeb.logger = Logger.new($stdout)
  end

  OpenVPNStatusWeb.logger.progname = 'openvpn-status-web'
  OpenVPNStatusWeb.logger.formatter = LogFormatter.new

  OpenVPNStatusWeb.logger.info 'Starting...'

  # 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

  # configure rack
  app = Daemon.new(config['vpns'])
  if ENV.fetch('RACK_ENV', nil) == 'development'
    app = BetterErrors::Middleware.new(app)
    BetterErrors.application_root = File.expand_path(__dir__)
  end

  Signal.trap('INT') do
    OpenVPNStatusWeb.logger.info 'Quitting...'
    Rackup::Handler::WEBrick.shutdown
  end
  Signal.trap('TERM') do
    OpenVPNStatusWeb.logger.info 'Quitting...'
    Rackup::Handler::WEBrick.shutdown
  end

  Rackup::Handler::WEBrick.run app, Host: config['host'], Port: config['port']
end

Instance Method Details

#call(env) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/openvpn-status-web.rb', line 52

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'] != '/'

  # variables for template
  vpns = @vpns
  stati = {}
  @vpns.each do |name, config|
    stati[name] = parse_status_log(config)
  end
  # eval
  html = @main_tmpl.result(binding)

  [200, {'Content-Type' => 'text/html'}, [html]]
end

#parse_status_log(vpn) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/openvpn-status-web.rb', line 74

def parse_status_log(vpn)
  text = File.read(vpn['status_file'], mode: 'rb')

  case vpn['version']
  when 1
    OpenVPNStatusWeb::Parser::V1.new.parse_status_log(text)
  when 2
    OpenVPNStatusWeb::Parser::V2.new.parse_status_log(text)
  when 3
    OpenVPNStatusWeb::Parser::V3.new.parse_status_log(text)
  else
    raise "No suitable parser for status-version #{vpn['version']}"
  end
end

#read_template(file) ⇒ Object



68
69
70
71
72
# File 'lib/openvpn-status-web.rb', line 68

def read_template(file)
  text = File.read(file, mode: 'rb')

  ERB.new(text)
end