Skip to content

Commit 22ae823

Browse files
authored
Change init vpn command behavior (#1169)
* change init vpn command * comment * revert to save method * formatting * grammar
1 parent be94713 commit 22ae823

File tree

2 files changed

+80
-24
lines changed

2 files changed

+80
-24
lines changed

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ pub struct InitVpnLocationArgs {
188188
pub dns: Option<String>,
189189
#[arg(long)]
190190
pub allowed_ips: Vec<IpNetwork>,
191+
#[arg(long)]
192+
pub id: Option<i64>,
191193
}
192194

193195
impl DefGuardConfig {

src/lib.rs

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -736,36 +736,90 @@ pub async fn init_dev_env(config: &DefGuardConfig) {
736736

737737
/// Create a new VPN location.
738738
/// Meant to be used to automate setting up a new defguard instance.
739-
/// Does not handle assigning device IPs, since no device should exist at this point.
739+
/// If the network ID has been specified, it will be assumed that the user wants to update the existing network or create a new one with a predefined ID.
740+
/// This is mainly used for deployment purposes where the network ID must be known beforehand.
741+
///
742+
/// If there is no ID specified, the function will only create the network if no other network exists.
743+
/// In other words, multiple networks can be created, but only if the ID is predefined for each network.
740744
pub async fn init_vpn_location(
741745
pool: &PgPool,
742746
args: &InitVpnLocationArgs,
743747
) -> Result<String, anyhow::Error> {
744-
// check if a VPN location exists already
745-
let networks = WireguardNetwork::all(pool).await?;
746-
if !networks.is_empty() {
747-
return Err(anyhow!(
748-
"Failed to initialize first VPN location. A location already exists."
749-
));
748+
// The ID is predefined
749+
let network = if let Some(location_id) = args.id {
750+
let mut transaction = pool.begin().await?;
751+
// If the network already exists, update it, assuming that's the user's intent.
752+
let network = if let Some(mut network) =
753+
WireguardNetwork::find_by_id(&mut *transaction, location_id).await?
754+
{
755+
network.name = args.name.clone();
756+
network.address = vec![args.address];
757+
network.port = args.port;
758+
network.endpoint = args.endpoint.clone();
759+
network.dns = args.dns.clone();
760+
network.allowed_ips = args.allowed_ips.clone();
761+
network.save(&mut *transaction).await?;
762+
network.sync_allowed_devices(&mut transaction, None).await?;
763+
network
764+
}
765+
// Otherwise create it with the predefined ID
766+
else {
767+
let network = WireguardNetwork::new(
768+
args.name.clone(),
769+
vec![args.address],
770+
args.port,
771+
args.endpoint.clone(),
772+
args.dns.clone(),
773+
args.allowed_ips.clone(),
774+
false,
775+
DEFAULT_KEEPALIVE_INTERVAL,
776+
DEFAULT_DISCONNECT_THRESHOLD,
777+
false,
778+
false,
779+
)?
780+
.save(&mut *transaction)
781+
.await?;
782+
if network.id != location_id {
783+
return Err(anyhow!(
784+
"Failed to initialize VPN location. The ID of the newly created network ({}) does not match \
785+
the predefined ID ({location_id}). The predefined ID must be the next available ID.",
786+
network.id
787+
));
788+
}
789+
network.add_all_allowed_devices(&mut transaction).await?;
790+
network
791+
};
792+
transaction.commit().await?;
793+
network
794+
}
795+
// No predefined ID, add the network if no other networks are present
796+
else {
797+
// check if a VPN location exists already
798+
let networks = WireguardNetwork::all(pool).await?;
799+
if !networks.is_empty() {
800+
return Err(anyhow!(
801+
"Failed to initialize first VPN location. Location already exists."
802+
));
803+
};
804+
805+
// create a new network
806+
WireguardNetwork::new(
807+
args.name.clone(),
808+
vec![args.address],
809+
args.port,
810+
args.endpoint.clone(),
811+
args.dns.clone(),
812+
args.allowed_ips.clone(),
813+
false,
814+
DEFAULT_KEEPALIVE_INTERVAL,
815+
DEFAULT_DISCONNECT_THRESHOLD,
816+
false,
817+
false,
818+
)?
819+
.save(pool)
820+
.await?
750821
};
751822

752-
// create a new network
753-
let network = WireguardNetwork::new(
754-
args.name.clone(),
755-
vec![args.address],
756-
args.port,
757-
args.endpoint.clone(),
758-
args.dns.clone(),
759-
args.allowed_ips.clone(),
760-
false,
761-
DEFAULT_KEEPALIVE_INTERVAL,
762-
DEFAULT_DISCONNECT_THRESHOLD,
763-
false,
764-
false,
765-
)?
766-
.save(pool)
767-
.await?;
768-
769823
// generate gateway token
770824
let token = Claims::new(
771825
ClaimsType::Gateway,

0 commit comments

Comments
 (0)