Browse Source

Don't crash on error

new-scoring
aeris 4 months ago
parent
commit
b8d1e898a8
3 changed files with 99 additions and 102 deletions
  1. +2
    -3
      Gemfile
  2. +96
    -98
      lib/cryptcheck/tls/host.rb
  3. +1
    -1
      set-env

+ 2
- 3
Gemfile View File

@@ -3,7 +3,6 @@ source 'https://rubygems.org'
gemspec

group :test do
gem 'simplecov', require: false
gem 'fuubar', require: false
gem 'simplecov', require: false
gem 'fuubar', require: false
end


+ 96
- 98
lib/cryptcheck/tls/host.rb View File

@@ -3,114 +3,112 @@ AwesomePrint.force_colors = true
require 'timeout'

module CryptCheck
module Tls
class AnalysisFailure
attr_reader :error
module Tls
class AnalysisFailure
attr_reader :error

def initialize(error)
@error = error
end
def initialize(error)
@error = error
end

def to_s
@error.to_s
end
end
def to_s
@error.to_s
end
end

class TooLongAnalysis < AnalysisFailure
def initialize
super "Too long analysis (max #{Host::MAX_ANALYSIS_DURATION.humanize})"
end
end
class TooLongAnalysis < AnalysisFailure
def initialize
super "Too long analysis (max #{Host::MAX_ANALYSIS_DURATION.humanize})"
end
end

class Host
MAX_ANALYSIS_DURATION = 600
class Host
MAX_ANALYSIS_DURATION = 600

attr_reader :servers, :error
attr_reader :servers, :error

def initialize(hostname, port)
@hostname, @port = hostname, port
def initialize(hostname, port)
@hostname, @port = hostname, port

first = true
@servers = resolve.collect do |args|
_, ip = args
first ? (first = false) : Logger.info { '' }
result = begin
server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
server(*args)
end
Logger.info ''
Logger.info { "Grade : #{server.grade.to_s.colorize server.grade_status}" }
Logger.info { server.states.ai }
server
rescue Engine::TLSException, Engine::ConnectionError, Engine::Timeout => e
# Logger.error { e.backtrace }
Logger.error { e }
AnalysisFailure.new e
raise
rescue ::Timeout::Error
# Logger.error { e.backtrace }
Logger.error { e }
TooLongAnalysis.new
raise
end
[[@hostname, ip, @port], result]
end.to_h
rescue => e
Logger.error { e.backtrace }
Logger.error { e }
@error = e
raise
end
first = true
@servers = resolve.collect do |args|
_, ip = args
first ? (first = false) : Logger.info { '' }
result = begin
server = ::Timeout.timeout MAX_ANALYSIS_DURATION do
server(*args)
end
Logger.info ''
Logger.info { "Grade : #{server.grade.to_s.colorize server.grade_status}" }
Logger.info { server.states.ai }
server
rescue Engine::TLSException, Engine::ConnectionError, Engine::Timeout => e
# Logger.error { e.backtrace }
Logger.error { e }
{ error: AnalysisFailure.new(e) }
rescue ::Timeout::Error
# Logger.error { e.backtrace }
Logger.error { e }
{ error: TooLongAnalysis.new }
end
[[@hostname, ip, @port], result]
end.to_h
rescue => e
# Logger.error { e.backtrace }
Logger.error { e }
@error = e
# raise
end

def key
{ hostname: @hostname, port: @port }
end
def key
{ hostname: @hostname, port: @port }
end

def to_h
if @error
target = { error: @error }
else
target = @servers.collect do |host, server|
hostname, ip, port = host
host = {
hostname: hostname,
ip: ip,
port: port
}
case server
when Server
host[:handshakes] = server.to_h
host[:states] = server.states
host[:grade] = server.grade
else
host[:error] = server.to_s
end
host
end
end
target
end
def to_h
if @error
target = [self.key.merge(error: @error.to_s)]
else
target = @servers.collect do |host, server|
hostname, ip, port = host
host = {
hostname: hostname,
ip: ip,
port: port
}
case server
when Server
host[:handshakes] = server.to_h
host[:states] = server.states
host[:grade] = server.grade
else
host[:error] = server[:error].to_s
end
host
end
end
target
end

private
private

def resolve
begin
begin
ip = IPAddr.new @hostname
return [[nil, ip.to_s, ip.family, @port]]
rescue IPAddr::InvalidAddressError
end
::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
.collect { |a| [@hostname, a.ip_address, a.afamily, @port] }
end.reject do |family, *_|
(ENV['DISABLE_IPv6'] && family == Socket::AF_INET6) ||
(ENV['DISABLE_IPv4'] && family == Socket::AF_INET)
end
end
def resolve
begin
begin
ip = IPAddr.new @hostname
return [[nil, ip.to_s, ip.family, @port]]
rescue IPAddr::InvalidAddressError
end
::Addrinfo.getaddrinfo(@hostname, nil, nil, :STREAM)
.collect { |a| [@hostname, a.ip_address, a.afamily, @port] }
end.reject do |family, *_|
(ENV['DISABLE_IPv6'] && family == Socket::AF_INET6) ||
(ENV['DISABLE_IPv4'] && family == Socket::AF_INET)
end
end

def server(*args)
TcpServer.new *args
end
end
end
def server(*args)
TcpServer.new *args
end
end
end
end

+ 1
- 1
set-env View File

@@ -1,4 +1,4 @@
DIR="$(dirname "$(readlink -e "$0")")"
DIR="$(dirname "$(readlink -e "${BASH_SOURCE:-$0}")")"
export LIBRARY_PATH="$DIR/lib"
export C_INCLUDE_PATH="$DIR/build/openssl/include"
export CPLUS_INCLUDE_PATH="$DIR/build/openssl/include"


Loading…
Cancel
Save