2021-03-29 17:22:14 +00:00
|
|
|
use colored::*;
|
2021-04-09 13:42:29 +00:00
|
|
|
use dialoguer::Confirm;
|
2021-05-06 03:32:54 +00:00
|
|
|
use hyper::{http, server::conn::AddrStream, Body, Request, Response};
|
2021-03-29 17:22:14 +00:00
|
|
|
use indoc::printdoc;
|
|
|
|
use ipnetwork::IpNetwork;
|
2021-05-08 15:32:51 +00:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2021-03-29 17:22:14 +00:00
|
|
|
use rusqlite::Connection;
|
2021-05-06 03:32:54 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-04-17 03:18:50 +00:00
|
|
|
use shared::{AddCidrOpts, AddPeerOpts, IoErrorContext, INNERNET_PUBKEY_HEADER};
|
2021-03-29 17:22:14 +00:00
|
|
|
use std::{
|
2021-05-08 15:32:51 +00:00
|
|
|
collections::{HashMap, VecDeque},
|
2021-05-06 03:32:54 +00:00
|
|
|
convert::TryInto,
|
2021-03-29 17:22:14 +00:00
|
|
|
env,
|
|
|
|
fs::File,
|
|
|
|
io::prelude::*,
|
2021-04-05 07:34:21 +00:00
|
|
|
net::{IpAddr, SocketAddr, TcpListener},
|
2021-03-29 17:22:14 +00:00
|
|
|
ops::Deref,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::Arc,
|
2021-05-08 15:32:51 +00:00
|
|
|
time::Duration,
|
2021-03-29 17:22:14 +00:00
|
|
|
};
|
|
|
|
use structopt::StructOpt;
|
2021-04-09 04:48:00 +00:00
|
|
|
use subtle::ConstantTimeEq;
|
|
|
|
use wgctrl::{DeviceConfigBuilder, DeviceInfo, InterfaceName, Key, PeerConfigBuilder};
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
pub mod db;
|
|
|
|
pub mod error;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
2021-05-06 03:32:54 +00:00
|
|
|
pub mod util;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
mod initialize;
|
|
|
|
|
|
|
|
use db::{DatabaseCidr, DatabasePeer};
|
|
|
|
pub use error::ServerError;
|
2021-04-19 12:56:18 +00:00
|
|
|
use initialize::InitializeOpts;
|
2021-03-29 17:22:14 +00:00
|
|
|
use shared::{prompts, wg, CidrTree, Error, Interface, SERVER_CONFIG_DIR, SERVER_DATABASE_DIR};
|
|
|
|
pub use shared::{Association, AssociationContents};
|
|
|
|
|
|
|
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(name = "innernet-server", about)]
|
|
|
|
struct Opt {
|
|
|
|
#[structopt(subcommand)]
|
|
|
|
command: Command,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
enum Command {
|
|
|
|
/// Create a new network.
|
|
|
|
#[structopt(alias = "init")]
|
2021-04-17 16:12:15 +00:00
|
|
|
New {
|
|
|
|
#[structopt(flatten)]
|
2021-04-19 12:56:18 +00:00
|
|
|
opts: InitializeOpts,
|
2021-04-17 16:12:15 +00:00
|
|
|
},
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-04-09 13:42:29 +00:00
|
|
|
/// Permanently uninstall a created network, rendering it unusable. Use with care.
|
|
|
|
Uninstall { interface: Interface },
|
|
|
|
|
2021-03-29 17:22:14 +00:00
|
|
|
/// Serve the coordinating server for an existing network.
|
|
|
|
Serve { interface: Interface },
|
|
|
|
|
|
|
|
/// Add a peer to an existing network.
|
2021-04-14 15:25:31 +00:00
|
|
|
AddPeer {
|
|
|
|
interface: Interface,
|
|
|
|
|
|
|
|
#[structopt(flatten)]
|
2021-04-17 03:18:50 +00:00
|
|
|
args: AddPeerOpts,
|
2021-04-14 15:25:31 +00:00
|
|
|
},
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
/// Add a new CIDR to an existing network.
|
2021-04-14 15:25:31 +00:00
|
|
|
AddCidr {
|
|
|
|
interface: Interface,
|
|
|
|
|
|
|
|
#[structopt(flatten)]
|
2021-04-17 03:18:50 +00:00
|
|
|
args: AddCidrOpts,
|
2021-04-14 15:25:31 +00:00
|
|
|
},
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Db = Arc<Mutex<Connection>>;
|
2021-05-08 15:32:51 +00:00
|
|
|
pub type Endpoints = Arc<RwLock<HashMap<String, SocketAddr>>>;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Context {
|
|
|
|
pub db: Db,
|
2021-05-08 15:32:51 +00:00
|
|
|
pub endpoints: Arc<RwLock<HashMap<String, SocketAddr>>>,
|
2021-04-09 01:28:37 +00:00
|
|
|
pub interface: InterfaceName,
|
2021-04-09 04:48:00 +00:00
|
|
|
pub public_key: Key,
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Session {
|
|
|
|
pub context: Context,
|
|
|
|
pub peer: DatabasePeer,
|
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
impl Session {
|
|
|
|
pub fn admin_capable(&self) -> bool {
|
|
|
|
self.peer.is_admin && self.user_capable()
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
pub fn user_capable(&self) -> bool {
|
|
|
|
!self.peer.is_disabled && self.peer.is_redeemed
|
|
|
|
}
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
pub fn redeemable(&self) -> bool {
|
|
|
|
!self.peer.is_disabled && !self.peer.is_redeemed
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
pub struct ConfigFile {
|
|
|
|
/// The server's WireGuard key
|
|
|
|
pub private_key: String,
|
|
|
|
|
|
|
|
/// The listen port of the server
|
|
|
|
pub listen_port: u16,
|
|
|
|
|
|
|
|
/// The internal WireGuard IP address assigned to the server
|
|
|
|
pub address: IpAddr,
|
|
|
|
|
|
|
|
/// The CIDR prefix of the WireGuard network
|
|
|
|
pub network_cidr_prefix: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigFile {
|
|
|
|
pub fn write_to_path<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
|
|
|
|
let mut invitation_file = File::create(&path).with_path(&path)?;
|
2021-04-09 06:00:53 +00:00
|
|
|
shared::chmod(&invitation_file, 0o600)?;
|
2021-03-29 17:22:14 +00:00
|
|
|
invitation_file
|
|
|
|
.write_all(toml::to_string(self).unwrap().as_bytes())
|
|
|
|
.with_path(path)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
|
2021-04-09 06:00:53 +00:00
|
|
|
let path = path.as_ref();
|
|
|
|
let file = File::open(path).with_path(path)?;
|
|
|
|
if shared::chmod(&file, 0o600)? {
|
2021-04-09 13:42:29 +00:00
|
|
|
println!(
|
|
|
|
"{} updated permissions for {} to 0600.",
|
|
|
|
"[!]".yellow(),
|
|
|
|
path.display()
|
|
|
|
);
|
2021-04-09 06:00:53 +00:00
|
|
|
}
|
2021-03-29 17:22:14 +00:00
|
|
|
Ok(toml::from_slice(&std::fs::read(&path).with_path(path)?)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct ServerConfig {
|
|
|
|
wg_manage_dir_override: Option<PathBuf>,
|
|
|
|
wg_dir_override: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerConfig {
|
|
|
|
fn database_dir(&self) -> &Path {
|
|
|
|
self.wg_manage_dir_override
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(*SERVER_DATABASE_DIR)
|
|
|
|
}
|
|
|
|
|
2021-04-09 01:28:37 +00:00
|
|
|
fn database_path(&self, interface: &InterfaceName) -> PathBuf {
|
2021-03-29 17:22:14 +00:00
|
|
|
PathBuf::new()
|
|
|
|
.join(self.database_dir())
|
2021-04-09 01:28:37 +00:00
|
|
|
.join(interface.to_string())
|
2021-03-29 17:22:14 +00:00
|
|
|
.with_extension("db")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config_dir(&self) -> &Path {
|
|
|
|
self.wg_dir_override
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(*SERVER_CONFIG_DIR)
|
|
|
|
}
|
|
|
|
|
2021-04-09 01:28:37 +00:00
|
|
|
fn config_path(&self, interface: &InterfaceName) -> PathBuf {
|
2021-03-29 17:22:14 +00:00
|
|
|
PathBuf::new()
|
|
|
|
.join(self.config_dir())
|
2021-04-09 01:28:37 +00:00
|
|
|
.join(interface.to_string())
|
2021-03-29 17:22:14 +00:00
|
|
|
.with_extension("conf")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
if env::var_os("RUST_LOG").is_none() {
|
|
|
|
// Set some default log settings.
|
|
|
|
env::set_var("RUST_LOG", "warn,warp=info,wg_manage_server=info");
|
|
|
|
}
|
|
|
|
|
|
|
|
pretty_env_logger::init();
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
|
|
|
if unsafe { libc::getuid() } != 0 {
|
|
|
|
return Err("innernet-server must run as root.".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let conf = ServerConfig::default();
|
|
|
|
|
|
|
|
match opt.command {
|
2021-04-17 16:12:15 +00:00
|
|
|
Command::New { opts } => {
|
|
|
|
if let Err(e) = initialize::init_wizard(&conf, opts) {
|
2021-03-29 17:22:14 +00:00
|
|
|
println!("{}: {}.", "creation failed".red(), e);
|
|
|
|
}
|
2021-05-09 15:09:50 +00:00
|
|
|
}
|
2021-04-09 13:42:29 +00:00
|
|
|
Command::Uninstall { interface } => uninstall(&interface, &conf)?,
|
2021-05-08 15:32:51 +00:00
|
|
|
Command::Serve { interface } => serve(*interface, &conf).await?,
|
2021-04-14 15:25:31 +00:00
|
|
|
Command::AddPeer { interface, args } => add_peer(&interface, &conf, args)?,
|
|
|
|
Command::AddCidr { interface, args } => add_cidr(&interface, &conf, args)?,
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open_database_connection(
|
2021-04-09 01:28:37 +00:00
|
|
|
interface: &InterfaceName,
|
2021-03-29 17:22:14 +00:00
|
|
|
conf: &ServerConfig,
|
|
|
|
) -> Result<rusqlite::Connection, Box<dyn std::error::Error>> {
|
|
|
|
let database_path = conf.database_path(&interface);
|
|
|
|
if !Path::new(&database_path).exists() {
|
|
|
|
return Err(format!(
|
|
|
|
"no database file found at {}",
|
|
|
|
database_path.to_string_lossy()
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
let conn = Connection::open(&database_path)?;
|
|
|
|
// Foreign key constraints aren't on in SQLite by default. Enable.
|
|
|
|
conn.pragma_update(None, "foreign_keys", &1)?;
|
2021-05-08 15:32:51 +00:00
|
|
|
db::auto_migrate(&conn)?;
|
2021-05-06 03:32:54 +00:00
|
|
|
Ok(conn)
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 15:25:31 +00:00
|
|
|
fn add_peer(
|
|
|
|
interface: &InterfaceName,
|
|
|
|
conf: &ServerConfig,
|
2021-04-17 16:12:15 +00:00
|
|
|
opts: AddPeerOpts,
|
2021-04-14 15:25:31 +00:00
|
|
|
) -> Result<(), Error> {
|
2021-04-09 01:28:37 +00:00
|
|
|
let config = ConfigFile::from_file(conf.config_path(interface))?;
|
2021-03-29 17:22:14 +00:00
|
|
|
let conn = open_database_connection(interface, conf)?;
|
|
|
|
let peers = DatabasePeer::list(&conn)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|dp| dp.inner)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let cidrs = DatabaseCidr::list(&conn)?;
|
|
|
|
let cidr_tree = CidrTree::new(&cidrs[..]);
|
|
|
|
|
2021-04-17 16:12:15 +00:00
|
|
|
if let Some((peer_request, keypair)) = shared::prompts::add_peer(&peers, &cidr_tree, &opts)? {
|
2021-03-29 17:22:14 +00:00
|
|
|
let peer = DatabasePeer::create(&conn, peer_request)?;
|
|
|
|
if cfg!(not(test)) && DeviceInfo::get_by_name(interface).is_ok() {
|
|
|
|
// Update the current WireGuard interface with the new peers.
|
|
|
|
DeviceConfigBuilder::new()
|
|
|
|
.add_peer((&*peer).into())
|
|
|
|
.apply(interface)
|
|
|
|
.map_err(|_| ServerError::WireGuard)?;
|
|
|
|
|
|
|
|
println!("adding to WireGuard interface: {}", &*peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
let server_peer = DatabasePeer::get(&conn, 1)?;
|
|
|
|
prompts::save_peer_invitation(
|
|
|
|
interface,
|
|
|
|
&peer,
|
|
|
|
&*server_peer,
|
|
|
|
&cidr_tree,
|
|
|
|
keypair,
|
|
|
|
&SocketAddr::new(config.address, config.listen_port),
|
2021-04-17 16:12:15 +00:00
|
|
|
&opts.save_config,
|
2021-03-29 17:22:14 +00:00
|
|
|
)?;
|
|
|
|
} else {
|
|
|
|
println!("exited without creating peer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:25:31 +00:00
|
|
|
fn add_cidr(
|
|
|
|
interface: &InterfaceName,
|
|
|
|
conf: &ServerConfig,
|
2021-04-17 16:12:15 +00:00
|
|
|
opts: AddCidrOpts,
|
2021-04-14 15:25:31 +00:00
|
|
|
) -> Result<(), Error> {
|
2021-03-29 17:22:14 +00:00
|
|
|
let conn = open_database_connection(interface, conf)?;
|
|
|
|
let cidrs = DatabaseCidr::list(&conn)?;
|
2021-04-17 16:12:15 +00:00
|
|
|
if let Some(cidr_request) = shared::prompts::add_cidr(&cidrs, &opts)? {
|
2021-03-29 17:22:14 +00:00
|
|
|
let cidr = DatabaseCidr::create(&conn, cidr_request)?;
|
|
|
|
printdoc!(
|
|
|
|
"
|
|
|
|
CIDR \"{cidr_name}\" added.
|
|
|
|
|
|
|
|
Right now, peers within {cidr_name} can only see peers in the same CIDR, and in
|
|
|
|
the special \"innernet-server\" CIDR that includes the innernet server peer.
|
|
|
|
|
|
|
|
You'll need to add more associations for peers in diffent CIDRs to communicate.
|
|
|
|
",
|
|
|
|
cidr_name = cidr.name.bold()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!("exited without creating CIDR.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
fn uninstall(interface: &InterfaceName, conf: &ServerConfig) -> Result<(), Error> {
|
|
|
|
if Confirm::with_theme(&*prompts::THEME)
|
|
|
|
.with_prompt(&format!(
|
|
|
|
"Permanently delete network \"{}\"?",
|
|
|
|
interface.as_str_lossy().yellow()
|
|
|
|
))
|
|
|
|
.default(false)
|
|
|
|
.interact()?
|
|
|
|
{
|
|
|
|
println!("{} bringing down interface (if up).", "[*]".dimmed());
|
|
|
|
wg::down(interface).ok();
|
|
|
|
let config = conf.config_path(interface);
|
|
|
|
let data = conf.database_path(interface);
|
|
|
|
std::fs::remove_file(&config)
|
|
|
|
.with_path(&config)
|
|
|
|
.map_err(|e| println!("[!] {}", e.to_string().yellow()))
|
|
|
|
.ok();
|
|
|
|
std::fs::remove_file(&data)
|
|
|
|
.with_path(&data)
|
|
|
|
.map_err(|e| println!("[!] {}", e.to_string().yellow()))
|
|
|
|
.ok();
|
|
|
|
println!(
|
|
|
|
"{} network {} is uninstalled.",
|
|
|
|
"[*]".dimmed(),
|
|
|
|
interface.as_str_lossy().yellow()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-08 15:32:51 +00:00
|
|
|
fn spawn_endpoint_refresher(interface: InterfaceName) -> Endpoints {
|
|
|
|
let endpoints = Arc::new(RwLock::new(HashMap::new()));
|
|
|
|
tokio::task::spawn({
|
|
|
|
let endpoints = endpoints.clone();
|
|
|
|
async move {
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(10));
|
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
|
|
|
if let Ok(info) = DeviceInfo::get_by_name(&interface) {
|
|
|
|
for peer in info.peers {
|
|
|
|
if let Some(endpoint) = peer.config.endpoint {
|
|
|
|
endpoints
|
|
|
|
.write()
|
|
|
|
.insert(peer.config.public_key.to_base64(), endpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_expired_invite_sweeper(db: Db) {
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
let mut interval = tokio::time::interval(Duration::from_secs(10));
|
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
|
|
|
match DatabasePeer::delete_expired_invites(&db.lock()) {
|
|
|
|
Ok(deleted) => log::info!("Deleted {} expired peer invitations.", deleted),
|
|
|
|
Err(e) => log::error!("Failed to delete expired peer invitations: {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn serve(interface: InterfaceName, conf: &ServerConfig) -> Result<(), Error> {
|
|
|
|
let config = ConfigFile::from_file(conf.config_path(&interface))?;
|
|
|
|
let conn = open_database_connection(&interface, conf)?;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
let peers = DatabasePeer::list(&conn)?;
|
|
|
|
let peer_configs = peers
|
|
|
|
.iter()
|
|
|
|
.map(|peer| peer.deref().into())
|
|
|
|
.collect::<Vec<PeerConfigBuilder>>();
|
|
|
|
|
|
|
|
log::info!("bringing up interface.");
|
|
|
|
wg::up(
|
2021-05-08 15:32:51 +00:00
|
|
|
&interface,
|
2021-03-29 17:22:14 +00:00
|
|
|
&config.private_key,
|
|
|
|
IpNetwork::new(config.address, config.network_cidr_prefix)?,
|
|
|
|
Some(config.listen_port),
|
|
|
|
None,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
DeviceConfigBuilder::new()
|
|
|
|
.add_peers(&peer_configs)
|
|
|
|
.apply(&interface)?;
|
|
|
|
|
|
|
|
log::info!("{} peers added to wireguard interface.", peers.len());
|
|
|
|
|
2021-04-09 04:48:00 +00:00
|
|
|
let public_key = wgctrl::Key::from_base64(&config.private_key)?.generate_public();
|
2021-03-29 17:22:14 +00:00
|
|
|
let db = Arc::new(Mutex::new(conn));
|
2021-05-08 15:32:51 +00:00
|
|
|
let endpoints = spawn_endpoint_refresher(interface);
|
|
|
|
spawn_expired_invite_sweeper(db.clone());
|
|
|
|
|
2021-03-29 17:22:14 +00:00
|
|
|
let context = Context {
|
|
|
|
db,
|
|
|
|
endpoints,
|
2021-05-09 15:09:50 +00:00
|
|
|
interface,
|
2021-04-09 04:48:00 +00:00
|
|
|
public_key,
|
2021-03-29 17:22:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
log::info!("innernet-server {} starting.", VERSION);
|
2021-04-05 07:34:21 +00:00
|
|
|
|
2021-05-08 15:32:51 +00:00
|
|
|
let listener = get_listener((config.address, config.listen_port).into(), &interface)?;
|
2021-04-05 07:34:21 +00:00
|
|
|
|
|
|
|
let make_svc = hyper::service::make_service_fn(move |socket: &AddrStream| {
|
|
|
|
let remote_addr = socket.remote_addr();
|
2021-05-06 03:32:54 +00:00
|
|
|
let context = context.clone();
|
2021-04-05 07:34:21 +00:00
|
|
|
async move {
|
2021-05-06 03:32:54 +00:00
|
|
|
Ok::<_, http::Error>(hyper::service::service_fn(move |req: Request<Body>| {
|
|
|
|
hyper_service(req, context.clone(), remote_addr)
|
|
|
|
}))
|
2021-04-05 07:34:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
let server = hyper::Server::from_tcp(listener)?.serve(make_svc);
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
server.await?;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-04-09 13:42:29 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:28:11 +00:00
|
|
|
/// This function differs per OS, because different operating systems have
|
|
|
|
/// opposing characteristics when binding to a specific IP address.
|
|
|
|
/// On Linux, binding to a specific local IP address does *not* bind it to
|
|
|
|
/// that IP's interface, allowing for spoofing attacks.
|
|
|
|
///
|
|
|
|
/// See https://github.com/tonarino/innernet/issues/26 for more details.
|
2021-04-05 07:34:21 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
2021-04-09 01:28:37 +00:00
|
|
|
fn get_listener(addr: SocketAddr, interface: &InterfaceName) -> Result<TcpListener, Error> {
|
2021-04-05 07:34:21 +00:00
|
|
|
let listener = TcpListener::bind(&addr)?;
|
|
|
|
listener.set_nonblocking(true)?;
|
|
|
|
let sock = socket2::Socket::from(listener);
|
2021-04-09 01:28:37 +00:00
|
|
|
sock.bind_device(Some(interface.as_str_lossy().as_bytes()))?;
|
2021-04-05 07:34:21 +00:00
|
|
|
Ok(sock.into())
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:28:11 +00:00
|
|
|
/// BSD-likes do seem to bind to an interface when binding to an IP,
|
|
|
|
/// according to the internet, but we may want to explicitly use
|
|
|
|
/// IP_BOUND_IF in the future regardless. This isn't currently in
|
|
|
|
/// the socket2 crate however, so we aren't currently using it.
|
|
|
|
///
|
|
|
|
/// See https://github.com/tonarino/innernet/issues/26 for more details.
|
2021-04-05 07:34:21 +00:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
2021-04-09 01:28:37 +00:00
|
|
|
fn get_listener(addr: SocketAddr, _interface: &InterfaceName) -> Result<TcpListener, Error> {
|
2021-04-05 07:34:21 +00:00
|
|
|
let listener = TcpListener::bind(&addr)?;
|
|
|
|
listener.set_nonblocking(true)?;
|
|
|
|
Ok(listener)
|
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
pub(crate) async fn hyper_service(
|
|
|
|
req: Request<Body>,
|
2021-03-29 17:22:14 +00:00
|
|
|
context: Context,
|
2021-05-06 03:32:54 +00:00
|
|
|
remote_addr: SocketAddr,
|
|
|
|
) -> Result<Response<Body>, http::Error> {
|
|
|
|
// Break the path into components.
|
|
|
|
let components: VecDeque<_> = req
|
|
|
|
.uri()
|
|
|
|
.path()
|
|
|
|
.trim_start_matches('/')
|
|
|
|
.split('/')
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
routes(req, context, remote_addr, components)
|
|
|
|
.await
|
|
|
|
.or_else(TryInto::try_into)
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
async fn routes(
|
|
|
|
req: Request<Body>,
|
2021-03-29 17:22:14 +00:00
|
|
|
context: Context,
|
2021-05-06 03:32:54 +00:00
|
|
|
remote_addr: SocketAddr,
|
|
|
|
mut components: VecDeque<String>,
|
|
|
|
) -> Result<Response<Body>, ServerError> {
|
|
|
|
// Must be "/v1/[something]"
|
|
|
|
if components.pop_front().as_deref() != Some("v1") {
|
|
|
|
Err(ServerError::NotFound)
|
|
|
|
} else {
|
|
|
|
let session = get_session(&req, context, remote_addr.ip())?;
|
|
|
|
let component = components.pop_front();
|
|
|
|
match component.as_deref() {
|
|
|
|
Some("user") => api::user::routes(req, components, session).await,
|
|
|
|
Some("admin") => api::admin::routes(req, components, session).await,
|
|
|
|
_ => Err(ServerError::NotFound),
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 04:48:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
fn get_session(
|
|
|
|
req: &Request<Body>,
|
2021-04-09 04:48:00 +00:00
|
|
|
context: Context,
|
2021-05-06 03:32:54 +00:00
|
|
|
addr: IpAddr,
|
|
|
|
) -> Result<Session, ServerError> {
|
|
|
|
let pubkey = req
|
|
|
|
.headers()
|
|
|
|
.get(INNERNET_PUBKEY_HEADER)
|
|
|
|
.ok_or(ServerError::Unauthorized)?;
|
|
|
|
let pubkey = pubkey.to_str().map_err(|_| ServerError::Unauthorized)?;
|
|
|
|
let pubkey = Key::from_base64(&pubkey).map_err(|_| ServerError::Unauthorized)?;
|
2021-04-09 04:48:00 +00:00
|
|
|
if pubkey.0.ct_eq(&context.public_key.0).into() {
|
2021-05-06 03:32:54 +00:00
|
|
|
let peer = DatabasePeer::get_from_ip(&context.db.lock(), addr).map_err(|e| match e {
|
|
|
|
rusqlite::Error::QueryReturnedNoRows => ServerError::Unauthorized,
|
|
|
|
e => ServerError::Database(e),
|
|
|
|
})?;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
if !peer.is_disabled {
|
2021-04-09 04:48:00 +00:00
|
|
|
return Ok(Session { context, peer });
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
2021-04-09 04:48:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
Err(ServerError::Unauthorized)
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::test;
|
|
|
|
use anyhow::Result;
|
2021-05-06 03:32:54 +00:00
|
|
|
use hyper::StatusCode;
|
2021-03-29 17:22:14 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
#[test]
|
2021-05-08 15:32:51 +00:00
|
|
|
fn test_init_wizard() -> Result<(), Error> {
|
2021-03-29 17:22:14 +00:00
|
|
|
// This runs init_wizard().
|
|
|
|
let server = test::Server::new()?;
|
|
|
|
|
|
|
|
assert!(Path::new(&server.wg_conf_path()).exists());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2021-05-08 15:32:51 +00:00
|
|
|
async fn test_with_session_disguised_with_headers() -> Result<(), Error> {
|
2021-03-29 17:22:14 +00:00
|
|
|
let server = test::Server::new()?;
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
let req = Request::builder()
|
|
|
|
.uri(format!("http://{}/v1/admin/peers", test::WG_MANAGE_PEER_IP))
|
2021-03-29 17:22:14 +00:00
|
|
|
.header("Forwarded", format!("for={}", test::ADMIN_PEER_IP))
|
|
|
|
.header("X-Forwarded-For", test::ADMIN_PEER_IP)
|
|
|
|
.header("X-Real-IP", test::ADMIN_PEER_IP)
|
2021-05-06 03:32:54 +00:00
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Request from an unknown IP, trying to disguise as an admin using HTTP headers.
|
|
|
|
let res = server.raw_request("10.80.80.80", req).await;
|
2021-03-29 17:22:14 +00:00
|
|
|
|
|
|
|
// addr::remote() filter only look at remote_addr from TCP socket.
|
|
|
|
// HTTP headers are not considered. This also means that innernet
|
|
|
|
// server would not function behind an HTTP proxy.
|
|
|
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-09 04:48:00 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
2021-05-08 15:32:51 +00:00
|
|
|
async fn test_incorrect_public_key() -> Result<(), Error> {
|
2021-04-09 04:48:00 +00:00
|
|
|
let server = test::Server::new()?;
|
|
|
|
|
|
|
|
let key = Key::generate_private().generate_public();
|
|
|
|
|
|
|
|
// Request from an unknown IP, trying to disguise as an admin using HTTP headers.
|
2021-05-06 03:32:54 +00:00
|
|
|
let req = Request::builder()
|
|
|
|
.uri(format!("http://{}/v1/admin/peers", test::WG_MANAGE_PEER_IP))
|
2021-04-09 04:48:00 +00:00
|
|
|
.header(shared::INNERNET_PUBKEY_HEADER, key.to_base64())
|
2021-05-06 03:32:54 +00:00
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
|
|
|
let res = server.raw_request("10.80.80.80", req).await;
|
2021-04-09 04:48:00 +00:00
|
|
|
|
|
|
|
// addr::remote() filter only look at remote_addr from TCP socket.
|
|
|
|
// HTTP headers are not considered. This also means that innernet
|
|
|
|
// server would not function behind an HTTP proxy.
|
|
|
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2021-05-08 15:32:51 +00:00
|
|
|
async fn test_unparseable_public_key() -> Result<(), Error> {
|
2021-04-09 04:48:00 +00:00
|
|
|
let server = test::Server::new()?;
|
|
|
|
|
2021-05-06 03:32:54 +00:00
|
|
|
let req = Request::builder()
|
|
|
|
.uri(format!("http://{}/v1/admin/peers", test::WG_MANAGE_PEER_IP))
|
|
|
|
.header(shared::INNERNET_PUBKEY_HEADER, "!!!")
|
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
|
|
|
let res = server.raw_request("10.80.80.80", req).await;
|
2021-04-09 04:48:00 +00:00
|
|
|
|
|
|
|
// addr::remote() filter only look at remote_addr from TCP socket.
|
|
|
|
// HTTP headers are not considered. This also means that innernet
|
|
|
|
// server would not function behind an HTTP proxy.
|
|
|
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-29 17:22:14 +00:00
|
|
|
}
|