Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tools/defguard_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ This means that if you have a development environment set up already it should j

```bash
cargo run -p defguard_generator -- vpn-session-stats \
--location-id 1 \
--num-users 10 \
--devices-per-user 2 \
--sessions-per-device 5

# optional: limit generation to one VPN location --location-id <ID>
```

### Session generation logic

For each device the generator always starts with creating an active (not disconnected) session.
For each device the generator always starts with creating an active (not disconnected) session.
If there are more sessions per device to be generated it goes backwards in time and creates
additional disconnected sessions.
Session duration and gaps between sessions are randomized but there is no logic to verify if
sessions are overlapping so by default the generator runs a `TRUNCATE` query at the start.
To disable this behavior (for example when running it multiple times for separate locations)
use the `--no-truncate` CLI flag.

39 changes: 39 additions & 0 deletions tools/defguard_generator/data/device_names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Acer Swift
Apple iMac
Apple Mac mini
Apple Mac Pro
Apple Mac Studio
Apple MacBook Air
Apple MacBook Pro
Asus ROG Zephyrus
Asus ZenBook
Chromebook
Dell Alienware
Dell Inspiron
Dell Latitude
Dell OptiPlex
Dell Precision
Dell XPS
Google Pixel
Google Pixel Pro
HP EliteBook
HP Envy
HP Pavilion
HP ProDesk
HP Spectre
iPhone
iPhone Pro
iPhone Pro Max
Lenovo IdeaPad
Lenovo Legion
Lenovo ThinkCentre
Lenovo ThinkPad
Microsoft Surface Laptop
Microsoft Surface Studio
Motorola Edge
OnePlus
Samsung Galaxy A
Samsung Galaxy S
Samsung Galaxy Z Flip
Samsung Galaxy Z Fold
Sony Xperia
99 changes: 99 additions & 0 deletions tools/defguard_generator/data/first_names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Elsie
Edward
William
Robert
Annie
Katie
Alma
Thelma
Ellen
Rebecca
Estella
Corine
Margie
Emmie
Myrtis
Pinkie
Myrtie
Jeanette
Rubie
Maud
Trudie
Barbara
Glennie
Lassie
Liza
Hettie
Iona
Lorraine
Willie
Fannie
Sarah
Myrtle
Rosie
Lillian
Nellie
Mamie
Edna
Evelyn
Laura
Mabel
Josie
Pearlie
Rachel
Era
Bettie
Jean
Anne
Lucinda
Dessie
Linnie
Marion
Dovie
Ira
Ossie
Abbie
Clyde
Goldie
Lenora
Aline
Gennie
Vinnie
Exie
Tressie
Joe
Louis
Alfred
Julius
Sidney
Nelson
Isaac
Jimmy
King
Porter
Tommy
Cleo
Dennis
Gerald
Moses
Green
Luke
Richard
Clarence
Ernest
Ralph
Emmett
Dave
Harvey
Ray
Simon
Leslie
Wilson
Emmitt
Gordon
Hollis
Claud
Clayton
Dewey
Lamar
Ruth
100 changes: 100 additions & 0 deletions tools/defguard_generator/data/last_names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Smith
Johnson
Williams
Brown
Jones
Miller
Davis
Wilson
Anderson
Thomas
Taylor
Moore
Jackson
Martin
Thompson
White
Harris
Clark
Lewis
Robinson
Walker
Young
Allen
King
Wright
Scott
Hill
Green
Adams
Nelson
Baker
Hall
Campbell
Mitchell
Carter
Roberts
Phillips
Evans
Turner
Parker
Edwards
Collins
Stewart
Morris
Murphy
Cook
Rogers
Morgan
Cooper
Peterson
Bailey
Reed
Kelly
Howard
Cox
Ward
Richardson
Watson
Brooks
Wood
James
Bennett
Gray
Hughes
Price
Sanders
Myers
Long
Ross
Foster
Powell
Jenkins
Perry
Russell
Sullivan
Bell
Coleman
Henderson
Barnes
Simmons
Patterson
Jordan
Reynolds
Hamilton
Graham
Wallace
Cole
West
Hayes
Woods
Harrison
Gibson
McDonald
Marshall
Murray
Freeman
Wells
Webb
Simpson
Tucker
2 changes: 1 addition & 1 deletion tools/defguard_generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum Commands {
/// Generates fake VPN session statistics.
VpnSessionStats {
#[arg(long)]
location_id: Id,
location_id: Option<Id>,
#[arg(long)]
num_users: usize,
#[arg(long)]
Expand Down
26 changes: 25 additions & 1 deletion tools/defguard_generator/src/user_devices.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::collections::HashSet;

use anyhow::Result;
use defguard_common::db::{
Id,
models::{Device, User},
models::{Device, DeviceType, User},
};
use rand::{Rng, rngs::ThreadRng};
use sqlx::PgPool;
use tracing::info;

const DEVICE_NAMES: &str = include_str!("../data/device_names.txt");

pub async fn prepare_user_devices(
pool: &PgPool,
rng: &mut ThreadRng,
Expand All @@ -24,14 +28,34 @@ pub async fn prepare_user_devices(
);
return Ok(user_devices[..devices_per_user].to_vec());
}
let device_names: Vec<&str> = DEVICE_NAMES.lines().collect();
let mut taken_names: HashSet<String> = user_devices
.iter()
.map(|device| device.name.clone())
.collect();

// if there are not enough users create new ones
for _ in 0..(devices_per_user - user_devices.len()) {
let mut device: Device = rng.r#gen();
let base_name = device_names[rng.gen_range(0..device_names.len())];
device.name = unique_device_name(base_name, &mut taken_names);
device.user_id = user.id;
device.device_type = DeviceType::User;
device.description = None;
let device = device.save(pool).await?;
user_devices.push(device);
}

Ok(user_devices)
}

fn unique_device_name(base: &str, taken: &mut HashSet<String>) -> String {
let mut name = base.to_string();
let mut suffix = 1;
while taken.contains(&name) {
suffix += 1;
name = format!("{base}{suffix}");
}
taken.insert(name.clone());
name
}
31 changes: 30 additions & 1 deletion tools/defguard_generator/src/users.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::collections::HashSet;

use anyhow::Result;
use defguard_common::db::{Id, models::User};
use rand::{Rng, rngs::ThreadRng};
use sqlx::PgPool;
use tracing::info;

const FIRST_NAMES: &str = include_str!("../data/first_names.txt");
const LAST_NAMES: &str = include_str!("../data/last_names.txt");

pub async fn prepare_users(
pool: &PgPool,
rng: &mut ThreadRng,
Expand All @@ -23,12 +28,36 @@ pub async fn prepare_users(
return Ok(all_users[..num_users].to_vec());
}

let first_names: Vec<&str> = FIRST_NAMES.lines().collect();
let last_names: Vec<&str> = LAST_NAMES.lines().collect();
let mut taken_usernames: HashSet<String> =
all_users.iter().map(|user| user.username.clone()).collect();

// if there are not enough users create new ones
for _ in 0..(num_users - all_users.len()) {
let user: User = rng.r#gen();
let mut user: User = rng.r#gen();
let first_name = first_names[rng.gen_range(0..first_names.len())];
let last_name = last_names[rng.gen_range(0..last_names.len())];
user.username = unique_username(first_name, last_name, &mut taken_usernames);
user.first_name = first_name.to_string();
user.last_name = last_name.to_string();
user.email = format!("{}@defguard.net", user.username);
user.phone = Some("123454321".to_string());
let user = user.save(pool).await?;
all_users.push(user);
}

Ok(all_users)
}

fn unique_username(first_name: &str, last_name: &str, taken: &mut HashSet<String>) -> String {
let base = format!("{first_name}.{last_name}").to_lowercase();
let mut username = base.clone();
let mut suffix = 1;
while taken.contains(&username) {
suffix += 1;
username = format!("{base}{suffix}");
}
taken.insert(username.clone());
username
}
Loading
Loading