Skip to content

Commit

Permalink
Merge pull request #581 from Devristo/devel
Browse files Browse the repository at this point in the history
Anonymous Tribler Test
  • Loading branch information
synctext committed May 13, 2014
2 parents 3dee766 + 86f4e76 commit 46c3d2c
Show file tree
Hide file tree
Showing 60 changed files with 6,594 additions and 116 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ We make use of submodules, so remember using the --recursive argument when cloni
## Dependencies

### Debian/Ubuntu/Mint
sudo apt-get install scons build-essential libevent-dev python-libtorrent python-apsw python-wxgtk2.8 python-netifaces python-m2crypto vlc
sudo apt-get install scons build-essential libevent-dev python-libtorrent python-apsw python-wxgtk2.8 python-netifaces python-m2crypto vlc python-igraph

### Windows
TODO
Expand Down
14 changes: 10 additions & 4 deletions Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from threading import Event, Thread, enumerate as enumerate_threads, currentThread
from Tribler.Core.ServerPortHandler import MultiHandler
from Tribler.Core.Utilities.configparser import CallbackConfigParser
from Tribler.community.anontunnel.endpoint import DispersyBypassEndpoint
from Tribler.community.privatesemantic.crypto.elgamalcrypto import ElgamalCrypto, \
NoElgamalCrypto

import logging
from traceback import print_exc
Expand Down Expand Up @@ -121,12 +124,12 @@ def register(self, session, sesslock):
if self.session.get_dispersy_tunnel_over_swift() and self.swift_process:
endpoint = TunnelEndpoint(self.swift_process)
else:
endpoint = RawserverEndpoint(self.rawserver, self.session.get_dispersy_port())
endpoint = DispersyBypassEndpoint(self.rawserver, self.session.get_dispersy_port())

callback = Callback("Dispersy") # WARNING NAME SIGNIFICANT
working_directory = unicode(self.session.get_state_dir())

self.dispersy = Dispersy(callback, endpoint, working_directory)
self.dispersy = Dispersy(callback, endpoint, working_directory, crypto=ElgamalCrypto())

# TODO: see if we can postpone dispersy.start to improve GUI responsiveness.
# However, for now we must start self.dispersy.callback before running
Expand Down Expand Up @@ -925,14 +928,17 @@ def sessconfig_changed_callback(self, section, name, new_value, old_value):
self.ltmgr.set_utp(new_value)
elif section == 'libtorrent' and name == 'lt_proxyauth':
if self.ltmgr:
self.ltmgr.set_proxy_settings(*self.session.get_libtorrent_proxy_settings())
self.ltmgr.set_proxy_settings(self.ltmgr.ltsession, *self.session.get_libtorrent_proxy_settings())
elif section == 'torrent_checking' and name == 'torrent_checking_period':
if self.rtorrent_handler and value_changed:
self.rtorrent_handler.set_max_num_torrents(new_value)
# Return True/False, depending on whether or not the config value can be changed at runtime.
elif (section == 'general' and name in ['nickname', 'mugshot', 'videoanalyserpath']) or \
(section == 'libtorrent' and name in ['lt_proxytype', 'lt_proxyserver']) or \
(section == 'libtorrent' and name in ['lt_proxytype', 'lt_proxyserver',
'anon_proxyserver', 'anon_proxytype', 'anon_proxyauth',
'anon_listen_port']) or \
(section == 'torrent_collecting' and name in ['stop_collecting_threshold']) or \
(section == 'proxy_community' and name in ['socks5_listen_port']) or \
(section == 'swift' and name in ['swiftmetadir']):
return True
else:
Expand Down
6 changes: 3 additions & 3 deletions Tribler/Core/CacheDB/Notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
# see LICENSE.txt for license information

import threading
import logging

from Tribler.Core.simpledefs import NTFY_MISC, NTFY_PEERS, NTFY_TORRENTS, \
NTFY_PLAYLISTS, NTFY_COMMENTS, NTFY_MODIFICATIONS, NTFY_MODERATIONS, \
NTFY_MARKINGS, NTFY_MYPREFERENCES, NTFY_ACTIVITIES, NTFY_REACHABLE, \
NTFY_CHANNELCAST, NTFY_VOTECAST, NTFY_DISPERSY, NTFY_TRACKERINFO, \
NTFY_UPDATE, NTFY_INSERT, NTFY_DELETE
NTFY_UPDATE, NTFY_INSERT, NTFY_DELETE, NTFY_ANONTUNNEL

import logging

class Notifier:

SUBJECTS = [NTFY_MISC, NTFY_PEERS, NTFY_TORRENTS, NTFY_PLAYLISTS,
NTFY_COMMENTS, NTFY_MODIFICATIONS, NTFY_MODERATIONS, NTFY_MARKINGS,
NTFY_MYPREFERENCES, NTFY_ACTIVITIES, NTFY_REACHABLE, NTFY_CHANNELCAST,
NTFY_VOTECAST, NTFY_DISPERSY, NTFY_TRACKERINFO]
NTFY_VOTECAST, NTFY_DISPERSY, NTFY_TRACKERINFO, NTFY_ANONTUNNEL]

# . . .
# todo: add all datahandler types+other observables
Expand Down
6 changes: 6 additions & 0 deletions Tribler/Core/DownloadConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def get_mode(self):
@return DLMODE_NORMAL/DLMODE_VOD """
return self.dlconfig.get('downloadconfig', 'mode')

def set_anon_mode(self, anon_mode):
self.dlconfig.set('downloadconfig', 'anon_mode', anon_mode)

def get_anon_mode(self):
return self.dlconfig.get('downloadconfig', 'anon_mode')

def set_selected_files(self, files):
""" Select which files in the torrent to download. The filenames must
be the names as they appear in the content def, including encoding.
Expand Down
7 changes: 6 additions & 1 deletion Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ def create_engine_wrapper(self, lm_network_engine_wrapper_created_callback, psta
with self.dllock:
if not self.cew_scheduled:
self.ltmgr = self.session.lm.ltmgr
if not self.ltmgr or (isinstance(self.tdef, TorrentDefNoMetainfo) and not self.ltmgr.is_dht_ready()):
if not self.ltmgr or (isinstance(self.tdef, TorrentDefNoMetainfo) and not self.ltmgr.is_dht_ready()) or \
(self.get_anon_mode() and not self.ltmgr.is_anon_ready()):
self._logger.info("LibtorrentDownloadImpl: LTMGR or DHT not ready, rescheduling create_engine_wrapper")
create_engine_wrapper_lambda = lambda: self.create_engine_wrapper(lm_network_engine_wrapper_created_callback, pstate, initialdlstatus=initialdlstatus)
self.session.lm.rawserver.add_task(create_engine_wrapper_lambda, 5)
Expand All @@ -275,6 +276,7 @@ def network_create_engine_wrapper(self, lm_network_engine_wrapper_created_callba
atp["paused"] = True
atp["auto_managed"] = False
atp["duplicate_is_error"] = True
atp["anon_mode"] = self.get_anon_mode()

resume_data = pstate.get('state', 'engineresumedata') if pstate else None
if not isinstance(self.tdef, TorrentDefNoMetainfo):
Expand Down Expand Up @@ -500,6 +502,9 @@ def on_file_renamed_alert(self, alert):
os.rmdir(self.unwanteddir_abs)

def on_performance_alert(self, alert):
if self.get_anon_mode():
return

# When the send buffer watermark is too low, double the buffer size to a maximum of 50MiB. This is the same mechanism as Deluge uses.
if alert.message().endswith("send buffer watermark too low (upload rate will suffer)"):
settings = self.ltmgr.ltsession.settings()
Expand Down
134 changes: 68 additions & 66 deletions Tribler/Core/Libtorrent/LibtorrentMgr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

# Written by Egbert Bouman
from libtorrent import proxy_type
import os
import time
import binascii
Expand Down Expand Up @@ -75,7 +76,7 @@ def __init__(self, trsession, ignore_singleton=False):
self.ltsession.add_dht_router('router.bitcomet.com', 6881)

# Load proxy settings
self.set_proxy_settings(*self.trsession.get_libtorrent_proxy_settings())
self.set_proxy_settings(self.ltsession, *self.trsession.get_libtorrent_proxy_settings())

self.set_utp(self.trsession.get_libtorrent_utp())

Expand All @@ -99,11 +100,14 @@ def __init__(self, trsession, ignore_singleton=False):
if not os.path.exists(self.metadata_tmpdir):
os.mkdir(self.metadata_tmpdir)

self.ltsession_anon = None

def getInstance(*args, **kw):
if LibtorrentMgr.__single is None:
LibtorrentMgr(*args, **kw)
return LibtorrentMgr.__single
getInstance = staticmethod(getInstance)
''' :type : () -> LibtorrentMgr '''

def delInstance():
del LibtorrentMgr.__single
Expand All @@ -114,6 +118,32 @@ def hasInstance():
return LibtorrentMgr.__single != None
hasInstance = staticmethod(hasInstance)

def is_anon_ready(self):
return self.ltsession_anon is not None

def create_anonymous_session(self):
settings = lt.session_settings()
settings.enable_outgoing_utp = True
settings.enable_incoming_utp = True
settings.enable_outgoing_tcp = False
settings.enable_incoming_tcp = False
settings.anonymous_mode = True
ltsession = lt.session(flags=1)
ltsession.set_settings(settings)
ltsession.set_alert_mask(lt.alert.category_t.stats_notification |
lt.alert.category_t.error_notification |
lt.alert.category_t.status_notification |
lt.alert.category_t.storage_notification |
lt.alert.category_t.performance_warning |
lt.alert.category_t.debug_notification)


self.set_proxy_settings(ltsession, *self.trsession.get_anon_proxy_settings())
self.ltsession_anon = ltsession

ltsession.listen_on(self.trsession.get_anon_listen_port(), self.trsession.get_anon_listen_port()+10)
self._logger.info("Started ANON LibTorrent session on port %d", ltsession.listen_port())

def shutdown(self):
# Save DHT state
dhtstate_file = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME), 'w')
Expand All @@ -127,7 +157,7 @@ def shutdown(self):
if os.path.exists(self.metadata_tmpdir):
rmtree(self.metadata_tmpdir)

def set_proxy_settings(self, ptype, server=None, auth=None):
def set_proxy_settings(self, ltsession, ptype, server=None, auth=None):
proxy_settings = lt.proxy_settings()
proxy_settings.type = lt.proxy_type(ptype)
if server:
Expand All @@ -138,10 +168,7 @@ def set_proxy_settings(self, ptype, server=None, auth=None):
proxy_settings.password = auth[1]
proxy_settings.proxy_hostnames = True
proxy_settings.proxy_peer_connections = True
self.ltsession.set_peer_proxy(proxy_settings)
self.ltsession.set_web_seed_proxy(proxy_settings)
self.ltsession.set_tracker_proxy(proxy_settings)
self.ltsession.set_dht_proxy(proxy_settings)
ltsession.set_proxy(proxy_settings)

def set_utp(self, enable):
settings = self.ltsession.settings()
Expand Down Expand Up @@ -173,41 +200,11 @@ def get_dht_nodes(self):
def is_dht_ready(self):
return self.dht_ready

def queue_position_up(self, infohash):
with self.torlock:
download = self.torrents.get(hexlify(infohash), None)
if download:
download.handle.queue_position_up()
self._refresh_queue_positions()

def queue_position_down(self, infohash):
with self.torlock:
download = self.torrents.get(hexlify(infohash), None)
if download:
download.handle.queue_position_down()
self._refresh_queue_positions()

def queue_position_top(self, infohash):
with self.torlock:
download = self.torrents.get(hexlify(infohash), None)
if download:
download.handle.queue_position_top()
self._refresh_queue_positions()

def queue_position_bottom(self, infohash):
with self.torlock:
download = self.torrents.get(hexlify(infohash), None)
if download:
download.handle.queue_position_bottom()
self._refresh_queue_positions()

def _refresh_queue_positions(self):
for d in self.torrents.values():
d.queue_position = d.handle.queue_position()

def add_torrent(self, torrentdl, atp):
# If we are collecting the torrent for this infohash, abort this first.
with self.metainfo_lock:
anon_mode = atp.pop('anon_mode', False)
ltsession = self.ltsession_anon if anon_mode else self.ltsession

if atp.has_key('ti'):
infohash = str(atp['ti'].info_hash())
Expand All @@ -220,14 +217,14 @@ def add_torrent(self, torrentdl, atp):
self._logger.info("LibtorrentMgr: killing get_metainfo request for %s", infohash)
handle, _, _ = self.metainfo_requests.pop(infohash)
if handle:
self.ltsession.remove_torrent(handle, 0)
ltsession.remove_torrent(handle, 0)

handle = self.ltsession.add_torrent(encode_atp(atp))
handle = ltsession.add_torrent(encode_atp(atp))
infohash = str(handle.info_hash())
with self.torlock:
if infohash in self.torrents:
raise DuplicateDownloadException()
self.torrents[infohash] = torrentdl
self.torrents[infohash] = (torrentdl, ltsession)

self._logger.debug("LibtorrentMgr: added torrent %s", infohash)

Expand All @@ -239,7 +236,7 @@ def remove_torrent(self, torrentdl, removecontent=False):
infohash = str(handle.info_hash())
with self.torlock:
if infohash in self.torrents:
self.ltsession.remove_torrent(handle, int(removecontent))
self.torrents[infohash][1].remove_torrent(handle, int(removecontent))
del self.torrents[infohash]
self._logger.debug("LibtorrentMgr: remove torrent %s", infohash)
else:
Expand All @@ -263,31 +260,36 @@ def delete_mappings(self):
self.upnp_mapper.delete_mapping(mapping)

def process_alerts(self):
if self.ltsession:
alert = self.ltsession.pop_alert()
while alert:
alert_type = str(type(alert)).split("'")[1].split(".")[-1]
if alert_type == 'external_ip_alert':
external_ip = str(alert).split()[-1]
if self.external_ip != external_ip:
self.external_ip = external_ip
self._logger.info('LibtorrentMgr: external IP is now %s', self.external_ip)
handle = getattr(alert, 'handle', None)
if handle:
if handle.is_valid():
infohash = str(handle.info_hash())
with self.torlock:
if infohash in self.torrents:
self.torrents[infohash].process_alert(alert, alert_type)
elif infohash in self.metainfo_requests:
if type(alert) == lt.metadata_received_alert:
self.got_metainfo(infohash)
else:
self._logger.debug("LibtorrentMgr: could not find torrent %s", infohash)
for ltsession in [self.ltsession, self.ltsession_anon]:
if ltsession:
alert = ltsession.pop_alert()
while alert:
self.process_alert(alert)
alert = ltsession.pop_alert()

self.trsession.lm.rawserver.add_task(self.process_alerts, 1)

def process_alert(self, alert):
alert_type = str(type(alert)).split("'")[1].split(".")[-1]
if alert_type == 'external_ip_alert':
external_ip = str(alert).split()[-1]
if self.external_ip != external_ip:
self.external_ip = external_ip
self._logger.info('LibtorrentMgr: external IP is now %s', self.external_ip)
handle = getattr(alert, 'handle', None)
if handle:
if handle.is_valid():
infohash = str(handle.info_hash())
with self.torlock:
if infohash in self.torrents:
self.torrents[infohash][0].process_alert(alert, alert_type)
elif infohash in self.metainfo_requests:
if type(alert) == lt.metadata_received_alert:
self.got_metainfo(infohash)
else:
self._logger.debug("LibtorrentMgr: alert for invalid torrent")
alert = self.ltsession.pop_alert()
self.trsession.lm.rawserver.add_task(self.process_alerts, 1)
self._logger.debug("LibtorrentMgr: could not find torrent %s", infohash)
else:
self._logger.debug("LibtorrentMgr: alert for invalid torrent")

def reachability_check(self):
if self.ltsession and self.ltsession.status().has_incoming_connections:
Expand Down
8 changes: 4 additions & 4 deletions Tribler/Core/RawServer/RawServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def scan_for_timeouts(self):
self.sockethandler.scan_for_timeouts()

def bind(self, port, bind='', reuse= False,
ipv6_socket_style=1):
self.sockethandler.bind(port, bind, reuse, ipv6_socket_style)
ipv6_socket_style=1, handler=None):
self.sockethandler.bind(port, bind, reuse, ipv6_socket_style, handler)

def find_and_bind(self, first_try, minport, maxport, bind='', reuse = False,
ipv6_socket_style=1, randomizer= False):
ipv6_socket_style=1, randomizer= False, handler=None):
# 2fastbt_
result = self.sockethandler.find_and_bind(first_try, minport, maxport, bind, reuse,
ipv6_socket_style, randomizer)
ipv6_socket_style, randomizer, handler)
# _2fastbt
return result

Expand Down
Loading

0 comments on commit 46c3d2c

Please sign in to comment.