Browse Source

Manage host

aeris 2 years ago
parent
commit
366a078dee

+ 2
- 11
lib/cryptcheck.rb View File

@@ -4,20 +4,9 @@ require 'timeout'
4 4
 require 'yaml'
5 5
 
6 6
 module CryptCheck
7
-	MAX_ANALYSIS_DURATION = 600
8 7
 	PARALLEL_ANALYSIS     = 10
9 8
 
10
-	class AnalysisFailure
11
-		attr_reader :error
12 9
 
13
-		def initialize(error)
14
-			@error = error
15
-		end
16
-
17
-		def to_s
18
-			@error.to_s
19
-		end
20
-	end
21 10
 
22 11
 	class NoTLSAvailableServer
23 12
 		attr_reader :server
@@ -43,11 +32,13 @@ module CryptCheck
43 32
 		autoload :TcpServer, 'cryptcheck/tls/server'
44 33
 		autoload :UdpServer, 'cryptcheck/tls/server'
45 34
 		autoload :Grade, 'cryptcheck/tls/grade'
35
+		autoload :Host, 'cryptcheck/tls/host'
46 36
 
47 37
 		autoload :Https, 'cryptcheck/tls/https'
48 38
 		module Https
49 39
 			autoload :Server, 'cryptcheck/tls/https/server'
50 40
 			autoload :Grade, 'cryptcheck/tls/https/grade'
41
+			autoload :Host, 'cryptcheck/tls/https/host'
51 42
 		end
52 43
 
53 44
 		autoload :Xmpp, 'cryptcheck/tls/xmpp'

+ 47
- 0
lib/cryptcheck/tls/host.rb View File

@@ -0,0 +1,47 @@
1
+require 'timeout'
2
+
3
+module CryptCheck
4
+	module Tls
5
+		class AnalysisFailure
6
+			attr_reader :error
7
+
8
+			def initialize(error)
9
+				@error = error
10
+			end
11
+
12
+			def to_s
13
+				@error.to_s
14
+			end
15
+		end
16
+
17
+		class TooLongAnalysis < AnalysisFailure
18
+			def initialize
19
+				super "Too long analysis (max #{Host::MAX_ANALYSIS_DURATION.humanize})"
20
+			end
21
+		end
22
+
23
+		class Host
24
+			MAX_ANALYSIS_DURATION = 600
25
+
26
+			attr_reader :servers
27
+
28
+			def initialize
29
+				first    = true
30
+				@servers = resolve.collect do |args|
31
+					first ? (first = false) : Logger.info { '' }
32
+					result = begin
33
+						server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
34
+							server(*args)
35
+						end
36
+						grade(server)
37
+					rescue Engine::TLSException => e
38
+						AnalysisFailure.new e
39
+					rescue ::Timeout::Error
40
+						TooLongAnalysis.new
41
+					end
42
+					[args, result]
43
+				end.to_h
44
+			end
45
+		end
46
+	end
47
+end

+ 31
- 0
lib/cryptcheck/tls/https/host.rb View File

@@ -0,0 +1,31 @@
1
+module CryptCheck
2
+	module Tls
3
+		module Https
4
+			class Host < Tls::Host
5
+				def initialize(hostname, port=443)
6
+					@hostname, @port = hostname, port
7
+					super()
8
+				end
9
+
10
+				private
11
+				def resolve
12
+					begin
13
+						ip = IPAddr.new @hostname
14
+						return [[nil, ip.to_s, ip.family]]
15
+					rescue IPAddr::InvalidAddressError
16
+					end
17
+					::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
18
+							.collect { |a| [@hostname, a.ip_address, a.afamily] }
19
+				end
20
+
21
+				def server(hostname, ip, family)
22
+					Https::Server.new hostname, ip, family, @port
23
+				end
24
+
25
+				def grade(server)
26
+					Https::Grade.new server
27
+				end
28
+			end
29
+		end
30
+	end
31
+end

+ 1
- 1
lib/cryptcheck/tls/https/server.rb View File

@@ -6,7 +6,7 @@ module CryptCheck
6 6
 			class Server < Tls::TcpServer
7 7
 				attr_reader :hsts
8 8
 
9
-				def initialize(hostname, family, ip, port=443)
9
+				def initialize(hostname, ip, family, port=443)
10 10
 					super
11 11
 					fetch_hsts
12 12
 				end

Loading…
Cancel
Save