-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
169 lines (145 loc) · 5.82 KB
/
core.py
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
''' Main module '''
#
# core.py
#
# Copyright (C) 2009 Lee Nguyen <[email protected]>
#
# Basic plugin template created by:
# Copyright (C) 2008 Martijn Voncken <[email protected]>
# Copyright (C) 2007-2009 Andrew Resch <[email protected]>
# Copyright (C) 2009 Damien Churchill <[email protected]>
#
# Deluge is free software.
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with deluge. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
#
from deluge.log import LOG as log
from deluge.plugins.pluginbase import CorePluginBase
import deluge.component as component
import deluge.configmanager
from deluge.core.rpcserver import export
from twisted.internet.task import LoopingCall
import piaportforward.pia_port_native as pia_port
DEFAULT_PREFS = {
'pia_username': '',
'pia_password': '',
}
class Core(CorePluginBase):
''' Core module '''
def __init__(self, plugin_name):
super(Core, self).__init__(plugin_name)
self.config = deluge.configmanager.ConfigManager(
'piaportforward.conf', DEFAULT_PREFS)
self.pia_client_id = pia_port.generate_client_id()
self.pia_username = ''
self.pia_password = ''
self.successfully_acquired_port = False
self.update_status_fast_timer = LoopingCall(self.fast_check)
self.update_status_slow_timer = LoopingCall(self.slow_check)
def enable(self):
''' enable '''
log.info('Enabling')
self.pia_username = self.config['pia_username']
self.pia_password = self.config['pia_password']
self.successfully_acquired_port = False
time_in_sec_interval = 5
self.update_status_fast_timer.start(time_in_sec_interval)
time_in_sec_interval = 3600
self.update_status_slow_timer.start(time_in_sec_interval)
def disable(self):
''' disable '''
log.info('Disabling')
self.update_status_fast_timer.stop()
self.update_status_slow_timer.stop()
self.config['pia_username'] = self.pia_username
self.config['pia_password'] = self.pia_password
def update(self):
''' update '''
pass
def slow_check(self):
'''
callback routine to check the VPN server
periodically just in case the VPN server
expires our port request
'''
if self.successfully_acquired_port:
log.info('Slow port refresh')
self.refresh_connection()
def fast_check(self):
'''
callback routine to check the VPN server
for an open port frequently
'''
if not self.successfully_acquired_port:
log.info('Fast port refresh')
self.refresh_connection()
def refresh_connection(self):
''' routine to request an open listening port '''
log.info('Refreshing listening port...')
local_ip = pia_port.get_active_local_ip()
if not local_ip.startswith('10.'):
log.info('Not a VPN local IP')
return
core = component.get('Core')
previous_listen_port = core.get_listen_port()
log.info('Current listening port: {}'.format(previous_listen_port))
def acquire_port(is_open):
''' Callback after test_listen_port is complete '''
if is_open:
log.info('port is open')
self.successfully_acquired_port = True
return
log.info('port is not open')
self.successfully_acquired_port = False
self.pia_username = self.config['pia_username']
self.pia_password = self.config['pia_password']
new_port = pia_port.acquire_port(self.pia_username,
self.pia_password,
self.pia_client_id,
local_ip,
log.info)
if new_port is None:
log.info('Failed to acquire port')
return
if new_port == previous_listen_port:
log.info(
'Something wrong with the port forward. Same port as the previous.')
return
core.set_config({'random_port': False})
core.set_config({'listen_ports': [new_port, new_port]})
log.info('Config successfully changed')
core.test_listen_port().addCallback(acquire_port)
@export
def set_config(self, config):
''' Sets the config dictionary '''
for key in config.keys():
self.config[key] = config[key]
self.config.save()
@export
def get_config(self):
''' Returns the config dictionary '''
return self.config.config