2020-07-18 19:26:54 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-08-02 00:54:12 +00:00
|
|
|
# Alojar el sitio como un servicio oculto de Tor, que en realidad es un
|
|
|
|
# link simbólico al DeployLocal.
|
2020-07-18 19:26:54 +00:00
|
|
|
class DeployHiddenService < DeployWww
|
2021-08-07 20:38:01 +00:00
|
|
|
validates :hostname, format: { with: /\A[a-z2-7]{56}.onion\z/ }
|
2020-07-18 19:26:54 +00:00
|
|
|
|
2021-08-07 21:59:23 +00:00
|
|
|
# Sufijo para todos los dominios temporales.
|
|
|
|
TEMPORARY_SUFFIX = 'temporary'
|
|
|
|
|
|
|
|
# Traer todos los servicios ocultos temporales.
|
|
|
|
scope :temporary, -> { where("hostname not like '#{TEMPORARY_SUFFIX}%'") }
|
|
|
|
|
2021-08-02 00:54:12 +00:00
|
|
|
# Los servicios ocultos son su propio transporte cifrado y
|
|
|
|
# autenticado.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def url
|
|
|
|
"http://#{hostname}"
|
2020-07-18 19:26:54 +00:00
|
|
|
end
|
|
|
|
|
2021-08-02 00:54:12 +00:00
|
|
|
# Los onions no son creados por Sutty sino por Tor y enviados luego a
|
|
|
|
# través de la API. El hostname por defecto es un nombre temporal que
|
|
|
|
# se parece a una dirección OnionV3.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def default_hostname
|
2021-08-07 21:59:23 +00:00
|
|
|
"#{TEMPORARY_SUFFIX}#{random_base32}.onion"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detecta si es una dirección temporal.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def temporary?
|
|
|
|
hostname.start_with? TEMPORARY_SUFFIX
|
2020-07-18 19:26:54 +00:00
|
|
|
end
|
|
|
|
|
2021-08-02 00:54:12 +00:00
|
|
|
private
|
|
|
|
|
2021-08-14 23:21:30 +00:00
|
|
|
# No soportamos cambiar de onion
|
|
|
|
def destination_changed?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2021-08-02 00:54:12 +00:00
|
|
|
def implements_hostname_validation?
|
|
|
|
true
|
2020-07-18 19:26:54 +00:00
|
|
|
end
|
2021-08-07 20:38:01 +00:00
|
|
|
|
|
|
|
# Adaptado de base32
|
|
|
|
#
|
|
|
|
# @see {https://github.com/stesla/base32/blob/master/lib/base32.rb}
|
|
|
|
# @see {https://github.com/stesla/base32/blob/master/LICENSE}
|
2021-08-07 21:59:23 +00:00
|
|
|
def random_base32(length = nil)
|
2021-08-07 20:38:01 +00:00
|
|
|
table = 'abcdefghijklmnopqrstuvwxyz234567'
|
2021-08-07 21:59:23 +00:00
|
|
|
length ||= 56 - TEMPORARY_SUFFIX.length
|
2021-08-07 20:38:01 +00:00
|
|
|
|
|
|
|
OpenSSL::Random.random_bytes(length).each_byte.map do |b|
|
|
|
|
table[b % 32]
|
|
|
|
end.join
|
|
|
|
end
|
2020-07-18 19:26:54 +00:00
|
|
|
end
|