#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![cfg_attr(not(unix), allow(unused))]
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tonic::metadata::{Ascii, MetadataValue};
use tonic::service::Interceptor;
use tonic::service::interceptor::InterceptedService;
use tonic::transport::Channel;
#[cfg(unix)]
use tonic::transport::Endpoint;
use weirdnet_ext_proto::v1::{self, host_client::HostClient};
#[cfg(unix)]
use weirdnet_ext_proto::{API_VERSION, ENV_FD};
use weirdnet_ext_proto::{ENV_ID, ENV_TOKEN, SESSION_HEADER};
pub use tonic::Status;
pub use weirdnet_ext_proto as proto;
pub use weirdnet_ext_proto::v1::health_report::State;
pub use weirdnet_ext_proto::v1::log_record::Level;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("not started by a Weirdnet extension host: {0}")]
NotHosted(String),
#[error("connecting to the host: {0}")]
Transport(#[from] tonic::transport::Error),
#[error("handshake: {0}")]
Handshake(#[from] Status),
#[error(transparent)]
Io(#[from] std::io::Error),
}
#[derive(Clone)]
pub struct Session(MetadataValue<Ascii>);
impl Interceptor for Session {
fn call(&mut self, mut r: tonic::Request<()>) -> Result<tonic::Request<()>, Status> {
r.metadata_mut().insert(SESSION_HEADER, self.0.clone());
Ok(r)
}
}
pub type Client = HostClient<InterceptedService<Channel, Session>>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Capability {
pub name: String,
pub patterns: Vec<String>,
}
fn caps(v: Vec<v1::Capability>) -> Vec<Capability> {
v.into_iter().map(|c| Capability { name: c.name, patterns: c.patterns }).collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
App {
kind: String,
payload: Vec<u8>,
},
Grants(Vec<Capability>),
Stop {
reason: String,
grace_ms: u32,
},
}
#[derive(Clone)]
pub struct Extension {
client: Client,
id: String,
host: String,
api_version: u32,
granted: Arc<Mutex<Vec<Capability>>>,
}
#[cfg(unix)]
fn take_stream() -> Result<tokio::net::UnixStream, Error> {
use std::os::fd::FromRawFd;
let fd: i32 = std::env::var(ENV_FD)
.map_err(|_| Error::NotHosted(format!("{ENV_FD} is not set")))?
.parse()
.map_err(|_| Error::NotHosted(format!("{ENV_FD} is not a number")))?;
if fd < 3 {
return Err(Error::NotHosted(format!("{ENV_FD} is not a socket")));
}
let s = unsafe { std::os::unix::net::UnixStream::from_raw_fd(fd) };
unsafe {
let flags = libc::fcntl(fd, libc::F_GETFD);
libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC);
}
s.set_nonblocking(true)?;
Ok(tokio::net::UnixStream::from_std(s)?)
}
static TAKEN: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
impl Extension {
pub async fn connect(version: &str) -> Result<Self, Error> {
let id = std::env::var(ENV_ID).map_err(|_| Error::NotHosted(format!("{ENV_ID} is not set")))?;
let token = std::env::var(ENV_TOKEN).map_err(|_| Error::NotHosted(format!("{ENV_TOKEN} is not set")))?;
if TAKEN.swap(true, std::sync::atomic::Ordering::SeqCst) {
return Err(Error::NotHosted("already connected".into()));
}
#[cfg(unix)]
let stream = take_stream()?;
#[cfg(not(unix))]
{
let _ = (version, token);
return Err(Error::NotHosted("native extensions don't run on this OS yet".into()));
}
#[cfg(unix)]
{
let slot = Arc::new(Mutex::new(Some(stream)));
let channel = Endpoint::from_static("http://weirdnet-ext.host")
.connect_with_connector(tower::service_fn(move |_| {
let s = slot.lock().unwrap().take();
async move {
s.map(hyper_util::rt::TokioIo::new)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotConnected, "the host connection is closed"))
}
}))
.await?;
let mut raw = HostClient::new(channel.clone());
let r = raw
.handshake(v1::HandshakeRequest {
token,
extension_id: id.clone(),
version: version.into(),
api_version: API_VERSION,
sdk: concat!("weirdnet-ext-sdk ", env!("CARGO_PKG_VERSION")).into(),
})
.await?
.into_inner();
let session = MetadataValue::try_from(r.session.as_str()).map_err(|_| Error::NotHosted("bad session".into()))?;
let client = HostClient::with_interceptor(channel, Session(session));
Ok(Self { client, id, host: r.host, api_version: r.api_version, granted: Arc::new(Mutex::new(caps(r.granted))) })
}
}
pub fn id(&self) -> &str {
&self.id
}
pub fn host(&self) -> &str {
&self.host
}
pub fn api_version(&self) -> u32 {
self.api_version
}
pub fn granted(&self) -> Vec<Capability> {
self.granted.lock().unwrap().clone()
}
pub fn has(&self, name: &str) -> bool {
self.granted.lock().unwrap().iter().any(|c| c.name == name)
}
pub fn client(&self) -> Client {
self.client.clone()
}
pub async fn call(&self, service: &str, method: &str, payload: Vec<u8>) -> Result<Vec<u8>, Status> {
let r = self.client.clone().call(v1::CallRequest { service: service.into(), method: method.into(), payload }).await?;
Ok(r.into_inner().payload)
}
pub async fn log(&self, level: Level, message: impl Into<String>, fields: &[(&str, &str)]) -> Result<(), Status> {
let fields: HashMap<String, String> = fields.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
self.client.clone().log(v1::LogRecord { level: level as i32, message: message.into(), fields }).await?;
Ok(())
}
pub async fn health(&self, state: State, detail: impl Into<String>) -> Result<(), Status> {
self.client.clone().health(v1::HealthReport { state: state as i32, detail: detail.into(), ping: 0 }).await?;
Ok(())
}
pub async fn run<F, Fut>(&self, mut on_event: F) -> Result<Option<String>, Status>
where
F: FnMut(Event) -> Fut,
Fut: std::future::Future<Output = ()>,
{
let mut s = self.client.clone().events(v1::EventsRequest {}).await?.into_inner();
self.health(State::Ready, "").await?;
use v1::event::Kind;
while let Some(e) = s.message().await? {
match e.kind {
Some(Kind::Ping(p)) => {
self.client.clone().health(v1::HealthReport { state: State::Ready as i32, detail: String::new(), ping: p.seq }).await?;
}
Some(Kind::Grants(g)) => {
let c = caps(g.granted);
*self.granted.lock().unwrap() = c.clone();
on_event(Event::Grants(c)).await;
}
Some(Kind::App(a)) => on_event(Event::App { kind: a.kind, payload: a.payload }).await,
Some(Kind::Stop(st)) => {
on_event(Event::Stop { reason: st.reason.clone(), grace_ms: st.grace_ms }).await;
return Ok(Some(st.reason));
}
None => {}
}
}
Ok(None)
}
}