Created comprehensive VPN setup tooling for Peaceful Spirit L2TP/IPsec connection and enhanced agent documentation framework. VPN Configuration (PST-NW-VPN): - Setup-PST-L2TP-VPN.ps1: Automated L2TP/IPsec setup with split-tunnel and DNS - Connect-PST-VPN.ps1: Connection helper with PPP adapter detection, DNS (192.168.0.2), and route config (192.168.0.0/24) - Connect-PST-VPN-Standalone.ps1: Self-contained connection script for remote deployment - Fix-PST-VPN-Auth.ps1: Authentication troubleshooting for CHAP/MSChapv2 - Diagnose-VPN-Interface.ps1: Comprehensive VPN interface and routing diagnostic - Quick-Test-VPN.ps1: Fast connectivity verification (DNS/router/routes) - Add-PST-VPN-Route-Manual.ps1: Manual route configuration helper - vpn-connect.bat, vpn-disconnect.bat: Simple batch file shortcuts - OpenVPN config files (Windows-compatible, abandoned for L2TP) Key VPN Implementation Details: - L2TP creates PPP adapter with connection name as interface description - UniFi auto-configures DNS (192.168.0.2) but requires manual route to 192.168.0.0/24 - Split-tunnel enabled (only remote traffic through VPN) - All-user connection for pre-login auto-connect via scheduled task - Authentication: CHAP + MSChapv2 for UniFi compatibility Agent Documentation: - AGENT_QUICK_REFERENCE.md: Quick reference for all specialized agents - documentation-squire.md: Documentation and task management specialist agent - Updated all agent markdown files with standardized formatting Project Organization: - Moved conversation logs to dedicated directories (guru-connect-conversation-logs, guru-rmm-conversation-logs) - Cleaned up old session JSONL files from projects/msp-tools/ - Added guru-connect infrastructure (agent, dashboard, proto, scripts, .gitea workflows) - Added guru-rmm server components and deployment configs Technical Notes: - VPN IP pool: 192.168.4.x (client gets 192.168.4.6) - Remote network: 192.168.0.0/24 (router at 192.168.0.10) - PSK: rrClvnmUeXEFo90Ol+z7tfsAZHeSK6w7 - Credentials: pst-admin / 24Hearts$ Files: 15 VPN scripts, 2 agent docs, conversation log reorganization, guru-connect/guru-rmm infrastructure additions Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
281 lines
8.1 KiB
Rust
281 lines
8.1 KiB
Rust
//! Site management API endpoints
|
|
|
|
use axum::{
|
|
extract::{Path, State},
|
|
http::StatusCode,
|
|
Json,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::auth::AuthUser;
|
|
use crate::db;
|
|
use crate::ws::{generate_api_key, hash_api_key};
|
|
use crate::AppState;
|
|
|
|
/// Response for site operations
|
|
#[derive(Debug, Serialize)]
|
|
pub struct SiteResponse {
|
|
pub id: Uuid,
|
|
pub client_id: Uuid,
|
|
pub client_name: Option<String>,
|
|
pub name: String,
|
|
pub site_code: String,
|
|
pub address: Option<String>,
|
|
pub notes: Option<String>,
|
|
pub is_active: bool,
|
|
pub created_at: chrono::DateTime<chrono::Utc>,
|
|
pub agent_count: i64,
|
|
}
|
|
|
|
/// Response when creating a site (includes one-time API key)
|
|
#[derive(Debug, Serialize)]
|
|
pub struct CreateSiteResponse {
|
|
pub site: SiteResponse,
|
|
/// The API key for agents at this site (shown only once!)
|
|
pub api_key: String,
|
|
pub message: String,
|
|
}
|
|
|
|
/// Request to create a new site
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct CreateSiteRequest {
|
|
pub client_id: Uuid,
|
|
pub name: String,
|
|
pub address: Option<String>,
|
|
pub notes: Option<String>,
|
|
}
|
|
|
|
/// Request to update a site
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct UpdateSiteRequest {
|
|
pub name: Option<String>,
|
|
pub address: Option<String>,
|
|
pub notes: Option<String>,
|
|
pub is_active: Option<bool>,
|
|
}
|
|
|
|
/// List all sites
|
|
pub async fn list_sites(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<Vec<SiteResponse>>, (StatusCode, String)> {
|
|
let sites = db::get_all_sites_with_details(&state.db)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
let responses: Vec<SiteResponse> = sites
|
|
.into_iter()
|
|
.map(|s| SiteResponse {
|
|
id: s.id,
|
|
client_id: s.client_id,
|
|
client_name: Some(s.client_name),
|
|
name: s.name,
|
|
site_code: s.site_code,
|
|
address: s.address,
|
|
notes: s.notes,
|
|
is_active: s.is_active,
|
|
created_at: s.created_at,
|
|
agent_count: s.agent_count,
|
|
})
|
|
.collect();
|
|
|
|
Ok(Json(responses))
|
|
}
|
|
|
|
/// List sites for a specific client
|
|
pub async fn list_sites_by_client(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
Path(client_id): Path<Uuid>,
|
|
) -> Result<Json<Vec<SiteResponse>>, (StatusCode, String)> {
|
|
let sites = db::get_sites_by_client(&state.db, client_id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
let responses: Vec<SiteResponse> = sites
|
|
.into_iter()
|
|
.map(|s| SiteResponse {
|
|
id: s.id,
|
|
client_id: s.client_id,
|
|
client_name: None,
|
|
name: s.name,
|
|
site_code: s.site_code,
|
|
address: s.address,
|
|
notes: s.notes,
|
|
is_active: s.is_active,
|
|
created_at: s.created_at,
|
|
agent_count: 0, // Would need separate query
|
|
})
|
|
.collect();
|
|
|
|
Ok(Json(responses))
|
|
}
|
|
|
|
/// Create a new site
|
|
pub async fn create_site(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
Json(req): Json<CreateSiteRequest>,
|
|
) -> Result<Json<CreateSiteResponse>, (StatusCode, String)> {
|
|
// Verify client exists
|
|
let client = db::get_client_by_id(&state.db, req.client_id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
|
.ok_or((StatusCode::NOT_FOUND, "Client not found".to_string()))?;
|
|
|
|
// Generate unique site code and API key
|
|
let site_code = db::generate_unique_site_code(&state.db)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
let api_key = generate_api_key(&state.config.auth.api_key_prefix);
|
|
let api_key_hash = hash_api_key(&api_key);
|
|
|
|
let create = db::CreateSiteInternal {
|
|
client_id: req.client_id,
|
|
name: req.name,
|
|
site_code: site_code.clone(),
|
|
api_key_hash,
|
|
address: req.address,
|
|
notes: req.notes,
|
|
};
|
|
|
|
let site = db::create_site(&state.db, create)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
Ok(Json(CreateSiteResponse {
|
|
site: SiteResponse {
|
|
id: site.id,
|
|
client_id: site.client_id,
|
|
client_name: Some(client.name),
|
|
name: site.name,
|
|
site_code: site.site_code,
|
|
address: site.address,
|
|
notes: site.notes,
|
|
is_active: site.is_active,
|
|
created_at: site.created_at,
|
|
agent_count: 0,
|
|
},
|
|
api_key,
|
|
message: "Site created. Save the API key - it will not be shown again.".to_string(),
|
|
}))
|
|
}
|
|
|
|
/// Get a specific site
|
|
pub async fn get_site(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<SiteResponse>, (StatusCode, String)> {
|
|
let site = db::get_site_by_id(&state.db, id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
|
.ok_or((StatusCode::NOT_FOUND, "Site not found".to_string()))?;
|
|
|
|
// Get client name and agent count
|
|
let client = db::get_client_by_id(&state.db, site.client_id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
let agents = db::get_agents_by_site(&state.db, id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
Ok(Json(SiteResponse {
|
|
id: site.id,
|
|
client_id: site.client_id,
|
|
client_name: client.map(|c| c.name),
|
|
name: site.name,
|
|
site_code: site.site_code,
|
|
address: site.address,
|
|
notes: site.notes,
|
|
is_active: site.is_active,
|
|
created_at: site.created_at,
|
|
agent_count: agents.len() as i64,
|
|
}))
|
|
}
|
|
|
|
/// Update a site
|
|
pub async fn update_site(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
Json(req): Json<UpdateSiteRequest>,
|
|
) -> Result<Json<SiteResponse>, (StatusCode, String)> {
|
|
let update = db::UpdateSite {
|
|
name: req.name,
|
|
address: req.address,
|
|
notes: req.notes,
|
|
is_active: req.is_active,
|
|
};
|
|
|
|
let site = db::update_site(&state.db, id, update)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
|
.ok_or((StatusCode::NOT_FOUND, "Site not found".to_string()))?;
|
|
|
|
Ok(Json(SiteResponse {
|
|
id: site.id,
|
|
client_id: site.client_id,
|
|
client_name: None,
|
|
name: site.name,
|
|
site_code: site.site_code,
|
|
address: site.address,
|
|
notes: site.notes,
|
|
is_active: site.is_active,
|
|
created_at: site.created_at,
|
|
agent_count: 0,
|
|
}))
|
|
}
|
|
|
|
/// Regenerate API key for a site
|
|
#[derive(Debug, Serialize)]
|
|
pub struct RegenerateApiKeyResponse {
|
|
pub api_key: String,
|
|
pub message: String,
|
|
}
|
|
|
|
pub async fn regenerate_api_key(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<Json<RegenerateApiKeyResponse>, (StatusCode, String)> {
|
|
// Verify site exists
|
|
let _site = db::get_site_by_id(&state.db, id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
|
.ok_or((StatusCode::NOT_FOUND, "Site not found".to_string()))?;
|
|
|
|
// Generate new API key
|
|
let api_key = generate_api_key(&state.config.auth.api_key_prefix);
|
|
let api_key_hash = hash_api_key(&api_key);
|
|
|
|
db::regenerate_site_api_key(&state.db, id, &api_key_hash)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
Ok(Json(RegenerateApiKeyResponse {
|
|
api_key,
|
|
message: "API key regenerated. Save it - it will not be shown again. Existing agents will need to be reconfigured.".to_string(),
|
|
}))
|
|
}
|
|
|
|
/// Delete a site
|
|
pub async fn delete_site(
|
|
_user: AuthUser,
|
|
State(state): State<AppState>,
|
|
Path(id): Path<Uuid>,
|
|
) -> Result<StatusCode, (StatusCode, String)> {
|
|
let deleted = db::delete_site(&state.db, id)
|
|
.await
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
|
|
if deleted {
|
|
Ok(StatusCode::NO_CONTENT)
|
|
} else {
|
|
Err((StatusCode::NOT_FOUND, "Site not found".to_string()))
|
|
}
|
|
}
|