use std::collections::BTreeMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread;
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use thiserror::Error;
pub const EXTENSION_PROTOCOL_VERSION: u64 = 1;
pub const DEFAULT_EXTENSION_HOST_NAME: &str = "com.douglance.apoc";
pub const DEFAULT_EXTENSION_ID: &str = "apoc-agent-probe";
pub const MAX_NATIVE_MESSAGE_BYTES: usize = 768 * 1024;
pub const MAX_CHUNK_COUNT: usize = 4096;
pub const MAX_PENDING_ASSEMBLIES: usize = 64;
#[derive(Debug, Error)]
pub enum HostError {
#[error("failed to encode JSON: {0}")]
JsonEncode(serde_json::Error),
#[error("failed to decode JSON: {0}")]
JsonDecode(serde_json::Error),
#[error("invalid native message size: {0}")]
InvalidNativeMessageSize(usize),
#[error("maxBytes must be a positive integer")]
InvalidMaxBytes,
#[error("maxBytes is too small for an extension chunk envelope")]
MaxBytesTooSmall,
#[error("extension message requires too many chunks: {0}")]
TooManyChunks(usize),
#[error("invalid extension chunk")]
InvalidChunk,
#[error("invalid extension chunk encoding")]
InvalidChunkEncoding,
#[error("invalid extension chunk utf-8")]
InvalidChunkUtf8,
#[error("inconsistent extension chunk total")]
InconsistentChunkTotal,
#[error("too many pending extension chunk assemblies")]
TooManyPendingAssemblies,
#[error("conflicting duplicate extension chunk")]
ConflictingDuplicateChunk,
#[error("daemon connection error: {0}")]
DaemonConnect(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostOptions {
pub socket_path: PathBuf,
pub extension_origin: Option<String>,
pub host_name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ChunkEnvelope {
pub v: u64,
#[serde(rename = "type")]
pub kind: String,
pub id: String,
pub index: usize,
pub total: usize,
pub data: String,
}
#[derive(Debug, Default)]
pub struct ChunkAssembler {
assemblies: BTreeMap<String, Assembly>,
}
#[derive(Debug)]
struct Assembly {
total: usize,
parts: Vec<Option<String>>,
}
impl ChunkAssembler {
pub fn new() -> Self {
Self::default()
}
pub fn accept(&mut self, value: Value) -> Result<Option<Value>, HostError> {
if !is_chunk_candidate(&value) {
return Ok(Some(value));
}
let chunk = parse_chunk(value)?;
match self.assemblies.get(&chunk.id) {
Some(existing) if existing.total != chunk.total => {
return Err(HostError::InconsistentChunkTotal);
}
None if self.assemblies.len() >= MAX_PENDING_ASSEMBLIES => {
return Err(HostError::TooManyPendingAssemblies);
}
_ => {}
}
let assembly = self
.assemblies
.entry(chunk.id.clone())
.or_insert_with(|| Assembly {
total: chunk.total,
parts: vec![None; chunk.total],
});
if let Some(existing) = &assembly.parts[chunk.index]
&& existing != &chunk.data
{
return Err(HostError::ConflictingDuplicateChunk);
}
assembly.parts[chunk.index] = Some(chunk.data);
if assembly.parts.iter().filter(|part| part.is_some()).count() != assembly.total {
return Ok(None);
}
let assembly = self.assemblies.remove(&chunk.id).expect("assembly exists");
let mut bytes = Vec::new();
for part in assembly.parts {
let part = part.ok_or(HostError::InvalidChunk)?;
let decoded = BASE64
.decode(part.as_bytes())
.map_err(|_| HostError::InvalidChunkEncoding)?;
bytes.extend_from_slice(&decoded);
}
let json = String::from_utf8(bytes).map_err(|_| HostError::InvalidChunkUtf8)?;
serde_json::from_str(&json)
.map(Some)
.map_err(HostError::JsonDecode)
}
}
pub fn split_native_message(
id: &str,
value: &Value,
max_bytes: usize,
) -> Result<Vec<Value>, HostError> {
if max_bytes == 0 {
return Err(HostError::InvalidMaxBytes);
}
let bytes = serde_json::to_vec(value).map_err(HostError::JsonEncode)?;
if bytes.len() <= max_bytes {
return Ok(vec![value.clone()]);
}
let mut low = 1;
let mut high = bytes.len().saturating_sub(1).min(max_bytes);
let mut result = None;
while low <= high {
let chunk_bytes = (low + high) / 2;
let candidate = make_chunks(id, &bytes, chunk_bytes)?;
let fits = candidate
.iter()
.all(|chunk| serde_json::to_vec(chunk).is_ok_and(|encoded| encoded.len() <= max_bytes));
if fits {
result = Some(candidate);
low = chunk_bytes + 1;
} else {
high = chunk_bytes.saturating_sub(1);
}
}
result.ok_or(HostError::MaxBytesTooSmall)
}
pub fn encode_native_frame(value: &Value) -> Result<Vec<u8>, HostError> {
let body = serde_json::to_vec(value).map_err(HostError::JsonEncode)?;
if body.len() > MAX_NATIVE_MESSAGE_BYTES {
return Err(HostError::InvalidNativeMessageSize(body.len()));
}
let mut frame = Vec::with_capacity(4 + body.len());
frame.extend_from_slice(&(body.len() as u32).to_le_bytes());
frame.extend_from_slice(&body);
Ok(frame)
}
pub fn decode_native_frames(buffer: &mut Vec<u8>) -> Result<Vec<Value>, HostError> {
let mut values = Vec::new();
loop {
if buffer.len() < 4 {
return Ok(values);
}
let size = u32::from_le_bytes([buffer[0], buffer[1], buffer[2], buffer[3]]) as usize;
if size > MAX_NATIVE_MESSAGE_BYTES {
return Err(HostError::InvalidNativeMessageSize(size));
}
if buffer.len() < 4 + size {
return Ok(values);
}
let body = buffer[4..4 + size].to_vec();
buffer.drain(..4 + size);
values.push(serde_json::from_slice(&body).map_err(HostError::JsonDecode)?);
}
}
pub fn default_socket_path() -> PathBuf {
if let Some(path) = std::env::var_os("APOC_EXTENSION_SOCKET") {
return PathBuf::from(path);
}
runtime_dir().join("apoc-extension.sock")
}
pub fn parse_options(args: &[String]) -> HostOptions {
let mut socket_path = default_socket_path();
let mut host_name = std::env::var("APOC_EXTENSION_HOST_NAME")
.unwrap_or_else(|_| DEFAULT_EXTENSION_HOST_NAME.to_string());
let mut index = 0;
while index < args.len() {
match args[index].as_str() {
"--socket" if index + 1 < args.len() => {
socket_path = PathBuf::from(&args[index + 1]);
index += 2;
}
"--host-name" if index + 1 < args.len() => {
host_name = args[index + 1].clone();
index += 2;
}
_ => index += 1,
}
}
let extension_origin = args
.iter()
.find(|arg| arg.starts_with("chrome-extension://"))
.cloned();
HostOptions {
socket_path,
extension_origin,
host_name,
}
}
pub fn bridge_connect_message(options: &HostOptions) -> Value {
let mut object = Map::new();
object.insert(
"cmd".to_string(),
Value::String("ext.bridge.connect".to_string()),
);
object.insert(
"hostName".to_string(),
Value::String(options.host_name.clone()),
);
if let Some(origin) = &options.extension_origin {
object.insert("extensionOrigin".to_string(), Value::String(origin.clone()));
}
Value::Object(object)
}
pub fn is_extension_host_envelope(value: &Value) -> bool {
let Some(object) = value.as_object() else {
return false;
};
if object.get("v").and_then(Value::as_u64) != Some(EXTENSION_PROTOCOL_VERSION) {
return false;
}
match object.get("type").and_then(Value::as_str) {
Some("hello") => {
object.get("extensionId").and_then(Value::as_str).is_some()
&& object.get("version").and_then(Value::as_str).is_some()
}
Some("response") => {
object.get("id").and_then(Value::as_str).is_some()
&& object.get("ok").and_then(Value::as_bool).is_some()
}
Some("event") => object.get("event").and_then(Value::as_object).is_some(),
_ => false,
}
}
pub fn is_extension_daemon_envelope(value: &Value) -> bool {
let Some(object) = value.as_object() else {
return false;
};
if object.get("v").and_then(Value::as_u64) != Some(EXTENSION_PROTOCOL_VERSION) {
return false;
}
match object.get("type").and_then(Value::as_str) {
Some("request") => {
object.get("id").and_then(Value::as_str).is_some()
&& object.get("request").and_then(Value::as_object).is_some()
}
Some("ack") => object.get("id").is_none_or(Value::is_string),
_ => false,
}
}
pub fn run_stdio_host(args: &[String]) -> Result<(), HostError> {
let options = parse_options(args);
let daemon = match UnixStream::connect(&options.socket_path) {
Ok(stream) => stream,
Err(error) => {
let event = lifecycle_event(format!("daemon connection error: {error}"));
write_native_value(&mut std::io::stdout(), &event)?;
return Err(HostError::DaemonConnect(error.to_string()));
}
};
let daemon_writer = Arc::new(Mutex::new(daemon.try_clone()?));
send_daemon_value(&daemon_writer, &bridge_connect_message(&options))?;
let stdout = Arc::new(Mutex::new(std::io::stdout()));
let daemon_stdout = Arc::clone(&stdout);
thread::spawn(move || {
let mut assembler = ChunkAssembler::new();
let mut reader = BufReader::new(daemon);
let mut line = String::new();
loop {
line.clear();
match reader.read_line(&mut line) {
Ok(0) | Err(_) => break,
Ok(_) => {
if line.trim().is_empty() {
continue;
}
let Ok(parsed) = serde_json::from_str::<Value>(&line) else {
continue;
};
let Ok(Some(assembled)) = assembler.accept(parsed) else {
continue;
};
if is_extension_daemon_envelope(&assembled) {
let mut output = daemon_stdout.lock().expect("stdout lock poisoned");
let _ = write_native_value(&mut *output, &assembled);
}
}
}
}
});
let mut assembler = ChunkAssembler::new();
let mut buffer = Vec::new();
let mut chunk = [0_u8; 8192];
let mut input = std::io::stdin();
loop {
let bytes_read = input.read(&mut chunk)?;
if bytes_read == 0 {
break;
}
buffer.extend_from_slice(&chunk[..bytes_read]);
for value in decode_native_frames(&mut buffer)? {
let Some(assembled) = assembler.accept(value)? else {
continue;
};
if is_extension_host_envelope(&assembled) {
send_daemon_value(&daemon_writer, &assembled)?;
}
}
}
Ok(())
}
fn runtime_dir() -> PathBuf {
if let Some(path) = std::env::var_os("APOC_RUNTIME_DIR") {
return PathBuf::from(path);
}
std::env::temp_dir().join(format!("apoc-{}", unsafe { libc::geteuid() }))
}
fn is_chunk_candidate(value: &Value) -> bool {
value
.as_object()
.and_then(|object| object.get("type"))
.and_then(Value::as_str)
== Some("chunk")
}
fn parse_chunk(value: Value) -> Result<ChunkEnvelope, HostError> {
let chunk: ChunkEnvelope =
serde_json::from_value(value).map_err(|_| HostError::InvalidChunk)?;
if chunk.v != EXTENSION_PROTOCOL_VERSION
|| chunk.kind != "chunk"
|| chunk.id.is_empty()
|| chunk.total == 0
|| chunk.total > MAX_CHUNK_COUNT
|| chunk.index >= chunk.total
{
return Err(HostError::InvalidChunk);
}
Ok(chunk)
}
fn make_chunks(id: &str, bytes: &[u8], chunk_bytes: usize) -> Result<Vec<Value>, HostError> {
if chunk_bytes == 0 {
return Err(HostError::InvalidMaxBytes);
}
let total = bytes.len().div_ceil(chunk_bytes);
if total > MAX_CHUNK_COUNT {
return Err(HostError::TooManyChunks(total));
}
let mut chunks = Vec::with_capacity(total);
for index in 0..total {
let start = index * chunk_bytes;
let end = (start + chunk_bytes).min(bytes.len());
chunks.push(
serde_json::to_value(ChunkEnvelope {
v: EXTENSION_PROTOCOL_VERSION,
kind: "chunk".to_string(),
id: id.to_string(),
index,
total,
data: BASE64.encode(&bytes[start..end]),
})
.map_err(HostError::JsonEncode)?,
);
}
Ok(chunks)
}
fn send_daemon_value(writer: &Arc<Mutex<UnixStream>>, value: &Value) -> Result<(), HostError> {
let messages = split_native_message("daemon", value, MAX_NATIVE_MESSAGE_BYTES)?;
let mut stream = writer.lock().expect("daemon writer lock poisoned");
for message in messages {
serde_json::to_writer(&mut *stream, &message).map_err(HostError::JsonEncode)?;
stream.write_all(b"\n")?;
}
stream.flush()?;
Ok(())
}
fn write_native_value(writer: &mut dyn Write, value: &Value) -> Result<(), HostError> {
for message in split_native_message("native", value, MAX_NATIVE_MESSAGE_BYTES)? {
let frame = encode_native_frame(&message)?;
writer.write_all(&frame)?;
}
writer.flush()?;
Ok(())
}
fn lifecycle_event(message: String) -> Value {
serde_json::json!({
"v": EXTENSION_PROTOCOL_VERSION,
"type": "event",
"event": {
"ts": now_ms(),
"kind": "lifecycle",
"message": message,
}
})
}
fn now_ms() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0)
}