示例#1
0
文件: phprpc.py 项目: mlzboy/resys
 def __key_exchange(self, body, sessionService, request, hash, callback, encode, encrypt, keylen):
     if encrypt == True:
         keylen, encrypt = DHParams.get(keylen)
         x = random.randint(1 << (keylen - 2), 1 << (keylen - 1))
         hash['x'] = str(x)
         hash['p'] = encrypt['p']
         hash['keylen'] = keylen
         encrypt['y'] = str(pow(long(encrypt['g']), x, long(encrypt['p'])))
         body.append('phprpc_encrypt="%s";' % self.__encode_string(phpformat.serialize(encrypt), encode))
         if keylen != 128:
             body.append('phprpc_keylen="%s";' % keylen)
         if sessionService:
             encodeURL = getattr(sessionService, 'encodeURL', None)
             if encodeURL:
                 sessionService.encodesSessionInURL = True
                 body.append('phprpc_url="%s";' % self.__encode_string(encodeURL(request.url), encode))
                 sessionService.encodesSessionInURL = False
     else:
         y = encrypt
         x = long(hash['x'])
         p = long(hash['p'])
         key = pow(y, x, p)
         if keylen == 128:
             key = hex(key)[2:-1].rjust(32, '0')
             hash['key'] = ''.join((chr(int(key[i*2:i*2+2], 16)) for i in xrange(16)))
         else:
             hash['key'] = md5.new(str(key)).digest()
     body.append(callback)
示例#2
0
 def __key_exchange(self, body, sessionService, request, hash, callback,
                    encode, encrypt, keylen):
     if encrypt == True:
         keylen, encrypt = DHParams.get(keylen)
         x = random.randint(1 << (keylen - 2), 1 << (keylen - 1))
         hash['x'] = str(x)
         hash['p'] = encrypt['p']
         hash['keylen'] = keylen
         encrypt['y'] = str(pow(long(encrypt['g']), x, long(encrypt['p'])))
         body.append(
             'phprpc_encrypt="%s";' %
             self.__encode_string(phpformat.serialize(encrypt), encode))
         if keylen != 128:
             body.append('phprpc_keylen="%s";' % keylen)
         if sessionService:
             encodeURL = getattr(sessionService, 'encodeURL', None)
             if encodeURL:
                 sessionService.encodesSessionInURL = True
                 body.append(
                     'phprpc_url="%s";' %
                     self.__encode_string(encodeURL(request.url), encode))
                 sessionService.encodesSessionInURL = False
     else:
         y = encrypt
         x = long(hash['x'])
         p = long(hash['p'])
         key = pow(y, x, p)
         if keylen == 128:
             key = hex(key)[2:-1].rjust(32, '0')
             hash['key'] = ''.join(
                 (chr(int(key[i * 2:i * 2 + 2], 16)) for i in xrange(16)))
         else:
             hash['key'] = md5.new(str(key)).digest()
     body.append(callback)
示例#3
0
文件: phprpc.py 项目: mlzboy/resys
 def __invoke(self, name, args, byRef, encryptmode):
     data = {'result' : None, 'warning' : None, 'output' : ''}
     try:
         try :
             if encryptmode == None:
                 encryptmode = self.__encryptmode
             self.__lock1.acquire()
             try:
                 encryptmode = self.__key_exchange(encryptmode)
             finally:
                 self.__lock1.release()
             result = self.__post("phprpc_func=%s&phprpc_args=%s&phprpc_encrypt=%s&phprpc_ref=%s" % (
                 name,
                 base64.b64encode(self.__encrypt(phpformat.serialize(args), 1, encryptmode)).replace('+', '%2B'),
                 encryptmode,
                 str(byRef).lower()
             ))
             if result.has_key('phprpc_errstr') and result.has_key('phprpc_errno'):
                 if (int(result['phprpc_errno']) == 0):
                     warning = None
                 else:
                     warning = PHPRPC_Error(int(result['phprpc_errno']), base64.b64decode(result['phprpc_errstr']))
             elif result.has_key('phprpc_functions'):
                 warning = PHPRPC_Error(1, "PHPRPC server haven't received the POST data!")
             else:
                 warning = PHPRPC_Error(1, "PHPRPC server occured unknown error!")
             data['warning'] = warning
             if result.has_key('phprpc_output'):
                 output = base64.b64decode(result['phprpc_output'])
                 if self.__server_version >= 3: output = self.__decrypt(output, 3, encryptmode)
             else:
                 output = ''
             data['output'] = output
             if result.has_key('phprpc_result'):
                 if result.has_key('phprpc_args'):
                     #arguments = phpformat.unserialize(self.__decrypt(base64.b64decode(result['phprpc_arg']), 1, encryptmode))
                     arguments = phpformat.unserialize(self.__decrypt(base64.b64decode(result['phprpc_args']), 1, encryptmode))
                     #for key in arguments: args[key] = arguments[key]
                     if isinstance(args, types.ListType) :
                         for key in arguments: args[key] = arguments[key]
                 data['result'] = phpformat.unserialize(self.__decrypt(base64.b64decode(result['phprpc_result']), 2, encryptmode))
             else:
                 data['result'] = warning
         except PHPRPC_Error, e:
             data['result'] = e
         except Exception, ex:
             if ex is types.StringType:
                 data['result'] = PHPRPC_Error(1, ex)
             else:
                 e = tuple(ex)
                 if (len(e) == 2) and (type(e[0]) is types.IntType) and (type(e[1]) is types.StringType):
                     data['result'] = PHPRPC_Error(e[0], e[1])
                 else:
                     data['result'] = PHPRPC_Error(1, str(ex))
示例#4
0
文件: phprpc.py 项目: mlzboy/resys
 def __call(self, environ):
     sessionService = environ.get(self.sessionName, None)
     if sessionService:
         session = getattr(sessionService, 'session', sessionService)
     else:
         session = None
     request = Request(environ)
     body = []
     callback = ''
     encode = True
     try:
         try :
             callback = base64.b64decode(request['phprpc_callback'])
             encode = self.__bool(request['phprpc_encode'])
             encrypt = self.__encrypt(request['phprpc_encrypt'])
             cid = "phprpc_%s" % (request['phprpc_id'] or '0')
             if request['phprpc_func']:
                 func = request['phprpc_func'].lower()
                 if self.__methods.has_key(func):
                     key = None
                     if session:
                         hash = self.__session(session, cid)
                         if hash.has_key('key'):
                             key = hash['key']
                         elif encrypt > 0:
                             encrypt = 0
                             raise Exception("Can't find the key for decryption.")
                     else:
                         encrypt = 0
                     ref = self.__bool(request['phprpc_ref'])
                     args = self.__args(request['phprpc_args'], key, encrypt)
                     if hasattr(self.__methods[func], 'func_code'):
                         func_code = self.__methods[func].func_code
                         has_session_args = (func_code.co_argcount > 0) and (func_code.co_varnames[func_code.co_argcount - 1] == 'session')
                         if has_session_args:
                             args.insert(func_code.co_argcount - 1, session)
                     result = self.__encode_string(self.__encrypt_string(
                         phpformat.serialize(self.__methods[func](*args)),
                     key, 2, encrypt), encode)
                     body.append('phprpc_result="%s";' % result)
                     if ref:
                         if has_session_args:
                             del args[func_code.co_argcount - 1]
                         args = self.__encode_string(self.__encrypt_string(
                             phpformat.serialize(args),
                         key, 1, encrypt), encode)
                         body.append('phprpc_args="%s";' % args)
                 else:
                     raise Exception("Can't find this function %s()." % func)
                 self.__write_error(body, 0, '', callback, encode)
             elif (encrypt != False) and (encrypt != 0) and (session != None):
                 hash = self.__session(session, cid)
                 keylen = self.__keylength(request['phprpc_keylen'], hash)
                 self.__key_exchange(body, sessionService, request, hash, callback, encode, encrypt, keylen)
                 self.__session(session, cid, hash)
             else:
                 self.__write_functions(body, callback, encode)
         except Exception, e:
             body = []
             if self.debug:
                 self.__write_error(body, 1, ''.join(traceback.format_exception(*sys.exc_info())), callback, encode)
             else:
                 self.__write_error(body, 1, e.message, callback, encode)
     finally:
         if session:
             if hasattr(session, 'save'):
                 session.save()
         return ['200 OK', self.__headers(), ['\r\n'.join(body)]]
示例#5
0
文件: phprpc.py 项目: mlzboy/resys
 def __write_functions(self, body, callback, encode):
     body.append('phprpc_functions="%s";' % self.__encode_string(phpformat.serialize(self.__methods.keys()), encode))
     body.append(callback)
示例#6
0
文件: phprpc.py 项目: mlzboy/resys
 def __session(self, session, cid, hash = None):
     if hash:
         session[cid] = phpformat.serialize(hash)
     else:
         return phpformat.unserialize(session.get(cid, 'a:0:{}'))
示例#7
0
 def __call(self, environ):
     sessionService = environ.get(self.sessionName, None)
     if sessionService:
         session = getattr(sessionService, 'session', sessionService)
     else:
         session = None
     request = Request(environ)
     body = []
     callback = ''
     encode = True
     try:
         try:
             callback = base64.b64decode(request['phprpc_callback'])
             encode = self.__bool(request['phprpc_encode'])
             encrypt = self.__encrypt(request['phprpc_encrypt'])
             cid = "phprpc_%s" % (request['phprpc_id'] or '0')
             if request['phprpc_func']:
                 func = request['phprpc_func'].lower()
                 if self.__methods.has_key(func):
                     key = None
                     if session:
                         hash = self.__session(session, cid)
                         if hash.has_key('key'):
                             key = hash['key']
                         elif encrypt > 0:
                             encrypt = 0
                             raise Exception(
                                 "Can't find the key for decryption.")
                     else:
                         encrypt = 0
                     ref = self.__bool(request['phprpc_ref'])
                     args = self.__args(request['phprpc_args'], key,
                                        encrypt)
                     if hasattr(self.__methods[func], 'func_code'):
                         func_code = self.__methods[func].func_code
                         has_session_args = (func_code.co_argcount >
                                             0) and (func_code.co_varnames[
                                                 func_code.co_argcount - 1]
                                                     == 'session')
                         if has_session_args:
                             args.insert(func_code.co_argcount - 1, session)
                     result = self.__encode_string(
                         self.__encrypt_string(
                             phpformat.serialize(
                                 self.__methods[func](*args)), key, 2,
                             encrypt), encode)
                     body.append('phprpc_result="%s";' % result)
                     if ref:
                         if has_session_args:
                             del args[func_code.co_argcount - 1]
                         args = self.__encode_string(
                             self.__encrypt_string(
                                 phpformat.serialize(args), key, 1,
                                 encrypt), encode)
                         body.append('phprpc_args="%s";' % args)
                 else:
                     raise Exception("Can't find this function %s()." %
                                     func)
                 self.__write_error(body, 0, '', callback, encode)
             elif (encrypt != False) and (encrypt != 0) and (session !=
                                                             None):
                 hash = self.__session(session, cid)
                 keylen = self.__keylength(request['phprpc_keylen'], hash)
                 self.__key_exchange(body, sessionService, request, hash,
                                     callback, encode, encrypt, keylen)
                 self.__session(session, cid, hash)
             else:
                 self.__write_functions(body, callback, encode)
         except Exception, e:
             body = []
             if self.debug:
                 self.__write_error(
                     body, 1,
                     ''.join(traceback.format_exception(*sys.exc_info())),
                     callback, encode)
             else:
                 self.__write_error(body, 1, e.message, callback, encode)
     finally:
         if session:
             if hasattr(session, 'save'):
                 session.save()
         return ['200 OK', self.__headers(), ['\r\n'.join(body)]]
示例#8
0
 def __write_functions(self, body, callback, encode):
     body.append('phprpc_functions="%s";' % self.__encode_string(
         phpformat.serialize(self.__methods.keys()), encode))
     body.append(callback)
示例#9
0
 def __session(self, session, cid, hash=None):
     if hash:
         session[cid] = phpformat.serialize(hash)
     else:
         return phpformat.unserialize(session.get(cid, 'a:0:{}'))
示例#10
0
 def __invoke(self, name, args, byRef, encryptmode):
     data = {'result': None, 'warning': None, 'output': ''}
     try:
         try:
             if encryptmode == None:
                 encryptmode = self.__encryptmode
             self.__lock1.acquire()
             try:
                 encryptmode = self.__key_exchange(encryptmode)
             finally:
                 self.__lock1.release()
             result = self.__post(
                 "phprpc_func=%s&phprpc_args=%s&phprpc_encrypt=%s&phprpc_ref=%s"
                 % (name,
                    base64.b64encode(
                        self.__encrypt(phpformat.serialize(args), 1,
                                       encryptmode)).replace('+', '%2B'),
                    encryptmode, str(byRef).lower()))
             if result.has_key('phprpc_errstr') and result.has_key(
                     'phprpc_errno'):
                 if (int(result['phprpc_errno']) == 0):
                     warning = None
                 else:
                     warning = PHPRPC_Error(
                         int(result['phprpc_errno']),
                         base64.b64decode(result['phprpc_errstr']))
             elif result.has_key('phprpc_functions'):
                 warning = PHPRPC_Error(
                     1, "PHPRPC server haven't received the POST data!")
             else:
                 warning = PHPRPC_Error(
                     1, "PHPRPC server occured unknown error!")
             data['warning'] = warning
             if result.has_key('phprpc_output'):
                 output = base64.b64decode(result['phprpc_output'])
                 if self.__server_version >= 3:
                     output = self.__decrypt(output, 3, encryptmode)
             else:
                 output = ''
             data['output'] = output
             if result.has_key('phprpc_result'):
                 if result.has_key('phprpc_args'):
                     #arguments = phpformat.unserialize(self.__decrypt(base64.b64decode(result['phprpc_arg']), 1, encryptmode))
                     arguments = phpformat.unserialize(
                         self.__decrypt(
                             base64.b64decode(result['phprpc_args']), 1,
                             encryptmode))
                     #for key in arguments: args[key] = arguments[key]
                     if isinstance(args, types.ListType):
                         for key in arguments:
                             args[key] = arguments[key]
                 data['result'] = phpformat.unserialize(
                     self.__decrypt(
                         base64.b64decode(result['phprpc_result']), 2,
                         encryptmode))
             else:
                 data['result'] = warning
         except PHPRPC_Error, e:
             data['result'] = e
         except Exception, ex:
             if ex is types.StringType:
                 data['result'] = PHPRPC_Error(1, ex)
             else:
                 e = tuple(ex)
                 if (len(e) == 2) and (type(e[0]) is types.IntType) and (
                         type(e[1]) is types.StringType):
                     data['result'] = PHPRPC_Error(e[0], e[1])
                 else:
                     data['result'] = PHPRPC_Error(1, str(ex))