def processOut(self): chunkSize = 512 read = 0 buff = b'' while not self.fin: print('waiting for server bytes') try: buff = self.conn.recv(chunkSize) except: print('timeout or error talking to server') if not self.doneWrite: time.sleep(1) continue else: buff = None if buff != None: read = read + len(buff) else: buff = b'' self.fin = True self.conn.close() print('sending ' + str(self.seq)) packet = ProxybackMessage() packet.createProxybackMessage(self.reqid, self.seq, self.fin, buff) print(packet) self.server.router.sendto(packet.msg, self.addr, service='socksProxyback') # FIXME - remove conn from self.server.conns on close print('done talking to server')
def handle(self, msock, msg, addr): # data=msg.decode('ascii') # data=bz2.decompress(msg).decode('ascii') # print('Socks Proxyback message from '+encodeAddress(addr)+': ') # print(msg) packet=ProxybackMessage() packet.decodeProxybackMessage(msg) # print(packet) print('reqid: '+encode(packet.reqid)) print('current: '+str(packet.seq)) if not encode(packet.reqid) in self.stats: print('Unknown reqid '+encode(packet.reqid)) print('Dropping packet with seq '+str(packet.seq)) return q=self.qs[encode(packet.reqid)] progress, last, chunks=self.stats[encode(packet.reqid)] if not packet.seq in chunks: # Store new chunks by seq number chunks[packet.seq]=packet.data if progress==None and packet.seq==0: # First packet arrived progress=0 if packet.fin: # If fin flag set, this is the last packet in the sequence last=packet.seq done=False print('reqid: '+encode(packet.reqid)) print('progress: '+str(progress)) print('last: '+str(last)) print('current: '+str(packet.seq)) if progress!=None: if last!=None: done=True for x in range(progress, last+1): # Send all sequential packets from last sent to new packet try: chunk=chunks[x] print('putting chunk '+encode(packet.reqid)+':'+str(x)+'/'+str(last)) q.put(chunk) progress=x+1 except: done=False break else: for x in range(progress, packet.seq+1): # Send all sequential packets from last sent to new packet try: chunk=chunks[x] print('putting chunk '+encode(packet.reqid)+':'+str(x)+'/?') q.put(chunk) progress=x+1 except: break if done: q.put(None) del self.qs[encode(packet.reqid)] del self.stats[encode(packet.reqid)] else: self.stats[encode(packet.reqid)]=[progress, last, chunks] print(q.qsize())