From 70c8bcbcd1b7db1240ea6cf498db3402f1c82391 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 7 Aug 2021 18:59:23 -0300 Subject: [PATCH] Detectar cuando el servicio oculto es opcional --- app/controllers/api/v1/sites_controller.rb | 4 ++-- app/models/deploy_hidden_service.rb | 18 ++++++++++++++++-- app/views/deploys/_deploy_hidden_service.haml | 5 +++-- test/models/deploy_hidden_service_test.rb | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/v1/sites_controller.rb b/app/controllers/api/v1/sites_controller.rb index 6abff704..6613dce5 100644 --- a/app/controllers/api/v1/sites_controller.rb +++ b/app/controllers/api/v1/sites_controller.rb @@ -16,7 +16,7 @@ module Api # # @return [Array] lista de nombres de sitios sin onion aun def hidden_services - render json: DeployHiddenService.where(values: nil).includes(:site).pluck(:name) + render json: DeployHiddenService.temporary.includes(:site).pluck(:name) end # Tor va a enviar el onion junto con el nombre del sitio y tenemos @@ -28,7 +28,7 @@ module Api site = Site.find_by(name: params[:name]) if site - usuarie = GitAuthor.new email: 'tor@' + Site.domain, name: 'Tor' + usuarie = GitAuthor.new email: "tor@#{Site.domain}", name: 'Tor' service = SiteService.new site: site, usuarie: usuarie, params: params service.add_onion diff --git a/app/models/deploy_hidden_service.rb b/app/models/deploy_hidden_service.rb index 8ca4b082..184ca2c2 100644 --- a/app/models/deploy_hidden_service.rb +++ b/app/models/deploy_hidden_service.rb @@ -5,6 +5,12 @@ class DeployHiddenService < DeployWww validates :hostname, format: { with: /\A[a-z2-7]{56}.onion\z/ } + # Sufijo para todos los dominios temporales. + TEMPORARY_SUFFIX = 'temporary' + + # Traer todos los servicios ocultos temporales. + scope :temporary, -> { where("hostname not like '#{TEMPORARY_SUFFIX}%'") } + # Los servicios ocultos son su propio transporte cifrado y # autenticado. # @@ -19,7 +25,14 @@ class DeployHiddenService < DeployWww # # @return [String] def default_hostname - "temporal#{random_base32}.onion" + "#{TEMPORARY_SUFFIX}#{random_base32}.onion" + end + + # Detecta si es una dirección temporal. + # + # @return [Boolean] + def temporary? + hostname.start_with? TEMPORARY_SUFFIX end private @@ -32,8 +45,9 @@ class DeployHiddenService < DeployWww # # @see {https://github.com/stesla/base32/blob/master/lib/base32.rb} # @see {https://github.com/stesla/base32/blob/master/LICENSE} - def random_base32(length = 48) + def random_base32(length = nil) table = 'abcdefghijklmnopqrstuvwxyz234567' + length ||= 56 - TEMPORARY_SUFFIX.length OpenSSL::Random.random_bytes(length).each_byte.map do |b| table[b % 32] diff --git a/app/views/deploys/_deploy_hidden_service.haml b/app/views/deploys/_deploy_hidden_service.haml index 38b33b5d..73992602 100644 --- a/app/views/deploys/_deploy_hidden_service.haml +++ b/app/views/deploys/_deploy_hidden_service.haml @@ -17,6 +17,7 @@ = sanitize_markdown t('.help', public_url: site.deploy_local.url), tags: %w[p strong em a] - = sanitize_markdown t('.help_2', url: deploy.object.url), - tags: %w[p strong em a] + - unless deploy.object.temporary? + = sanitize_markdown t('.help_2', url: deploy.object.url), + tags: %w[p strong em a] %hr/ diff --git a/test/models/deploy_hidden_service_test.rb b/test/models/deploy_hidden_service_test.rb index 799f3df3..cfa09b41 100644 --- a/test/models/deploy_hidden_service_test.rb +++ b/test/models/deploy_hidden_service_test.rb @@ -20,7 +20,7 @@ class DeployHiddenServiceTest < ActiveSupport::TestCase end test 'los hostnames pueden ser temporales' do - assert @deploy_hidden.hostname.start_with? 'temporal' + assert @deploy_hidden.hostname.start_with? 'temporary' end test 'el hostname tiene que ser único' do