2018-04-19 13:02:12 +02:00
|
|
|
"""
|
|
|
|
BMConfigParser class definition and default configuration settings
|
|
|
|
"""
|
2019-12-24 15:02:37 +01:00
|
|
|
# pylint: disable=no-self-use, arguments-differ
|
2019-09-26 16:08:19 +02:00
|
|
|
import configparser
|
2017-01-15 10:50:02 +01:00
|
|
|
import shutil
|
|
|
|
import os
|
2018-04-19 13:02:12 +02:00
|
|
|
from datetime import datetime
|
2016-12-06 10:47:39 +01:00
|
|
|
|
2017-01-15 10:50:02 +01:00
|
|
|
import state
|
2018-04-19 13:02:12 +02:00
|
|
|
from singleton import Singleton
|
2017-01-11 14:27:19 +01:00
|
|
|
|
2017-05-15 12:18:07 +02:00
|
|
|
BMConfigDefaults = {
|
|
|
|
"bitmessagesettings": {
|
|
|
|
"maxaddrperstreamsend": 500,
|
|
|
|
"maxbootstrapconnections": 20,
|
2017-05-24 16:49:16 +02:00
|
|
|
"maxdownloadrate": 0,
|
2017-05-15 12:18:07 +02:00
|
|
|
"maxoutboundconnections": 8,
|
|
|
|
"maxtotalconnections": 200,
|
2017-05-24 16:49:16 +02:00
|
|
|
"maxuploadrate": 0,
|
2018-03-22 16:48:21 +01:00
|
|
|
"apiinterface": "127.0.0.1",
|
|
|
|
"apiport": 8442
|
2017-05-24 16:49:16 +02:00
|
|
|
},
|
2017-07-10 07:08:10 +02:00
|
|
|
"threads": {
|
|
|
|
"receive": 3,
|
|
|
|
},
|
2017-05-24 16:49:16 +02:00
|
|
|
"network": {
|
2017-08-09 17:34:47 +02:00
|
|
|
"bind": '',
|
2018-02-03 11:46:39 +01:00
|
|
|
"dandelion": 90,
|
2017-05-27 19:09:21 +02:00
|
|
|
},
|
|
|
|
"inventory": {
|
|
|
|
"storage": "sqlite",
|
2017-06-02 15:43:35 +02:00
|
|
|
"acceptmismatch": False,
|
2017-05-15 12:18:07 +02:00
|
|
|
},
|
2017-06-24 12:17:01 +02:00
|
|
|
"knownnodes": {
|
|
|
|
"maxnodes": 20000,
|
|
|
|
},
|
2017-05-15 12:18:07 +02:00
|
|
|
"zlib": {
|
|
|
|
'maxsize': 1048576
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:27:19 +01:00
|
|
|
|
2018-04-19 13:02:12 +02:00
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
@Singleton
|
2019-09-26 16:08:19 +02:00
|
|
|
class BMConfigParser(configparser.ConfigParser):
|
|
|
|
"""Singleton class inherited from ConfigParsedadfeConfigParser
|
2018-04-19 13:02:12 +02:00
|
|
|
with additional methods specific to bitmessage config."""
|
2019-12-24 15:02:37 +01:00
|
|
|
# pylint: disable=too-many-ancestors
|
2019-09-05 17:31:16 +02:00
|
|
|
_temp = {}
|
|
|
|
|
2016-12-06 10:47:39 +01:00
|
|
|
def set(self, section, option, value=None):
|
2016-12-06 11:01:17 +01:00
|
|
|
if self._optcre is self.OPTCRE or value:
|
2019-09-26 16:08:19 +02:00
|
|
|
if not isinstance(value, str):
|
2016-12-06 11:01:17 +01:00
|
|
|
raise TypeError("option values must be strings")
|
2017-06-27 13:19:12 +02:00
|
|
|
if not self.validate(section, option, value):
|
2018-04-19 13:02:12 +02:00
|
|
|
raise ValueError("Invalid value %s" % value)
|
2019-09-26 16:08:19 +02:00
|
|
|
return configparser.ConfigParser.set(self, section, option, value)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2019-12-24 15:02:37 +01:00
|
|
|
def get(self, section, option, raw=False, variables=None):
|
|
|
|
# pylint: disable=unused-argument
|
2017-05-24 16:49:16 +02:00
|
|
|
try:
|
|
|
|
if section == "bitmessagesettings" and option == "timeformat":
|
2019-09-26 16:08:19 +02:00
|
|
|
return configparser.ConfigParser.get(
|
|
|
|
self, section, option, raw=True, vars=variables)
|
2019-09-05 17:31:16 +02:00
|
|
|
try:
|
|
|
|
return self._temp[section][option]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2019-09-26 16:08:19 +02:00
|
|
|
return configparser.ConfigParser.get(
|
|
|
|
self, section, option, raw=True, vars=variables)
|
|
|
|
except configparser.InterpolationError:
|
|
|
|
return configparser.ConfigParser.get(
|
|
|
|
self, section, option, raw=True, vars=variables)
|
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError) as e:
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
|
|
|
return BMConfigDefaults[section][option]
|
2017-05-24 16:49:16 +02:00
|
|
|
except (KeyError, ValueError, AttributeError):
|
2017-05-15 12:18:07 +02:00
|
|
|
raise e
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2019-09-05 17:31:16 +02:00
|
|
|
def setTemp(self, section, option, value=None):
|
|
|
|
"""Temporary set option to value, not saving."""
|
|
|
|
try:
|
|
|
|
self._temp[section][option] = value
|
|
|
|
except KeyError:
|
|
|
|
self._temp[section] = {option: value}
|
|
|
|
|
2017-01-11 14:27:19 +01:00
|
|
|
def safeGetBoolean(self, section, field):
|
2019-12-24 15:02:37 +01:00
|
|
|
"""Return value as boolean, False on exceptions"""
|
2019-09-26 16:08:19 +02:00
|
|
|
config = configparser.ConfigParser()
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2019-12-24 15:02:37 +01:00
|
|
|
# Used in the python2.7
|
2019-09-26 16:08:19 +02:00
|
|
|
# return self.getboolean(section, field)
|
2019-12-24 15:02:37 +01:00
|
|
|
# Used in the python3.5.2
|
2019-09-26 16:08:19 +02:00
|
|
|
return config.getboolean(section, field)
|
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
2018-04-19 13:02:12 +02:00
|
|
|
ValueError, AttributeError):
|
2017-05-15 12:18:07 +02:00
|
|
|
return False
|
2017-01-11 14:27:19 +01:00
|
|
|
|
2017-02-26 17:46:02 +01:00
|
|
|
def safeGetInt(self, section, field, default=0):
|
2019-12-24 15:02:37 +01:00
|
|
|
"""Return value as integer, default on exceptions,
|
|
|
|
0 if default missing"""
|
2019-09-26 16:08:19 +02:00
|
|
|
config = configparser.ConfigParser()
|
2019-12-23 12:18:37 +01:00
|
|
|
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2019-12-24 15:02:37 +01:00
|
|
|
# Used in the python2.7
|
2019-09-26 16:08:19 +02:00
|
|
|
# return self.getint(section, field)
|
2019-12-24 15:02:37 +01:00
|
|
|
# Used in the python3.5.2
|
2019-09-26 16:08:19 +02:00
|
|
|
return config.getint(section, field)
|
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
2018-04-19 13:02:12 +02:00
|
|
|
ValueError, AttributeError):
|
2017-05-15 12:18:07 +02:00
|
|
|
return default
|
2017-01-14 23:18:06 +01:00
|
|
|
|
2018-04-19 13:02:12 +02:00
|
|
|
def safeGet(self, section, option, default=None):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Return value as is, default on exceptions, None if default missing"""
|
2017-05-15 12:18:07 +02:00
|
|
|
try:
|
2017-01-11 14:27:19 +01:00
|
|
|
return self.get(section, option)
|
2019-09-26 16:08:19 +02:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError,
|
2018-04-19 13:02:12 +02:00
|
|
|
ValueError, AttributeError):
|
2017-01-11 14:27:19 +01:00
|
|
|
return default
|
|
|
|
|
2017-06-24 12:13:35 +02:00
|
|
|
def items(self, section, raw=False, variables=None):
|
2019-12-24 15:02:37 +01:00
|
|
|
"""Return section variables as parent,
|
|
|
|
but override the "raw" argument to always True"""
|
2019-09-26 16:08:19 +02:00
|
|
|
return configparser.ConfigParser.items(self, section, True, variables)
|
2016-12-06 11:01:17 +01:00
|
|
|
|
2017-05-15 12:18:07 +02:00
|
|
|
def addresses(self):
|
2019-12-24 15:02:37 +01:00
|
|
|
"""Return a list of local bitmessage addresses (from section labels)"""
|
2019-09-26 16:08:19 +02:00
|
|
|
return [x for x in BMConfigParser().sections() if x.startswith('BM-')]
|
2017-05-15 12:18:07 +02:00
|
|
|
|
2017-06-27 13:19:12 +02:00
|
|
|
def read(self, filenames):
|
2019-09-26 16:08:19 +02:00
|
|
|
configparser.ConfigParser.read(self, filenames)
|
2017-06-27 13:19:12 +02:00
|
|
|
for section in self.sections():
|
|
|
|
for option in self.options(section):
|
|
|
|
try:
|
2018-04-19 13:02:12 +02:00
|
|
|
if not self.validate(
|
|
|
|
section, option,
|
2019-10-03 16:57:22 +02:00
|
|
|
self[section][option]
|
2018-04-19 13:02:12 +02:00
|
|
|
):
|
2017-06-27 13:19:12 +02:00
|
|
|
try:
|
|
|
|
newVal = BMConfigDefaults[section][option]
|
2019-10-03 16:57:22 +02:00
|
|
|
except configparser.NoSectionError:
|
|
|
|
continue
|
2017-06-27 13:19:12 +02:00
|
|
|
except KeyError:
|
|
|
|
continue
|
2019-09-26 16:08:19 +02:00
|
|
|
configparser.ConfigParser.set(
|
2018-04-19 13:02:12 +02:00
|
|
|
self, section, option, newVal)
|
2019-09-26 16:08:19 +02:00
|
|
|
except configparser.InterpolationError:
|
2017-06-27 13:19:12 +02:00
|
|
|
continue
|
|
|
|
|
2017-01-15 10:50:02 +01:00
|
|
|
def save(self):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Save the runtime config onto the filesystem"""
|
2017-01-15 10:50:02 +01:00
|
|
|
fileName = os.path.join(state.appdata, 'keys.dat')
|
2018-04-19 13:02:12 +02:00
|
|
|
fileNameBak = '.'.join([
|
|
|
|
fileName, datetime.now().strftime("%Y%j%H%M%S%f"), 'bak'])
|
|
|
|
# create a backup copy to prevent the accidental loss due to
|
|
|
|
# the disk write failure
|
2017-01-15 10:50:02 +01:00
|
|
|
try:
|
|
|
|
shutil.copyfile(fileName, fileNameBak)
|
|
|
|
# The backup succeeded.
|
|
|
|
fileNameExisted = True
|
2017-02-08 14:19:02 +01:00
|
|
|
except (IOError, Exception):
|
2018-04-19 13:02:12 +02:00
|
|
|
# The backup failed. This can happen if the file
|
|
|
|
# didn't exist before.
|
2017-01-15 10:50:02 +01:00
|
|
|
fileNameExisted = False
|
|
|
|
# write the file
|
2019-09-26 16:08:19 +02:00
|
|
|
with open(fileName, 'w') as configfile:
|
2017-01-15 10:50:02 +01:00
|
|
|
self.write(configfile)
|
|
|
|
# delete the backup
|
|
|
|
if fileNameExisted:
|
|
|
|
os.remove(fileNameBak)
|
2017-06-27 13:19:12 +02:00
|
|
|
|
|
|
|
def validate(self, section, option, value):
|
2019-11-04 15:45:02 +01:00
|
|
|
"""Input validator interface (using factory pattern)"""
|
2017-06-27 13:19:12 +02:00
|
|
|
try:
|
2019-12-24 15:02:37 +01:00
|
|
|
return getattr(self, 'validate_{}_{}'.format(
|
|
|
|
section, option))(value)
|
2017-06-27 13:19:12 +02:00
|
|
|
except AttributeError:
|
|
|
|
return True
|
|
|
|
|
2019-11-04 15:45:02 +01:00
|
|
|
@staticmethod
|
|
|
|
def validate_bitmessagesettings_maxoutboundconnections(value):
|
|
|
|
"""Reject maxoutboundconnections that are too high or too low"""
|
2017-06-27 13:19:12 +02:00
|
|
|
try:
|
|
|
|
value = int(value)
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
if value < 0 or value > 8:
|
|
|
|
return False
|
|
|
|
return True
|