-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.rb
executable file
·196 lines (159 loc) · 4.48 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env ruby
# encoding: utf-8
$:.unshift File.expand_path('lib')
$:.unshift File.expand_path('lib/collector')
require 'sinatra'
require 'digest/sha1'
require 'redis'
require 'yaml'
require 'json'
require 'net/https'
require 'uri'
require 'redis_worker'
require 'collector/hosts'
include RedisWorker
include Collector::Hosts
configure do
set :config_options, YAML.load_file('config/mtarep.yml')
set :this_version, IO.read('VERSION').chomp
set :public_folder, Proc.new { File.join(root, 'vendor') }
set :environment, :production
set :show_exceptions, true
enable :sessions, :logging
authfile = settings.config_options['http_auth_file']
authlist = IO.read(authfile).split("\n")
use Rack::Auth::Basic, 'Protected Area' do |user, pass|
set :username, user
pass = Digest::SHA1.base64digest(pass)
match = authlist.grep(/#{user}:/)
match = match[0].split(':{SHA}') unless match.empty?
user == match[0] && pass == match[1]
end
end
before do
@mta_keys = []
@redis = redis_connection(settings.config_options['redis_server'])
@graph_domains = settings.config_options['graph_domains']
@mta_map_config = mta_map(settings.config_options['mta_map'])
@mta_map_config.each {|hash| @mta_keys << hash[:ip]}
end
get '/' do
count = 0
begin
@assistance_links = settings.config_options['assistance_links']
@mta_redis_data_hash = {}
@mta_keys.each do |ip|
@mta_redis_data_hash[ip] = @redis.lrange(ip, 0, -1)
new_hash = @mta_redis_data_hash.clone
@mta_redis_data_hash.each_pair {|ip ,data| new_hash[ip] = Hash[*data.flatten]}
@mta_redis_data_hash = new_hash
end
rescue
count += 1
sleep 1
retry unless count > 5
end
erb :main
end
get '/help' do
erb :help
end
get '/graphs' do
fbl = []
sent = []
bounced = []
expired = []
date = Time.now.to_s.split[0].gsub('-', '')
def graph_counts(type, graph_domains)
results = type.inject {|x, y| x.merge(y) {|k, ov, nv| ov + nv}}
if results.nil?
results = {}
graph_domains.each{|domain| results[domain] = 0}
end
counts = []
graph_domains.each {|domain| counts << results[domain]}
return counts
end
count = 0
begin
graph_keys = @redis.keys("*:#{date}:*")
rescue
count += 1
retry unless count > 5
end
graph_keys.map! {|x| x.encode('UTF-16BE', :invalid=>:replace, :replace=>'?').encode('UTF-8')}
graph_keys_new = graph_keys.clone
graph_keys.each do |key|
if key =~ /:bounced/
graph_keys_new.delete(key) unless graph_keys.include?(key.gsub('bounced', 'sent'))
end
end
graph_keys_new.each do |graph_key|
@graph_domains.each do |domain|
count = @redis.hget(graph_key, domain)
count = 0 if count.nil?
count = count.to_i
if graph_key.split(':')[2] == 'sent'
sent << {domain => count}
elsif graph_key.split(':')[2] == 'expired'
expired << {domain => count}
elsif graph_key.split(':')[2] == 'fbl'
fbl << {domain => count}
else
bounced << {domain => count}
end
end
end
@fbl_data = graph_counts(fbl, @graph_domains)
@sent_data = graph_counts(sent, @graph_domains)
@bounced_data = graph_counts(bounced, @graph_domains)
@expired_data = graph_counts(expired, @graph_domains)
erb :graphs
end
get '/version' do
def latest_version
begin
uri = URI.parse('https://raw.githubusercontent.com/phantasm66/mtarep/master/VERSION')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
if response.code.to_s == '200'
response.body.to_s
else
response.code.to_s
end
rescue SocketError => error
return error
end
end
@this_version = settings.this_version
@latest_version = latest_version
erb :version
end
post '/ack' do
timestamp = Time.now.to_s.split[0..1].join(' ')
key = params[:ack]
count = 0
begin
@redis.set(key, "#{settings.username} #{timestamp}")
rescue
count += 1
retry unless count > 5
end
redirect to('/')
end
get "/api/:fqdn" do
found = []
@mta_map_config.each {|x| found << x[:ip] if x[:fqdn] == params[:fqdn]}
begin
raise if found.empty?
response_data = @redis.lrange(found[0], 0, -1)
content_type :json
JSON.generate(Hash[*response_data])
rescue
content_type :json
JSON.generate({:message => "No mtarep data found for #{params[:fqdn]}"})
end
end