瀏覽代碼

Change grade to module

new-scoring
aeris 2 年之前
父節點
當前提交
9cfea50107

+ 2
- 6
lib/cryptcheck.rb 查看文件

@@ -37,20 +37,17 @@ module CryptCheck
37 37
 		autoload :Https, 'cryptcheck/tls/https'
38 38
 		module Https
39 39
 			autoload :Server, 'cryptcheck/tls/https/server'
40
-			autoload :Grade, 'cryptcheck/tls/https/grade'
41 40
 			autoload :Host, 'cryptcheck/tls/https/host'
42 41
 		end
43 42
 
44 43
 		autoload :Xmpp, 'cryptcheck/tls/xmpp'
45 44
 		module Xmpp
46 45
 			autoload :Server, 'cryptcheck/tls/xmpp/server'
47
-			autoload :Grade, 'cryptcheck/tls/xmpp/grade'
48 46
 		end
49 47
 
50 48
 		autoload :Smtp, 'cryptcheck/tls/smtp'
51 49
 		module Smtp
52 50
 			autoload :Server, 'cryptcheck/tls/smtp/server'
53
-			autoload :Grade, 'cryptcheck/tls/smtp/grade'
54 51
 		end
55 52
 	end
56 53
 
@@ -59,7 +56,6 @@ module CryptCheck
59 56
 		autoload :Packet, 'cryptcheck/ssh/packet'
60 57
 		autoload :Server, 'cryptcheck/ssh/server'
61 58
 		autoload :SshNotSupportedServer, 'cryptcheck/ssh/server'
62
-		autoload :Grade, 'cryptcheck/ssh/grade'
63 59
 	end
64 60
 
65 61
 	private
@@ -106,7 +102,7 @@ module CryptCheck
106 102
 		end.to_h
107 103
 	end
108 104
 
109
-	def self.analyze(host, port, server, grade, *args, **kargs)
105
+	def self.analyze(host, port, server, *args, **kargs)
110 106
 		addresses = begin
111 107
 			addresses host
112 108
 		rescue ::SocketError => e
@@ -115,7 +111,7 @@ module CryptCheck
115 111
 			error = AnalysisFailure.new "Unable to resolve #{host}"
116 112
 			return { key => error }
117 113
 		end
118
-		analyze_addresses host, addresses, port, server, grade, *args, **kargs
114
+		analyze_addresses host, addresses, port, server, *args, **kargs
119 115
 	end
120 116
 
121 117
 	def self.analyze_hosts(hosts, template, output, groups: nil, &block)

+ 1
- 1
lib/cryptcheck/ssh.rb 查看文件

@@ -1,7 +1,7 @@
1 1
 module CryptCheck
2 2
 	module Ssh
3 3
 		def self.analyze(host, port=22)
4
-			::CryptCheck.analyze host, port, Server, Grade
4
+			::CryptCheck.analyze host, port, Server
5 5
 		end
6 6
 	end
7 7
 end

+ 0
- 5
lib/cryptcheck/state.rb 查看文件

@@ -61,11 +61,6 @@ module CryptCheck
61 61
 			a <=> b
62 62
 		end
63 63
 
64
-		def performed_checks
65
-			self.states # Force internal resolution
66
-			@performed_checks
67
-		end
68
-
69 64
 		protected
70 65
 		def checks
71 66
 			@checks ||= self.available_checks.collect { |c| perform_check c }.flatten(1) + children.collect(&:checks).flatten(1)

+ 1
- 1
lib/cryptcheck/tls.rb 查看文件

@@ -4,7 +4,7 @@ require 'parallel'
4 4
 module CryptCheck
5 5
 	module Tls
6 6
 		def self.analyze(host, port)
7
-			::CryptCheck.analyze host, port, TcpServer, Grade
7
+			::CryptCheck.analyze host, port, TcpServer
8 8
 		end
9 9
 
10 10
 		def self.key_to_s(key)

+ 16
- 56
lib/cryptcheck/tls/grade.rb 查看文件

@@ -1,71 +1,31 @@
1 1
 module CryptCheck
2 2
 	module Tls
3
-		class Grade
4
-			attr_reader :server, :grade
5
-
6
-			def initialize(server)
7
-				@server = server
8
-				@states = @server.states
9
-				@checks = @server.performed_checks
10
-				Logger.info { '' }
11
-				Logger.ap :checks, @checks
12
-				Logger.ap :states, @states
13
-				@grade = calculate_grade
14
-
15
-				color = case @grade
16
-							when 'A', 'A+'
17
-								:best
18
-							when 'B', 'B+'
19
-								:great
20
-							when 'C', 'C+'
21
-								:good
22
-							when 'E'
23
-								:warning
24
-							when 'F'
25
-								:error
26
-							when 'G'
27
-								:critical
28
-							when 'T', 'V'
29
-								:unknown
30
-						end
31
-
32
-				Logger.info { "Grade : #{self.grade.colorize color }" }
33
-			end
34
-
35
-			def to_h
36
-				{ checks: @checks, states: @states }
3
+		module Grade
4
+			def grade
5
+				@grade ||= calculate_grade
37 6
 			end
38 7
 
39 8
 			private
40 9
 			def calculate_grade
41
-				return 'V' unless @server.valid?
42
-				return 'T' unless @server.trusted?
10
+				return :V unless self.valid?
11
+				return :T unless self.trusted?
12
+
13
+				states = self.states
43 14
 
44
-				case
45
-					when !@states[:critical].empty?
46
-						return 'G'
47
-					when !@states[:error].empty?
48
-						return 'F'
49
-					when !@states[:warning].empty?
50
-						return 'E'
15
+				{ critical: :G, error: :F, warning: :E }.each do |type, grade|
16
+					return grade if states[type].any? { |s| s == true }
51 17
 				end
52 18
 
53
-				[[:good, 'D', 'C'],
54
-				 [:great, 'C', 'B'],
55
-				 [:best, 'B', 'A']].each do |type, score1, score2|
56
-					expected = @checks[type]
57
-					unless expected.empty?
58
-						available = @states[type]
59
-						return score1 if available.empty?
60
-						missed = expected - available
61
-						unless missed.empty?
62
-							Logger.info { "Missing #{type} : #{missed}" }
63
-							return score2
64
-						end
19
+				{good: %i(D C), great: %i(C B), best: %i(B A)}.each do |type, scores|
20
+					state = states[type]
21
+					return scores.first if state.all? { |s| s != false }
22
+					if state.any? { |s| s == false }
23
+						Logger.info { "Missing #{type} : #{states[type].select { |s| s == false }.collect &:key}" }
24
+						return scores.last
65 25
 					end
66 26
 				end
67 27
 
68
-				'A+'
28
+				:'A+'
69 29
 			end
70 30
 		end
71 31
 	end

+ 10
- 11
lib/cryptcheck/tls/host.rb 查看文件

@@ -36,7 +36,9 @@ module CryptCheck
36 36
 						server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
37 37
 							server(*args)
38 38
 						end
39
-						grade server
39
+						Logger.info ''
40
+						Logger.info { "Grade : #{server.grade.to_s.colorize server.status}" }
41
+						server
40 42
 					rescue Engine::TLSException, Engine::ConnectionError, Engine::Timeout => e
41 43
 						AnalysisFailure.new e
42 44
 					rescue ::Timeout::Error
@@ -57,19 +59,20 @@ module CryptCheck
57 59
 				if @error
58 60
 					target[:error] = @error
59 61
 				else
60
-					target[:hosts] = @servers.collect do |host, grade|
62
+					target[:hosts] = @servers.collect do |host, server|
61 63
 						hostname, ip, port = host
62 64
 						host               = {
63 65
 								hostname: hostname,
64 66
 								ip:       ip,
65 67
 								port:     port
66 68
 						}
67
-						case grade
68
-							when Grade
69
-								host[:analysis] = grade.server.to_h
70
-								host[:status]   = grade.to_h
69
+						case server
70
+							when Server
71
+								host[:handshakes] = server.to_h
72
+								host[:states]   = server.states
73
+								host[:grade]   = server.grade
71 74
 							else
72
-								host[:error] = grade.message
75
+								host[:error] = server.message
73 76
 						end
74 77
 						host
75 78
 					end
@@ -91,10 +94,6 @@ module CryptCheck
91 94
 			def server(*args)
92 95
 				TcpServer.new *args
93 96
 			end
94
-
95
-			def grade(server)
96
-				Grade.new server
97
-			end
98 97
 		end
99 98
 	end
100 99
 end

+ 0
- 16
lib/cryptcheck/tls/https/grade.rb 查看文件

@@ -1,16 +0,0 @@
1
-module CryptCheck
2
-	module Tls
3
-		module Https
4
-			class Grade < Tls::Grade
5
-				CHECKS = {
6
-						good:    %i(hsts),
7
-						perfect: %i(hsts_long)
8
-				}
9
-
10
-				def checks
11
-					State.merge super, CHECKS
12
-				end
13
-			end
14
-		end
15
-	end
16
-end

+ 0
- 4
lib/cryptcheck/tls/https/host.rb 查看文件

@@ -6,10 +6,6 @@ module CryptCheck
6 6
 				def server(*args)
7 7
 					Https::Server.new *args
8 8
 				end
9
-
10
-				def grade(server)
11
-					Https::Grade.new server
12
-				end
13 9
 			end
14 10
 		end
15 11
 	end

+ 1
- 0
lib/cryptcheck/tls/server.rb 查看文件

@@ -85,6 +85,7 @@ module CryptCheck
85 85
 			end
86 86
 
87 87
 			include Engine
88
+			include Grade
88 89
 		end
89 90
 
90 91
 		class TcpServer < Server

+ 0
- 14
lib/cryptcheck/tls/smtp/grade.rb 查看文件

@@ -1,14 +0,0 @@
1
-module CryptCheck
2
-	module Tls
3
-		module Smtp
4
-			class Grade < Tls::Grade
5
-				CHECKS = {
6
-				}
7
-
8
-				def checks
9
-					State.merge super, CHECKS
10
-				end
11
-			end
12
-		end
13
-	end
14
-end

+ 0
- 15
lib/cryptcheck/tls/xmpp/grade.rb 查看文件

@@ -1,15 +0,0 @@
1
-module CryptCheck
2
-	module Tls
3
-		module Xmpp
4
-			class Grade < Tls::Grade
5
-				CHECKS = {
6
-						good: %i(required)
7
-				}
8
-
9
-				def checks
10
-					State.merge super, CHECKS
11
-				end
12
-			end
13
-		end
14
-	end
15
-end

Loading…
取消
儲存