def _LogThreadLocals(out): out.write('Thread Locals:') if hasattr(__builtin__, 'session'): out.write(' session was ') out.write(str(session)) else: out.write('sorry, no session for ya.') try: ctk = localstorage.GetLocalStorage().get('calltimer.key', None) except NameError: return if ctk: out.write(' calltimer.key was ') out.write(ctk) currentcall = localstorage.GetLocalStorage().get('base.currentcall', None) if currentcall: try: currentcall = currentcall() out.write(' currentcall was: ') out.write(str(currentcall)) except ReferenceError: pass out.write('\n')
def TagPacketSizes(self, req, rsp=None): ctk = localstorage.GetLocalStorage().get('calltimer.key', None) if ctk is not None: ct = base.GetCallTimes() try: s = session._Obj() except: sys.exc_clear() s = session if s: if not s.role & ROLE_SERVICE: if boot.role == 'client': ct = (ct[2], s.calltimes) else: ct = (ct[0], s.calltimes) else: ct = (ct[1], s.calltimes) else: ct = (ct[1], ) for calltimes in ct: if ctk in calltimes: calltimes[ctk][4] += req.GetPickleSize(self.machoNet) if rsp is not None: with bluepy.Timer( 'machoNet::HandleMessage::SessionCall::TagPacketSizes::GetPickleSize::Rsp' ): calltimes[ctk][5] += rsp.GetPickleSize( self.machoNet)
def Write(self, message, jitSession = 1): """ Writes a MachoPacket to our transport, performing elementary packet corrections. """ if macho.mode == 'proxy' and jitSession: if message.source.addressType == const.ADDRESS_TYPE_CLIENT: self._JitSessionToOtherSide(message.source.clientID) elif base.IsInClientContext() and session and hasattr(session, 'clientID'): clientID = session.clientID self._JitSessionToOtherSide(clientID) message.contextKey = clientID ls = localstorage.GetLocalStorage() message.applicationID = ls.get('applicationID', None) message.languageID = ls.get('languageID', None) if hasattr(self, 'userID'): message.userID = self.userID if message.source.addressType == const.ADDRESS_TYPE_ANY and not message.command % 2: message.source.nodeID = self.machoNet.nodeID message.source.addressType = const.ADDRESS_TYPE_NODE message.Changed() elif message.source.addressType == const.ADDRESS_TYPE_NODE and message.source.nodeID is None: message.source.nodeID = self.machoNet.nodeID message.Changed() thePickle = message.GetPickle() if message.command != const.cluster.MACHONETMSG_TYPE_MOVEMENTNOTIFICATION or MACHONET_LOGMOVEMENT: self.LogInfo('Write: ', message) if self.transportName != 'tcp:packet:machoNet' and message.compressedPart * 100 / len(thePickle) < self.compressionPercentageThreshold and len(thePickle) - message.compressedPart > self.compressionThreshold and not MACHONET_COMPRESSION_DISABLED: before = len(thePickle) try: with bluepy.Timer('machoNet::MachoTransport::Write::Compress'): compressed = zlib.compress(thePickle, 1) except zlib.error as e: raise RuntimeError('Compression Failure: ' + strx(e)) after = len(compressed) if after > before: self.LogInfo('Compress would have exploded data from ', before, ' to ', after, ' bytes. Sending uncompressed.') elif (before - after) * 100 / before <= 5: self.LogInfo("Compress didn't help one bit. Would have compressed data from ", before, ' to ', after, " bytes, which is insignificant. Sending uncompressed, rather than wasting the recipient's CPU power for nothing.") else: thePickle = compressed self.machoNet.compressedBytes.Add(before - after) if self.transportName == 'tcp:packet:client' and macho.mode == 'proxy': for objectID, refID in message.oob.get('OID+', {}).iteritems(): s = self.sessions.get(self.clientID, None) if s is not None: s.RegisterMachoObject(objectID, None, refID) if self.largePacketLogSpamThreshold != None and len(thePickle) > self.largePacketLogSpamThreshold: log.LogTraceback(extraText='Packet larger than the %d byte largePacketLogSpamTreshhold being written out to wire (%d > %d)' % (self.largePacketLogSpamThreshold, len(thePickle), self.largePacketLogSpamThreshold)) if len(thePickle) > self.dropPacketThreshold: if self.transportName == 'tcp:packet:client' or macho.mode == 'server' and (message.destination.addressType == const.ADDRESS_TYPE_CLIENT or message.destination.addressType == const.ADDRESS_TYPE_BROADCAST and message.destination.idtype not in ('nodeID', '+nodeID')): self.machoNet.LogError('Attempted to send a deadly (len=', len(thePickle), ') packet to client(s), PACKET DROPPED') self.machoNet.LogError('Packet =', repr(message)[:1024]) self.machoNet.LogError('Pickle starts with =', repr(thePickle)[:1024]) return self.transport.Write(thePickle) self.machoNet.dataSent.Add(len(thePickle))
def CachedMethodCalled(cacheKey, details): try: cacheScope = localstorage.GetLocalStorage( )['base.MethodCachingContext'] if details: clientTimes = details['versionCheck'] if clientTimes: clientTime = clientTimes[0] cacheScope[cacheKey] = (sm.GetService( 'objectCaching').__versionchecktimes__[clientTime], 'sessionInfo' in details) except KeyError: pass
def ToPickle(self, value, nonblocking=1): """ Performs pickling for us, using the magical helper functions. """ try: self.compressedPart = 0 self.added_objects = {} ret = blue.marshal.Save(value, self.GetObjectID) if nonblocking and len( ret) > self.machoNet.largeResponseWarningThreshold: ctk = localstorage.GetLocalStorage().get('calltimer.key', None) if len(ret) > self.machoNet.largeResponseErrorThreshold: if ctk is not None: self.machoNet.LogError( 'Unacceptably large response from ', ctk, ', ', len(ret), ' bytes') else: try: self.machoNet.LogError( 'Unacceptably large response from unknown source, ', len(ret), ' bytes - ', repr(value)[:1024]) except Exception: self.machoNet.LogError( 'Unacceptably large response from unknown source 2, ', len(ret), ' bytes - ', ret[:256]) log.LogTraceback() elif ctk is not None: self.machoNet.LogWarn('Unacceptably large response from ', ctk, ', ', len(ret), ' bytes') else: try: self.machoNet.LogWarn( 'Unacceptably large response from unknown source, ', len(ret), ' bytes - ', repr(value)[:1024]) except Exception: self.machoNet.LogWarn( 'Unacceptably large response from unknown source 2, ', len(ret), ' bytes - ', ret[:256]) return ret except StandardError: self.machoNet.LogError( "That was NOT a nice thing to do! Keep yo' unpicklables to yourself." ) log.LogTraceback() raise
def GetLanguageID(): try: ret = None try: ls = localstorage.GetLocalStorage() ret = ls['languageID'] crestUtil.SetTLSMarkerLocalized() except KeyError: pass if ret is None: ret = GetStandardizedLanguageID(session.languageID) return ret except (KeyError, AttributeError): return 'en-us'
def CachedMethodCalled(cacheKey, details): """ A cached method was called, we should remember the caching details for it if we are in a CachePublic scope """ try: cacheScope = localstorage.GetLocalStorage( )['base.MethodCachingContext'] if details: clientTimes = details['versionCheck'] if clientTimes: clientTime = clientTimes[0] cacheScope[cacheKey] = (sm.GetService( 'objectCaching').__versionchecktimes__[clientTime], 'sessionInfo' in details) except KeyError: pass
def Read(self): self.currentReaders += 1 try: thePickle = self.transport.Read() finally: self.currentReaders -= 1 if getattr(self, 'userID', None) and len(thePickle) > 100000: self.machoNet.LogWarn( 'Read a ', len(thePickle), ' byte packet (before decompression) from userID=', getattr(self, 'userID', 'non-user'), ' on address ', self.transport.address) elif len(thePickle) > 5000000: self.machoNet.LogWarn( 'Read a ', len(thePickle), ' byte packet (before decompression) from userID=', getattr(self, 'userID', 'non-user'), ' on address ', self.transport.address) if thePickle[0] not in '}~': before = len(thePickle) try: with bluepy.Timer( 'machoNet::MachoTransport::Read::DeCompress'): thePickle = zlib.decompress(thePickle) except zlib.error as e: raise RuntimeError('Decompression Failure: ' + strx(e)) after = len(thePickle) if after <= before: self.machoNet.LogError('Decompress shrank data from ', before, ' to ', after, ' bytes') else: self.machoNet.decompressedBytes.Add(after - before) if getattr(self, 'userID', None) and len(thePickle) > 100000: self.machoNet.LogWarn( 'Read a ', len(thePickle), ' byte packet (after decompression, if appropriate) from userID=', getattr(self, 'userID', 'non-user'), ' on address ', self.transport.address) elif len(thePickle) > 5000000: self.machoNet.LogWarn( 'Read a ', len(thePickle), ' byte packet (after decompression, if appropriate) from userID=', getattr(self, 'userID', 'non-user'), ' on address ', self.transport.address) if self.clientID: self.machoNet.dataReceived.Add(len(thePickle)) else: self.machoNet.dataReceived.AddFrom(self.nodeID, len(thePickle)) try: message = macho.Loads(thePickle) except GPSTransportClosed as e: self.transport.Close(**e.GetCloseArgs()) raise except StandardError: if self.transportName == 'tcp:packet:client': self._LogPotentialAttackAndClose(thePickle) raise message.SetPickle(thePickle) if macho.mode == 'client' and message.source.addressType == const.ADDRESS_TYPE_NODE and message.destination.addressType == const.ADDRESS_TYPE_BROADCAST: message.oob['sn'] = self.machoNet.notifySequenceIDByNodeID[ message.source.nodeID] self.machoNet.notifySequenceIDByNodeID[message.source.nodeID] += 1 if self.transportName == 'tcp:packet:client': message.source = MachoAddress(clientID=self.clientID, callID=message.source.callID) if message.contextKey is not None or message.applicationID is not None or message.languageID is not None: self._LogPotentialAttackAndClose(thePickle) raise StandardError( 'Packet containing contextKey received on a client transport. Hack?' ) if hasattr(self, 'userID'): message.userID = self.userID if message.command != const.cluster.MACHONETMSG_TYPE_MOVEMENTNOTIFICATION or MACHONET_LOGMOVEMENT: self.LogInfo('Read: ', message) if macho.mode == 'proxy': for objectID, refID in message.oob.get('OID-', {}).iteritems(): s = self.sessions.get(self.clientID, None) if s is None: s = self.sessions.get(self.nodeID, None) if s is not None: s.UnregisterMachoObject(objectID, refID) if macho.mode == 'server': ls = localstorage.GetLocalStorage() ls['applicationID'] = message.applicationID ls['languageID'] = message.languageID return message