forked from fdocr/udl-server
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.rb
83 lines (74 loc) · 1.93 KB
/
server.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
require 'sinatra'
if !production?
require 'sinatra/reloader'
require 'dotenv/load'
require 'byebug'
end
require 'uri'
require 'json'
require 'redis-activesupport'
require 'rack/attack'
Rack::Attack.cache.store = ActiveSupport::Cache.lookup_store :redis_store
if ENV['UDL_THROTTLE_LIMIT'].present? && ENV['UDL_THROTTLE_PERIOD'].present?
limit = ENV['UDL_THROTTLE_LIMIT'].to_i
period = ENV['UDL_THROTTLE_PERIOD'].to_i
Rack::Attack.throttle('requests/ip', limit: limit, period: period) do |request|
request.ip
end
end
if ENV['UDL_SAFELIST_REGEXP'].present?
safelist_regexp = Regexp.new(ENV['UDL_SAFELIST_REGEXP'])
Rack::Attack.safelist("allow safelist") do |request|
# Requests will be safelisted if the 'r' param matches the regexp
request.params["r"] =~ safelist_regexp
end
end
if ENV['UDL_BLOCKLIST_REGEXP'].present?
blocklist_regexp = Regexp.new(ENV['UDL_BLOCKLIST_REGEXP'])
Rack::Attack.blocklist("deny blocklist") do |request|
# Requests will be blocklisted if the 'r' param matches the regexp
request.params["r"] =~ blocklist_regexp
end
end
get '/' do
begin
redirect URI(params[:r])
rescue => error
@error = error
logger.info @error.inspect
erb :fallback
end
end
get '/.well-known/apple-app-site-association' do
content_type :json
aasa_app_id = ENV['AASA_APP_ID'].to_s
if aasa_app_id.present?
{
"applinks": {
"apps": [],
"details":[
{
"appID": aasa_app_id,
"paths": ["/*"]
}
]
},
"activitycontinuation": {
"apps": [aasa_app_id]
}
}.to_json
else
{ error: 'AASA_APP_ID not configured' }.to_json
end
end
get '/*' do
begin
target_url = URI(params['splat'].first.gsub('https:/', 'https://'))
raise 'Invalid redirect URL' unless target_url.host.present?
redirect target_url
rescue => error
@error = error
logger.info @error.inspect
erb :fallback
end
end