Skip to content

Commit

Permalink
Merge pull request #1325 from rjruigrok/exitnode-switch
Browse files Browse the repository at this point in the history
Exiting data over tunnels is only allowed after opt-in via settings
  • Loading branch information
NielsZeilemaker committed Apr 20, 2015
2 parents faac4f6 + b3439a4 commit 17ac69b
Show file tree
Hide file tree
Showing 16 changed files with 627 additions and 174 deletions.
14 changes: 12 additions & 2 deletions Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,13 @@ def create_engine_wrapper(self, lm_network_engine_wrapper_created_callback, psta
if not self.cew_scheduled:
self.ltmgr = self.session.lm.ltmgr
dht_ok = not isinstance(self.tdef, TorrentDefNoMetainfo) or self.ltmgr.is_dht_ready()
session_ok = self.ltmgr.tunnels_ready(self) == 1
tunnel_community = self.ltmgr.trsession.lm.tunnel_community
if tunnel_community:
tunnels_ready = tunnel_community.tunnels_ready(self.get_hops(), self.get_def().is_anonymous())
else:
tunnels_ready = 1

session_ok = tunnels_ready == 1

if not self.ltmgr or not dht_ok or not session_ok:
self._logger.info(u"LTMGR/DHT/session not ready, rescheduling create_engine_wrapper")
Expand Down Expand Up @@ -892,7 +898,11 @@ def network_get_state(self, usercallback, getpeerlist, sessioncalling=False):
if self.dlstate != DLSTATUS_CIRCUITS:
progress = self.progressbeforestop
else:
progress = self.ltmgr.tunnels_ready(self)
tunnel_community = self.ltmgr.trsession.lm.tunnel_community
if tunnel_community:
progress = tunnel_community.tunnels_ready(self.get_hops(), self.get_def().is_anonymous())
else:
progress = 1
ds = DownloadState(self, self.dlstate, self.error, progress)
else:
(status, stats, seeding_stats, logmsgs) = self.network_get_stats(getpeerlist)
Expand Down
16 changes: 0 additions & 16 deletions Tribler/Core/Libtorrent/LibtorrentMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,6 @@ def get_session(self, hops=0):

return self.ltsessions[hops]

def tunnels_ready(self, download):
hops = download.get_hops()
if hops > 0:
tunnel_community = self.trsession.lm.tunnel_community
if tunnel_community:
if download.get_def().is_anonymous():
current_hops = tunnel_community.circuits_needed.get(hops, 0)
tunnel_community.circuits_needed[hops] = max(1, current_hops)
return bool(tunnel_community.active_data_circuits(hops))
else:
tunnel_community.circuits_needed[hops] = tunnel_community.settings.max_circuits
return min(1, len(tunnel_community.active_data_circuits(hops)) /
float(tunnel_community.settings.min_circuits))
return 0
return 1

def shutdown(self):
# Save DHT state
dhtstate_file = open(os.path.join(self.trsession.get_state_dir(), DHTSTATE_FILENAME), 'w')
Expand Down
16 changes: 16 additions & 0 deletions Tribler/Core/SessionConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ def get_tunnel_community_socks5_listen_ports(self):
path = u'tunnel_community~socks5_listen_ports~'
return [self._get_random_port(path + unicode(index)) if port < 0 else port for index, port in enumerate(ports)]

def set_tunnel_community_hs_timeout_switch(self, value):
self.sessconfig.set(u'tunnel_community', u'hs_timeout_switch', value)

def get_tunnel_community_hs_timeout_switch(self):
""" Returns whether hidden services switch to anonymous downloading on timeout
@return Boolean. """
return self.sessconfig.get(u'tunnel_community', u'hs_timeout_switch')

def set_tunnel_community_exitnode_enabled(self, value):
self.sessconfig.set(u'tunnel_community', u'exitnode_enabled', value)

def get_tunnel_community_exitnode_enabled(self):
""" Returns whether being an exitnode is allowed
@return Boolean. """
return self.sessconfig.get(u'tunnel_community', u'exitnode_enabled')

def get_listen_port(self):
""" Returns the current UDP/TCP listen port.
@return Port number. """
Expand Down
5 changes: 4 additions & 1 deletion Tribler/Core/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
# Version 2: as released in Tribler 4.5.0
# Version 3: cleanup unused params
# Version 4: remove swift
#
# Version 7: exitnode optin switch added

SESSDEFAULTS_VERSION = 7
sessdefaults = OrderedDict()
Expand Down Expand Up @@ -73,6 +73,9 @@
sessdefaults['tunnel_community']['optin_dialog_shown'] = False
sessdefaults['tunnel_community']['enabled'] = False
sessdefaults['tunnel_community']['socks5_listen_ports'] = [-1] * 5
sessdefaults['tunnel_community']['exitnode_enabled'] = False
sessdefaults['tunnel_community']['hs_timeout_switch'] = True


# Mainline DHT settings
sessdefaults['mainline_dht'] = OrderedDict()
Expand Down
4 changes: 3 additions & 1 deletion Tribler/Main/tribler_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
SKIP_TUNNEL_DIALOG = os.environ.get("TRIBLER_SKIP_OPTIN_DLG", "False") == "True"
# used by the anon tunnel tests as there's no way to mess with the Session before running the test ATM.
FORCE_ENABLE_TUNNEL_COMMUNITY = False
TUNNEL_COMMUNITY_DO_TEST = True

#
#
Expand Down Expand Up @@ -519,7 +520,8 @@ def define_communities(*args):
if self.sconfig.get_tunnel_community_enabled():
keypair = dispersy.crypto.generate_key(u"curve25519")
dispersy_member = dispersy.get_member(private_key=dispersy.crypto.key_to_bin(keypair),)
settings = TunnelSettings(session.get_install_dir())
settings = TunnelSettings(session.get_install_dir(), tribler_session=session)
settings.do_test = TUNNEL_COMMUNITY_DO_TEST
tunnel_kwargs = {'tribler_session': session, 'settings': settings}

self.tunnel_community = dispersy.define_auto_load(HiddenTunnelCommunity, dispersy_member, load=True,
Expand Down
4 changes: 2 additions & 2 deletions Tribler/Main/vwxGUI/MainFrame.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ def do_gui():
def monitorHiddenSerivcesProgress(self, ds, tdef, dscfg, selectedFiles):
if ds.get_status() in [DLSTATUS_ALLOCATING_DISKSPACE, DLSTATUS_HASHCHECKING, DLSTATUS_WAITING4HASHCHECK]:
return (5.0, True)

if ds.get_current_speed(DOWNLOAD) == 0:
if ds.get_current_speed(DOWNLOAD) == 0 and self.utility.session.get_tunnel_community_hs_timeout_switch():
download = ds.get_download()
self.utility.session.remove_download(download)

Expand Down
12 changes: 7 additions & 5 deletions Tribler/Main/vwxGUI/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,8 @@ def AddComponents(self):
self.circuit_list.InsertColumn(0, 'ID', wx.LIST_FORMAT_LEFT, 25)
self.circuit_list.InsertColumn(1, 'Online', wx.LIST_FORMAT_RIGHT, 50)
self.circuit_list.InsertColumn(2, 'Hops', wx.LIST_FORMAT_RIGHT, 45)
self.circuit_list.InsertColumn(3, u'Bytes \u2191', wx.LIST_FORMAT_RIGHT, 63)
self.circuit_list.InsertColumn(4, u'Bytes \u2193', wx.LIST_FORMAT_RIGHT, 63)
self.circuit_list.InsertColumn(3, u'Bytes \u2191', wx.LIST_FORMAT_RIGHT, 83)
self.circuit_list.InsertColumn(4, u'Bytes \u2193', wx.LIST_FORMAT_RIGHT, 83)
self.circuit_list.InsertColumn(5, 'Uptime', wx.LIST_FORMAT_RIGHT, 54)
self.circuit_list.setResizeColumn(0)
self.circuit_list.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
Expand All @@ -660,7 +660,8 @@ def AddComponents(self):
self.log_text = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.BORDER_SIMPLE | wx.HSCROLL & wx.VSCROLL)
self.log_text.SetEditable(False)
self.log_text.Show(self.fullscreen)
self.num_circuits_label = wx.StaticText(self, -1, "You have 0 circuit(s); 0 relay(s); 0 exit socket(s)")
self.num_circuits_label = wx.StaticText(self, -1, "You have 0 circuit(s); 0 relay(s); \
0 exit socket(s); 0 candidate(s)")

self.vSizer = wx.BoxSizer(wx.VERTICAL)
self.vSizer.Add(self.circuit_list, 1, wx.EXPAND | wx.RESERVE_SPACE_EVEN_IF_HIDDEN, 0)
Expand Down Expand Up @@ -697,10 +698,11 @@ def OnUpdateCircuits(self, event):
return

if self.fullscreen:
self.num_circuits_label.SetLabel("You have %d circuit(s); %d relay(s); %d exit socket(s)" %
self.num_circuits_label.SetLabel("You have %d circuit(s); %d relay(s); %d exit socket(s); %d candidate(s)" %
(len(self.tunnel_community.circuits),
len(self.tunnel_community.relay_from_to),
len(self.tunnel_community.exit_sockets)))
len(self.tunnel_community.exit_sockets),
sum(1 for _ in self.tunnel_community.dispersy_yield_verified_candidates())))

new_circuits = dict(self.tunnel_community.circuits)
self.circuits = {k: v for k, v in new_circuits.iteritems() if v.goal_hops == self.hops or self.hops < 0}
Expand Down
36 changes: 36 additions & 0 deletions Tribler/Main/vwxGUI/settingsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ def __init__(self):
self._bandwidth_panel, self._bandwidth_id = self.__create_s3(tree_root, hsizer)
self._seeding_panel, self._seeding_id = self.__create_s4(tree_root, hsizer)
self._experimental_panel, self._experimental_id = self.__create_s5(tree_root, hsizer)
self._tunnel_panel, self._tunnel_id = self.__create_s6(tree_root, hsizer)

self._general_panel.Show(True)
self._conn_panel.Show(False)
self._bandwidth_panel.Show(False)
self._seeding_panel.Show(False)
self._experimental_panel.Show(False)
self._tunnel_panel.Show(False)

self._save_btn = wx.Button(self, wx.ID_OK, label="Save")
self._cancel_btn = wx.Button(self, wx.ID_CANCEL, label="Cancel")
Expand Down Expand Up @@ -207,6 +209,16 @@ def saveAll(self, event):
self.utility.write_config('use_webui', useWebUI)
restart = True

becomeExitNode = self._become_exitnode.IsChecked()
if becomeExitNode != scfg.get_tunnel_community_exitnode_enabled():
scfg.set_tunnel_community_exitnode_enabled(becomeExitNode)
restart = True

switchHsOnTimeout = self._switch_hs_timeout.IsChecked()
if switchHsOnTimeout != scfg.get_tunnel_community_hs_timeout_switch():
scfg.set_tunnel_community_hs_timeout_switch(switchHsOnTimeout)
restart = True

valwebuiport = self._webui_port.GetValue()
if valwebuiport != str(self.utility.read_config('webui_port')):
self.utility.write_config('webui_port', valwebuiport)
Expand Down Expand Up @@ -353,6 +365,7 @@ def saveDefaultDownloadConfig(self, scfg):

scfg.save(cfgfilename)


def moveCollectedTorrents(self, old_dir, new_dir):
def rename_or_merge(old, new, ignore=True):
if os.path.exists(old):
Expand Down Expand Up @@ -729,3 +742,26 @@ def __create_s5(self, tree_root, sizer):
self._webui_port.SetValue(str(self.utility.read_config('webui_port')))

return exp_panel, item_id


def __create_s6(self, tree_root, sizer):
exp_panel, exp_vsizer = create_section(self, sizer, "Anonimity")

item_id = self._tree_ctrl.AppendItem(tree_root, "Anonimity", data=wx.TreeItemData(exp_panel))

exp_s1_sizer = create_subsection(exp_panel, exp_vsizer, "Relaying", 1, 3)
self._become_exitnode = wx.CheckBox(exp_panel, label="Allow being an exit node")
exp_s1_sizer.Add(self._become_exitnode, 0, wx.EXPAND)
self._switch_hs_timeout = wx.CheckBox(exp_panel, label="Switch from hidden services to exit nodes")
exp_s1_sizer.Add(self._switch_hs_timeout, 0, wx.EXPAND)

exp_s1_faq_text = wx.StaticText(
exp_panel, label="By allowing Tribler to be an exit node, it's possible to become a proxy for someone elses traffic. \nThis may cause problems in some countries.")
exp_vsizer.Add(exp_s1_faq_text, 0, wx.EXPAND | wx.TOP, 10)

# load values
self._become_exitnode.SetValue(self.utility.session.get_tunnel_community_exitnode_enabled())
self._switch_hs_timeout.SetValue(self.utility.session.get_tunnel_community_hs_timeout_switch())


return exp_panel, item_id
2 changes: 2 additions & 0 deletions Tribler/Test/test_as_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def tearDown(self, annotate=True):
for dc in delayed_calls:
self._logger.debug("> %s" % dc)
self.assertFalse(delayed_calls, "The reactor was dirty when tearing down the test")
self.assertFalse(Session.has_instance(), 'A session instance is still present when tearing down the test')

def tearDownCleanup(self):
self.setUpCleanup()
Expand Down Expand Up @@ -301,6 +302,7 @@ class TestGuiAsServer(TestAsServer):
"""

def setUp(self):
self.assertFalse(Session.has_instance(), 'A session instance is already present when setting up the test')
AbstractServer.setUp(self, annotate=False)

self.app = wx.GetApp()
Expand Down
Loading

0 comments on commit 17ac69b

Please sign in to comment.