diff --git a/Gemfile b/Gemfile index 14585f4..e229aa4 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,8 @@ source 'https://rubygems.org' gem 'httparty' gem 'nokogiri' -gem 'net-sftp' +gem 'net-ssh', '>= 2.9.2.beta' +gem 'net-scp' gem 'tcp_timeout' gem 'parallel' gem 'ruby-progressbar' diff --git a/lib/sslcheck.rb b/lib/sslcheck.rb index 28aab2c..1897251 100644 --- a/lib/sslcheck.rb +++ b/lib/sslcheck.rb @@ -2,6 +2,7 @@ require 'erb' require 'logging' require 'parallel' require 'thread' +require 'yaml' module SSLCheck module SSLLabs @@ -10,10 +11,12 @@ module SSLCheck autoload :Server, 'sslcheck/server' autoload :Grade, 'sslcheck/grade' + PARALLEL_ANALYSIS = 20 + SYN_TIMEOUT = 600 @@log = Logging.logger[SSLCheck] def self.grade(hostname, port=443) - timeout 600 do + timeout SYN_TIMEOUT do Grade.new Server.new hostname, port end rescue Exception => e @@ -21,10 +24,11 @@ module SSLCheck NoSslTlsGrade.new NoSslTlsServer.new hostname, port end - def self.analyze(hosts, output) + def self.analyze(hosts, output, groups = nil) results = {} semaphore = Mutex.new - Parallel.each hosts, progress: 'Testing', in_threads: 10 do |description, host| + Parallel.each hosts, progress: 'Analysing', in_threads: PARALLEL_ANALYSIS, + finish: lambda { |item, _, _| puts item[1] } do |description, host| result = SSLCheck.grade host.strip semaphore.synchronize do if results.include? description @@ -35,6 +39,8 @@ module SSLCheck end end + results = Hash[groups.collect { |g| [g, results[g]] }] if groups + results.each do |d, _| results[d].sort! do |a, b| cmp = score(a) <=> score(b) @@ -48,7 +54,19 @@ module SSLCheck end end - File.write "output/#{output}.html", ERB.new(File.read('output/sslcheck.erb')).result(binding) + File.write output, ERB.new(File.read('output/sslcheck.erb')).result(binding) + end + + def self.analyze_from_file(file, output) + config = YAML.load_file file + hosts = [] + groups = [] + config.each do |c| + d, hs = c['description'], c['hostnames'] + groups << d + hs.each { |host| hosts << [d, host] } + end + self.analyze hosts, output, groups end private diff --git a/sslcheck b/sslcheck index 8fc3e26..e329bcb 100755 --- a/sslcheck +++ b/sslcheck @@ -1,11 +1,10 @@ #!/usr/bin/env ruby -ENV['LD_LIBRARY_PATH'] = '/home/aeris/Workspace/external/sslscan/openssl' -require 'logging' $:.unshift 'lib' +require 'logging' require 'sslcheck' Logging.logger.root.appenders = Logging.appenders.stdout Logging.logger.root.level = :warn -p server = SSLCheck::Server.new(ARGV[0]) +server = SSLCheck::Server.new(ARGV[0], ARGV[1] || 443) p grade = SSLCheck::Grade.new(server) diff --git a/sslcheck-alexa b/sslcheck-alexa index e5e9f62..d980960 100755 --- a/sslcheck-alexa +++ b/sslcheck-alexa @@ -16,4 +16,4 @@ File.open('top-1m.csv', 'r') do |file| end end -SSLCheck.analyze hosts, 'alexa' +SSLCheck.analyze hosts, 'output/alexa.html' diff --git a/sslcheck-all b/sslcheck-all index 268a62b..f9665b9 100755 --- a/sslcheck-all +++ b/sslcheck-all @@ -1,16 +1,5 @@ #!/usr/bin/env ruby -require 'yaml' $:.unshift 'lib' require 'sslcheck' - -Logging.logger.root.appenders = Logging.appenders.stdout -Logging.logger.root.level = :error - -config = YAML.load_file 'hosts.yml' -hosts = [] -config.each do |c| - d, hs = c['description'], c['hostnames'] - hs.each { |host| hosts << [d, host] } -end - -SSLCheck.analyze hosts, 'results' +name = ARGV[0] || 'index' +SSLCheck.analyze_from_file "output/#{name}.yml", "output/#{name}.html"