lib/zold/node/front.rb in zold-0.14.28 vs lib/zold/node/front.rb in zold-0.14.29
- old
+ new
@@ -43,25 +43,27 @@
# License:: MIT
module Zold
# Web front
class Front < Sinatra::Base
configure do
+ Thread.current.name = 'sinatra'
set :bind, '0.0.0.0'
set :suppress_messages, true
set :start, Time.now
set :lock, false
set :show_exceptions, false
set :server, 'webrick'
+ set :log, nil? # to be injected at node.rb
+ set :trace, nil? # to be injected at node.rb
set :halt, '' # to be injected at node.rb
set :dump_errors, false # to be injected at node.rb
set :version, VERSION # to be injected at node.rb
set :protocol, PROTOCOL # to be injected at node.rb
set :ignore_score_weakness, false # to be injected at node.rb
set :reboot, false # to be injected at node.rb
set :home, nil? # to be injected at node.rb
set :logging, true # to be injected at node.rb
- set :log, nil? # to be injected at node.rb
set :address, nil? # to be injected at node.rb
set :farm, nil? # to be injected at node.rb
set :metronome, nil? # to be injected at node.rb
set :entrance, nil? # to be injected at node.rb
set :network, nil? # to be injected at node.rb
@@ -93,19 +95,18 @@
settings.log.debug("#{request.url}: we are in standalone mode, won't update remotes")
end
s = Score.parse_text(header)
error(400, 'The score is invalid') unless s.valid?
error(400, 'The score is weak') if s.strength < Score::STRENGTH && !settings.ignore_score_weakness
- if s.value > 3
- require_relative '../commands/remote'
- cmd = Remote.new(remotes: settings.remotes, log: settings.log)
- cmd.run(['remote', 'add', s.host, s.port.to_s, '--force', "--network=#{settings.network}"])
- cmd.run(%w[remote trim])
- cmd.run(%w[remote select])
- else
- settings.log.debug("#{request.url}: the score is too weak: #{s}")
+ if settings.address == "#{s.host}:#{s.port}" && !settings.ignore_score_weakness
+ error(400, 'Self-requests are prohibited')
end
+ require_relative '../commands/remote'
+ cmd = Remote.new(remotes: settings.remotes, log: settings.log)
+ cmd.run(['remote', 'add', s.host, s.port.to_s, "--network=#{settings.network}"])
+ cmd.run(%w[remote trim])
+ cmd.run(%w[remote select])
end
end
# @todo #357:30min Test that the headers are being set correctly.
# Currently there are no tests at all that would verify the headers.
@@ -136,10 +137,15 @@
get '/score' do
content_type 'text/plain'
score.to_s
end
+ get '/trace' do
+ content_type 'text/plain'
+ settings.trace.to_s
+ end
+
get '/favicon.ico' do
if score.value >= 16
redirect 'https://www.zold.io/images/logo-green.png'
elsif score.value >= 4
redirect 'https://www.zold.io/images/logo-orange.png'
@@ -173,122 +179,147 @@
)
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'application/json'
- {
- version: settings.version,
- alias: settings.node_alias,
- protocol: settings.protocol,
- id: wallet.id.to_s,
- score: score.to_h,
- wallets: settings.wallets.all.count,
- mtime: wallet.mtime.utc.iso8601,
- size: File.size(wallet.path),
- digest: wallet.digest,
- balance: wallet.balance.to_i,
- body: AtomicFile.new(wallet.path).read
- }.to_json
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'application/json'
+ {
+ version: settings.version,
+ alias: settings.node_alias,
+ protocol: settings.protocol,
+ id: wallet.id.to_s,
+ score: score.to_h,
+ wallets: settings.wallets.all.count,
+ mtime: wallet.mtime.utc.iso8601,
+ size: File.size(wallet.path),
+ digest: wallet.digest,
+ copies: Copies.new(File.join(settings.copies, id)).all.count,
+ balance: wallet.balance.to_i,
+ body: AtomicFile.new(wallet.path).read
+ }.to_json
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16}).json} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'application/json'
- {
- version: settings.version,
- alias: settings.node_alias,
- protocol: settings.protocol,
- id: wallet.id.to_s,
- score: score.to_h,
- wallets: settings.wallets.all.count,
- key: wallet.key.to_pub,
- mtime: wallet.mtime.utc.iso8601,
- digest: wallet.digest,
- balance: wallet.balance.to_i,
- txns: wallet.txns.count
- }.to_json
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'application/json'
+ {
+ version: settings.version,
+ alias: settings.node_alias,
+ protocol: settings.protocol,
+ id: wallet.id.to_s,
+ score: score.to_h,
+ wallets: settings.wallets.all.count,
+ key: wallet.key.to_pub,
+ mtime: wallet.mtime.utc.iso8601,
+ digest: wallet.digest,
+ balance: wallet.balance.to_i,
+ txns: wallet.txns.count
+ }.to_json
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/balance} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- wallet.balance.to_i.to_s
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ wallet.balance.to_i.to_s
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/key} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- wallet.key.to_pub
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ wallet.key.to_pub
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/mtime} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- wallet.mtime.utc.iso8601.to_s
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ wallet.mtime.utc.iso8601.to_s
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/digest} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- wallet.digest
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ wallet.digest
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.txt} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- [
- wallet.network,
- wallet.protocol,
- wallet.id.to_s,
- wallet.key.to_pub,
- '',
- wallet.txns.map(&:to_text).join("\n"),
- '',
- '--',
- "Balance: #{wallet.balance.to_zld}",
- "Transactions: #{wallet.txns.count}",
- "Wallet size: #{File.size(wallet.path)} bytes",
- "Modified: #{wallet.mtime.utc.iso8601}",
- "Digest: #{wallet.digest}"
- ].join("\n")
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ [
+ wallet.network,
+ wallet.protocol,
+ wallet.id.to_s,
+ wallet.key.to_pub,
+ '',
+ wallet.txns.map(&:to_text).join("\n"),
+ '',
+ '--',
+ "Balance: #{wallet.balance.to_zld} ZLD (#{wallet.balance.to_i} zents)",
+ "Transactions: #{wallet.txns.count}",
+ "File size: #{File.size(wallet.path)} bytes (#{Copies.new(File.join(settings.copies, id)).all.count} copies)",
+ "Modified: #{wallet.mtime.utc.iso8601}",
+ "Digest: #{wallet.digest}"
+ ].join("\n")
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.bin} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- AtomicFile.new(wallet.path).read
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ AtomicFile.new(wallet.path).read
+ end
end
get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copies} do
id = Id.new(params[:id])
- wallet = settings.wallets.find(id)
- error 404 unless wallet.exists?
- content_type 'text/plain'
- Copies.new(File.join(settings.copies, id)).all.map do |c|
- wallet = Wallet.new(c[:path])
- "#{c[:name]}: #{c[:score]} #{wallet.balance}/#{wallet.txns.count}t/\
-#{wallet.digest[0, 6]}/#{File.size(c[:path])}b/#{Age.new(File.mtime(c[:path]))}"
- end.join("\n")
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ content_type 'text/plain'
+ copies = Copies.new(File.join(settings.copies, id))
+ copies.load.map { |c| "#{c[:name]}: #{c[:host]}:#{c[:port]} #{c[:score]} #{c[:time]}" }.join("\n") +
+ "\n\n" +
+ copies.all.map do |c|
+ w = Wallet.new(c[:path])
+ "#{c[:name]}: #{c[:score]} #{w.balance}/#{w.txns.count}t/\
+ #{w.digest[0, 6]}/#{File.size(c[:path])}b/#{Age.new(File.mtime(c[:path]))}"
+ end.join("\n")
+ end
end
+ get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copy/(?<name>[0-9]+)} do
+ id = Id.new(params[:id])
+ name = params[:name]
+ settings.wallets.find(id) do |wallet|
+ error 404 unless wallet.exists?
+ copy = Copies.new(File.join(settings.copies, id)).all.find { |c| c[:name] == name }
+ error 404 if copy.nil?
+ content_type 'text/plain'
+ File.read(copy[:path])
+ end
+ end
+
put %r{/wallet/(?<id>[A-Fa-f0-9]{16})/?} do
request.body.rewind
modified = settings.entrance.push(Id.new(params[:id]), request.body.read.to_s)
if modified.empty?
status 304
@@ -329,10 +360,10 @@
end
error 400 do
status 400
content_type 'text/plain'
- env['sinatra.error'].message
+ env['sinatra.error'] ? env['sinatra.error'].message : 'Invalid request'
end
error do
status 503
e = env['sinatra.error']