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.
cryptcheck/lib/cryptcheck/tls/xmpp/server.rb

60 lines
1.8 KiB

8 years ago
require 'nokogiri'
module CryptCheck
module Tls
module Xmpp
TLS_NAMESPACE = 'urn:ietf:params:xml:ns:xmpp-tls'
8 years ago
class Server < Tls::TcpServer
8 years ago
attr_reader :domain
7 years ago
def initialize(hostname, family, ip, port=nil, domain: nil, type: :s2s)
domain ||= hostname
8 years ago
@type, @domain = type, domain
port = case type
when :s2s
5269
when :c2s
5222
end unless port
super family, ip, port, hostname: hostname
7 years ago
Logger.info { '' }
Logger.info { self.required? ? 'Required'.colorize(:green) : 'Not required'.colorize(:yellow) }
8 years ago
end
def ssl_connect(socket, context, method, &block)
8 years ago
type = case @type
when :s2s then
'jabber:server'
when :c2s then
'jabber:client'
end
8 years ago
socket.write "<?xml version='1.0' ?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='#{type}' to='#{@domain}' version='1.0'>"
response = ''
loop do
response += socket.recv 1024
xml = ::Nokogiri::XML response
error = xml.xpath '//stream:error'
raise Exception, error.text unless error.empty?
8 years ago
unless xml.xpath('//stream:features').empty?
response = xml
break
end
end
8 years ago
starttls = response.xpath '//tls:starttls', tls: TLS_NAMESPACE
raise TLSNotAvailableException unless starttls
@required = !starttls.xpath('//tls:required', tls: TLS_NAMESPACE).nil?
socket.write "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls' />\r\n"
response = ::Nokogiri::XML socket.recv 4096
raise TLSNotAvailableException unless response.xpath '//tls:proceed', tls: TLS_NAMESPACE
super
end
def required?
@required
end
end
end
end
end