<s> import uuid <EOL> import re <EOL> import hashlib <EOL> import base64 <EOL> from twisted . internet . protocol import Protocol as BaseProtocol <EOL> from exception import FrameError , ProtocolError <EOL> from frame import Frame <EOL> import random <EOL> __VERSION__ = "<STR_LIT>" <EOL> __DESCRIPTION__ = "<STR_LIT>" <EOL> handshake = '<STR_LIT>' <EOL> class Protocol ( BaseProtocol , object ) : <EOL> def __init__ ( self , users = { } ) : <EOL> self . bufferIn = "<STR_LIT>" <EOL> self . bufferOut = "<STR_LIT>" <EOL> self . users = users <EOL> self . id = str ( uuid . uuid4 ( ) ) <EOL> self . users [ self . id ] = self <EOL> self . websocket_ready = False <EOL> def buildHandcheck ( self ) : <EOL> buf = self . bufferIn <EOL> pos = buf . find ( "<STR_LIT>" ) <EOL> if pos == - <NUM_LIT:1> : <EOL> raise ProtocolError ( "<STR_LIT>" ) <EOL> self . onHandshake ( buf ) <EOL> cmd = buf [ : pos + <NUM_LIT:5> ] <EOL> self . bufferIn = buf [ pos + <NUM_LIT:4> : ] <EOL> key = re . search ( "<STR_LIT>" , cmd ) <EOL> if not key : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> key = key . group ( <NUM_LIT:1> ) <EOL> self . key = key <EOL> key = key + '<STR_LIT>' <EOL> key = base64 . b64encode ( hashlib . sha1 ( key ) . digest ( ) ) <EOL> return handshake % key <EOL> def sendHandcheck ( self ) : <EOL> try : <EOL> hc = self . buildHandcheck ( ) <EOL> except ProtocolError : <EOL> pass <EOL> except Exception : <EOL> self . transport . abortConnection ( ) <EOL> else : <EOL> self . transport . write ( hc ) <EOL> self . websocket_ready = True <EOL> def dataReceived ( self , data ) : <EOL> self . bufferIn += data <EOL> if not self . websocket_ready : <EOL> self . sendHandcheck ( ) <EOL> self . onConnect ( ) <EOL> else : <EOL> try : <EOL> frame = Frame ( self . bufferIn ) <EOL> except FrameError , e : <EOL> pass <EOL> else : <EOL> f_len = frame . length ( ) <EOL> self . bufferIn = self . bufferIn [ f_len : ] <EOL> self . onMessage ( frame . message ( ) ) <EOL> def connectionLost ( self , * args , ** kwargs ) : <EOL> _id = self . id <EOL> if self . id in self . users : <EOL> del self . users [ self . id ] <EOL> self . onDisconnect ( ) <EOL> def loseConnection ( self ) : <EOL> _id = self . id <EOL> if self . id in self . users : <EOL> del self . users [ self . id ] <EOL> self . onDisconnect ( ) <EOL> self . transport . loseConnection ( ) <EOL> def abortConnection ( self ) : <EOL> _id = self . id <EOL> if self . id in self . users : <EOL> del self . users [ self . id ] <EOL> self . onDisconnect ( ) <EOL> self . transport . abortConnection ( ) <EOL> def sendMessage ( self , msg ) : <EOL> self . bufferOut += Frame . buildMessage ( msg , mask = False ) <EOL> if not self . websocket_ready : <EOL> return <EOL> self . transport . write ( self . bufferOut ) <EOL> self . bufferOut = "<STR_LIT>" <EOL> def onHandshake ( self , header ) : <EOL> pass <EOL> def onConnect ( self ) : <EOL> pass <EOL> def onDisconnect ( self ) : <EOL> pass <EOL> def onMessage ( self , msg ) : <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from twisted . internet . protocol import Factory <EOL> from twisted . internet import reactor , ssl <EOL> import sys <EOL> import argparse <EOL> class WebSocketHandler ( Protocol ) : <EOL> def onHandshake ( self , header ) : <EOL> g = re . search ( '<STR_LIT>' , header ) <EOL> if not g : return <EOL> print "<STR_LIT>" % ( self . id , g . group ( <NUM_LIT:1> ) ) <EOL> def onConnect ( self ) : <EOL> print "<STR_LIT>" % self . id <EOL> for _id , user in self . users . items ( ) : <EOL> user . sendMessage ( "<STR_LIT>" % self . id ) <EOL> print "<STR_LIT>" % ( self . id , _id , self . id ) <EOL> def onDisconnect ( self ) : <EOL> print "<STR_LIT>" % self . id <EOL> for _id , user in self . users . items ( ) : <EOL> user . sendMessage ( "<STR_LIT>" % self . id ) <EOL> print "<STR_LIT>" % ( self . id , _id , self . id ) <EOL> def onMessage ( self , msg ) : <EOL> for _id , user in self . users . items ( ) : <EOL> user . sendMessage ( msg ) <EOL> print "<STR_LIT>" % ( self . id , _id , msg ) <EOL> class WebSocketFactory ( Factory ) : <EOL> def __init__ ( self ) : <EOL> self . users = { } <EOL> def buildProtocol ( self , addr ) : <EOL> return WebSocketHandler ( self . users ) <EOL> parser = argparse . ArgumentParser ( <EOL> description = __DESCRIPTION__ , <EOL> prog = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , type = int , default = <NUM_LIT> ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" ) <EOL> options = parser . parse_args ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> if options . version : <EOL> print __VERSION__ <EOL> exit ( <NUM_LIT:0> ) <EOL> if options . ssl == False : <EOL> reactor . listenTCP ( options . port , WebSocketFactory ( ) ) <EOL> print "<STR_LIT>" % options . port <EOL> else : <EOL> if not options . key or not options . cert : <EOL> parser . print_help ( ) <EOL> exit ( - <NUM_LIT:1> ) <EOL> with open ( options . key ) as keyFile : <EOL> with open ( options . cert ) as certFile : <EOL> cert = ssl . PrivateCertificate . loadPEM ( keyFile . read ( ) + certFile . read ( ) ) <EOL> reactor . listenSSL ( options . port , WebSocketFactory ( ) , cert . options ( ) ) <EOL> print "<STR_LIT>" % options . port <EOL> reactor . run ( ) </s>
