diff --git a/app/controllers/check_controller.rb b/app/controllers/check_controller.rb index 712cb8d..ac66944 100644 --- a/app/controllers/check_controller.rb +++ b/app/controllers/check_controller.rb @@ -29,12 +29,12 @@ class CheckController < ApplicationController end protected - def default_port + def default_args end def enqueue_host - @analysis = Analysis.pending! self.type, @host, (@port || self.default_port) - self.worker.perform_async @analysis.host, @analysis.port + @analysis = Analysis.pending! self.type, @host, @args + self.worker.perform_async @analysis.host, *@analysis.args end def check_host @@ -45,22 +45,15 @@ class CheckController < ApplicationController request.format = :json end - @host, @port = @id.split ':' + @host, @args = @id.split ':' @host = SimpleIDN.to_ascii @host.downcase if /[^a-zA-Z0-9.-]/ =~ @host flash[:danger] = "Hôte #{@host} invalide" redirect_to action: :index return false end - if @port - @port = @port.to_i - else - @port = self.default_port - end + @args ||= default_args - @analysis = Analysis[self.type, @host, @port] - # file = File.join Rails.root, 'config/host.yml' - # File.write file, YAML.dump(@result) - # @result = YAML.load File.read file + @analysis = Analysis[self.type, @host, @args] end end diff --git a/app/controllers/https_controller.rb b/app/controllers/https_controller.rb index 043a391..994d580 100644 --- a/app/controllers/https_controller.rb +++ b/app/controllers/https_controller.rb @@ -12,7 +12,7 @@ class HttpsController < CheckController 'HTTPS' end - def default_port + def default_args 443 end end diff --git a/app/controllers/smtp_controller.rb b/app/controllers/smtp_controller.rb index 65c80ed..e36cafb 100644 --- a/app/controllers/smtp_controller.rb +++ b/app/controllers/smtp_controller.rb @@ -11,8 +11,4 @@ class SmtpController < CheckController def tls_type 'STARTTLS' end - - def default_port - 25 - end end diff --git a/app/controllers/xmpp_controller.rb b/app/controllers/xmpp_controller.rb index 2fa08b1..4e81cbe 100644 --- a/app/controllers/xmpp_controller.rb +++ b/app/controllers/xmpp_controller.rb @@ -1,14 +1,19 @@ class XmppController < CheckController - protected - def type - :xmpp - end - - def worker - XMPPWorker - end - - def tls_type - 'STARTTLS' - end + protected + + def type + :xmpp + end + + def worker + XMPPWorker + end + + def tls_type + 'STARTTLS' + end + + def default_args + :c2s + end end diff --git a/app/helpers/ssh_helper.rb b/app/helpers/ssh_helper.rb index ac78632..5237a05 100644 --- a/app/helpers/ssh_helper.rb +++ b/app/helpers/ssh_helper.rb @@ -22,7 +22,7 @@ module SshHelper end private - def label(name, color) - "  #{name}".html_safe - end + #def label(name, color) + # "  #{name}".html_safe + #end end diff --git a/app/models/analysis.rb b/app/models/analysis.rb index ed99f55..da1e8d6 100644 --- a/app/models/analysis.rb +++ b/app/models/analysis.rb @@ -3,13 +3,13 @@ class Analysis < ApplicationRecord validates :service, presence: true validates :host, presence: true - def self.[](service, host, port) - key = self.key service, host, port + def self.[](service, host, args) + key = self.key service, host, args self.find_by key end - def self.pending!(service, host, port) - key = self.key service, host, port + def self.pending!(service, host, args) + key = self.key service, host, args analysis = self.find_or_create_by! key analysis.pending! end @@ -19,8 +19,8 @@ class Analysis < ApplicationRecord self end - def self.post!(service, host, port, result) - analysis = self[service, host, port] + def self.post!(service, host, args, result) + analysis = self[service, host, args] analysis.post! result end @@ -30,7 +30,7 @@ class Analysis < ApplicationRecord private - def self.key(service, host, port) - { service: service, host: host, port: port } + def self.key(service, host, args) + { service: service, host: host, args: args } end end diff --git a/app/workers/check_worker.rb b/app/workers/check_worker.rb index a22c5f1..bb1c9a8 100644 --- a/app/workers/check_worker.rb +++ b/app/workers/check_worker.rb @@ -1,11 +1,11 @@ class CheckWorker - include Sidekiq::Worker - sidekiq_options retry: false + include Sidekiq::Worker + sidekiq_options retry: false - def perform(host, port) - # analysis = Analysis.pending self.type, host, port - host = SimpleIDN.to_ascii host.downcase - result = self.analyze host, port - Analysis.post! self.type, host, port, result - end + def perform(host, *args) + host = SimpleIDN.to_ascii host.downcase + result = self.analyze host, *args + args = nil if args.empty? + Analysis.post! self.type, host, args, result + end end diff --git a/app/workers/https_worker.rb b/app/workers/https_worker.rb index 8b1d621..e5047c0 100644 --- a/app/workers/https_worker.rb +++ b/app/workers/https_worker.rb @@ -1,12 +1,12 @@ class HTTPSWorker < CheckWorker - sidekiq_options retry: false + sidekiq_options retry: false - protected - def analyze(host, port=443) - CryptCheck::Tls::Https.analyze host, port - end + protected + def analyze(host, port) + CryptCheck::Tls::Https.analyze host, port + end - def type - :https - end + def type + :https + end end diff --git a/app/workers/smtp_worker.rb b/app/workers/smtp_worker.rb index a716363..3befdc8 100644 --- a/app/workers/smtp_worker.rb +++ b/app/workers/smtp_worker.rb @@ -3,7 +3,7 @@ class SMTPWorker < CheckWorker protected def analyze(host) - CryptCheck::Tls::Smtp.analyze_domain host + CryptCheck::Tls::Smtp.analyze host end def type diff --git a/app/workers/xmpp_worker.rb b/app/workers/xmpp_worker.rb index b56c92b..35477b2 100644 --- a/app/workers/xmpp_worker.rb +++ b/app/workers/xmpp_worker.rb @@ -2,17 +2,11 @@ class XMPPWorker < CheckWorker sidekiq_options retry: false protected - def analyze(host) - CryptCheck::Tls::Xmpp.analyze_domain host + def analyze(host, type) + CryptCheck::Tls::Xmpp.analyze host, type end def type :xmpp end - - def to_json(server) - result = super - result[:required] = server.required? - result - end end diff --git a/db/migrate/20191201192510_convert_to_args.rb b/db/migrate/20191201192510_convert_to_args.rb new file mode 100644 index 0000000..66dd2b6 --- /dev/null +++ b/db/migrate/20191201192510_convert_to_args.rb @@ -0,0 +1,15 @@ +class ConvertToArgs < ActiveRecord::Migration[5.2] + def self.up + add_column :analyses, :args, :jsonb, after: :host + Analysis.all.each { |a| a.update! args: { port: a.port }.compact } + add_index :analyses, %i[service host args], unique: true + remove_column :analyses, :port + end + + def self.down + add_column :analyses, :port, :integer + Analysis.all.each { |a| a.update! port: a.args } + remove_column :analyses, :args + add_index :analyses, %i[service host port], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6ff9fb1..7d5bba0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_09_13_211227) do +ActiveRecord::Schema.define(version: 2019_12_01_192510) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -19,12 +19,12 @@ ActiveRecord::Schema.define(version: 2019_09_13_211227) do create_table "analyses", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "service", null: false t.string "host", null: false - t.integer "port" t.boolean "pending", default: true, null: false t.jsonb "result" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["service", "host", "port"], name: "index_analyses_on_service_and_host_and_port", unique: true + t.jsonb "args" + t.index ["service", "host", "args"], name: "index_analyses_on_service_and_host_and_args", unique: true end end