-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsixnines.rb
374 lines (344 loc) · 9.29 KB
/
sixnines.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# frozen_string_literal: true
# SPDX-FileCopyrightText: Copyright (c) 2017-2025 Yegor Bugayenko
# SPDX-License-Identifier: MIT
require 'haml'
require 'haml/template/options'
require 'sinatra'
require 'sinatra/cookies'
require 'sass'
require 'futex'
require 'uri'
require 'yaml'
require 'json'
require 'aws-sdk-dynamodb'
require 'stripe'
require 'twitter'
require 'raven'
require 'net/http'
require 'glogin'
require_relative 'version'
require_relative 'objects/helpers'
require_relative 'objects/exec'
require_relative 'objects/base'
require_relative 'objects/dynamo'
require_relative 'objects/endpoint/ep_favicon'
require_relative 'objects/endpoint/ep_data'
if ENV['RACK_ENV'] != 'test'
require 'rack/ssl'
use Rack::SSL
end
configure do
Haml::Options.defaults[:format] = :xhtml
config = if ENV['RACK_ENV'] == 'test'
{
'cookie_secret' => '',
'github' => {
'client_id' => 'test',
'client_secret' => 'test'
},
'twitter' => {
'consumer_key' => 'test',
'consumer_secret' => 'test',
'access_token' => 'test',
'access_token_secret' => 'test'
},
'sentry' => '',
'stripe' => {
'live' => {
'public_key' => 'test'
}
},
'proxies' => [''],
'coupons' => ['test']
}
else
YAML.safe_load(File.open(File.join(Dir.pwd, 'config.yml')))
end
Raven.configure do |c|
c.dsn = config['sentry']
end
set :config, config
set :glogin, GLogin::Auth.new(
config['github']['client_id'],
config['github']['client_secret'],
'https://www.sixnines.io/oauth'
)
set :base, Base.new(Dynamo.new(config).aws)
set :proxies, config['proxies']
set :twitter, (Twitter::REST::Client.new do |c|
c.consumer_key = config['twitter']['consumer_key']
c.consumer_secret = config['twitter']['consumer_secret']
c.access_token = config['twitter']['access_token']
c.access_token_secret = config['twitter']['access_token_secret']
end)
set :pings, TotalPings.new(0)
end
before '/*' do
@locals = {
ver: VERSION,
login_link: settings.glogin.login_uri
}
cookies[:glogin] = params[:cookie] if params[:cookie]
if cookies[:glogin]
begin
@locals[:user] = GLogin::Cookie::Closed.new(
cookies[:glogin],
settings.config['cookie_secret']
).to_user
rescue GLogin::Codec::DecodingError => _e
@locals.delete(:user)
end
end
end
before '/a*' do
redirect to('/') unless @locals[:user]
end
get '/oauth' do
code = params[:code]
return 'The code is missing' if code.nil?
cookies[:glogin] = GLogin::Cookie::Open.new(
settings.glogin.user(code),
settings.config['cookie_secret']
).to_s
redirect to('/')
end
get '/logout' do
cookies.delete(:glogin)
redirect to('/')
end
get '/terms' do
haml :terms, layout: :layout, locals: @locals.merge(
title: 'Terms of use',
description: 'Terms of use'
)
end
get '/' do
haml :index, layout: :layout, locals: @locals.merge(
title: 'SixNines',
description: 'Website Availability Monitor',
query: params[:q],
found: params[:q] ? settings.base.find(params[:q]) : [],
flips: settings.base.flips,
ping_count: settings.pings.count
)
end
get '/rss' do
require 'rss'
content_type 'application/rss+xml'
RSS::Maker.make('atom') do |m|
m.channel.author = 'SixNines.io'
m.channel.updated = Time.now.to_s
m.channel.about = 'https://sixnines.io/rss'
m.channel.title = 'SixNines recent flips'
settings.base.flips.each do |e|
m.items.new_item do |i|
i.link = "https://www.sixnines.io/h/#{e.to_h[:id]}"
i.title = "#{e.to_h[:hostname]} flipped"
i.updated = Time.now.to_s
end
end
end.to_s
end
get '/sitemap.xml' do
require 'xml-sitemap'
content_type 'application/xml'
XmlSitemap::Map.new('sixnines.io') do |m|
settings.base.flips.each do |e|
m.add(
"/h/#{e.to_h[:id]}",
updated: e.to_h[:flipped],
period: :never
)
end
end.render
end
# Badge of the endpoint
get '/b/:id' do
response.headers['Cache-Control'] = 'no-cache, private'
badge = EpBadge.new(settings.base.take(params[:id]))
style = params[:style] == 'flat' ? 'flat' : 'round'
if params[:format] && params[:format] == 'png'
content_type 'image/png'
badge.to_png(style)
else
content_type 'image/svg+xml'
badge.to_svg(style)
end
rescue Base::EndpointNotFound
404
end
# History page of the endpoint
get '/h/:id' do
ep = settings.base.take(params[:id])
haml :history, layout: :layout, locals: @locals.merge(
title: "sn:#{ep.to_h[:hostname]}",
description: "#{ep.to_h[:hostname]}: availability report",
amphtml: "/h-amp/#{params[:id]}",
e: Endpoint::Cached.new(ep)
)
rescue Base::EndpointNotFound
404
end
# History page of the endpoint (AMP)
get '/h-amp/:id' do
ep = settings.base.take(params[:id])
haml :history_amp, layout: :amp, locals: @locals.merge(
title: ep.to_h[:hostname],
description: ep.to_h[:hostname],
canonical: "/h/#{params[:id]}",
e: Endpoint::Cached.new(ep)
)
rescue Base::EndpointNotFound
404
end
# SVG graph of the endpoint
get '/g/:id' do
response.headers['Cache-Control'] = 'no-cache, private'
content_type 'image/svg+xml'
EpGraph.new(Endpoint::Cached.new(settings.base.take(params[:id]))).to_svg
rescue Base::EndpointNotFound
404
end
# Favicon of the endpoint
get '/f/:id' do
response.headers['Cache-Control'] = "max-age=#{5 * 60 * 60}"
content_type 'image/png'
EpFavicon.new(settings.base.take(params[:id])).png
rescue Base::EndpointNotFound
404
end
# Data of the endpoint
get '/d/:id' do
content_type 'application/json'
EpData.new(settings.base.take(params[:id])).to_json
rescue Base::EndpointNotFound
404
end
# Flush the endpoint
get '/flush/:id' do
raise 'You are not allowed to do this' \
if @locals[:user].nil? || @locals[:user][:id] != 'yegor256'
begin
ep = settings.base.take(params[:id])
ep.flush
redirect(to("/h/#{params[:id]}"))
rescue Base::EndpointNotFound
404
end
end
get '/ping' do
content_type 'text/plain'
txt = Futex.new('/tmp/sixnines.lock', timeout: 1).open do
settings.base.ping(settings.pings, settings.proxies) do |up, ep|
next if ENV['RACK_ENV'] == 'test'
href = "https://www.sixnines.io#{EpBadge.new(ep).to_href}"
event = 'is down'
if up
event = 'is up'
if ep.to_h[:flipped]
event = "went back up after \
#{time_ago_in_words(ep.to_h[:flipped])} \
of downtime"
end
end
begin
settings.twitter.update(
"#{ep.to_h[:hostname]} #{event}! \
Availability: #{EpAvailability.new(ep).short} \
(#{EpAvailability.new(ep).full}). #{href}"
)
rescue Twitter::Error::Unauthorized
puts 'Can\'t tweet, account is locked'
rescue Twitter::Error::ServiceUnavailable
puts 'Can\'t tweet, service unavailable'
end
end
end
return 'Nothing to ping' if txt.empty?
Process.detach(
fork do
sleep(10)
Net::HTTP.get_response(URI.parse('https://www.sixnines.io/ping?fork'))
end
)
rescue Futex::CantLock
'Locked, try again a bit later'
end
get '/robots.txt' do
'sitemap: https://www.sixnines.io/sitemap.xml'
end
get '/version' do
VERSION
end
get '/a' do
haml :account, layout: :layout, locals: @locals.merge(
title: "@#{@locals[:user][:id]}",
description: "Account of @#{@locals[:user][:id]}",
endpoints: settings.base.endpoints(@locals[:user][:id]).list,
stripe_key: settings.config['stripe']['live']['public_key']
)
end
post '/a/add' do
if params[:coupon].empty?
Stripe.api_key = settings.config['stripe']['live']['secret_key']
customer = Stripe::Customer.create(
email: params[:stripeEmail],
source: params[:stripeToken]
)
Stripe::Charge.create(
amount: 495,
description: params[:endpoint],
currency: 'usd',
customer: customer.id
)
else
raise "Invalid coupon \"#{params[:coupon]}\"" unless settings.config['coupons'].include?(params[:coupon])
end
settings.base.endpoints(@locals[:user][:id]).add(params[:endpoint])
redirect to('/a')
end
# @todo #93:30min We should check that there were no successful pings before
# changing the endpoint to ensure no one uses this feature to register new
# sites without charge.
post '/a/edit' do
settings.base.endpoints(@locals[:user][:id]).del(params[:old])
settings.base.endpoints(@locals[:user][:id]).add(params[:new])
redirect to('/a')
end
get '/a/del' do
settings.base.endpoints(@locals[:user][:id]).del(params[:endpoint])
redirect to('/a')
end
get '/ping_count' do
content_type 'application/json'
{ ping_count: settings.pings.count }.to_json
end
get '/css/*.css' do
name = params[:splat].first
file = File.join('assets/sass', "#{name}.sass")
error(404, "File not found: #{file}") unless File.exist?(file)
content_type 'text/css', charset: 'utf-8'
Sass::Engine.new(File.read(file)).render
end
not_found do
status 404
haml :not_found, layout: :layout, locals: @locals.merge(
title: 'Page not found',
description: 'Page not found'
)
end
error do
status 503
content_type 'text/html'
e = env['sinatra.error']
Raven.capture_exception(e)
haml(
:error,
layout: :layout,
locals: @locals.merge(
title: 'Error',
description: 'Internal server error',
error: "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
)
)
end