Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created April 22, 2011 10:14
Show Gist options
  • Save scturtle/936410 to your computer and use it in GitHub Desktop.
Save scturtle/936410 to your computer and use it in GitHub Desktop.
a proxy cheating the upload of BT/PT
# -*- coding: utf-8 -*-
import re, time, sys
import socket
import threading
from time import time
from random import random
PORT = 8080
mbps=10
tinfo=0
lasttime=0
upsum=0
regex = re.compile(r'http://(.*?)/', re.IGNORECASE)
reup = re.compile(r'uploaded=\d*')
reinfo= re.compile(r'info_hash=[^&\s]*')
class ConnectionThread(threading.Thread):
def __init__(self, (conn,addr)):
self.conn = conn
self.addr = addr
threading.Thread.__init__(self)
def change(self,data):
global mbps,tinfo,upsum,lasttime
s = reup.findall(data)
info = reinfo.findall(data)
if info:
print 'INFO_HASH:',info[0]
if not tinfo:
tinfo=info[0]
if s and tinfo==info[0]:
print 'FOR',info[0],'UPLOADED:',s[0]
if not lasttime:
lasttime=time()
t = int(1024*1024* int(mbps*random()*(time()-lasttime)))
#if t>5*1024*1024*1024:
#print 'TOO BIG!!!!!!!!!!!!!!'
#else:
upsum+=t
lasttime=time()
data=data.replace(s[0],'uploaded=%d' % upsum)
print 'CHANGED TO:',upsum
return data
def run(self):
data = self.conn.recv(1024*1024)
#print data
host = regex.search(data).groups()[0]
print host
try:
i=host.find(':')
if i>=0:
host_port=host[:i],int(host[i+1:])
else:
host_port=host,80
request = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
try:
request.connect(host_port)
except:
request = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
request.connect(host_port)
data=self.change(data)
request.send(data)
while 1:
temp = request.recv(1024)
if ('' == temp):
break
self.conn.send(temp)
except Exception,data:
print Exception,":",data
self.conn.close()
class ProxyThread(threading.Thread):
def __init__(self, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind(('localhost', port))
threading.Thread.__init__(self)
def run(self):
self.sock.listen(10)
while 1:
temp = ConnectionThread(self.sock.accept())
temp.daemon = True
temp.start()
if __name__ == "__main__":
proxy = ProxyThread(PORT)
proxy.daemon = True
proxy.start()
print "Started a proxy on port", PORT
t = raw_input('Enter to exit...\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment