mirror of
https://github.com/jayofelony/pwnagotchi.git
synced 2025-07-01 18:37:27 -04:00
save in dotted format
This commit is contained in:
@ -17,6 +17,7 @@ from pwnagotchi.agent import Agent
|
|||||||
from pwnagotchi.ui.display import Display
|
from pwnagotchi.ui.display import Display
|
||||||
from pwnagotchi import restart
|
from pwnagotchi import restart
|
||||||
from pwnagotchi import fs
|
from pwnagotchi import fs
|
||||||
|
from pwnagotchi.utils import DottedTomlEncoder
|
||||||
|
|
||||||
|
|
||||||
def do_clear(display):
|
def do_clear(display):
|
||||||
@ -122,9 +123,10 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
config = utils.load_config(args)
|
config = utils.load_config(args)
|
||||||
if args.print_config:
|
if args.print_config:
|
||||||
print(toml.dumps(config))
|
print(toml.dumps(config, encoder=DottedTomlEncoder()))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
pwnagotchi.config = config
|
||||||
fs.setup_mounts(config)
|
fs.setup_mounts(config)
|
||||||
log.setup_logging(args, config)
|
log.setup_logging(args, config)
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ from pwnagotchi import fs
|
|||||||
from pwnagotchi._version import __version__
|
from pwnagotchi._version import __version__
|
||||||
|
|
||||||
_name = None
|
_name = None
|
||||||
|
config = None
|
||||||
|
|
||||||
|
|
||||||
def set_name(new_name):
|
def set_name(new_name):
|
||||||
|
@ -5,6 +5,9 @@ import threading
|
|||||||
import importlib, importlib.util
|
import importlib, importlib.util
|
||||||
import logging
|
import logging
|
||||||
from pwnagotchi.ui import view
|
from pwnagotchi.ui import view
|
||||||
|
from pwnagotchi import config
|
||||||
|
from pwnagotchi.utils import save_config
|
||||||
|
|
||||||
|
|
||||||
default_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default")
|
default_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "default")
|
||||||
loaded = {}
|
loaded = {}
|
||||||
@ -36,11 +39,17 @@ def toggle_plugin(name, enable=True):
|
|||||||
|
|
||||||
returns True if changed, otherwise False
|
returns True if changed, otherwise False
|
||||||
"""
|
"""
|
||||||
global loaded, database
|
global loaded, database, config
|
||||||
|
|
||||||
|
if config:
|
||||||
|
config['main']['plugins'][name] = enable
|
||||||
|
save_config(config, '/etc/pwnagotchi/config.toml')
|
||||||
|
|
||||||
if not enable and name in loaded:
|
if not enable and name in loaded:
|
||||||
if getattr(loaded[name], 'on_unload', None):
|
if getattr(loaded[name], 'on_unload', None):
|
||||||
loaded[name].on_unload(view.ROOT)
|
loaded[name].on_unload(view.ROOT)
|
||||||
del loaded[name]
|
del loaded[name]
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if enable and name in database and name not in loaded:
|
if enable and name in database and name not in loaded:
|
||||||
|
@ -2,8 +2,8 @@ import logging
|
|||||||
import json
|
import json
|
||||||
import toml
|
import toml
|
||||||
import _thread
|
import _thread
|
||||||
import pwnagotchi.plugins as plugins
|
from pwnagotchi import restart, plugins
|
||||||
from pwnagotchi import restart
|
from pwnagotchi.utils import save_config
|
||||||
from flask import abort
|
from flask import abort
|
||||||
from flask import render_template_string
|
from flask import render_template_string
|
||||||
|
|
||||||
@ -500,9 +500,8 @@ class WebConfig(plugins.Plugin):
|
|||||||
if path == "save-config":
|
if path == "save-config":
|
||||||
try:
|
try:
|
||||||
parsed_toml = toml.loads(request.get_json())
|
parsed_toml = toml.loads(request.get_json())
|
||||||
with open('/etc/pwnagotchi/config.toml') as config_file:
|
save_config(parsed_toml, '/etc/pwnagotchi/config.toml')
|
||||||
toml.dump(parsed_toml, config_file)
|
|
||||||
|
|
||||||
_thread.start_new_thread(restart, (self.mode,))
|
_thread.start_new_thread(restart, (self.mode,))
|
||||||
return "success"
|
return "success"
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
@ -10,11 +10,43 @@ import json
|
|||||||
import shutil
|
import shutil
|
||||||
import toml
|
import toml
|
||||||
import sys
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
import pwnagotchi
|
import pwnagotchi
|
||||||
|
from toml.encoder import TomlEncoder, _dump_str
|
||||||
from pwnagotchi.fs import ensure_write
|
from pwnagotchi.fs import ensure_write
|
||||||
|
|
||||||
|
|
||||||
|
class DottedTomlEncoder(TomlEncoder):
|
||||||
|
"""
|
||||||
|
Dumps the toml into the dotted-key format
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, _dict=dict):
|
||||||
|
super(DottedTomlEncoder, self).__init__(_dict)
|
||||||
|
|
||||||
|
def dump_sections(self, o, sup):
|
||||||
|
retstr = ""
|
||||||
|
pre = ""
|
||||||
|
|
||||||
|
if sup:
|
||||||
|
pre = sup + "."
|
||||||
|
|
||||||
|
for section, value in o.items():
|
||||||
|
section = str(section)
|
||||||
|
qsection = section
|
||||||
|
if not re.match(r'^[A-Za-z0-9_-]+$', section):
|
||||||
|
qsection = _dump_str(section)
|
||||||
|
if value is not None:
|
||||||
|
if isinstance(value, dict):
|
||||||
|
toadd, _ = self.dump_sections(value, pre + qsection)
|
||||||
|
retstr += toadd
|
||||||
|
else:
|
||||||
|
retstr += (pre + qsection + " = " +
|
||||||
|
str(self.dump_value(value)) + '\n')
|
||||||
|
return (retstr, self._dict())
|
||||||
|
|
||||||
|
|
||||||
# https://stackoverflow.com/questions/823196/yaml-merge-in-python
|
# https://stackoverflow.com/questions/823196/yaml-merge-in-python
|
||||||
def merge_config(user, default):
|
def merge_config(user, default):
|
||||||
if isinstance(user, dict) and isinstance(default, dict):
|
if isinstance(user, dict) and isinstance(default, dict):
|
||||||
@ -44,6 +76,11 @@ def keys_to_str(data):
|
|||||||
|
|
||||||
return converted_dict
|
return converted_dict
|
||||||
|
|
||||||
|
def save_config(config, target):
|
||||||
|
with open(target, 'wt') as fp:
|
||||||
|
fp.write(toml.dumps(config, encoder=DottedTomlEncoder()))
|
||||||
|
return True
|
||||||
|
|
||||||
def load_config(args):
|
def load_config(args):
|
||||||
default_config_path = os.path.dirname(args.config)
|
default_config_path = os.path.dirname(args.config)
|
||||||
if not os.path.exists(default_config_path):
|
if not os.path.exists(default_config_path):
|
||||||
|
Reference in New Issue
Block a user