Ejemplo n.º 1
0
 def handle(self):
     if SHOW_TIME:
         start_time = datetime.utcnow()
     uid = self.request.recv(UID_LEN)
     if len(uid) != UID_LEN:
         show_error(self, 'failed to handle, invalid head')
         return
     buf = self.request.recv(FLG_LEN)
     if len(buf) != FLG_LEN:
         show_error(self, 'failed to handle, invalid head')
         return
     flg, = struct.unpack('I', buf)
     if flg == FLG_SEC:
         if not self.server.rpcserver.user:
             show_error(self, 'user is not initialized')
             raise Exception('user is not initialized')
         key = self.server.cache_get(uid)
         if not key:
             key = self.server.rpcserver.user.get_private_key(uid)
             if not key:
                 show_error(self, 'failed to handle, invalid private key')
                 return
             key = rsa.PrivateKey.load_pkcs1(key)
             self.server.cache_update(uid, key)
         stream = Stream(self.request, uid=uid, key=key)
     else:
         stream = Stream(self.request)
     buf = stream.read()
     if buf:
         res = self.server.rpcserver.proc(buf)
         if flg == FLG_SEC:
             stream = Stream(self.request)
         stream.write(res)
     if SHOW_TIME:
         self._print('handle, time=%d sec' % (datetime.utcnow() - start_time).seconds)
Ejemplo n.º 2
0
 def handle(self):
     uid = self.request.recv(UID_LEN)
     if len(uid) != UID_LEN:
         show_error(self, 'failed to handle, invalid head')
         return
     addr = self._get_backend(uid)
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((addr, BACKEND_PORT))
     try:
         self._forward(uid, self.request, sock)
         stream = Stream(sock)
         _, _, res = stream.readall()
         stream = Stream(self.request)
         stream.write(res)
     finally:
         sock.close()
Ejemplo n.º 3
0
 def _request(self, addr, port, buf):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((addr, port))
     try:
         if self._key:
             stream = Stream(sock, uid=self._uid, key=self._key)
         else:
             stream = Stream(sock)
         stream.write(buf)
         if self._key:
             stream = Stream(sock)
         _, _, res = stream.readall()
         return res
     finally:
         sock.close()
Ejemplo n.º 4
0
 def handle(self):
     if SHOW_TIME:
         start_time = datetime.utcnow()
     uid = self.request.recv(UID_LEN)
     if len(uid) != UID_LEN:
         show_error(self, 'failed to handle, invalid head')
         return
     buf = self.request.recv(FLG_LEN)
     if len(buf) != FLG_LEN:
         show_error(self, 'failed to handle, invalid head')
         return
     flg, = struct.unpack('I', buf)
     if flg == FLG_SEC:
         if not self.server.rpcserver.user:
             show_error(self, 'user is not initialized')
             raise Exception('user is not initialized')
         key = self.server.cache_get(uid)
         if not key:
             key = self.server.rpcserver.user.get_private_key(uid)
             if not key:
                 show_error(self, 'failed to handle, invalid private key')
                 return
             key = rsa.PrivateKey.load_pkcs1(key)
             self.server.cache_update(uid, key)
         stream = Stream(self.request, uid=uid, key=key)
     else:
         stream = Stream(self.request)
     buf = stream.read()
     if buf:
         res = self.server.rpcserver.proc(buf)
         if flg == FLG_SEC:
             stream = Stream(self.request)
         stream.write(res)
     if SHOW_TIME:
         self._print('handle, time=%d sec' % (datetime.utcnow() - start_time).seconds)
Ejemplo n.º 5
0
 def handle(self):
     uid = self.request.recv(UID_LEN)
     if len(uid) != UID_LEN:
         show_error(self, 'failed to handle, invalid head')
         return
     addr = self._get_backend(uid)
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((addr, BACKEND_PORT))
     try:
         self._forward(uid, self.request, sock)
         stream = Stream(sock)
         _, _, res = stream.readall()
         stream = Stream(self.request)
         stream.write(res)
     finally:
         sock.close()
Ejemplo n.º 6
0
 def _request(self, addr, port, buf):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((addr, port))
     try:
         if self._key:
             stream = Stream(sock, uid=self._uid, key=self._key)
         else:
             stream = Stream(sock)
         stream.write( buf)
         if self._key:
             stream = Stream(sock)
         _, _, res = stream.readall()
         return res
     finally:
         sock.close()