Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

server.rb 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require 'nokogiri'
  2. module CryptCheck
  3. module Tls
  4. module Xmpp
  5. TLS_NAMESPACE = 'urn:ietf:params:xml:ns:xmpp-tls'
  6. class Server < Tls::TcpServer
  7. attr_reader :domain
  8. def initialize(hostname, family, ip, port=nil, domain: nil, type: :s2s)
  9. domain ||= hostname
  10. @type, @domain = type, domain
  11. port = case type
  12. when :s2s
  13. 5269
  14. when :c2s
  15. 5222
  16. end unless port
  17. super hostname, family, ip, port
  18. Logger.info { '' }
  19. Logger.info { self.required? ? 'Required'.colorize(:green) : 'Not required'.colorize(:yellow) }
  20. end
  21. def ssl_connect(socket, context, method, &block)
  22. type = case @type
  23. when :s2s then
  24. 'jabber:server'
  25. when :c2s then
  26. 'jabber:client'
  27. end
  28. socket.puts "<?xml version='1.0' ?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='#{type}' to='#{@domain}' version='1.0'>"
  29. response = ''
  30. loop do
  31. response += socket.recv 1024
  32. xml = ::Nokogiri::XML response
  33. error = xml.xpath '//stream:error'
  34. raise ConnectionError, error.first.child.to_s unless error.empty?
  35. unless xml.xpath('//stream:features').empty?
  36. response = xml
  37. break
  38. end
  39. end
  40. starttls = response.xpath '//tls:starttls', tls: TLS_NAMESPACE
  41. raise TLSNotAvailableException unless starttls
  42. @required = !starttls.xpath('//tls:required', tls: TLS_NAMESPACE).empty?
  43. socket.write "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />\r\n"
  44. response = ::Nokogiri::XML socket.recv 4096
  45. raise TLSNotAvailableException unless response.xpath '//tls:proceed', tls: TLS_NAMESPACE
  46. super
  47. end
  48. def required?
  49. @required
  50. end
  51. end
  52. end
  53. end
  54. end