2020-11-17 14:28:04 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-05-11 18:21:45 +02:00
|
|
|
use slog::Logger;
|
|
|
|
|
2020-09-07 17:23:42 +02:00
|
|
|
use smol::net::TcpStream;
|
2020-05-11 18:21:45 +02:00
|
|
|
|
2020-10-29 13:04:20 +01:00
|
|
|
use crate::error::{Error, Result};
|
2020-09-07 09:45:55 +02:00
|
|
|
use crate::auth;
|
|
|
|
use crate::api;
|
2020-05-11 18:21:45 +02:00
|
|
|
|
2020-10-22 13:00:58 +02:00
|
|
|
pub use crate::schema::connection_capnp;
|
2020-11-17 12:09:45 +01:00
|
|
|
use crate::db::Databases;
|
2020-05-10 17:23:43 +02:00
|
|
|
|
2020-10-29 13:04:20 +01:00
|
|
|
use capnp_rpc::{twoparty, rpc_twoparty_capnp};
|
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
use capnp::capability::{Params, Results, Promise, FromServer};
|
2020-05-11 18:21:45 +02:00
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
/// Connection context
|
2020-11-17 14:28:04 +01:00
|
|
|
// TODO this should track over several connections
|
|
|
|
pub struct Session {
|
2020-10-29 13:04:20 +01:00
|
|
|
log: Logger,
|
2020-10-23 15:29:32 +02:00
|
|
|
user: Option<auth::User>,
|
|
|
|
}
|
2020-05-11 18:21:45 +02:00
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
impl Session {
|
2020-10-29 13:04:20 +01:00
|
|
|
pub fn new(log: Logger) -> Self {
|
|
|
|
let user = None;
|
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
Session { log, user }
|
2020-10-29 13:04:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
struct Bootstrap {
|
|
|
|
session: Arc<Session>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bootstrap {
|
|
|
|
pub fn new(session: Arc<Session>) -> Self {
|
|
|
|
Self { session }
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 23:19:26 +02:00
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
use connection_capnp::bootstrap::*;
|
2020-11-17 14:28:04 +01:00
|
|
|
impl connection_capnp::bootstrap::Server for Bootstrap {
|
2020-10-23 15:29:32 +02:00
|
|
|
fn auth(&mut self,
|
|
|
|
_: Params<auth_params::Owned>,
|
|
|
|
mut res: Results<auth_results::Owned>
|
|
|
|
) -> Promise<(), capnp::Error> {
|
|
|
|
// Forbid mutltiple authentication for now
|
|
|
|
// TODO: When should we allow multiple auth and how do me make sure that does not leak
|
|
|
|
// priviledges (e.g. due to previously issues caps)?
|
2020-11-17 14:28:04 +01:00
|
|
|
if self.session.user.is_none() {
|
2020-10-23 15:29:32 +02:00
|
|
|
res.get().set_auth(capnp_rpc::new_client(auth::Auth::new()))
|
2020-05-11 18:21:45 +02:00
|
|
|
}
|
2020-05-14 23:19:26 +02:00
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
Promise::ok(())
|
2020-05-14 23:19:26 +02:00
|
|
|
}
|
2020-09-07 09:45:55 +02:00
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
fn permissions(&mut self,
|
|
|
|
_: Params<permissions_params::Owned>,
|
|
|
|
mut res: Results<permissions_results::Owned>
|
|
|
|
) -> Promise<(), capnp::Error> {
|
2020-11-17 14:28:04 +01:00
|
|
|
if self.session.user.is_some() {
|
2020-09-07 09:45:55 +02:00
|
|
|
}
|
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
Promise::ok(())
|
2020-09-07 09:45:55 +02:00
|
|
|
}
|
2020-11-17 12:09:45 +01:00
|
|
|
|
|
|
|
fn machines(&mut self,
|
|
|
|
_: Params<machines_params::Owned>,
|
|
|
|
mut res: Results<machines_results::Owned>
|
|
|
|
) -> Promise<(), capnp::Error> {
|
|
|
|
Promise::ok(())
|
|
|
|
}
|
2020-10-23 15:29:32 +02:00
|
|
|
}
|
2020-05-14 23:19:26 +02:00
|
|
|
|
2020-10-29 13:04:20 +01:00
|
|
|
async fn handshake(log: &Logger, stream: &mut TcpStream) -> Result<()> {
|
2020-11-10 13:34:09 +01:00
|
|
|
if let Some(m) = capnp_futures::serialize::read_message(stream.clone(), Default::default()).await? {
|
2020-10-29 13:04:20 +01:00
|
|
|
let greeting = m.get_root::<connection_capnp::greeting::Reader>()?;
|
|
|
|
let major = greeting.get_major();
|
|
|
|
let minor = greeting.get_minor();
|
|
|
|
|
2020-11-10 13:34:09 +01:00
|
|
|
if major != 0 {
|
2020-10-29 13:04:20 +01:00
|
|
|
Err(Error::BadVersion((major, minor)))
|
|
|
|
} else {
|
2020-11-10 13:34:09 +01:00
|
|
|
let program = format!("{}-{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
|
|
|
|
|
|
|
|
let mut answer = ::capnp::message::Builder::new_default();
|
|
|
|
let mut b = answer.init_root::<connection_capnp::greeting::Builder>();
|
|
|
|
b.set_program(&program);
|
|
|
|
b.set_host("localhost");
|
|
|
|
b.set_major(0);
|
|
|
|
b.set_minor(1);
|
|
|
|
capnp_futures::serialize::write_message(stream, answer).await?;
|
2020-10-29 13:04:20 +01:00
|
|
|
info!(log, "Handshake successful with peer {} running {}, API {}.{}",
|
|
|
|
greeting.get_host()?, greeting.get_program()?, major, minor);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 15:29:32 +02:00
|
|
|
pub async fn handle_connection(log: Logger, mut stream: TcpStream) -> Result<()> {
|
2020-11-10 13:34:09 +01:00
|
|
|
//handshake(&log, &mut stream).await?;
|
2020-10-29 13:04:20 +01:00
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
let session = Arc::new(Session::new(log));
|
|
|
|
let boots = Bootstrap::new(session);
|
|
|
|
let rpc: connection_capnp::bootstrap::Client = capnp_rpc::new_client(boots);
|
2020-10-29 13:04:20 +01:00
|
|
|
|
|
|
|
let network = twoparty::VatNetwork::new(stream.clone(), stream,
|
|
|
|
rpc_twoparty_capnp::Side::Server, Default::default());
|
|
|
|
let rpc_system = capnp_rpc::RpcSystem::new(Box::new(network),
|
|
|
|
Some(rpc.client));
|
|
|
|
|
2020-11-17 14:28:04 +01:00
|
|
|
rpc_system.await.unwrap();
|
2020-10-29 13:04:20 +01:00
|
|
|
Ok(())
|
2020-05-11 18:21:45 +02:00
|
|
|
}
|