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 890B

1234567891011121314151617181920212223242526272829303132333435
  1. require 'resolv'
  2. module CryptCheck
  3. module Tls
  4. module Smtp
  5. class Server < Tls::TcpServer
  6. RESOLVER = Resolv::DNS.new
  7. attr_reader :domain
  8. def initialize(domain, port=25)
  9. @domain = domain
  10. srv = RESOLVER.getresources(domain, Resolv::DNS::Resource::IN::MX).sort_by(&:preference).first
  11. if srv
  12. hostname = srv.exchange.to_s
  13. else # DNS is not correctly set, guess config…
  14. hostname = domain
  15. end
  16. super hostname, port
  17. end
  18. def ssl_connect(socket, context, method, &block)
  19. socket.recv 1024
  20. socket.write "EHLO #{Socket.gethostbyname(Socket.gethostname).first}\r\n"
  21. features = socket.recv(1024).split "\r\n"
  22. starttls = features.find { |f| /250[- ]STARTTLS/ =~ f }
  23. raise TLSNotAvailableException unless starttls
  24. socket.write "STARTTLS\r\n"
  25. socket.recv 1024
  26. super
  27. end
  28. end
  29. end
  30. end
  31. end