forked from Bitmessage/PyBitmessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_startup.py
374 lines (332 loc) · 15.1 KB
/
helper_startup.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
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
"""
Startup operations.
"""
# pylint: disable=too-many-branches,too-many-statements
import ctypes
import logging
import os
import platform
import socket
import sys
import time
from distutils.version import StrictVersion
from struct import pack
try:
import defaults
import helper_random
import paths
import state
from bmconfigparser import config, config_ready
except ImportError:
from . import defaults, helper_random, paths, state
from bmconfigparser import config, config_ready
try:
from plugins.plugin import get_plugin
except ImportError:
get_plugin = None
logger = logging.getLogger('default')
# The user may de-select Portable Mode in the settings if they want
# the config files to stay in the application data folder.
StoreConfigFilesInSameDirectoryAsProgramByDefault = False
def loadConfig():
"""Load the config"""
if state.appdata:
config.read(state.appdata + 'keys.dat')
# state.appdata must have been specified as a startup option.
needToCreateKeysFile = config.safeGet(
'bitmessagesettings', 'settingsversion') is None
if not needToCreateKeysFile:
logger.info(
'Loading config files from directory specified'
' on startup: %s', state.appdata)
else:
config.read(paths.lookupExeFolder() + 'keys.dat')
if config.safeGet('bitmessagesettings', 'settingsversion'):
logger.info('Loading config files from same directory as program.')
needToCreateKeysFile = False
state.appdata = paths.lookupExeFolder()
else:
# Could not load the keys.dat file in the program directory.
# Perhaps it is in the appdata directory.
state.appdata = paths.lookupAppdataFolder()
config.read(state.appdata + 'keys.dat')
needToCreateKeysFile = config.safeGet(
'bitmessagesettings', 'settingsversion') is None
if not needToCreateKeysFile:
logger.info(
'Loading existing config files from %s', state.appdata)
if needToCreateKeysFile:
# This appears to be the first time running the program; there is
# no config file (or it cannot be accessed). Create config file.
# config.add_section('bitmessagesettings')
config.read()
config.set('bitmessagesettings', 'settingsversion', '10')
if 'linux' in sys.platform:
config.set('bitmessagesettings', 'minimizetotray', 'false')
# This isn't implimented yet and when True on
# Ubuntu causes Bitmessage to disappear while
# running when minimized.
else:
config.set('bitmessagesettings', 'minimizetotray', 'true')
config.set(
'bitmessagesettings', 'defaultnoncetrialsperbyte',
str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte))
config.set(
'bitmessagesettings', 'defaultpayloadlengthextrabytes',
str(defaults.networkDefaultPayloadLengthExtraBytes))
config.set('bitmessagesettings', 'dontconnect', 'true')
# UI setting to stop trying to send messages after X days/months
# config.set('bitmessagesettings', 'stopresendingafterxdays', '')
# config.set('bitmessagesettings', 'stopresendingafterxmonths', '')
# Are you hoping to add a new option to the keys.dat file? You're in
# the right place for adding it to users who install the software for
# the first time. But you must also add it to the keys.dat file of
# existing users. To do that, search the class_sqlThread.py file
# for the text: "right above this line!"
if StoreConfigFilesInSameDirectoryAsProgramByDefault:
# Just use the same directory as the program and forget about
# the appdata folder
state.appdata = ''
logger.info(
'Creating new config files in same directory as program.')
else:
logger.info('Creating new config files in %s', state.appdata)
if not os.path.exists(state.appdata):
os.makedirs(state.appdata)
if not sys.platform.startswith('win'):
os.umask(0o077)
config.save()
else:
updateConfig()
config_ready.set()
def updateConfig():
"""Save the config"""
settingsversion = config.getint('bitmessagesettings', 'settingsversion')
if settingsversion == 1:
config.set('bitmessagesettings', 'socksproxytype', 'none')
config.set('bitmessagesettings', 'sockshostname', 'localhost')
config.set('bitmessagesettings', 'socksport', '9050')
config.set('bitmessagesettings', 'socksauthentication', 'false')
config.set('bitmessagesettings', 'socksusername', '')
config.set('bitmessagesettings', 'sockspassword', '')
config.set('bitmessagesettings', 'sockslisten', 'false')
config.set('bitmessagesettings', 'keysencrypted', 'false')
config.set('bitmessagesettings', 'messagesencrypted', 'false')
settingsversion = 2
# let class_sqlThread update SQL and continue
elif settingsversion == 4:
config.set(
'bitmessagesettings', 'defaultnoncetrialsperbyte',
str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte))
config.set(
'bitmessagesettings', 'defaultpayloadlengthextrabytes',
str(defaults.networkDefaultPayloadLengthExtraBytes))
settingsversion = 5
if settingsversion == 5:
config.set(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', '0')
config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', '0')
settingsversion = 7
if not config.has_option('bitmessagesettings', 'sockslisten'):
config.set('bitmessagesettings', 'sockslisten', 'false')
if not config.has_option('bitmessagesettings', 'userlocale'):
config.set('bitmessagesettings', 'userlocale', 'system')
if not config.has_option('bitmessagesettings', 'sendoutgoingconnections'):
config.set('bitmessagesettings', 'sendoutgoingconnections', 'True')
if not config.has_option('bitmessagesettings', 'useidenticons'):
config.set('bitmessagesettings', 'useidenticons', 'True')
if not config.has_option('bitmessagesettings', 'identiconsuffix'):
# acts as a salt
config.set(
'bitmessagesettings', 'identiconsuffix', ''.join(
helper_random.randomchoice(
"123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
"abcdefghijkmnopqrstuvwxyz") for x in range(12))
) # a twelve character pseudo-password to salt the identicons
# Add settings to support no longer resending messages after
# a certain period of time even if we never get an ack
if settingsversion == 7:
config.set('bitmessagesettings', 'stopresendingafterxdays', '')
config.set('bitmessagesettings', 'stopresendingafterxmonths', '')
settingsversion = 8
# With the change to protocol version 3, reset the user-settable
# difficulties to 1
if settingsversion == 8:
config.set(
'bitmessagesettings', 'defaultnoncetrialsperbyte',
str(defaults.networkDefaultProofOfWorkNonceTrialsPerByte))
config.set(
'bitmessagesettings', 'defaultpayloadlengthextrabytes',
str(defaults.networkDefaultPayloadLengthExtraBytes))
previousTotalDifficulty = int(
config.getint(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte')
) / 320
previousSmallMessageDifficulty = int(
config.getint(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes')
) / 14000
config.set(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte',
str(previousTotalDifficulty * 1000))
config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes',
str(previousSmallMessageDifficulty * 1000))
settingsversion = 9
# Adjust the required POW values for each of this user's addresses
# to conform to protocol v3 norms.
if settingsversion == 9:
for addressInKeysFile in config.addresses():
try:
previousTotalDifficulty = float(
config.getint(
addressInKeysFile, 'noncetrialsperbyte')) / 320
previousSmallMessageDifficulty = float(
config.getint(
addressInKeysFile, 'payloadlengthextrabytes')) / 14000
if previousTotalDifficulty <= 2:
previousTotalDifficulty = 1
if previousSmallMessageDifficulty < 1:
previousSmallMessageDifficulty = 1
config.set(
addressInKeysFile, 'noncetrialsperbyte',
str(int(previousTotalDifficulty * 1000)))
config.set(
addressInKeysFile, 'payloadlengthextrabytes',
str(int(previousSmallMessageDifficulty * 1000)))
except Exception:
continue
config.set('bitmessagesettings', 'maxdownloadrate', '0')
config.set('bitmessagesettings', 'maxuploadrate', '0')
settingsversion = 10
# sanity check
if config.safeGetInt(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte') == 0:
config.set(
'bitmessagesettings', 'maxacceptablenoncetrialsperbyte',
str(defaults.ridiculousDifficulty
* defaults.networkDefaultProofOfWorkNonceTrialsPerByte)
)
if config.safeGetInt(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes') == 0:
config.set(
'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes',
str(defaults.ridiculousDifficulty
* defaults.networkDefaultPayloadLengthExtraBytes)
)
if not config.has_option('bitmessagesettings', 'onionhostname'):
config.set('bitmessagesettings', 'onionhostname', '')
if not config.has_option('bitmessagesettings', 'onionport'):
config.set('bitmessagesettings', 'onionport', '8444')
if not config.has_option('bitmessagesettings', 'onionbindip'):
config.set('bitmessagesettings', 'onionbindip', '127.0.0.1')
if not config.has_option('bitmessagesettings', 'smtpdeliver'):
config.set('bitmessagesettings', 'smtpdeliver', '')
if not config.has_option(
'bitmessagesettings', 'hidetrayconnectionnotifications'):
config.set(
'bitmessagesettings', 'hidetrayconnectionnotifications', 'false')
if config.safeGetInt('bitmessagesettings', 'maxoutboundconnections') < 1:
config.set('bitmessagesettings', 'maxoutboundconnections', '8')
logger.warning('Your maximum outbound connections must be a number.')
# TTL is now user-specifiable. Let's add an option to save
# whatever the user selects.
if not config.has_option('bitmessagesettings', 'ttl'):
config.set('bitmessagesettings', 'ttl', '367200')
config.set('bitmessagesettings', 'settingsversion', str(settingsversion))
config.save()
def adjustHalfOpenConnectionsLimit():
"""Check and satisfy half-open connections limit (mainly XP and Vista)"""
if config.safeGet(
'bitmessagesettings', 'socksproxytype', 'none') != 'none':
state.maximumNumberOfHalfOpenConnections = 4
return
is_limited = False
try:
if sys.platform[0:3] == "win":
# Some XP and Vista systems can only have 10 outgoing
# connections at a time.
VER_THIS = StrictVersion(platform.version())
is_limited = (
StrictVersion("5.1.2600") <= VER_THIS
and StrictVersion("6.0.6000") >= VER_THIS
)
except ValueError:
pass
state.maximumNumberOfHalfOpenConnections = 9 if is_limited else 64
def fixSocket():
"""Add missing socket options and methods mainly on Windows"""
if sys.platform.startswith('linux'):
socket.SO_BINDTODEVICE = 25
if not sys.platform.startswith('win'):
return
# Python 2 on Windows doesn't define a wrapper for
# socket.inet_ntop but we can make one ourselves using ctypes
if not hasattr(socket, 'inet_ntop'):
addressToString = ctypes.windll.ws2_32.WSAAddressToStringA
def inet_ntop(family, host):
"""Converting an IP address in packed
binary format to string format"""
if family == socket.AF_INET:
if len(host) != 4:
raise ValueError("invalid IPv4 host")
host = pack("hH4s8s", socket.AF_INET, 0, host, "\0" * 8)
elif family == socket.AF_INET6:
if len(host) != 16:
raise ValueError("invalid IPv6 host")
host = pack("hHL16sL", socket.AF_INET6, 0, 0, host, 0)
else:
raise ValueError("invalid address family")
buf = "\0" * 64
lengthBuf = pack("I", len(buf))
addressToString(host, len(host), None, buf, lengthBuf)
return buf[0:buf.index("\0")]
socket.inet_ntop = inet_ntop
# Same for inet_pton
if not hasattr(socket, 'inet_pton'):
stringToAddress = ctypes.windll.ws2_32.WSAStringToAddressA
def inet_pton(family, host):
"""Converting an IP address in string format
to a packed binary format"""
buf = "\0" * 28
lengthBuf = pack("I", len(buf))
if stringToAddress(str(host),
int(family),
None,
buf,
lengthBuf) != 0:
raise socket.error("illegal IP address passed to inet_pton")
if family == socket.AF_INET:
return buf[4:8]
elif family == socket.AF_INET6:
return buf[8:24]
else:
raise ValueError("invalid address family")
socket.inet_pton = inet_pton
# These sockopts are needed on for IPv6 support
if not hasattr(socket, 'IPPROTO_IPV6'):
socket.IPPROTO_IPV6 = 41
if not hasattr(socket, 'IPV6_V6ONLY'):
socket.IPV6_V6ONLY = 27
def start_proxyconfig():
"""Check socksproxytype and start any proxy configuration plugin"""
if not get_plugin:
return
config_ready.wait()
proxy_type = config.safeGet('bitmessagesettings', 'socksproxytype')
if proxy_type and proxy_type not in ('none', 'SOCKS4a', 'SOCKS5'):
try:
proxyconfig_start = time.time()
if not get_plugin('proxyconfig', name=proxy_type)(config):
raise TypeError()
except TypeError:
# cannot import shutdown here ):
logger.error(
'Failed to run proxy config plugin %s',
proxy_type, exc_info=True)
config.setTemp('bitmessagesettings', 'dontconnect', 'true')
else:
logger.info(
'Started proxy config plugin %s in %s sec',
proxy_type, time.time() - proxyconfig_start)