Browse Source

Mongoid integration

new-scoring
aeris 2 years ago
parent
commit
de685b1757

+ 1
- 1
Gemfile View File

@@ -6,7 +6,7 @@ gem 'cryptcheck', '~> 2.0.0', path: File.expand_path(File.join File.dirname(__FI

gem 'dotenv-rails'
gem 'http_accept_language'
gem 'mongo'
gem 'mongoid'
gem 'simpleidn'

gem 'redis-namespace'

+ 8
- 14
app/controllers/check_controller.rb View File

@@ -7,16 +7,11 @@ class CheckController < ApplicationController
@host = SimpleIDN.to_unicode @host
respond_to do |format|
format.html do
return render :processing if @result[:pending]
@result = OpenStruct.deep @result
return render :processing if @result.pending
end
format.json do
render json: case
when @result[:pending] then
:pending
else
JSON.pretty_generate @result
end
ap @result
render json: JSON.pretty_generate(JSON.parse @result.to_json)
end
end
end
@@ -38,9 +33,8 @@ class CheckController < ApplicationController
protected

def enqueue_host
Datastore.pending self.type, @host, @port
@result = Analysis.pending self.type, @host, @port
self.worker.perform_async *(@port.blank? ? [@host] : [@host, @port])
@result = OpenStruct.new pending: true, date: Time.now
end

def check_host
@@ -60,9 +54,9 @@ class CheckController < ApplicationController
end
@port = @port.to_i if @port

#@result = Datastore.host self.type, @host, @port
file = File.join Rails.root, 'config/host.yml'
#File.write file, YAML.dump(@result)
@result = YAML.load File.read file
@result = 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
end
end

+ 0
- 29
app/lib/datastore.rb View File

@@ -1,29 +0,0 @@
class Datastore
@@client = Mongo::Client.new ENV['MONGO_URL']

def self.host(type, host, port)
key = self.key host, port
@@client[type].find(key).first
# result = @@index.type(type).get key
# result.date = Time.parse result.date
# result
end

def self.pending(type, host, port)
self.post type, host, port, { pending: true, date: DateTime.now }
end

def self.post(type, host, port, data)
# entry = self.host type, host, port
# entry.delete if entry
#
key = self.key host, port
data = data.merge key
@@client[type].update_one key, data, {upsert: true}
end

private
def self.key(host, port)
{ host: host, port: port }
end
end

+ 56
- 0
app/models/analysis.rb View File

@@ -0,0 +1,56 @@
class Analysis
include Mongoid::Document
include Mongoid::Timestamps

field :type, type: Symbol
field :host, type: String
field :port, type: Numeric
field :pending, type: Boolean
field :date, type: Time
field :result, type: Array

validates_presence_of :type
validates_presence_of :host
validates_presence_of :port
validates_uniqueness_of :type, scope: %i[host port]

index type: 1
index({ type: 1, host: 1, port: 1 }, { unique: true })

def self.[](type, host, port)
key = self.key type, host, port
self.where(key).first
end

def self.pending(type, host, port)
analysis = self[type, host, port]
if analysis
analysis.remove_attribute :result
analysis.update_attributes pending: true, date: Time.now
analysis
else
self.create! type: type, host: host, port: port, pending: true, date: Time.now
end
end

def self.result(type, host, port, result)
analysis = self[type, host, port]
if analysis
analysis.remove_attribute :pending
analysis.update_attributes result: result, date: Time.now
analysis
else
self.create! type: type, host: host, port: port, result: result, date: Time.now
end
end

def publish(result)
self.remove_attribute :pending
self.update_attribute :result, result
end

private
def self.key(type, host, port)
{ type: type, host: host, port: port }
end
end

+ 5
- 28
app/workers/check_worker.rb View File

@@ -2,33 +2,10 @@ class CheckWorker
include Sidekiq::Worker
sidekiq_options retry: false

def key_to_json(key)
key.nil? ? nil : { type: key.type, size: key.size }
end

def perform(host, port=nil)
host = SimpleIDN.to_ascii host.downcase
result = self.analyze *(port ? [host, port] : [host])
result = result.to_h
result[:date] = DateTime.now
Datastore.post self.type, host, port, result
end

protected
def to_json(server)
{
key: key_to_json(server.key),
dh: server.dh.collect { |k| key_to_json k },
protocols: server.supported_protocols,
ciphers: server.supported_ciphers.collect { |c| { protocol: c.protocol, name: c.name, size: c.size, dh: key_to_json(c.dh) } },

}
end

private
def grade_to_json(grade)
{
rank: grade.grade,
}
def perform(host, port)
# analysis = Analysis.pending self.type, host, port
host = SimpleIDN.to_ascii host.downcase
result = self.analyze host, port
Analysis.result self.type, host, port, result
end
end

+ 1
- 0
config/application.rb View File

@@ -4,6 +4,7 @@ require File.expand_path('../boot', __FILE__)
action_controller
action_view
active_job
active_model
rails/test_unit
sprockets
).each do |framework|

+ 22
- 0
config/mongoid.yml View File

@@ -0,0 +1,22 @@
development:
clients:
default:
database: <%= ENV['MONGO_DATABASE'] %>_development
hosts:
- <%= ENV['MONGO_URL'] %>
test:
clients:
default:
database: <%= ENV['MONGO_DATABASE'] %>_test
hosts:
- <%= ENV['MONGO_URL'] %>
options:
read:
mode: :primary
max_pool_size: 1
production:
clients:
default:
database: <%= ENV['MONGO_DATABASE'] %>
hosts:
- <%= ENV['MONGO_URL'] %>

Loading…
Cancel
Save