Merge remote-tracking branch 'Snuf/master' into pwnagotchi-torch

# Conflicts:
#	bin/pwnagotchi
#	builder/data/usr/bin/pwnlib
#	pwnagotchi/ai/__init__.py
This commit is contained in:
Jeroen Oudshoorn
2023-07-24 21:20:37 +02:00
19 changed files with 670 additions and 41 deletions

View File

@ -41,6 +41,7 @@ class MemTemp(plugins.Plugin):
ALLOWED_FIELDS = {
'mem': 'mem_usage',
'cpu': 'cpu_load',
'cpus': 'cpu_load_since',
'temp': 'cpu_temp',
'freq': 'cpu_freq'
}
@ -50,6 +51,7 @@ class MemTemp(plugins.Plugin):
FIELD_WIDTH = 4
def on_loaded(self):
self._last_cpu_load = self._cpu_stat()
logging.info("memtemp plugin loaded.")
def mem_usage(self):
@ -58,6 +60,28 @@ class MemTemp(plugins.Plugin):
def cpu_load(self):
return f"{int(pwnagotchi.cpu_load() * 100)}%"
def _cpu_stat(self):
"""
Returns the splitted first line of the /proc/stat file
"""
with open('/proc/stat', 'rt') as fp:
return list(map(int,fp.readline().split()[1:]))
def cpu_load_since(self):
"""
Returns the % load, since last time called
"""
parts0 = self._cpu_stat()
parts1 = self._last_cpu_load
self._last_cpu_load = parts0
parts_diff = [p1 - p0 for (p0, p1) in zip(parts0, parts1)]
user, nice, sys, idle, iowait, irq, softirq, steal, _guest, _guest_nice = parts_diff
idle_sum = idle + iowait
non_idle_sum = user + nice + sys + irq + softirq + steal
total = idle_sum + non_idle_sum
return f"{int(non_idle_sum / total * 100)}%"
def cpu_temp(self):
if self.options['scale'] == "fahrenheit":
temp = (pwnagotchi.temperature() * 9 / 5) + 32

View File

@ -2,8 +2,9 @@ import logging
import json
import toml
import _thread
import pwnagotchi
from pwnagotchi import restart, plugins
from pwnagotchi.utils import save_config
from pwnagotchi.utils import save_config, merge_config
from flask import abort
from flask import render_template_string
@ -180,7 +181,10 @@ INDEX = """
<span><select id="selAddType"><option value="text">Text</option><option value="number">Number</option></select></span>
<span><button id="btnAdd" type="button" onclick="addOption()">+</button></span>
</div>
<button id="btnSave" type="button" onclick="saveConfig()">Save and restart</button>
<div id="divSaveTop">
<button id="btnSave" type="button" onclick="saveConfig()">Save and restart</button>
<button id="btnSave" type="button" onclick="saveConfigNoRestart()">Merge and Save (CAUTION)</button>
</div>
<div id="content"></div>
{% endblock %}
@ -240,6 +244,24 @@ INDEX = """
});
}
}
function saveConfigNoRestart(){
// get table
var table = document.getElementById("tableOptions");
if (table) {
var json = tableToJson(table);
sendJSON("webcfg/merge-save-config", json, function(response) {
if (response) {
if (response.status == "200") {
alert("Config got updated");
} else {
alert("Error while updating the config (err-code: " + response.status + ")");
}
}
});
}
}
var searchInput = document.getElementById("searchText");
searchInput.onkeyup = function() {
var filter, table, tr, td, i, txtValue;
@ -471,15 +493,18 @@ class WebConfig(plugins.Plugin):
def __init__(self):
self.ready = False
self.mode = 'MANU'
self._agent = None
def on_config_changed(self, config):
self.config = config
self.ready = True
def on_ready(self, agent):
self._agent = agent
self.mode = 'MANU' if agent.mode == 'manual' else 'AUTO'
def on_internet_available(self, agent):
self._agent = agent
self.mode = 'MANU' if agent.mode == 'manual' else 'AUTO'
def on_loaded(self):
@ -513,4 +538,18 @@ class WebConfig(plugins.Plugin):
except Exception as ex:
logging.error(ex)
return "config error", 500
elif path == "merge-save-config":
try:
self.config = merge_config(request.get_json(), self.config)
pwnagotchi.config = merge_config(request.get_json(), pwnagotchi.config)
logging.debug("PWNAGOTCHI CONFIG:\n%s" % repr(pwnagotchi.config))
if self._agent:
self._agent._config = merge_config(request.get_json(), self._agent._config)
logging.debug(" Agent CONFIG:\n%s" % repr(self._agent._config))
logging.debug(" Updated CONFIG:\n%s" % request.get_json())
save_config(request.get_json(), '/etc/pwnagotchi/config.toml') # test
return "success"
except Exception as ex:
logging.error("[webcfg mergesave] %s" % ex)
return "config error", 500
abort(404)