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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. }
  21. if header = response.headers['strict-transport-security']
  22. name, value = header.split '='
  23. if name == 'max-age'
  24. @hsts = value.to_i
  25. Logger.info { "HSTS : #{@hsts.to_s.colorize hsts_long? ? :green : nil}" }
  26. return
  27. end
  28. end
  29. rescue
  30. end
  31. Logger.info { 'No HSTS'.colorize :yellow }
  32. @hsts = nil
  33. end
  34. def hsts?
  35. !@hsts.nil?
  36. end
  37. def hsts_long?
  38. hsts? and @hsts >= 6*30*24*60*60
  39. end
  40. end
  41. end
  42. end
  43. end