Skip to content

Instantly share code, notes, and snippets.

@wpK
Created February 7, 2013 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpK/17de0b9a2c03b517afea to your computer and use it in GitHub Desktop.
Save wpK/17de0b9a2c03b517afea to your computer and use it in GitHub Desktop.
Example code to reproduce Ejabberd bug EJAB-1382 (https://support.process-one.net/browse/EJAB-1382)
#!/usr/bin/env python
"""
Usage: ejabberd_compression_test.py <server> <port> <username> <password>
Example code to reproduce Ejabberd bug EJAB-1382 (https://support.process-one.net/browse/EJAB-1382)
"""
import sys
import socket
import ssl
import base64
def main(args):
if len(args) != 4:
print "Usage: ejabberd_compression_test.py <server> <port> <username> <password>"
# Initialize parameter args
server = args[0]
port = int(args[1])
username = args[2]
password = args[3]
try:
# Initialize new socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server
print "Connecting to XMPP server %s on port %d..." % (server, port)
s.connect((server, port))
print "Successfully connected to server."
# Send initial steam
send(s, "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>" % server)
# Read server stream
print "[R] %s" % s.recv(2048)
# Read server features
print "[R] %s" % s.recv(2048)
# Send starttls
send(s, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>")
# Read proceed response
print "[R] %s" % s.recv(2048)
#
# TLS Negotiation
# http://xmpp.org/rfcs/rfc6120.html#tls
#
# Wrap socket with SSL
ssl_sock = ssl.wrap_socket(s)
# Write new (SSL) stream
sendssl(ssl_sock, "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>" % server)
# Read server stream
print "[R] %s" % ssl_sock.read()
# Read server features
print "[R] %s" % ssl_sock.read()
#
# SASL Negotiation
# http://xmpp.org/rfcs/rfc6120.html#sasl
#
# Send auth
sendssl(ssl_sock, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN' client-uses-full-bind-result='true'>%s</auth>" % base64.b64encode("\0" + username + "\0" + password))
# Read success
print "[R] %s" % ssl_sock.read()
# Write new stream
sendssl(ssl_sock, "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>" % server)
# Read stream
print "[R] %s" % ssl_sock.read()
# Read server features
print "[R] %s" % ssl_sock.read()
#
# Stream Compression
# http://xmpp.org/extensions/xep-0138.html
#
# Send compression
sendssl(ssl_sock, "<compress xmlns='http://jabber.org/protocol/compress'><method>zlib</method></compress>")
# Read compressed
print "[R] %s" % ssl_sock.read()
#
# Resource Binding
# http://xmpp.org/rfcs/rfc6120.html#bind
#
# Send bind
sendssl(ssl_sock, "<iq type='set' id='bind'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>")
# Read bind result
print "[R] %s" % ssl_sock.read()
# Close stream
send(s, "</stream:stream>")
# Read close
print "[R] %s" % s.recv(2048)
# Close socket
s.close()
except socket.error, msg:
if s != None:
s.close()
print "Unable to connect to chat server. Reason: %s" % msg
raw_input()
except KeyboardInterrupt:
if s != None:
s.close()
def send(s, data):
print "[S] %s" % data
s.sendall(data)
def sendssl(s, data):
print "[S] %s" % data
s.write(data)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment