mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
Add vpn get and config rpc calls
This commit is contained in:
parent
21c1f303aa
commit
c75944f990
4 changed files with 113 additions and 0 deletions
25
src/api/config.rs
Normal file
25
src/api/config.rs
Normal 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)
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
|
mod config;
|
||||||
mod firewall;
|
mod firewall;
|
||||||
mod network;
|
mod network;
|
||||||
mod object;
|
mod object;
|
||||||
mod service;
|
mod service;
|
||||||
mod system;
|
mod system;
|
||||||
|
mod vpn;
|
||||||
|
|
||||||
use crate::state::RpcState;
|
use crate::state::RpcState;
|
||||||
use jsonrpsee::{
|
use jsonrpsee::{
|
||||||
|
@ -89,5 +91,41 @@ pub fn new_rpc_module(state: RpcState) -> RpcModule<RpcState> {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.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
|
module
|
||||||
}
|
}
|
||||||
|
|
41
src/api/vpn.rs
Normal file
41
src/api/vpn.rs
Normal 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)
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
|
use crate::api::ApiError;
|
||||||
|
|
||||||
use super::definitions::config::Config;
|
use super::definitions::config::Config;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::sync::{Arc, Mutex, MutexGuard};
|
use std::sync::{Arc, Mutex, MutexGuard};
|
||||||
|
@ -17,6 +19,13 @@ custom_error! { pub ConfigError
|
||||||
UnsupportedVersionError = "unsupported config version",
|
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 CURRENT_CONFIG_PATH: &str = "config.json";
|
||||||
pub const PENDING_CONFIG_PATH: &str = "pending.json";
|
pub const PENDING_CONFIG_PATH: &str = "pending.json";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue