def POST(self, mode, name): user_data = web.input() # GTO request (URI download) if "token" in user_data and len(user_data) == 1: gto_file = os.path.join(GTO_BASE_PATH, name, name + ".gto") # open torrent file in binary mode on Windows torrent = open(gto_file, "rb") try: torrent_data = torrent.read() finally: torrent.close() return torrent_data # Upload request # Add CA cert to GTO and set the tracker address if "token" in user_data and "file" in user_data and len(user_data) == 2: gtodict = decode(user_data.file) if not "nossl" in mode: cert_file = open(CA_CERT, "r") try: ca_cert_data = cert_file.read() finally: cert_file.close() gtodict["info"]["ssl-cert"] = ca_cert_data gtodict["announce"] = TRACKER_ADDRESS return encode_dict(gtodict)
def scrape(self, user_data): infohash = user_data.info_hash ip = web.ctx.ip # return state = 0 if the completed flag is set response_dict = {"files": {infohash: {"state": 0, "complete": 0, "downloaded": 0, "incomplete": 0}}} if infohash in self.torrents_completed and self.torrents_completed[infohash]: response_dict = {"files": {infohash: {"state": 1, "complete": 0, "downloaded": 0, "incomplete": 0}}} return encode_dict(response_dict)
def announce(self, user_data): infohash = user_data.info_hash ip = web.ctx.ip if not "ip" in user_data else user_data.ip # add torrent to dicts, if new if not infohash in self.torrents: self.torrents[infohash] = [] self.torrents_completed[infohash] = False # set completed flag when completed event arrives if "event" in user_data and user_data.event == "completed": self.torrents_completed[infohash] = True # TODO only remove peer-id # if 'event' in user_data and user_data.event == 'stopped': # del self.torrents[infohash] # create a peer dict entry for requester # webpy is giving us unicode strings, so coerce into legacy strings peer_dict = {"peer id": str(user_data.peer_id), "ip": str(ip), "port": int(user_data.port)} # add peer dict entry to swarm dict, if it doesn't exist if not peer_dict in self.torrents[infohash]: self.torrents[infohash].append(peer_dict) # only give server peers to clients, and client peers to servers swarm = self.torrents[infohash] filtered_swarm = [] # figure out which partition of the swarm to return (clients or servers) if GT_CLIENT_TAG in user_data.peer_id: # requester a client, only return servers filtered_swarm = [x for x in swarm if GT_SERVER_TAG in x["peer id"]] else: # requester a server, only return clients filtered_swarm = [x for x in swarm if GT_CLIENT_TAG in x["peer id"]] # assemble response dict response_dict = {"interval": 5, "min interval": 5, "peers": filtered_swarm} # bencode response return encode_dict(response_dict)