-
Notifications
You must be signed in to change notification settings - Fork 85
/
main.rb
149 lines (131 loc) · 3.83 KB
/
main.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
require './require.rb'
set :root, File.dirname(__FILE__)
set :public_folder, File.join(settings.root, 'src', 'public')
set :views, File.join(settings.root, 'src', 'views')
set :template, File.join(settings.root, 'src', 'templates')
Config.setup do |config|
config.use_env = true
config.env_prefix = 'SETTINGS'
config.env_separator = '_'
config.env_converter = :downcase
config.env_parse_values = true
end
register Config
logging = Settings.verboseLogging
if logging == "true"
puts "Verbose logging is enabled".green
logging = true
else
puts "Verbose logging is disabled".red
logging = false
end
set :logging, logging
set :show_exceptions, logging
set :components, File.join(settings.root, 'src', 'views', 'components')
cookie_options = {
:key => 'UserAllowed',
:path => '/',
:expire_after => 86400, # In seconds (1 day)
:secret => SecureRandom.alphanumeric(2048),
:secure => true,
:httponly => true
}
#Validate the YML file
validateYML()
#Validate the ENV variables
validateEnv()
#Setup DB when Private is true
if Settings.private == "true" && Settings.multiuser == "true"
dbSetup()
end
#Encrypted cookies
use Rack::Session::EncryptedCookie, cookie_options
#csrf
use Rack::Csrf, :raise => true
#Compression
use Rack::Deflater
#Auth
use Auth
before do
if request.path_info == '/auth'
return
#any route on the main domain
elsif Settings.private == "false"
if request.url.include? ENV['DOMAIN'] || Settings.mainURL
return
else
auth()
end
else
auth()
end
end
# UV "middleware"
set :uvPath, File.join(settings.root, 'node_modules', '@titaniumnetwork-dev', 'ultraviolet', 'dist')
uvPath()
set :epoxyPath, File.join(settings.root, 'node_modules', '@mercuryworkshop', 'epoxy-transport', 'dist')
epoxyPath()
set :libcurlPath, File.join(settings.root, 'node_modules', '@mercuryworkshop', 'libcurl-transport', 'dist')
libcurlPath()
set :baremuxPath, File.join(settings.root, 'node_modules', '@mercuryworkshop', 'bare-mux', 'dist')
baremuxPath()
set :rammerheadPath, File.join(settings.root, 'node_modules', '@rubynetwork', 'rammerhead-browser', 'dist')
rammerheadPath()
set :baremodulePath, File.join(settings.root, 'node_modules', '@mercuryworkshop', 'bare-as-module3', 'dist')
baremodulePath()
mime_type :wasm, 'application/wasm'
$latestRelease = getLatestRelease()
# Other routes
get '/health/?' do
return { status: "ok" }.to_json
end
get '/:unlock?' do
erb :index, :layout => :"layouts/index"
end
get '/version/?' do
return { version: $latestRelease }.to_json
end
get '/search/:q?' do
content_type :json
query = params[:q]
resp = HTTParty.get("https://search.brave.com/api/suggest?q=#{query}&format=json")
return resp.body
end
#Auth to login to the site
post '/auth' do
if Settings.corlink.enabled == "true" && Settings.private == "false"
password = params[:password]
username = params[:username]
req = HTTParty.post("#{Settings.corlink.url}", headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{Settings.corlink.apiKey}", 'Key' => "#{password}" })
if req.code == 200
if username == "ruby"
session[:auth] = true
session[:uid] = SecureRandom.alphanumeric(2048)
redirect '/'
else
redirect '/'
end
else
redirect '/'
end
else
if Settings.private == "false" || Settings.multiuser == "false"
if params[:password] == Settings.password && params[:username].downcase == Settings.username.downcase
session[:auth] = true
session[:uid] = SecureRandom.alphanumeric(2048)
redirect '/'
else
redirect '/'
end
else
loggedIn = login(params[:username], params[:password])
if loggedIn == true
session[:auth] = true
session[:uid] = SecureRandom.alphanumeric(2048)
redirect '/'
else
redirect '/'
end
end
end
end