SolvedPython Proxy

Posts 1–2 of 2 · Page 1 of 1
Python Proxy
Hi, today I started to make a proxy for rotmg in python, but there seems to be a problem with the communication: it is painfully slow, like if I had a <10KB/s up/download speed, everything is lagging, the map loads slowly and it disconnects like every minute.

Here is the code:
Code:
	def run(self):
		try:
			data = bytes()
			while 1:
				#time.sleep(0.01)
				data = data+self.cFrom.recv(2048)
				if not data:
					break
				if len(data) < 4: continue
				pLen, = struct.unpack('!I', data[:4])
				if len(data) < pLen: continue
				pType, pData = struct.unpack('!B'+str(pLen-5)+'s', data[4:pLen])
				data = data[pLen:]
				pData = self.rc4crypt(pData)
				print(self.cFrom.getpeername()[0],'->',self.cTo.getpeername()[0],':',pLen, pType)
				#print(pData)
				
				self.sendPacket(pType, pData)
		finally:
			print(self.cTo.getpeername()[0],'disconnected.')
			self.cTo.sendall(data)
			self.cTo.close()
	def sendPacket(self,t,d):
		"""Encrypt and send data"""
		d = self.rc4crypt(d)
		data = struct.pack('!IB'+str(len(d))+'s', len(d)+5, t, d)
		self.cTo.sendall(data)
Also, if someone knows a better way to read fragmented TCP streams in py, please share it.

---------- Post added at 07:29 PM ---------- Previous post was at 07:06 PM ----------

Never mind, found the bug.
Here is the correct code:
Code:
	def run(self):
		try:
			data = bytes()
			while 1:
				#time.sleep(0.01)
				data = data+self.cFrom.recv(2048)
				if not data: break
				while 1:
					if len(data) < 4: break
					pLen, = struct.unpack('!I', data[:4])
					if len(data) < pLen: break
					pType, pData = struct.unpack('!B'+str(pLen-5)+'s', data[4:pLen])
					data = data[pLen:]
					pData = self.rc4crypt(pData)
					print(self.cFrom.getpeername()[0],'->',self.cTo.getpeername()[0],':',pLen, pType)
					#print(pData)
					
					self.sendPacket(pType, pData)
			print(self.cTo.getpeername()[0],'disconnected.')
		finally:
			self.cTo.sendall(data)
			self.cTo.close()
	def sendPacket(self,t,d):
		"""Encrypt and send data"""
		d = self.rc4crypt(d)
		data = struct.pack('!IB'+str(len(d))+'s', len(d)+5, t, d)
		self.cTo.sendall(data)
1 TCP packet can contain more than one rotmg packet.
okay solved by your self.

solved.
Posts 1–2 of 2 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us