registered-sites/app/models/registered_site.rb

109 lines
3.0 KiB
Ruby

class RegisteredSite
include Mongoid::Document
include Mongoid::Timestamps
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
field :ip
field :real_ip
field :site_domain
field :title, as: :slug_title, localize: true
field :description, localize: true
field :access_granted, type: Boolean, :default => false
field :admin_email
field :site_confirmed, type: Boolean, :default => false
field :confirmation_token
field :is_hidden,type: Boolean,:default => false
has_many :bundle_update_logs
has_many :site_logs
has_many :installed_modules
has_many :tickets
has_many :site_statuses
before_save :generate_ip
after_create do
self.bundle_update_logs.create
end
index({ confirmation_token: 1}, { unique: true })
scope :active, ->{where(:access_granted => true,:site_confirmed => true)}
def generate_ip
self[:ip] = %x[dig +short '#{self[:site_domain]}'].split("\n")
if self[:ip].count==0
self[:ip] = self[:site_domain]
end
end
def site_token
self.uid
end
def show_name
self.title.nil? ? self.site_domain : self.title
end
def generate_confirmation_token
self.confirmation_token = SecureRandom.hex(5)
self.save
end
def active?
return self.access_granted && self.site_confirmed
end
def is_alive?
urls = self.self_test
if !urls[0].to_s.empty? || !urls[1].to_s.empty?
true
else
false
end
end
def self_test
domain = self.site_domain
types = ['http','https']
urls = types.collect do |type|
root_url = "#{type}://#{domain}"
uri0 = URI(root_url)
r = Net::HTTP.start(uri0.host, uri0.port,:use_ssl => uri0.scheme == 'https',open_timeout: 20,read_timeout: 20,verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
req = Net::HTTP::Get.new(uri0)
req['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
http.request req
end rescue nil
code = r.code.to_s rescue nil
if code == '200'
root_url
elsif code.nil?
nil
else
''
end
end
end
def fetch_web_url_and_log
if self.bundle_update_logs.first.nil?
log = self.bundle_update_logs.create()
else
log = self.bundle_update_logs.first
end
urls = self.self_test
log.last_time = Time.now
if urls == [nil,nil]
log.message = ['domain not found']
log.last_message = 'domain not found'
log.status = 'failed'
root_url = nil
elsif !urls[0].to_s.empty? || !urls[1].to_s.empty?
if !urls[0].empty?
root_url = urls[0]
else
root_url = urls[1]
end
log.message = ["open the web by #{root_url}"]
log.status = 'running'
log.last_message = "open the web by #{root_url}"
else
log.message = ['Maybe the web need to restart']
log.last_message = 'Maybe the web need to restart'
log.status = 'failed'
root_url = nil
end
log.save!
[root_url,log]
end
end