You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.rb 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. require 'httparty'
  2. module CryptCheck
  3. module Tls
  4. module Https
  5. class Server < Tls::TcpServer
  6. attr_reader :hsts
  7. def initialize(hostname, family, ip, port=443)
  8. super
  9. fetch_hsts
  10. end
  11. def fetch_hsts
  12. port = @port == 443 ? '' : ":#{@port}"
  13. begin
  14. response = ::HTTParty.head "https://#{@hostname}#{port}/",
  15. {
  16. follow_redirects: false,
  17. verify: false,
  18. timeout: SSL_TIMEOUT,
  19. ssl_version: self.supported_protocols.first,
  20. ciphers: 'ALL:COMPLEMENTOFALL'
  21. }
  22. if header = response.headers['strict-transport-security']
  23. name, value = header.split '='
  24. if name == 'max-age'
  25. @hsts = value.to_i
  26. Logger.info { "HSTS : #{@hsts.to_s.colorize hsts_long? ? :good : nil}" }
  27. return
  28. end
  29. end
  30. rescue
  31. end
  32. Logger.info { 'No HSTS'.colorize :warning }
  33. @hsts = nil
  34. end
  35. def hsts?
  36. !@hsts.nil?
  37. end
  38. LONG_HSTS = 6*30*24*60*60
  39. def hsts_long?
  40. hsts? and @hsts >= LONG_HSTS
  41. end
  42. end
  43. end
  44. end
  45. end