2019-12-31 00:57:14 +00:00
|
|
|
"""
|
2019-06-12 20:12:56 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-04-27 15:43:16 +00:00
|
|
|
|
|
|
|
This file handles configuration setting and getting from the HTTP API
|
2019-12-31 00:57:14 +00:00
|
|
|
"""
|
|
|
|
"""
|
2019-04-27 15:43:16 +00:00
|
|
|
This program is free software: you can 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.
|
|
|
|
|
|
|
|
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-12-31 00:57:14 +00:00
|
|
|
"""
|
2019-04-27 15:43:16 +00:00
|
|
|
import json
|
|
|
|
from flask import Blueprint, request, Response, abort
|
|
|
|
import config, onionrutils
|
2020-02-12 10:29:34 +00:00
|
|
|
|
|
|
|
from onionrutils.bytesconverter import bytes_to_str
|
2019-04-27 15:43:16 +00:00
|
|
|
config.reload()
|
|
|
|
|
|
|
|
config_BP = Blueprint('config_BP', __name__)
|
|
|
|
|
|
|
|
@config_BP.route('/config/get')
|
|
|
|
def get_all_config():
|
2019-12-31 00:57:14 +00:00
|
|
|
"""Simply return all configuration as JSON string"""
|
2019-04-29 02:08:10 +00:00
|
|
|
return Response(json.dumps(config.get_config(), indent=4, sort_keys=True))
|
2019-04-27 15:43:16 +00:00
|
|
|
|
|
|
|
@config_BP.route('/config/get/<key>')
|
|
|
|
def get_by_key(key):
|
2019-12-31 00:57:14 +00:00
|
|
|
"""Return a config setting by key"""
|
2019-04-27 15:43:16 +00:00
|
|
|
return Response(json.dumps(config.get(key)))
|
|
|
|
|
|
|
|
@config_BP.route('/config/setall', methods=['POST'])
|
|
|
|
def set_all_config():
|
2019-12-31 00:57:14 +00:00
|
|
|
"""Overwrite existing JSON config with new JSON string"""
|
2019-04-27 15:43:16 +00:00
|
|
|
try:
|
2019-04-29 02:08:10 +00:00
|
|
|
new_config = request.get_json(force=True)
|
2019-04-27 15:43:16 +00:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
abort(400)
|
|
|
|
else:
|
|
|
|
config.set_config(new_config)
|
2019-04-29 02:08:10 +00:00
|
|
|
config.save()
|
2019-04-27 15:43:16 +00:00
|
|
|
return Response('success')
|
|
|
|
|
|
|
|
@config_BP.route('/config/set/<key>', methods=['POST'])
|
|
|
|
def set_by_key(key):
|
2019-12-31 00:57:14 +00:00
|
|
|
"""Overwrite/set only 1 config key"""
|
|
|
|
"""
|
2019-04-27 15:43:16 +00:00
|
|
|
{
|
|
|
|
'data': data
|
|
|
|
}
|
2019-12-31 00:57:14 +00:00
|
|
|
"""
|
2019-04-27 15:43:16 +00:00
|
|
|
try:
|
2020-02-12 10:29:34 +00:00
|
|
|
data = json.loads(bytes_to_str(request.data))
|
2019-04-27 15:43:16 +00:00
|
|
|
except (json.JSONDecodeError, KeyError):
|
|
|
|
abort(400)
|
|
|
|
config.set(key, data, True)
|
|
|
|
return Response('success')
|