2021-03-29 17:22:14 +00:00
|
|
|
use crate::*;
|
2021-06-10 13:57:47 +00:00
|
|
|
use anyhow::anyhow;
|
2022-01-11 07:54:59 +00:00
|
|
|
use clap::Parser;
|
2021-03-29 17:22:14 +00:00
|
|
|
use db::DatabaseCidr;
|
|
|
|
use dialoguer::{theme::ColorfulTheme, Input};
|
|
|
|
use indoc::printdoc;
|
2022-02-01 05:01:21 +00:00
|
|
|
use ipnet::IpNet;
|
2021-05-09 12:04:49 +00:00
|
|
|
use publicip::Preference;
|
2021-03-29 17:22:14 +00:00
|
|
|
use rusqlite::{params, Connection};
|
2022-02-01 05:01:21 +00:00
|
|
|
use shared::{
|
|
|
|
prompts, CidrContents, Endpoint, IpNetExt, PeerContents, PERSISTENT_KEEPALIVE_INTERVAL_SECS,
|
|
|
|
};
|
2021-09-15 03:29:58 +00:00
|
|
|
use wireguard_control::KeyPair;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
fn create_database<P: AsRef<Path>>(
|
|
|
|
database_path: P,
|
|
|
|
) -> Result<Connection, Box<dyn std::error::Error>> {
|
|
|
|
let conn = Connection::open(&database_path)?;
|
|
|
|
conn.pragma_update(None, "foreign_keys", &1)?;
|
|
|
|
conn.execute(db::peer::CREATE_TABLE_SQL, params![])?;
|
|
|
|
conn.execute(db::association::CREATE_TABLE_SQL, params![])?;
|
|
|
|
conn.execute(db::cidr::CREATE_TABLE_SQL, params![])?;
|
2021-05-08 15:32:51 +00:00
|
|
|
conn.pragma_update(None, "user_version", &db::CURRENT_VERSION)?;
|
2021-09-01 09:58:46 +00:00
|
|
|
log::debug!("set database version to db::CURRENT_VERSION");
|
2021-05-08 15:32:51 +00:00
|
|
|
|
2021-03-29 17:22:14 +00:00
|
|
|
Ok(conn)
|
|
|
|
}
|
|
|
|
|
2022-01-11 07:51:32 +00:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Parser)]
|
2021-04-17 16:12:15 +00:00
|
|
|
pub struct InitializeOpts {
|
|
|
|
/// The network name (ex: evilcorp)
|
2022-01-11 07:51:32 +00:00
|
|
|
#[clap(long)]
|
2021-11-05 03:36:35 +00:00
|
|
|
pub network_name: Option<Interface>,
|
2021-04-17 16:12:15 +00:00
|
|
|
|
|
|
|
/// The network CIDR (ex: 10.42.0.0/16)
|
2022-01-11 07:51:32 +00:00
|
|
|
#[clap(long)]
|
2022-02-01 05:01:21 +00:00
|
|
|
pub network_cidr: Option<IpNet>,
|
2021-04-17 16:12:15 +00:00
|
|
|
|
|
|
|
/// This server's external endpoint (ex: 100.100.100.100:51820)
|
2022-01-11 07:51:32 +00:00
|
|
|
#[clap(long, conflicts_with = "auto-external-endpoint")]
|
2021-04-20 15:35:10 +00:00
|
|
|
pub external_endpoint: Option<Endpoint>,
|
2021-04-17 16:12:15 +00:00
|
|
|
|
|
|
|
/// Auto-resolve external endpoint
|
2022-01-11 07:51:32 +00:00
|
|
|
#[clap(long = "auto-external-endpoint")]
|
2021-04-17 16:12:15 +00:00
|
|
|
pub auto_external_endpoint: bool,
|
|
|
|
|
|
|
|
/// Port to listen on (for the WireGuard interface)
|
2022-01-11 07:51:32 +00:00
|
|
|
#[clap(long)]
|
2021-04-17 16:12:15 +00:00
|
|
|
pub listen_port: Option<u16>,
|
|
|
|
}
|
|
|
|
|
2021-03-29 17:22:14 +00:00
|
|
|
struct DbInitData {
|
2021-04-17 16:12:15 +00:00
|
|
|
network_name: String,
|
2022-02-01 05:01:21 +00:00
|
|
|
network_cidr: IpNet,
|
|
|
|
server_cidr: IpNet,
|
2021-03-29 17:22:14 +00:00
|
|
|
our_ip: IpAddr,
|
|
|
|
public_key_base64: String,
|
2021-04-20 15:35:10 +00:00
|
|
|
endpoint: Endpoint,
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn populate_database(conn: &Connection, db_init_data: DbInitData) -> Result<(), Error> {
|
|
|
|
const SERVER_NAME: &str = "innernet-server";
|
|
|
|
|
|
|
|
let root_cidr = DatabaseCidr::create(
|
2021-06-22 02:27:29 +00:00
|
|
|
conn,
|
2021-03-29 17:22:14 +00:00
|
|
|
CidrContents {
|
2021-04-17 16:12:15 +00:00
|
|
|
name: db_init_data.network_name.clone(),
|
|
|
|
cidr: db_init_data.network_cidr,
|
2021-03-29 17:22:14 +00:00
|
|
|
parent: None,
|
|
|
|
},
|
|
|
|
)
|
2021-06-10 13:57:47 +00:00
|
|
|
.map_err(|_| anyhow!("failed to create root CIDR"))?;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
let server_cidr = DatabaseCidr::create(
|
2021-06-22 02:27:29 +00:00
|
|
|
conn,
|
2021-03-29 17:22:14 +00:00
|
|
|
CidrContents {
|
|
|
|
name: SERVER_NAME.into(),
|
|
|
|
cidr: db_init_data.server_cidr,
|
|
|
|
parent: Some(root_cidr.id),
|
|
|
|
},
|
|
|
|
)
|
2021-06-10 13:57:47 +00:00
|
|
|
.map_err(|_| anyhow!("failed to create innernet-server CIDR"))?;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
let _me = DatabasePeer::create(
|
2021-06-22 02:27:29 +00:00
|
|
|
conn,
|
2021-03-29 17:22:14 +00:00
|
|
|
PeerContents {
|
2021-06-10 13:57:47 +00:00
|
|
|
name: SERVER_NAME.parse().map_err(|e: &str| anyhow!(e))?,
|
2021-03-29 17:22:14 +00:00
|
|
|
ip: db_init_data.our_ip,
|
|
|
|
cidr_id: server_cidr.id,
|
|
|
|
public_key: db_init_data.public_key_base64,
|
|
|
|
endpoint: Some(db_init_data.endpoint),
|
|
|
|
is_admin: true,
|
|
|
|
is_disabled: false,
|
|
|
|
is_redeemed: true,
|
|
|
|
persistent_keepalive_interval: Some(PERSISTENT_KEEPALIVE_INTERVAL_SECS),
|
2021-05-08 15:32:51 +00:00
|
|
|
invite_expires: None,
|
2021-09-01 09:58:46 +00:00
|
|
|
candidates: vec![],
|
2021-03-29 17:22:14 +00:00
|
|
|
},
|
|
|
|
)
|
2021-06-10 13:57:47 +00:00
|
|
|
.map_err(|_| anyhow!("failed to create innernet peer."))?;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-17 16:12:15 +00:00
|
|
|
pub fn init_wizard(conf: &ServerConfig, opts: InitializeOpts) -> Result<(), Error> {
|
2021-03-29 17:22:14 +00:00
|
|
|
let theme = ColorfulTheme::default();
|
|
|
|
|
|
|
|
shared::ensure_dirs_exist(&[conf.config_dir(), conf.database_dir()]).map_err(|_| {
|
2021-06-10 13:57:47 +00:00
|
|
|
anyhow!(
|
2021-03-29 17:22:14 +00:00
|
|
|
"Failed to create config and database directories {}",
|
|
|
|
"(are you not running as root?)".bold()
|
|
|
|
)
|
|
|
|
})?;
|
2021-05-09 12:04:49 +00:00
|
|
|
printdoc!(
|
|
|
|
"\nTime to setup your innernet network.
|
|
|
|
|
|
|
|
Your network name can be any hostname-valid string, i.e. \"evilcorp\", and
|
|
|
|
your network CIDR should be in the RFC1918 IPv4 (10/8, 172.16/12, or 192.168/16),
|
|
|
|
or RFC4193 IPv6 (fd00::/8) ranges.
|
|
|
|
|
|
|
|
The external endpoint specified is a <host>:<port> string that is the address clients
|
|
|
|
will connect to. It's up to you to forward/open ports in your routers/firewalls
|
|
|
|
as needed.
|
|
|
|
|
|
|
|
For more usage instructions, see https://github.com/tonarino/innernet#usage
|
|
|
|
\n"
|
|
|
|
);
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-11-05 03:36:35 +00:00
|
|
|
let name: Interface = if let Some(name) = opts.network_name {
|
2021-05-06 03:32:54 +00:00
|
|
|
name
|
2021-04-17 16:12:15 +00:00
|
|
|
} else {
|
|
|
|
Input::with_theme(&theme)
|
2021-03-29 17:22:14 +00:00
|
|
|
.with_prompt("Network name")
|
2021-04-17 16:12:15 +00:00
|
|
|
.interact()?
|
|
|
|
};
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2022-02-01 05:01:21 +00:00
|
|
|
let root_cidr: IpNet = if let Some(cidr) = opts.network_cidr {
|
2021-05-06 03:32:54 +00:00
|
|
|
cidr
|
2021-04-17 16:12:15 +00:00
|
|
|
} else {
|
|
|
|
Input::with_theme(&theme)
|
2021-03-29 17:22:14 +00:00
|
|
|
.with_prompt("Network CIDR")
|
|
|
|
.with_initial_text("10.42.0.0/16")
|
2021-04-17 16:12:15 +00:00
|
|
|
.interact()?
|
|
|
|
};
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-04-17 16:12:15 +00:00
|
|
|
let listen_port: u16 = if let Some(listen_port) = opts.listen_port {
|
|
|
|
listen_port
|
|
|
|
} else {
|
2021-03-29 17:22:14 +00:00
|
|
|
Input::with_theme(&theme)
|
|
|
|
.with_prompt("Listen port")
|
|
|
|
.default(51820)
|
|
|
|
.interact()
|
2021-06-10 13:57:47 +00:00
|
|
|
.map_err(|_| anyhow!("failed to get listen port."))?
|
2021-04-17 16:12:15 +00:00
|
|
|
};
|
|
|
|
|
2021-09-15 11:43:38 +00:00
|
|
|
log::info!("listen port: {}", listen_port);
|
|
|
|
|
|
|
|
let endpoint: Endpoint = if let Some(endpoint) = opts.external_endpoint {
|
|
|
|
endpoint
|
|
|
|
} else if opts.auto_external_endpoint {
|
|
|
|
let ip = publicip::get_any(Preference::Ipv4)
|
|
|
|
.ok_or_else(|| anyhow!("couldn't get external IP"))?;
|
|
|
|
SocketAddr::new(ip, listen_port).into()
|
|
|
|
} else {
|
|
|
|
prompts::ask_endpoint(listen_port)?
|
|
|
|
};
|
|
|
|
|
2021-03-29 17:22:14 +00:00
|
|
|
let our_ip = root_cidr
|
2022-02-01 05:01:21 +00:00
|
|
|
.hosts()
|
|
|
|
.find(|ip| root_cidr.is_assignable(ip))
|
2021-03-29 17:22:14 +00:00
|
|
|
.unwrap();
|
|
|
|
let config_path = conf.config_path(&name);
|
|
|
|
let our_keypair = KeyPair::generate();
|
|
|
|
|
|
|
|
let config = ConfigFile {
|
|
|
|
private_key: our_keypair.private.to_base64(),
|
|
|
|
listen_port,
|
|
|
|
address: our_ip,
|
2022-02-01 05:01:21 +00:00
|
|
|
network_cidr_prefix: root_cidr.prefix_len(),
|
2021-03-29 17:22:14 +00:00
|
|
|
};
|
|
|
|
config.write_to_path(&config_path)?;
|
|
|
|
|
|
|
|
let db_init_data = DbInitData {
|
2021-04-17 16:12:15 +00:00
|
|
|
network_name: name.to_string(),
|
|
|
|
network_cidr: root_cidr,
|
2022-02-01 05:01:21 +00:00
|
|
|
server_cidr: IpNet::new(our_ip, root_cidr.max_prefix_len())?,
|
2021-03-29 17:22:14 +00:00
|
|
|
our_ip,
|
|
|
|
public_key_base64: our_keypair.public.to_base64(),
|
|
|
|
endpoint,
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(bschwind) - Clean up the config file and database
|
|
|
|
// if any errors occur in these init calls.
|
|
|
|
|
|
|
|
let database_path = conf.database_path(&name);
|
|
|
|
let conn = create_database(&database_path).map_err(|_| {
|
2021-06-10 13:57:47 +00:00
|
|
|
anyhow!(
|
2021-03-29 17:22:14 +00:00
|
|
|
"failed to create database {}",
|
|
|
|
"(are you not running as root?)".bold()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
populate_database(&conn, db_init_data)?;
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{} Created database at {}\n",
|
|
|
|
"[*]".dimmed(),
|
|
|
|
database_path.to_string_lossy().bold()
|
|
|
|
);
|
|
|
|
printdoc!(
|
|
|
|
"
|
|
|
|
{star} Setup finished.
|
|
|
|
|
2021-05-09 12:04:49 +00:00
|
|
|
Network {interface} has been {created}, but it's not started yet!
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
Your new network starts with only one peer: this innernet server. Next,
|
|
|
|
you'll want to create additional CIDRs and peers using the commands:
|
|
|
|
|
|
|
|
{wg_manage_server} {add_cidr} {interface}, and
|
|
|
|
{wg_manage_server} {add_peer} {interface}
|
|
|
|
|
2021-05-09 12:04:49 +00:00
|
|
|
See https://github.com/tonarino/innernet for more detailed instruction
|
|
|
|
on designing your network.
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
When you're ready to start the network, you can auto-start the server:
|
|
|
|
|
|
|
|
{systemctl_enable}{interface}
|
|
|
|
|
|
|
|
",
|
|
|
|
star = "[*]".dimmed(),
|
2021-04-09 01:28:37 +00:00
|
|
|
interface = name.to_string().yellow(),
|
2021-03-29 17:22:14 +00:00
|
|
|
created = "created".green(),
|
|
|
|
wg_manage_server = "innernet-server".yellow(),
|
|
|
|
add_cidr = "add-cidr".yellow(),
|
|
|
|
add_peer = "add-peer".yellow(),
|
|
|
|
systemctl_enable = "systemctl enable --now innernet-server@".yellow(),
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|