Browse Source

Mongoid integration

aeris 1 year 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
6 6
 
7 7
 gem 'dotenv-rails'
8 8
 gem 'http_accept_language'
9
-gem 'mongo'
9
+gem 'mongoid'
10 10
 gem 'simpleidn'
11 11
 
12 12
 gem 'redis-namespace'

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

@@ -7,16 +7,11 @@ class CheckController < ApplicationController
7 7
 		@host = SimpleIDN.to_unicode @host
8 8
 		respond_to do |format|
9 9
 			format.html do
10
-				return render :processing if @result[:pending]
11
-				@result = OpenStruct.deep @result
10
+				return render :processing if @result.pending
12 11
 			end
13 12
 			format.json do
14
-				render json: case
15
-								 when @result[:pending] then
16
-									 :pending
17
-								 else
18
-									 JSON.pretty_generate @result
19
-							 end
13
+				ap @result
14
+				render json: JSON.pretty_generate(JSON.parse @result.to_json)
20 15
 			end
21 16
 		end
22 17
 	end
@@ -38,9 +33,8 @@ class CheckController < ApplicationController
38 33
 	protected
39 34
 
40 35
 	def enqueue_host
41
-		Datastore.pending self.type, @host, @port
36
+		@result = Analysis.pending self.type, @host, @port
42 37
 		self.worker.perform_async *(@port.blank? ? [@host] : [@host, @port])
43
-		@result = OpenStruct.new pending: true, date: Time.now
44 38
 	end
45 39
 
46 40
 	def check_host
@@ -60,9 +54,9 @@ class CheckController < ApplicationController
60 54
 		end
61 55
 		@port = @port.to_i if @port
62 56
 
63
-		#@result = Datastore.host self.type, @host, @port
64
-		file = File.join Rails.root, 'config/host.yml'
65
-		#File.write file, YAML.dump(@result)
66
-		@result = YAML.load File.read file
57
+		@result = Analysis[self.type, @host, @port]
58
+		# file = File.join Rails.root, 'config/host.yml'
59
+		# File.write file, YAML.dump(@result)
60
+		# @result = YAML.load File.read file
67 61
 	end
68 62
 end

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

@@ -1,29 +0,0 @@
1
-class Datastore
2
-	@@client = Mongo::Client.new ENV['MONGO_URL']
3
-
4
-	def self.host(type, host, port)
5
-		key = self.key host, port
6
-		@@client[type].find(key).first
7
-#		result = @@index.type(type).get key
8
-#		result.date = Time.parse result.date
9
-#		result
10
-	end
11
-
12
-	def self.pending(type, host, port)
13
-		self.post type, host, port, { pending: true, date: DateTime.now }
14
-	end
15
-
16
-	def self.post(type, host, port, data)
17
-		# entry = self.host type, host, port
18
-		# entry.delete if entry
19
-		#
20
-		key = self.key host, port
21
-		data = data.merge key
22
-		@@client[type].update_one key, data, {upsert: true}
23
-	end
24
-
25
-	private
26
-	def self.key(host, port)
27
-		{ host: host, port: port }
28
-	end
29
-end

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

@@ -0,0 +1,56 @@
1
+class Analysis
2
+	include Mongoid::Document
3
+	include Mongoid::Timestamps
4
+
5
+	field :type, type: Symbol
6
+	field :host, type: String
7
+	field :port, type: Numeric
8
+	field :pending, type: Boolean
9
+	field :date, type: Time
10
+	field :result, type: Array
11
+
12
+	validates_presence_of :type
13
+	validates_presence_of :host
14
+	validates_presence_of :port
15
+	validates_uniqueness_of :type, scope: %i[host port]
16
+
17
+	index type: 1
18
+	index({ type: 1, host: 1, port: 1 }, { unique: true })
19
+
20
+	def self.[](type, host, port)
21
+		key = self.key type, host, port
22
+		self.where(key).first
23
+	end
24
+
25
+	def self.pending(type, host, port)
26
+		analysis = self[type, host, port]
27
+		if analysis
28
+			analysis.remove_attribute :result
29
+			analysis.update_attributes pending: true, date: Time.now
30
+			analysis
31
+		else
32
+			self.create! type: type, host: host, port: port, pending: true, date: Time.now
33
+		end
34
+	end
35
+
36
+	def self.result(type, host, port, result)
37
+		analysis = self[type, host, port]
38
+		if analysis
39
+			analysis.remove_attribute :pending
40
+			analysis.update_attributes result: result, date: Time.now
41
+			analysis
42
+		else
43
+			self.create! type: type, host: host, port: port, result: result, date: Time.now
44
+		end
45
+	end
46
+
47
+	def publish(result)
48
+		self.remove_attribute :pending
49
+		self.update_attribute :result, result
50
+	end
51
+
52
+	private
53
+	def self.key(type, host, port)
54
+		{ type: type, host: host, port: port }
55
+	end
56
+end

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

@@ -2,33 +2,10 @@ class CheckWorker
2 2
 	include Sidekiq::Worker
3 3
 	sidekiq_options retry: false
4 4
 
5
-	def key_to_json(key)
6
-		key.nil? ? nil : { type: key.type, size: key.size }
7
-	end
8
-
9
-	def perform(host, port=nil)
10
-		host          = SimpleIDN.to_ascii host.downcase
11
-		result        = self.analyze *(port ? [host, port] : [host])
12
-		result        = result.to_h
13
-		result[:date] = DateTime.now
14
-		Datastore.post self.type, host, port, result
15
-	end
16
-
17
-	protected
18
-	def to_json(server)
19
-		{
20
-				key:       key_to_json(server.key),
21
-				dh:        server.dh.collect { |k| key_to_json k },
22
-				protocols: server.supported_protocols,
23
-				ciphers:   server.supported_ciphers.collect { |c| { protocol: c.protocol, name: c.name, size: c.size, dh: key_to_json(c.dh) } },
24
-
25
-		}
26
-	end
27
-
28
-	private
29
-	def grade_to_json(grade)
30
-		{
31
-				rank: grade.grade,
32
-		}
5
+	def perform(host, port)
6
+		# analysis = Analysis.pending self.type, host, port
7
+		host   = SimpleIDN.to_ascii host.downcase
8
+		result = self.analyze host, port
9
+		Analysis.result self.type, host, port, result
33 10
 	end
34 11
 end

+ 1
- 0
config/application.rb View File

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

+ 22
- 0
config/mongoid.yml View File

@@ -0,0 +1,22 @@
1
+development:
2
+  clients:
3
+    default:
4
+      database: <%= ENV['MONGO_DATABASE'] %>_development
5
+      hosts:
6
+        - <%= ENV['MONGO_URL'] %>
7
+test:
8
+  clients:
9
+    default:
10
+      database: <%= ENV['MONGO_DATABASE'] %>_test
11
+      hosts:
12
+        - <%= ENV['MONGO_URL'] %>
13
+      options:
14
+        read:
15
+          mode: :primary
16
+        max_pool_size: 1
17
+production:
18
+  clients:
19
+    default:
20
+      database: <%= ENV['MONGO_DATABASE'] %>
21
+      hosts:
22
+        - <%= ENV['MONGO_URL'] %>

Loading…
Cancel
Save