<commit_before>#!/usr/bin/python

from twisted.internet import protocol, reactor, stdio
from twisted.protocols import basic
from twisted.python import log, logfile
from twisted.internet.endpoints import TCP4ServerEndpoint
from commands import commandList
from PSOCryptoUtils import PSO2RC4
import packetUtils, io, struct, time, bans, calendar, datetime, os, exceptions, sys, packets
import data.blocks as blocks
import data.ships as ships
import data.clients as clients
from queryProtocols import BlockScraperFactory, ShipAdvertiserFactory
from config import packetLogging as logPackets
from config import myIpAddr as myIp
from config import bindIp as ifaceIp
from config import noisy as verbose

class ShipProxy(protocol.Protocol):
	peer = None
	psoClient = False
	bufPacket = None

	loaded = False
	changingBlocks = False

	connTimestamp = None
	playerId = None
	myUsername = None
	packetCount = 0
	readBuffer = ''
	c4crypto = None
	orphans = []

	def setPeer(self, peer):
		self.peer = peer

	def setIsClient(self, isclient):
		self.psoClient = isclient

	def connectionLost(self, reason):
		if self.peer is not None:
			self.peer.transport.loseConnection()
			self.peer = None
		if self.playerId is not None and not self.changingBlocks:
			clients.removeClient(self)
		if self.psoClient and self.myUsername is not None:
			print("[ShipProxy] %s logged out or changed blocks." % self.myUsername)
		elif self.psoClient and self.myUsername is None:
			print("[ShipProxy] Client at %s lost connection." % self.transport.getPeer().host)

	def sendCryptoPacket(self, data):
		if self.c4crypto is not None:
			if logPackets: 
				with open('packets/%i.constucted.%s.bin' % (self.packetCount, self.transport.getPeer().host), 'wb') as f:
					f.write(data)
			if verbose:
				print("[ShipProxy] Sending %s a constucted packet..." % self.transport.getPeer().host)
			data = self.peer.c4crypto.encrypt(data)
			self.transport.write(data)


	def dataReceived(self, data):
		if verbose: print("[ShipProxy] [%i] Received data from %s!" % (self.packetCount, self.transport.getPeer().host,))

		encryptionEnabled = (self.c4crypto is not None)
		if encryptionEnabled:
			data = self.c4crypto.decrypt(data)<commit_msg>More none checking<commit_after>#!/usr/bin/python

from twisted.internet import protocol, reactor, stdio
from twisted.protocols import basic
from twisted.python import log, logfile
from twisted.internet.endpoints import TCP4ServerEndpoint
from commands import commandList
from PSOCryptoUtils import PSO2RC4
import packetUtils, io, struct, time, bans, calendar, datetime, os, exceptions, sys, packets
import data.blocks as blocks
import data.ships as ships
import data.clients as clients
from queryProtocols import BlockScraperFactory, ShipAdvertiserFactory
from config import packetLogging as logPackets
from config import myIpAddr as myIp
from config import bindIp as ifaceIp
from config import noisy as verbose

class ShipProxy(protocol.Protocol):
	peer = None
	psoClient = False
	bufPacket = None

	loaded = False
	changingBlocks = False

	connTimestamp = None
	playerId = None
	myUsername = None
	packetCount = 0
	readBuffer = ''
	c4crypto = None
	orphans = []

	def setPeer(self, peer):
		self.peer = peer

	def setIsClient(self, isclient):
		self.psoClient = isclient

	def connectionLost(self, reason):
		if self.peer is not None:
			self.peer.transport.loseConnection()
			self.peer = None
		if self.playerId is not None and not self.changingBlocks:
			clients.removeClient(self)
		if self.psoClient and self.myUsername is not None:
			print("[ShipProxy] %s logged out or changed blocks." % self.myUsername)
		elif self.psoClient and self.myUsername is None:
			print("[ShipProxy] Client at %s lost connection." % self.transport.getPeer().host)

	def sendCryptoPacket(self, data):
		if self.c4crypto is not None and self.peer is not None:
			if logPackets: 
				with open('packets/%i.constucted.%s.bin' % (self.packetCount, self.transport.getPeer().host), 'wb') as f:
					f.write(data)
			if verbose:
				print("[ShipProxy] Sending %s a constucted packet..." % self.transport.getPeer().host)
			data = self.peer.c4crypto.encrypt(data)
			self.transport.write(data)


	def dataReceived(self, data):
		if verbose: print("[ShipProxy] [%i] Received data from %s!" % (self.packetCount, self.transport.getPeer().host,))

		encryptionEnabled = (self.c4crypto is not None)
		if encryptionEnabled:
			data = self.c4crypto.decrypt(data)
{'fraction_non_alphanumeric': 0.09439062851993692, 'fraction_numerical': 0.004054967334985357, 'mean_word_length': 4.6131479140328695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5172721', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1364, 'n_words': 436}