Browse Source

Better error handling

master
Aeris 3 years ago
parent
commit
c34802c7ec

+ 11
- 7
lib/cryptcheck.rb View File

@@ -27,9 +27,7 @@ module CryptCheck
27 27
 		autoload :Server, 'cryptcheck/tls/server'
28 28
 		autoload :TcpServer, 'cryptcheck/tls/server'
29 29
 		autoload :UdpServer, 'cryptcheck/tls/server'
30
-		autoload :TlsNotSupportedServer, 'cryptcheck/tls/server'
31 30
 		autoload :Grade, 'cryptcheck/tls/grade'
32
-		autoload :TlsNotSupportedGrade, 'cryptcheck/tls/grade'
33 31
 
34 32
 		autoload :Https, 'cryptcheck/tls/https'
35 33
 		module Https
@@ -82,10 +80,14 @@ module CryptCheck
82 80
 						else
83 81
 							server.new *a, **kargs
84 82
 						end
85
-					g = grade.new s
86
-					Logger.info { '' }
87
-					g.display
88
-					[key, g]
83
+					if grade
84
+						g = grade.new s
85
+						Logger.info { '' }
86
+						g.display
87
+						[key, g]
88
+					else
89
+						[key, s]
90
+					end
89 91
 				end
90 92
 			rescue => e
91 93
 				e = "Too long analysis (max #{MAX_ANALYSIS_DURATION.humanize})" if e.message == 'execution expired'
@@ -100,7 +102,9 @@ module CryptCheck
100 102
 			addresses host
101 103
 		rescue ::SocketError => e
102 104
 			Logger::error e
103
-			return AnalysisFailure.new "Unable to resolve #{host}"
105
+			key = [host, nil, port]
106
+			error = AnalysisFailure.new "Unable to resolve #{host}"
107
+			return { key => error }
104 108
 		end
105 109
 		analyze_addresses host, addresses, port, server, grade, *args, **kargs
106 110
 	end

+ 1
- 3
lib/cryptcheck/ssh.rb View File

@@ -1,9 +1,7 @@
1 1
 module CryptCheck
2 2
 	module Ssh
3 3
 		def self.analyze(host, port=22)
4
-			::CryptCheck.analyze(host, port, Proc.new { SshNotSupportedServer.new host, port }) do |_, ip, host|
5
-				Server.new ip, port, hostname: host
6
-			end
4
+			::CryptCheck.analyze host, port, Server, Grade
7 5
 		end
8 6
 	end
9 7
 end

+ 14
- 0
lib/cryptcheck/ssh/grade.rb View File

@@ -0,0 +1,14 @@
1
+module CryptCheck
2
+	module Ssh
3
+		class Grade
4
+			attr_reader :server
5
+
6
+			def initialize(server)
7
+				@server = server
8
+			end
9
+
10
+			def display
11
+			end
12
+		end
13
+	end
14
+end

+ 2
- 4
lib/cryptcheck/ssh/server.rb View File

@@ -4,8 +4,6 @@ module CryptCheck
4 4
 	module Ssh
5 5
 		class Server
6 6
 			TCP_TIMEOUT = 10
7
-			class SshNotAvailableException < Exception
8
-			end
9 7
 
10 8
 			attr_reader :ip, :port, :hostname, :kex, :encryption, :hmac, :compression, :key
11 9
 
@@ -81,7 +79,7 @@ module CryptCheck
81 79
 					'ssh-dss-cert-v00@openssh.com'             => :red,		# DSA
82 80
 			}
83 81
 
84
-			def initialize(ip, port=22, hostname:)
82
+			def initialize(hostname, _, ip, port=22)
85 83
 				@ip, @port, @hostname = ip, port, hostname
86 84
 
87 85
 				Logger.info { name.colorize :blue }
@@ -105,7 +103,7 @@ module CryptCheck
105 103
 				@key.each { |k| Logger.info { "Key type : #{k.colorize KEY[k]}" } }
106 104
 			rescue => e
107 105
 				Logger.debug { "SSH not supported : #{e}" }
108
-				raise SshNotAvailableException, e
106
+				raise
109 107
 			end
110 108
 
111 109
 			private

+ 2
- 2
lib/cryptcheck/tls/fixture.rb View File

@@ -5,9 +5,9 @@ class Integer
5 5
 		secs = self
6 6
 		[[60, :second], [60, :minute], [24, :hour], [30, :day], [12, :month]].map { |count, name|
7 7
 			if secs > 0
8
-				secs, n = self.divmod count
8
+				secs, n = secs.divmod count
9 9
 				n = n.to_i
10
-				"#{n} #{name}#{n > 1 ? 's' : ''}"
10
+				n > 0 ? "#{n} #{name}#{n > 1 ? 's' : ''}" : nil
11 11
 			end
12 12
 		}.compact.reverse.join(' ')
13 13
 	end

+ 3
- 1
lib/cryptcheck/tls/smtp.rb View File

@@ -8,7 +8,9 @@ module CryptCheck
8 8
 			def self.analyze_domain(domain)
9 9
 				srv = Resolv::DNS.new.getresources(domain, Resolv::DNS::Resource::IN::MX).sort_by &:preference
10 10
 				hosts = srv.empty? ? [domain] : srv.collect { |s| s.exchange.to_s }
11
-				hosts.collect { |h| self.analyze h, domain: domain }.flatten(1)
11
+				results = {}
12
+				hosts.each { |h| results.merge! self.analyze(h, domain: domain) }
13
+				results
12 14
 			end
13 15
 
14 16
 			def self.analyze_file(input, output)

+ 5
- 8
lib/cryptcheck/tls/xmpp.rb View File

@@ -16,14 +16,11 @@ module CryptCheck
16 16
 									when :c2s
17 17
 										['_xmpp-client', 5222]
18 18
 								end
19
-				srv = Resolv::DNS.new.getresources("#{service}._tcp.#{domain}", Resolv::DNS::Resource::IN::SRV)
20
-							  .sort_by(&:priority).first
21
-				if srv
22
-					hostname, port = srv.target.to_s, srv.port
23
-				else # DNS is not correctly set, guess config…
24
-					hostname = domain
25
-				end
26
-				self.analyze hostname, port, domain: domain, type: type
19
+				srv = Resolv::DNS.new.getresources("#{service}._tcp.#{domain}", Resolv::DNS::Resource::IN::SRV).sort_by &:priority
20
+				hosts = srv.empty? ? [[domain, port]] : srv.collect { |s| [s.target.to_s, s.port] }
21
+				results = {}
22
+				hosts.each { |host, port| results.merge! self.analyze(host, port, domain: domain, type: type) }
23
+				results
27 24
 			end
28 25
 
29 26
 			def self.analyze_file(input, output)

Loading…
Cancel
Save