Add SSH host/login/password/key when creating VPN servers
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+89
-11
@@ -68,15 +68,56 @@ async def servers_page(request: Request, db: AsyncSession = Depends(get_db), adm
|
||||
"request": request,
|
||||
"admin": admin,
|
||||
"servers": servers,
|
||||
"protocols": Protocol,
|
||||
"app_name": request.app.state.settings.app_name,
|
||||
"flash": request.query_params.get("flash"),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/servers")
|
||||
async def create_server(
|
||||
name: str = Form(""),
|
||||
protocol: str = Form(...),
|
||||
ssh_host: str = Form(...),
|
||||
ssh_port: int = Form(22),
|
||||
ssh_username: str = Form(...),
|
||||
ssh_password: str = Form(""),
|
||||
ssh_private_key: str = Form(""),
|
||||
public_host: str = Form(""),
|
||||
public_port: str = Form(""),
|
||||
dns: str = Form("1.1.1.1"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
admin=Depends(require_admin),
|
||||
):
|
||||
if _is_redirect(admin):
|
||||
return admin
|
||||
try:
|
||||
port_value = int(public_port) if public_port.strip() else None
|
||||
await vpn_service.create_server(
|
||||
db,
|
||||
name=name,
|
||||
protocol=protocol,
|
||||
ssh_host=ssh_host,
|
||||
ssh_port=ssh_port,
|
||||
ssh_username=ssh_username,
|
||||
ssh_password=ssh_password,
|
||||
ssh_private_key=ssh_private_key,
|
||||
public_host=public_host or None,
|
||||
public_port=port_value,
|
||||
dns=dns,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return RedirectResponse(
|
||||
f"/admin/servers?flash=error:{exc}",
|
||||
status_code=status.HTTP_303_SEE_OTHER,
|
||||
)
|
||||
return RedirectResponse("/admin/servers?flash=created", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
@router.post("/servers/{server_id}/sync")
|
||||
async def sync_server(
|
||||
server_id: int,
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
admin=Depends(require_admin),
|
||||
):
|
||||
@@ -86,12 +127,9 @@ async def sync_server(
|
||||
return RedirectResponse("/admin/servers", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
@router.post("/servers/{server_id}")
|
||||
async def update_server(
|
||||
@router.post("/servers/{server_id}/delete")
|
||||
async def delete_server(
|
||||
server_id: int,
|
||||
public_host: str = Form(...),
|
||||
public_port: int = Form(...),
|
||||
dns: str = Form("1.1.1.1"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
admin=Depends(require_admin),
|
||||
):
|
||||
@@ -99,12 +137,52 @@ async def update_server(
|
||||
return admin
|
||||
server = await db.get(VpnServer, server_id)
|
||||
if server:
|
||||
server.public_host = public_host.strip()
|
||||
server.public_port = public_port
|
||||
server.dns = dns.strip() or "1.1.1.1"
|
||||
await db.delete(server)
|
||||
await db.commit()
|
||||
await vpn_service.sync_server_config(db, server_id)
|
||||
return RedirectResponse("/admin/servers", status_code=status.HTTP_303_SEE_OTHER)
|
||||
return RedirectResponse("/admin/servers?flash=deleted", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
@router.post("/servers/{server_id}")
|
||||
async def update_server(
|
||||
server_id: int,
|
||||
name: str = Form(""),
|
||||
public_host: str = Form(...),
|
||||
public_port: int = Form(...),
|
||||
dns: str = Form("1.1.1.1"),
|
||||
ssh_host: str = Form(""),
|
||||
ssh_port: int = Form(22),
|
||||
ssh_username: str = Form(""),
|
||||
ssh_password: str = Form(""),
|
||||
ssh_private_key: str = Form(""),
|
||||
clear_ssh_password: str = Form(""),
|
||||
clear_ssh_private_key: str = Form(""),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
admin=Depends(require_admin),
|
||||
):
|
||||
if _is_redirect(admin):
|
||||
return admin
|
||||
try:
|
||||
await vpn_service.update_server_settings(
|
||||
db,
|
||||
server_id,
|
||||
name=name or None,
|
||||
public_host=public_host,
|
||||
public_port=public_port,
|
||||
dns=dns,
|
||||
ssh_host=ssh_host,
|
||||
ssh_port=ssh_port,
|
||||
ssh_username=ssh_username,
|
||||
ssh_password=ssh_password,
|
||||
ssh_private_key=ssh_private_key,
|
||||
clear_ssh_password=clear_ssh_password == "1",
|
||||
clear_ssh_private_key=clear_ssh_private_key == "1",
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return RedirectResponse(
|
||||
f"/admin/servers?flash=error:{exc}",
|
||||
status_code=status.HTTP_303_SEE_OTHER,
|
||||
)
|
||||
return RedirectResponse("/admin/servers?flash=saved", status_code=status.HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
@router.get("/clients", response_class=HTMLResponse)
|
||||
|
||||
Reference in New Issue
Block a user