GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — VortexPanel/panel/plugins.py at main · BrowserlessAPI/VortexPanel

Expand file tree

/

plugins.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
296 lines (253 loc) · 10.9 KB
/

plugins.py

Copy path

File metadata and controls

296 lines (253 loc) · 10.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""Plugins (add-on modules such as the Hosting Module).
A plugin lives in its own folder, outside the panel's code, so panel updates
never touch it and removing it is deleting the folder:
/opt/vortexpanel/plugins/<id>/
plugin.json manifest (below)
plugin.py def register(ctx): ... (Flask blueprints, timers)
static/ served at /api/plugins/<id>/assets/... (signed-in admins)
pages/ HTML fragments for admin pages
plugin.json:
{
"id": "hosting", "name": "Hosting Module", "version": "3.0.0",
"requires_core": ">=3.8.0,<4.0.0",
"entry": "plugin.py",
"pages": [{"id": "hosting-accounts", "label": "Hosting Accounts",
"icon": "users", "group": "Overview",
"fragment": "pages/accounts.html"}],
"scripts": ["static/hosting.js"],
"styles": ["static/hosting.css"]
}
Rules the loader enforces so a plugin can never take the panel down:
- every plugin is imported and registered inside try/except; a failure is
logged, shown on Settings > Plugins and `vortexpanel plugins`, and the
panel starts without it
- a plugin whose requires_core does not match this panel is not loaded
- VP_SAFE_MODE=1 (or `vortexpanel plugins safe-mode on`) loads none
- config.json "plugins_disabled": [ids] skips those
- plugin routes are admin-only unless the plugin's own blueprint says
otherwise (the central role check in app.py denies unknown paths for the
site and viewer roles)
"""
import importlib.util
import json
import os
import re
import sys
import time
import traceback
PANEL_DIR = '/opt/vortexpanel'
PLUGINS_DIR = os.environ.get('VP_PLUGINS_DIR', os.path.join(PANEL_DIR, 'plugins'))
DATA_DIR = os.path.join(PANEL_DIR, 'data', 'plugins')
CONFIG_FILE = os.path.join(PANEL_DIR, 'config.json')
_ID_RE = re.compile(r'^[a-z][a-z0-9_-]{1,40}$')
# id -> {"manifest", "dir", "status": loaded|error|disabled|incompatible|safe-mode, "error", "loaded_at"}
REGISTRY = {}
def core_version():
for p in (os.path.join(PANEL_DIR, 'VERSION'),
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'VERSION')):
try:
return open(p).read().strip()
except OSError:
continue
return '0.0.0'
def _vtuple(v):
parts = re.findall(r'\d+', str(v))[:3]
return tuple(int(x) for x in (parts + ['0', '0', '0'])[:3])
def version_matches(version, spec):
"""'>=3.8.0,<4.0.0' style specs; empty spec matches everything."""
v = _vtuple(version)
for clause in (spec or '').split(','):
clause = clause.strip()
if not clause:
continue
m = re.match(r'^(>=|<=|==|>|<|!=)?\s*([\d.]+)$', clause)
if not m:
return False
op, ref = m.group(1) or '==', _vtuple(m.group(2))
ok = {'>=': v >= ref, '<=': v <= ref, '==': v == ref, '>': v > ref,
'<': v < ref, '!=': v != ref}[op]
if not ok:
return False
return True
def _config():
try:
return json.load(open(CONFIG_FILE))
except Exception:
return {}
def disabled_ids():
return set(_config().get('plugins_disabled') or [])
def safe_mode():
return os.environ.get('VP_SAFE_MODE') == '1' or bool(_config().get('plugins_safe_mode'))
def discover():
"""[(id, dir, manifest or None, error)] for every folder in PLUGINS_DIR."""
out = []
try:
names = sorted(os.listdir(PLUGINS_DIR))
except OSError:
return out
for name in names:
d = os.path.join(PLUGINS_DIR, name)
mf = os.path.join(d, 'plugin.json')
if not os.path.isdir(d) or not os.path.isfile(mf):
continue
try:
man = json.load(open(mf))
except Exception as e:
out.append((name, d, None, f'plugin.json is not valid JSON: {e}'))
continue
pid = str(man.get('id') or '')
if pid != name or not _ID_RE.match(pid):
out.append((name, d, None, 'plugin.json "id" must match the folder name (a-z, 0-9, - and _)'))
continue
out.append((pid, d, man, ''))
return out
class PluginContext:
"""What register(ctx) receives."""
def __init__(self, app, pid, plugin_dir, manifest):
self.app = app
self.id = pid
self.dir = plugin_dir
self.manifest = manifest
self.core_version = core_version()
self.data_dir = os.path.join(DATA_DIR, pid)
os.makedirs(self.data_dir, mode=0o700, exist_ok=True)
def register_blueprint(self, bp, **kw):
self.app.register_blueprint(bp, **kw)
def log(self, msg):
print(f'[plugin {self.id}] {msg}', file=sys.stderr, flush=True)
_RESERVED = {'panel', 'app', 'web', 'flask', 'werkzeug', 'jinja2', 'json', 'os', 'sys', 're', 'subprocess',
'requests', 'cryptography', 'gunicorn'}
def _top_level_names(d):
"""Importable top-level modules a plugin folder provides (packages and .py files)."""
out = set()
try:
names = os.listdir(d)
except OSError:
return out
for n in names:
p = os.path.join(d, n)
if n.endswith('.py') and os.path.isfile(p):
out.add(n[:-3])
elif os.path.isdir(p) and os.path.isfile(os.path.join(p, '__init__.py')):
out.add(n)
elif n.endswith('.so') and os.path.isfile(p):
out.add(n.split('.', 1)[0])
out.discard('plugin') # every plugin has one; it is loaded by path under a unique name
return out
def _name_clash(pid, d, man, claimed):
"""Error text when this plugin's Python names would collide with the core, a library
or another plugin (two plugins with a 'common' package would silently share one)."""
names = _top_level_names(d)
bad = sorted(names & _RESERVED)
if bad:
return f'plugin provides Python module(s) {", ".join(bad)} that would replace the panel\'s own; rename them'
if man.get('kind') == 'app':
stray = sorted(n for n in names if not n.startswith('vpapp_'))
if stray:
return f'App modules must live in a vpapp_* package (found {", ".join(stray)})'
for n in sorted(names):
if n in claimed and claimed[n] != pid:
return f'conflicts with plugin "{claimed[n]}": both provide the Python module "{n}"'
for n in names:
claimed[n] = pid
return ''
def load_all(app):
"""Load every enabled, compatible plugin into the Flask app."""
REGISTRY.clear()
claimed = {}
cv = core_version()
sm = safe_mode()
off = disabled_ids()
for pid, d, man, err in discover():
entry = {'id': pid, 'dir': d, 'manifest': man or {}, 'status': 'error', 'error': err, 'loaded_at': None}
REGISTRY[pid] = entry
if err:
continue
if sm:
entry.update(status='safe-mode', error='Safe mode is on: no plugins are loaded')
continue
if pid in off:
entry.update(status='disabled', error='')
continue
spec = man.get('requires_core', '')
if not version_matches(cv, spec):
entry.update(status='incompatible',
error=f'needs VortexPanel {spec}, this panel is {cv}. Update the plugin or the panel.')
continue
src = os.path.realpath(os.path.join(d, man.get('entry') or 'plugin.py'))
if not src.startswith(os.path.realpath(d) + os.sep) or not src.endswith('.py'):
entry.update(status='error', error='plugin.json "entry" must be a .py file inside the plugin folder')
continue
clash = _name_clash(pid, d, man, claimed)
if clash:
entry.update(status='error', error=clash)
continue
try:
# Appended, not prepended: a plugin can never shadow the panel's own
# modules or installed libraries (a plugin file named json.py or a
# folder named panel/ would otherwise replace them for everyone).
if d not in sys.path:
sys.path.append(d)
spec_ = importlib.util.spec_from_file_location(f'vp_plugin_{pid.replace("-", "_")}', src)
mod = importlib.util.module_from_spec(spec_)
sys.modules[spec_.name] = mod
spec_.loader.exec_module(mod)
if not hasattr(mod, 'register'):
raise RuntimeError(f'{os.path.basename(src)} has no register(ctx) function')
mod.register(PluginContext(app, pid, d, man))
entry.update(status='loaded', error='', loaded_at=time.time())
except Exception as e:
tb = traceback.format_exc(limit=6)
entry.update(status='error', error=f'{type(e).__name__}: {e}')
print(f'[plugins] {pid} failed to load and was skipped:\n{tb}', file=sys.stderr, flush=True)
return REGISTRY
def public_list():
"""For /api/plugins: what the admin UI needs (no file system paths)."""
out = []
for pid, e in sorted(REGISTRY.items()):
m = e['manifest'] or {}
loaded = e['status'] == 'loaded'
out.append({
'id': pid, 'name': m.get('name') or pid, 'version': m.get('version') or '',
'description': m.get('description') or '', 'requires_core': m.get('requires_core') or '',
'status': e['status'], 'error': e['error'],
'pages': [{k: p.get(k) for k in ('id', 'label', 'icon', 'group')} for p in (m.get('pages') or [])
if loaded and isinstance(p, dict) and _ID_RE.match(str(p.get('id') or ''))],
'scripts': [f'/api/plugins/{pid}/assets/' + s.split('static/', 1)[-1] for s in (m.get('scripts') or [])] if loaded else [],
'styles': [f'/api/plugins/{pid}/assets/' + s.split('static/', 1)[-1] for s in (m.get('styles') or [])] if loaded else [],
})
return out
def fragment_path(pid, page_id):
e = REGISTRY.get(pid)
if not e or e['status'] != 'loaded':
return None
for p in (e['manifest'].get('pages') or []):
if isinstance(p, dict) and p.get('id') == page_id and p.get('fragment'):
path = os.path.realpath(os.path.join(e['dir'], p['fragment']))
if path.startswith(os.path.realpath(e['dir']) + os.sep) and os.path.isfile(path):
return path
return None
def asset_dir(pid):
e = REGISTRY.get(pid)
if not e or e['status'] != 'loaded':
return None
d = os.path.join(e['dir'], 'static')
return d if os.path.isdir(d) else None
def set_enabled(pid, enabled):
cfg = _config()
off = set(cfg.get('plugins_disabled') or [])
(off.discard if enabled else off.add)(pid)
cfg['plugins_disabled'] = sorted(off)
tmp = CONFIG_FILE + '.tmp'
with open(tmp, 'w') as f:
json.dump(cfg, f, indent=2)
os.chmod(tmp, 0o600)
os.replace(tmp, CONFIG_FILE)
def set_safe_mode(on):
cfg = _config()
cfg['plugins_safe_mode'] = bool(on)
tmp = CONFIG_FILE + '.tmp'
with open(tmp, 'w') as f:
json.dump(cfg, f, indent=2)
os.chmod(tmp, 0o600)
os.replace(tmp, CONFIG_FILE)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.