Add vpn get and config rpc calls

This commit is contained in:
Samuel Lorch 2023-10-28 21:02:31 +02:00
parent 21c1f303aa
commit c75944f990
4 changed files with 113 additions and 0 deletions

25
src/api/config.rs Normal file
View file

@ -0,0 +1,25 @@
use jsonrpsee::types::Params;
use crate::state::RpcState;
use super::ApiError;
pub fn get_pending_changelog(_: Params, state: &RpcState) -> Result<(), ApiError> {
Err(ApiError::Leet)
}
pub fn apply_pending_changes(_: Params, state: &RpcState) -> Result<(), ApiError> {
state
.config_manager
.clone()
.apply_pending_changes()
.map_err(|source| ApiError::Leet)
}
pub fn discard_pending_changes(_: Params, state: &RpcState) -> Result<(), ApiError> {
state
.config_manager
.clone()
.discard_pending_changes()
.map_err(|source| ApiError::Leet)
}

View file

@ -1,8 +1,10 @@
mod config;
mod firewall;
mod network;
mod object;
mod service;
mod system;
mod vpn;
use crate::state::RpcState;
use jsonrpsee::{
@ -89,5 +91,41 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
)
.unwrap();
module
.register_method(
"config.get_pending_changelog",
config::get_pending_changelog,
)
.unwrap();
module
.register_method(
"config.apply_pending_changes",
config::apply_pending_changes,
)
.unwrap();
module
.register_method(
"config.discard_pending_changes",
config::discard_pending_changes,
)
.unwrap();
module
.register_method("vpn.get_wireguard_status", vpn::get_wireguard_status)
.unwrap();
module
.register_method(
"vpn.get_wireguard_interfaces",
vpn::get_wireguard_interfaces,
)
.unwrap();
module
.register_method("vpn.get_wireguard_peers", vpn::get_wireguard_peers)
.unwrap();
module
}

41
src/api/vpn.rs Normal file
View file

@ -0,0 +1,41 @@
use std::collections::HashMap;
use jsonrpsee::types::Params;
use crate::{
definitions::{
service::{DHCPServer, DNSServer, NTPServer},
vpn::{WireguardInterface, WireguardPeer},
},
state::RpcState,
};
use super::ApiError;
pub fn get_wireguard_status(_: Params, state: &RpcState) -> Result<String, ApiError> {
Ok("ok".to_string())
}
pub fn get_wireguard_interfaces(
_: Params,
state: &RpcState,
) -> Result<HashMap<String, WireguardInterface>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.vpn
.wireguard
.interfaces)
}
pub fn get_wireguard_peers(
_: Params,
state: &RpcState,
) -> Result<HashMap<String, WireguardPeer>, ApiError> {
Ok(state
.config_manager
.get_pending_config()
.vpn
.wireguard
.peers)
}

View file

@ -1,5 +1,7 @@
use validator::Validate;
use crate::api::ApiError;
use super::definitions::config::Config;
use std::fs;
use std::sync::{Arc, Mutex, MutexGuard};
@ -17,6 +19,13 @@ custom_error! { pub ConfigError
UnsupportedVersionError = "unsupported config version",
}
// TODO do Error conversion
impl Into<ApiError> for ConfigError {
fn into(self) -> ApiError {
ApiError::Leet
}
}
pub const CURRENT_CONFIG_PATH: &str = "config.json";
pub const PENDING_CONFIG_PATH: &str = "pending.json";