-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentinel_toggles_tui.py
More file actions
executable file
·181 lines (151 loc) · 7.48 KB
/
sentinel_toggles_tui.py
File metadata and controls
executable file
·181 lines (151 loc) · 7.48 KB
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
#!/usr/bin/env python3
"""
SENTINEL Toggle TUI (Standalone)
--------------------------------
A simple, secure terminal UI for toggling SENTINEL feature modules and core/security options.
- Run: ./sentinel_toggles_tui.py
- Uses npyscreen for the interface
- Edits ~/bashrc.postcustom and ~/bashrc.precustom
- Preserves comments and structure, makes backups
- Linux-first, security-focused
Author: SENTINEL Team
"""
import npyscreen
import os
import re
import shutil
import logging
BASHRC_PRECUSTOM = os.path.expanduser('~/bashrc.precustom')
BASHRC_POSTCUSTOM = os.path.expanduser('~/bashrc.postcustom')
BACKUP_SUFFIX = '.bak'
logging.basicConfig(filename=os.path.expanduser('~/logs/toggle_tui.log'),
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
def parse_exports(filepath, keys=None):
exports = {}
try:
with open(filepath, 'r') as f:
for i, line in enumerate(f):
m = re.match(r'\s*export\s+([A-Za-z0-9_]+)=(\d+)', line)
if m:
var, val = m.group(1), m.group(2)
if (keys is None) or (var in keys):
exports[var] = (val, i, line.rstrip('\n'))
except Exception as e:
logging.error(f"Failed to parse {filepath}: {e}")
return exports
def update_exports(filepath, updates):
try:
with open(filepath, 'r') as f:
lines = f.readlines()
for var, new_val in updates.items():
for i, line in enumerate(lines):
if re.match(rf'\s*export\s+{re.escape(var)}=', line):
lines[i] = re.sub(r'(export\s+' + re.escape(var) + r'=)\d+', rf'\g<1>{new_val}', line)
shutil.copy2(filepath, filepath + BACKUP_SUFFIX)
with open(filepath, 'w') as f:
f.writelines(lines)
logging.info(f"Updated {filepath}: {updates}")
return True
except Exception as e:
logging.error(f"Failed to update {filepath}: {e}")
return False
FEATURE_TOGGLES = [
'SENTINEL_OBFUSCATE_ENABLED',
'SENTINEL_FZF_ENABLED',
'SENTINEL_ML_ENABLED',
'SENTINEL_OSINT_ENABLED',
'SENTINEL_CYBERSEC_ENABLED',
'SENTINEL_GITSTAR_ENABLED',
'SENTINEL_CHAT_ENABLED',
]
# Configuration cache and module system optimization toggles
CONFIG_CACHE_TOGGLES = [
'SENTINEL_CONFIG_CACHE_ENABLED',
'SENTINEL_CONFIG_FORCE_REFRESH',
'SENTINEL_CONFIG_VERIFY_HASH',
'SENTINEL_MODULE_DEBUG',
'SENTINEL_MODULE_AUTOLOAD',
'SENTINEL_MODULE_CACHE_ENABLED',
'SENTINEL_MODULE_VERIFY'
]
CORE_TOGGLES = [
'U_BINS', 'U_FUNCS', 'U_ALIASES', 'U_AGENTS', 'ENABLE_LESSPIPE', 'U_MODULES_ENABLE',
'VENV_AUTO', 'SENTINEL_SECURE_RM', 'SENTINEL_QUIET_MODULES', 'SENTINEL_SECURE_BASH_HISTORY',
'SENTINEL_SECURE_SSH_KNOWN_HOSTS', 'SENTINEL_SECURE_CLEAN_CACHE', 'SENTINEL_SECURE_BROWSER_CACHE',
'SENTINEL_SECURE_RECENT', 'SENTINEL_SECURE_VIM_UNDO', 'SENTINEL_SECURE_CLIPBOARD',
'SENTINEL_SECURE_CLEAR_SCREEN', 'U_LAZY_LOAD', 'BASHRC_PROFILE'
]
class ToggleForm(npyscreen.ActionForm):
def create(self):
self.add(npyscreen.FixedText, value="SENTINEL Toggle TUI", editable=False, color='STANDOUT')
self.add(npyscreen.FixedText, value="(Use arrows/space to toggle, ^S to save, ^Q to quit)", editable=False)
self.add(
npyscreen.FixedText,
value="\n[Security Warning] Only trusted users should edit these files!",
editable=False,
color='DANGER')
self.add(npyscreen.FixedText, value="\nFeature Module Toggles:", editable=False, color='LABEL')
self.feature_toggles = self.add(npyscreen.TitleMultiSelect, max_height=8, name="Feature Modules",
values=FEATURE_TOGGLES, scroll_exit=True)
self.add(npyscreen.FixedText, value="\nConfiguration Caching and Module System:", editable=False, color='LABEL')
self.cache_toggles = self.add(npyscreen.TitleMultiSelect, max_height=8, name="Config Cache/Modules",
values=CONFIG_CACHE_TOGGLES, scroll_exit=True)
self.add(npyscreen.FixedText, value="\nCore/Security Toggles:", editable=False, color='LABEL')
self.core_toggles = self.add(npyscreen.TitleMultiSelect, max_height=12, name="Core/Security",
values=CORE_TOGGLES, scroll_exit=True)
self.status = self.add(npyscreen.FixedText, value="", editable=False, color='CAUTION')
self.help_btn = self.add(npyscreen.ButtonPress, name="Help", when_pressed_function=self.show_help)
self.reload_btn = self.add(npyscreen.ButtonPress, name="Reload", when_pressed_function=self.reload)
self.reload()
def reload(self):
self.feature_vals = parse_exports(BASHRC_POSTCUSTOM, FEATURE_TOGGLES)
self.cache_vals = parse_exports(BASHRC_POSTCUSTOM, CONFIG_CACHE_TOGGLES)
self.core_vals = parse_exports(BASHRC_PRECUSTOM, CORE_TOGGLES)
self.feature_toggles.value = [i for i, k in enumerate(
FEATURE_TOGGLES) if self.feature_vals.get(k, ('0',))[0] == '1']
self.cache_toggles.value = [i for i, k in enumerate(
CONFIG_CACHE_TOGGLES) if self.cache_vals.get(k, ('0',))[0] == '1']
self.core_toggles.value = [i for i, k in enumerate(CORE_TOGGLES) if self.core_vals.get(k, ('0',))[0] == '1']
self.status.value = ""
self.display()
def on_ok(self):
feature_updates = {k: '1' if i in self.feature_toggles.value else '0' for i, k in enumerate(FEATURE_TOGGLES)}
cache_updates = {k: '1' if i in self.cache_toggles.value else '0' for i, k in enumerate(CONFIG_CACHE_TOGGLES)}
core_updates = {k: '1' if i in self.core_toggles.value else '0' for i, k in enumerate(CORE_TOGGLES)}
# Merge feature and cache updates for postcustom
postcustom_updates = {**feature_updates, **cache_updates}
if npyscreen.notify_yes_no("Save changes to toggles? (Backups will be made)", title="Confirm Save"):
ok1 = update_exports(BASHRC_POSTCUSTOM, postcustom_updates)
ok2 = update_exports(BASHRC_PRECUSTOM, core_updates)
if ok1 and ok2:
self.status.value = "Toggles updated successfully."
else:
self.status.value = "Error updating toggles. Check logs."
self.display()
def on_cancel(self):
if npyscreen.notify_yes_no("Exit without saving changes?", title="Exit"):
self.parentApp.setNextForm(None)
def show_help(self):
npyscreen.notify_confirm(
"Use arrows/space to select toggles.\n"
"Checked = enabled (1), unchecked = disabled (0).\n"
"^S to save, ^Q to quit.\n"
"Backups are made before writing.\n"
"Security: Only trusted users should edit these files!\n\n"
"Configuration Caching:\n"
" - SENTINEL_CONFIG_CACHE_ENABLED: Master toggle for config caching\n"
" - SENTINEL_MODULE_CACHE_ENABLED: Toggle for module caching\n"
" - SENTINEL_CONFIG_VERIFY_HASH: Validate config files with MD5\n"
" - SENTINEL_MODULE_VERIFY: Security verification for modules\n",
title="Help / Security Notice")
class SentinelToggleApp(npyscreen.NPSAppManaged):
def onStart(self):
self.addForm('MAIN', ToggleForm)
if __name__ == '__main__':
try:
app = SentinelToggleApp()
app.run()
except Exception as e:
logging.error(f"Fatal error in TUI: {e}")
print(f"[ERROR] {e}\nSee ~/logs/toggle_tui.log for details.")