summaryrefslogtreecommitdiffstats
path: root/deluge/core/preferencesmanager.py
blob: c250130c01e6b472e0c9f2bb36bf78edeb11743b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008-2010 Andrew Resch <andrewresch@gmail.com>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#


from __future__ import unicode_literals

import logging
import os
import platform
import random
import threading

from twisted.internet.task import LoopingCall

import deluge.common
import deluge.component as component
import deluge.configmanager
from deluge._libtorrent import lt
from deluge.event import ConfigValueChangedEvent

try:
    import GeoIP
except ImportError:
    GeoIP = None

try:
    from urllib.parse import quote_plus
    from urllib.request import urlopen
except ImportError:
    from urllib import quote_plus
    from urllib2 import urlopen

log = logging.getLogger(__name__)

DEFAULT_PREFS = {
    'send_info': False,
    'info_sent': 0.0,
    'daemon_port': 58846,
    'allow_remote': False,
    'pre_allocate_storage': False,
    'download_location': deluge.common.get_default_download_dir(),
    'listen_ports': [6881, 6891],
    'listen_interface': '',
    'outgoing_interface': '',
    'random_port': True,
    'listen_random_port': None,
    'listen_use_sys_port': False,
    'listen_reuse_port': True,
    'outgoing_ports': [0, 0],
    'random_outgoing_ports': True,
    'copy_torrent_file': False,
    'del_copy_torrent_file': False,
    'torrentfiles_location': deluge.common.get_default_download_dir(),
    'plugins_location': os.path.join(deluge.configmanager.get_config_dir(), 'plugins'),
    'prioritize_first_last_pieces': False,
    'sequential_download': False,
    'dht': True,
    'upnp': True,
    'natpmp': True,
    'utpex': True,
    'lsd': True,
    'enc_in_policy': 1,
    'enc_out_policy': 1,
    'enc_level': 2,
    'max_connections_global': 200,
    'max_upload_speed': -1.0,
    'max_download_speed': -1.0,
    'max_upload_slots_global': 4,
    'max_half_open_connections': (
        lambda: deluge.common.windows_check()
        and (lambda: deluge.common.vista_check() and 4 or 8)()
        or 50
    )(),
    'max_connections_per_second': 20,
    'ignore_limits_on_local_network': True,
    'max_connections_per_torrent': -1,
    'max_upload_slots_per_torrent': -1,
    'max_upload_speed_per_torrent': -1,
    'max_download_speed_per_torrent': -1,
    'enabled_plugins': [],
    'add_paused': False,
    'max_active_seeding': 5,
    'max_active_downloading': 3,
    'max_active_limit': 8,
    'dont_count_slow_torrents': False,
    'queue_new_to_top': False,
    'stop_seed_at_ratio': False,
    'remove_seed_at_ratio': False,
    'stop_seed_ratio': 2.00,
    'share_ratio_limit': 2.00,
    'seed_time_ratio_limit': 7.00,
    'seed_time_limit': 180,
    'auto_managed': True,
    'move_completed': False,
    'move_completed_path': deluge.common.get_default_download_dir(),
    'move_completed_paths_list': [],
    'download_location_paths_list': [],
    'path_chooser_show_chooser_button_on_localhost': True,
    'path_chooser_auto_complete_enabled': True,
    'path_chooser_accelerator_string': 'Tab',
    'path_chooser_max_popup_rows': 20,
    'path_chooser_show_hidden_files': False,
    'new_release_check': True,
    'proxy': {
        'type': 0,
        'hostname': '',
        'username': '',
        'password': '',
        'port': 8080,
        'proxy_hostnames': True,
        'proxy_peer_connections': True,
        'proxy_tracker_connections': True,
        'force_proxy': False,
        'anonymous_mode': False,
    },
    'peer_tos': '0x00',
    'rate_limit_ip_overhead': True,
    'geoip_db_location': '/usr/share/GeoIP/GeoIP.dat',
    'cache_size': 512,
    'cache_expiry': 60,
    'auto_manage_prefer_seeds': False,
    'shared': False,
    'super_seeding': False,
}


class PreferencesManager(component.Component):
    def __init__(self):
        component.Component.__init__(self, 'PreferencesManager')
        self.config = deluge.configmanager.ConfigManager('core.conf', DEFAULT_PREFS)
        if 'proxies' in self.config:
            log.warning(
                'Updating config file for proxy, using "peer" values to fill new "proxy" setting'
            )
            self.config['proxy'].update(self.config['proxies']['peer'])
            log.warning('New proxy config is: %s', self.config['proxy'])
            del self.config['proxies']
        if 'i2p_proxy' in self.config and self.config['i2p_proxy']['hostname']:
            self.config['proxy'].update(self.config['i2p_proxy'])
            self.config['proxy']['type'] = 6
            del self.config['i2p_proxy']
        if 'anonymous_mode' in self.config:
            self.config['proxy']['anonymous_mode'] = self.config['anonymous_mode']
            del self.config['anonymous_mode']
        if 'proxy' in self.config:
            for key in DEFAULT_PREFS['proxy']:
                if key not in self.config['proxy']:
                    self.config['proxy'][key] = DEFAULT_PREFS['proxy'][key]

        self.core = component.get('Core')
        self.new_release_timer = None

    def start(self):
        # Set the initial preferences on start-up
        for key in DEFAULT_PREFS:
            self.do_config_set_func(key, self.config[key])

        self.config.register_change_callback(self._on_config_value_change)

    def stop(self):
        if self.new_release_timer and self.new_release_timer.running:
            self.new_release_timer.stop()

    # Config set functions
    def do_config_set_func(self, key, value):
        on_set_func = getattr(self, '_on_set_' + key, None)
        if on_set_func:
            if log.isEnabledFor(logging.DEBUG):
                log.debug('Config key: %s set to %s..', key, value)
            on_set_func(key, value)

    def _on_config_value_change(self, key, value):
        if self.get_state() == 'Started':
            self.do_config_set_func(key, value)
            component.get('EventManager').emit(ConfigValueChangedEvent(key, value))

    def _on_set_torrentfiles_location(self, key, value):
        if self.config['copy_torrent_file']:
            try:
                os.makedirs(value)
            except OSError as ex:
                log.debug('Unable to make directory: %s', ex)

    def _on_set_listen_ports(self, key, value):
        self.__set_listen_on()

    def _on_set_listen_interface(self, key, value):
        self.__set_listen_on()

    def _on_set_outgoing_interface(self, key, value):
        """Set interface name or IP address for outgoing BitTorrent connections."""
        value = value.strip() if value else ''
        self.core.apply_session_settings({'outgoing_interfaces': value})

    def _on_set_random_port(self, key, value):
        self.__set_listen_on()

    def __set_listen_on(self):
        """ Set the ports and interface address to listen for incoming connections on."""
        if self.config['random_port']:
            if not self.config['listen_random_port']:
                self.config['listen_random_port'] = random.randrange(49152, 65525)
            listen_ports = [
                self.config['listen_random_port']
            ] * 2  # use single port range
        else:
            self.config['listen_random_port'] = None
            listen_ports = self.config['listen_ports']

        if self.config['listen_interface']:
            interface = self.config['listen_interface'].strip()
        else:
            interface = '0.0.0.0'

        log.debug(
            'Listen Interface: %s, Ports: %s with use_sys_port: %s',
            interface,
            listen_ports,
            self.config['listen_use_sys_port'],
        )
        interfaces = [
            '%s:%s' % (interface, port)
            for port in range(listen_ports[0], listen_ports[1] + 1)
        ]
        self.core.apply_session_settings(
            {
                'listen_system_port_fallback': self.config['listen_use_sys_port'],
                'listen_interfaces': ','.join(interfaces),
            }
        )

    def _on_set_outgoing_ports(self, key, value):
        self.__set_outgoing_ports()

    def _on_set_random_outgoing_ports(self, key, value):
        self.__set_outgoing_ports()

    def __set_outgoing_ports(self):
        port = (
            0
            if self.config['random_outgoing_ports']
            else self.config['outgoing_ports'][0]
        )
        if port:
            num_ports = (
                self.config['outgoing_ports'][1] - self.config['outgoing_ports'][0]
            )
            num_ports = num_ports if num_ports > 1 else 5
        else:
            num_ports = 0
        log.debug('Outgoing port set to %s with range: %s', port, num_ports)
        self.core.apply_session_settings(
            {'outgoing_port': port, 'num_outgoing_ports': num_ports}
        )

    def _on_set_peer_tos(self, key, value):
        try:
            self.core.apply_session_setting('peer_tos', int(value, 16))
        except ValueError as ex:
            log.error('Invalid tos byte: %s', ex)

    def _on_set_dht(self, key, value):
        lt_bootstraps = self.core.session.get_settings()['dht_bootstrap_nodes']
        # Update list of lt bootstraps, using set to remove duplicates.
        dht_bootstraps = set(
            lt_bootstraps.split(',')
            + [
                'router.bittorrent.com:6881',
                'router.utorrent.com:6881',
                'router.bitcomet.com:6881',
                'dht.transmissionbt.com:6881',
                'dht.aelitis.com:6881',
            ]
        )
        self.core.apply_session_settings(
            {'dht_bootstrap_nodes': ','.join(dht_bootstraps), 'enable_dht': value}
        )

    def _on_set_upnp(self, key, value):
        self.core.apply_session_setting('enable_upnp', value)

    def _on_set_natpmp(self, key, value):
        self.core.apply_session_setting('enable_natpmp', value)

    def _on_set_lsd(self, key, value):
        self.core.apply_session_setting('enable_lsd', value)

    def _on_set_utpex(self, key, value):
        if value:
            self.core.session.add_extension('ut_pex')

    def _on_set_enc_in_policy(self, key, value):
        self._on_set_encryption(key, value)

    def _on_set_enc_out_policy(self, key, value):
        self._on_set_encryption(key, value)

    def _on_set_enc_level(self, key, value):
        self._on_set_encryption(key, value)

    def _on_set_encryption(self, key, value):
        # Convert Deluge enc_level values to libtorrent enc_level values.
        pe_enc_level = {
            0: lt.enc_level.plaintext,
            1: lt.enc_level.rc4,
            2: lt.enc_level.both,
        }
        self.core.apply_session_settings(
            {
                'out_enc_policy': lt.enc_policy(self.config['enc_out_policy']),
                'in_enc_policy': lt.enc_policy(self.config['enc_in_policy']),
                'allowed_enc_level': lt.enc_level(
                    pe_enc_level[self.config['enc_level']]
                ),
                'prefer_rc4': True,
            }
        )

    def _on_set_max_connections_global(self, key, value):
        self.core.apply_session_setting('connections_limit', value)

    def _on_set_max_upload_speed(self, key, value):
        # We need to convert Kb/s to B/s
        value = -1 if value < 0 else int(value * 1024)
        self.core.apply_session_setting('upload_rate_limit', value)

    def _on_set_max_download_speed(self, key, value):
        # We need to convert Kb/s to B/s
        value = -1 if value < 0 else int(value * 1024)
        self.core.apply_session_setting('download_rate_limit', value)

    def _on_set_max_upload_slots_global(self, key, value):
        self.core.apply_session_setting('unchoke_slots_limit', value)

    def _on_set_max_half_open_connections(self, key, value):
        self.core.apply_session_setting('half_open_limit', value)

    def _on_set_max_connections_per_second(self, key, value):
        self.core.apply_session_setting('connection_speed', value)

    def _on_set_ignore_limits_on_local_network(self, key, value):
        self.core.apply_session_setting('ignore_limits_on_local_network', value)

    def _on_set_share_ratio_limit(self, key, value):
        # This value is a float percentage in deluge, but libtorrent needs int percentage.
        self.core.apply_session_setting('share_ratio_limit', int(value * 100))

    def _on_set_seed_time_ratio_limit(self, key, value):
        # This value is a float percentage in deluge, but libtorrent needs int percentage.
        self.core.apply_session_setting('seed_time_ratio_limit', int(value * 100))

    def _on_set_seed_time_limit(self, key, value):
        # This value is stored in minutes in deluge, but libtorrent wants seconds
        self.core.apply_session_setting('seed_time_limit', int(value * 60))

    def _on_set_max_active_downloading(self, key, value):
        self.core.apply_session_setting('active_downloads', value)

    def _on_set_max_active_seeding(self, key, value):
        self.core.apply_session_setting('active_seeds', value)

    def _on_set_max_active_limit(self, key, value):
        self.core.apply_session_setting('active_limit', value)

    def _on_set_dont_count_slow_torrents(self, key, value):
        self.core.apply_session_setting('dont_count_slow_torrents', value)

    def _on_set_send_info(self, key, value):
        """sends anonymous stats home"""
        log.debug('Sending anonymous stats..')

        class SendInfoThread(threading.Thread):
            def __init__(self, config):
                self.config = config
                threading.Thread.__init__(self)

            def run(self):
                import time

                now = time.time()
                # check if we've done this within the last week or never
                if (now - self.config['info_sent']) >= (60 * 60 * 24 * 7):
                    try:
                        url = (
                            'http://deluge-torrent.org/stats_get.php?processor='
                            + platform.machine()
                            + '&python='
                            + platform.python_version()
                            + '&deluge='
                            + deluge.common.get_version()
                            + '&os='
                            + platform.system()
                            + '&plugins='
                            + quote_plus(':'.join(self.config['enabled_plugins']))
                        )
                        urlopen(url)
                    except IOError as ex:
                        log.debug('Network error while trying to send info: %s', ex)
                    else:
                        self.config['info_sent'] = now

        if value:
            SendInfoThread(self.config).start()

    def _on_set_new_release_check(self, key, value):
        if value:
            log.debug('Checking for new release..')
            threading.Thread(target=self.core.get_new_release).start()
            if self.new_release_timer and self.new_release_timer.running:
                self.new_release_timer.stop()
            # Set a timer to check for a new release every 3 days
            self.new_release_timer = LoopingCall(
                self._on_set_new_release_check, 'new_release_check', True
            )
            self.new_release_timer.start(72 * 60 * 60, False)
        else:
            if self.new_release_timer and self.new_release_timer.running:
                self.new_release_timer.stop()

    def _on_set_proxy(self, key, value):
        # Initialise with type none and blank hostnames.
        proxy_settings = {
            'proxy_type': lt.proxy_type_t.none,
            'i2p_hostname': '',
            'proxy_hostname': '',
            'proxy_hostnames': value['proxy_hostnames'],
            'proxy_peer_connections': value['proxy_peer_connections'],
            'proxy_tracker_connections': value['proxy_tracker_connections'],
            'force_proxy': value['force_proxy'],
            'anonymous_mode': value['anonymous_mode'],
        }

        if value['type'] == lt.proxy_type_t.i2p_proxy:
            proxy_settings.update(
                {
                    'proxy_type': lt.proxy_type_t.i2p_proxy,
                    'i2p_hostname': value['hostname'],
                    'i2p_port': value['port'],
                }
            )
        elif value['type'] != lt.proxy_type_t.none:
            proxy_settings.update(
                {
                    'proxy_type': value['type'],
                    'proxy_hostname': value['hostname'],
                    'proxy_port': value['port'],
                    'proxy_username': value['username'],
                    'proxy_password': value['password'],
                }
            )

        self.core.apply_session_settings(proxy_settings)

    def _on_set_rate_limit_ip_overhead(self, key, value):
        self.core.apply_session_setting('rate_limit_ip_overhead', value)

    def _on_set_geoip_db_location(self, key, geoipdb_path):
        # Load the GeoIP DB for country look-ups if available
        if os.path.exists(geoipdb_path):
            try:
                self.core.geoip_instance = GeoIP.open(
                    geoipdb_path, GeoIP.GEOIP_STANDARD
                )
            except AttributeError:
                log.warning('GeoIP Unavailable')
        else:
            log.warning('Unable to find GeoIP database file: %s', geoipdb_path)

    def _on_set_cache_size(self, key, value):
        self.core.apply_session_setting('cache_size', value)

    def _on_set_cache_expiry(self, key, value):
        self.core.apply_session_setting('cache_expiry', value)

    def _on_auto_manage_prefer_seeds(self, key, value):
        self.core.apply_session_setting('auto_manage_prefer_seeds', value)