GUI: migrate storage from PostgreSQL to SQLite and expose conf download
This commit is contained in:
40
gui/app.py
40
gui/app.py
@@ -2,18 +2,17 @@
|
||||
import base64
|
||||
import io
|
||||
import os
|
||||
import sqlite3
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
|
||||
import qrcode
|
||||
from flask import Flask, redirect, render_template, request, url_for, flash, Response
|
||||
from psycopg import connect
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.environ.get("APP_SECRET", "dev-secret")
|
||||
|
||||
DB_DSN = os.environ.get("DB_DSN", "postgresql://wgadmin:wgadmin@127.0.0.1:5432/wgadmin")
|
||||
DB_PATH = os.environ.get("DB_PATH", "/opt/wg-admin-gui/data/wgadmin.db")
|
||||
WG_INTERFACE = os.environ.get("WG_INTERFACE", "wg0")
|
||||
WG_META_FILE = os.environ.get("WG_META_FILE", "/etc/wireguard/wg-meta.env")
|
||||
ADMIN_USER = os.environ.get("ADMIN_USER", "admin")
|
||||
@@ -21,23 +20,25 @@ ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "")
|
||||
|
||||
|
||||
def db_conn():
|
||||
return connect(DB_DSN, row_factory=dict_row)
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
def ensure_schema():
|
||||
with db_conn() as conn, conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
with db_conn() as conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS peers (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
public_key TEXT UNIQUE NOT NULL,
|
||||
client_address TEXT,
|
||||
advertised_routes TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
"""
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
|
||||
|
||||
@@ -150,9 +151,10 @@ def _schema():
|
||||
def index():
|
||||
meta = load_meta()
|
||||
runtime = {p["public_key"]: p for p in wg_dump()}
|
||||
with db_conn() as conn, conn.cursor() as cur:
|
||||
with db_conn() as conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT * FROM peers ORDER BY id DESC")
|
||||
db_peers = cur.fetchall()
|
||||
db_peers = [dict(r) for r in cur.fetchall()]
|
||||
|
||||
items = []
|
||||
seen = set()
|
||||
@@ -260,16 +262,14 @@ def new_peer():
|
||||
client_conf = "\n".join(conf_lines)
|
||||
qr_b64 = to_png_b64(client_conf)
|
||||
|
||||
with db_conn() as conn, conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
with db_conn() as conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute("""
|
||||
INSERT INTO peers(name, public_key, client_address, advertised_routes)
|
||||
VALUES (%s,%s,%s,%s)
|
||||
VALUES (?,?,?,?)
|
||||
ON CONFLICT(public_key)
|
||||
DO UPDATE SET name=excluded.name, client_address=excluded.client_address, advertised_routes=excluded.advertised_routes
|
||||
""",
|
||||
(name, client_pub, client_addr, routes),
|
||||
)
|
||||
""", (name, client_pub, client_addr, routes))
|
||||
conn.commit()
|
||||
|
||||
return render_template(
|
||||
|
||||
Reference in New Issue
Block a user