def fuck_yourself(self): # set ipv6 address if needed if self.use_ipv6: ipv6 = ipv6_addr() if not ipv6: ptl_error("cannot get ipv6 address") self.str_ipv6 = "&ipv6=%s" % urlencode(ipv6) # generate a port number if not given if not self.port: self.port = randint(MIN_PORT, MAX_PORT) # generate client key self.client_key = client_key() client_info = BT_CLIENTS[self.client_id] # generate peer_id : based on client chosen prefix = client_info["prefix"] pid = peer_id(prefix) self.quoted_peer_id = urlencode(pid) # generate http header[user-agent] : based on client chosen user_agent = client_info["user-agent"] self.headers.update({"User-Agent": user_agent}) # supports scrape? self.scrapable = not self.no_scrape and client_info["scrape"] # create directories if not exist for up_down in (UP, DOWN): mkdir(DIR[up_down]) log.setLevel(DEBUG) log.debug("ptliar started, version: %s" % __version__) log.info("verbose : %s" % (self.logging_level == DEBUG)) log.info("ipv6 : %s" % self.use_ipv6) log.info("zero_rate : %s" % self.use_zero_rate) log.info("timer : %s" % ptime(self.timer)) log.info("max up bandwidth : %s/s" % psize(self.max_up_speed)) log.info("max down bandwidth : %s/s" % psize(self.max_down_speed)) log.info("max torrent speed : %s/s" % psize(self.max_torrent_speed)) log.info("fake bt client : %s" % self.client_id)
def main(argv): interrupt_off() try: opts, args = getopt(argv, "c:DefhM:m:nlp:s:t:vz") except GetoptError: _usage() ptl_exit(2) def parse_int(i): if i.isdigit(): return int(i) return 0 for opt, arg in opts: if opt == "-h": _usage() ptl_exit(0) if opt == "-l": client_list() ptl_exit(0) if opt == "-m": ps.max_up_speed = parse_int(arg) * KILO if ps.max_up_speed <= 0: ptl_error( "max upload bandwidth must be a positive integer, in KB/s") elif opt == "-M": ps.max_down_speed = parse_int(arg) * KILO if ps.max_down_speed <= 0: ptl_error( "max download bandwidth must be a positive integer, in KB/s" ) elif opt == "-s": ps.max_torrent_speed = parse_int(arg) * KILO if ps.max_torrent_speed <= 0: ptl_error( "max torrent speed must be a positive integer, in KB/s") elif opt == "-p": ps.port = parse_int(arg) if ps.port < MIN_PORT or ps.port > MAX_PORT: ptl_error("port number should be within (%s, %s)" % \ (MIN_PORT, MAX_PORT)) elif opt == "-v": ps.logging_level = DEBUG elif opt == "-e": ps.use_ipv6 = True elif opt == "-z": ps.use_zero_rate = True elif opt == "-f": ps.no_sleep = True elif opt == "-n": ps.no_scrape = True elif opt == "-c": if arg not in BT_CLIENTS: ptl_error("client not in supported client-list, see option -l") ps.client_id = arg elif opt == "-t": ps.timer = parse_int(arg) * HOUR if ps.timer < 1: ptl_error("timer must be a positive integer, in hours") print BANNER % {"version": __version__, "date": __date__, "url": __url__} if not ps.no_sleep: sleep(SLEEP_BANNER) ps.fuck_yourself() ts.fuck_yourself() if not ps.no_sleep: sleep(SLEEP_SETTINGS) tm.fool_around()
def _fool_around(self): def run_threads(box): for th in box: th.start() for th in box: th.join() thread_box = [] for torrent in self.torrents.values(): if not torrent.is_ready: continue thread_box.append(Thread(target=torrent.commit)) if len(thread_box) == CONNECTION_PER_BOX: run_threads(thread_box) thread_box = [] if not ps.no_sleep: sleep(SLEEP_THREAD) run_threads(thread_box) # calculate committed values all_torrents = self.torrents.values() uploaded = 0 downloaded = 0 for torrent in all_torrents: uploaded += torrent.uploaded downloaded += torrent.downloaded log.info("time: %s up_speed: %s/s down_speed: %s/s "\ "committed [up: %s down: %s]" % \ (ptime(time() - self.started), psize(ts.up_speed), psize(ts.down_speed), psize(uploaded), psize(downloaded))) if self.done: # say goodbye elapsed = time() - self.started log.info("time elapsed: %s" % ptime(elapsed)) log.info("avg up speed: %s/s" % psize(uploaded / elapsed)) log.info("avg down speed: %s/s" % psize(downloaded / elapsed)) log.debug("<= PTLiar ended") print "Bye~" ptl_exit(0) active_torrents = filter(lambda t: t.status != "error", all_torrents) if len(active_torrents) < 1: ptl_error("no torrents available") # calculate how long should we sleep next_commit = min(map(lambda t: t.next_commit_time, active_torrents)) left = max(0, next_commit - time()) # sleep one more second than needed zzz = min(SLEEP_SCAN, left + 1) log.info("next commit: %s from now, sleep for %s.." % \ (ptime(left), ptime(zzz))) print "press [Ctrl+C] to leave" try: interrupt_on() sleep(zzz) interrupt_off() except (KeyboardInterrupt, SystemExit): # gracefully shutdown interrupt_off() self.done = True if time() >= self.started + ps.timer: # timer self.done = True if self.done: log.info("stopping...") for torrent in active_torrents: torrent.status = "stopped" return # check whether we've got new torrent self._load_torrents()