Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement WASM plugin #19

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
da7f09c
run format; add plugin config
Hexilee Oct 11, 2021
b4986a1
add plugin mod in proxy handler
Hexilee Oct 12, 2021
86cecd2
apply plugins in proxy
Hexilee Oct 12, 2021
30f5628
add wasm plugin example
Hexilee Oct 12, 2021
612bc5b
add unit test for plugin handle_response
Hexilee Oct 13, 2021
6aff733
plugin pass basic test
Hexilee Oct 13, 2021
80c620a
add logger
Hexilee Oct 13, 2021
332423e
logger works
Hexilee Oct 13, 2021
034bf05
add macro register_response_handler and register_request_handler
Hexilee Oct 13, 2021
eaa9037
make macro easier to use
Hexilee Oct 13, 2021
12e2b02
update docs about plugin
Hexilee Oct 13, 2021
f6795e6
precompile plugin
Hexilee Oct 13, 2021
5796832
remove time calculate code in plugin test
Hexilee Oct 13, 2021
085fe9b
change toolchain to nightly
Hexilee Oct 13, 2021
8a9f57a
use json instead of bincode in uds service
Hexilee Oct 14, 2021
58f79e0
fix logs
Hexilee Oct 14, 2021
92520d6
fix dependencies
Hexilee Oct 14, 2021
8cedb11
plugin pass mannel test
Hexilee Oct 14, 2021
5c4d50d
update toolchain in stable test
Hexilee Oct 15, 2021
282f9ff
fix clippy
Hexilee Oct 15, 2021
6b6ddf6
add target wasm32-unknown-unknown in stable test
Hexilee Oct 15, 2021
321b700
fix wrong YAML
Hexilee Oct 15, 2021
cbd58f0
add uds_server in proxy
Hexilee Oct 15, 2021
1d4e58f
merge from master
Hexilee Oct 15, 2021
2a10471
refactor rs-tproxy-proxy
Hexilee Oct 17, 2021
3a11d76
fix lint
Hexilee Oct 17, 2021
b382858
add client for contoller
Hexilee Oct 18, 2021
8ccc511
successfully to cache plugin
Hexilee Oct 19, 2021
51da97d
fix integrations test
Hexilee Oct 19, 2021
7b4bd17
refactor cargo modules style to 2018 edition
Hexilee Oct 19, 2021
1c9458a
update config-examples
Hexilee Oct 19, 2021
560950b
update crates version
Hexilee Oct 19, 2021
347156d
update Carago.lock
Hexilee Oct 19, 2021
48670e0
fix integration test
Hexilee Oct 19, 2021
430029a
merge from master
Hexilee Oct 19, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
620 changes: 602 additions & 18 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name = "rs-tproxy"
path = "rs-tproxy-controller/src/main.rs"

[workspace]
members = ["rs-tproxy-controller", "rs-tproxy-proxy", "tests"]
members = ["rs-tproxy-controller", "rs-tproxy-proxy", "rs-tproxy-plugin", "examples/plugin-example", "tests"]

[dependencies]
anyhow = "1.0"
Expand Down Expand Up @@ -45,6 +45,7 @@ bincode = "1.3.3"
default-net = "0.2.0"
system_gateway = {git="https://github.com/aruntomar/system_gateway"}
base64 = "0.13.0"
log = "0.4"

[dev-dependencies]
test-case = "1.2"
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ WORKDIR /tproxy-build

RUN --mount=type=cache,target=/tproxy-build/target \
--mount=type=cache,target=/root/.cargo/registry \
cargo build --release --all --target $TARGET
cargo build --release --bin rs-tproxy --target $TARGET

RUN --mount=type=cache,target=/tproxy-build/target \
cp /tproxy-build/target/$TARGET/release/tproxy /tproxy
cp /tproxy-build/target/$TARGET/release/rs-tproxy /tproxy
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
build:
cargo build --all
cargo build --workspace
fmt:
cargo +nightly fmt
cargo fmt
run:
RUST_LOG=trace ./target/debug/tproxy $(config)
test:
cargo test --all
test: build
cargo test -p rs-tproxy-proxy -p rs-tproxy-plugin -p rs-tproxy-controller
lint:
cargo clippy --all-targets -- -D warnings
cargo clippy --all-targets -- -D warnings
clean:
cargo clean
image:
DOCKER_BUILDKIT=1 docker build --build-arg HTTP_PROXY=${HTTP_PROXY} --build-arg HTTPS_PROXY=${HTTPS_PROXY} . -t chaos-mesh/tproxy
2 changes: 2 additions & 0 deletions examples/plugin-example/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "wasm32-unknown-unknown"
20 changes: 20 additions & 0 deletions examples/plugin-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cargo-features = ["per-package-target"]

[package]
name = "plugin-example"
version = "0.1.0"
edition = "2018"
forced-target = "wasm32-unknown-unknown"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
rs-tproxy-plugin = { path = "../../rs-tproxy-plugin" }

serde_json = "1.0"
log = "0.4"
anyhow = "1.0"
http = "0.2"
15 changes: 15 additions & 0 deletions examples/plugin-example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use log::info;
use rs_tproxy_plugin::register_response_handler;

register_response_handler!(|resp| {
let content_type = resp
.headers()
.get("content-type")
.ok_or(anyhow::anyhow!("content-type not found"))?
.to_str()?;
info!("get content-type: {}", content_type);
Ok(serde_json::to_vec(&serde_json::json!({
"type": content_type,
"content": *resp.body(),
}))?)
});
3 changes: 2 additions & 1 deletion rs-tproxy-controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ futures-util = { version = "0.3.7", default-features = false, features = ["alloc
rs-tproxy-proxy = {path = "../rs-tproxy-proxy"}
pnet = "0.28.0"
default-net = "0.2.0"
system_gateway = {git="https://github.com/aruntomar/system_gateway"}
system_gateway = {git="https://github.com/aruntomar/system_gateway"}
log = "0.4"
12 changes: 6 additions & 6 deletions rs-tproxy-controller/src/cmd/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::convert::TryInto;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use log::Level;
use structopt::StructOpt;
use tokio::fs::read_to_string;
use tracing_subscriber::filter::LevelFilter;

use crate::proxy::config::Config;
use crate::raw_config::RawConfig;
Expand Down Expand Up @@ -44,12 +44,12 @@ pub struct Opt {
}

impl Opt {
pub fn get_level_filter(&self) -> LevelFilter {
pub fn get_level(&self) -> Level {
match self.verbose {
0 => LevelFilter::ERROR,
1 => LevelFilter::INFO,
2 => LevelFilter::DEBUG,
_ => LevelFilter::TRACE,
0 => Level::Error,
1 => Level::Info,
2 => Level::Debug,
_ => Level::Trace,
}
}

Expand Down
2 changes: 1 addition & 1 deletion rs-tproxy-controller/src/cmd/interactive/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl ConfigService {
raw_config.try_into()
}

#[instrument]
#[instrument(skip(proxy))]
async fn handle(proxy: &mut Proxy, request: Request<Body>) -> anyhow::Result<Response<Body>> {
if request.method() != Method::PUT {
return Ok(Response::builder()
Expand Down
5 changes: 4 additions & 1 deletion rs-tproxy-controller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub mod raw_config;
async fn main() -> anyhow::Result<()> {
let opt = Opt::from_args_checked()?;
tracing_subscriber::fmt()
.with_max_level(opt.get_level_filter())
.with_env_filter(tracing_subscriber::EnvFilter::new(format!(
"rs_tproxy={}",
opt.get_level()
)))
.with_writer(std::io::stderr)
.try_init()
.map_err(|err| anyhow!("{}", err))?;
Expand Down
6 changes: 4 additions & 2 deletions rs-tproxy-controller/src/proxy/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ pub(crate) fn get_free_port(ports: Option<Vec<u16>>) -> anyhow::Result<u16> {

#[cfg(test)]
mod tests {
use std::convert::TryInto;

use rs_tproxy_proxy::raw_config::RawConfig as ProxyRawConfig;

use crate::proxy::config::{get_free_port, Config};
use crate::raw_config::RawConfig;
use rs_tproxy_proxy::raw_config::RawConfig as ProxyRawConfig;
use std::convert::TryInto;

#[test]
fn test_get_free_port() {
Expand Down
12 changes: 7 additions & 5 deletions rs-tproxy-controller/src/proxy/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::process::Command;
use tokio::select;
use tokio::sync::oneshot::{channel, Receiver, Sender};
use tokio::task::JoinHandle;
use tracing::instrument;
use uuid::Uuid;

use crate::proxy::net::bridge::NetEnv;
Expand Down Expand Up @@ -52,8 +53,9 @@ impl Proxy {
}
}

#[instrument(skip(self, config))]
pub async fn exec(&mut self, config: ProxyRawConfig) -> anyhow::Result<()> {
tracing::info!(target : "transferring proxy raw config ", "{:?}" ,&config);
tracing::info!("transferring proxy raw config {:?}", &config);
let uds_server = UdsDataServer::new(config.clone(), self.opt.ipc_path.clone());
let listener = uds_server.bind()?;

Expand All @@ -76,7 +78,7 @@ impl Proxy {
Ok(path) => path,
};

tracing::info!(target: "Network device name", "{}", self.net_env.device.clone());
tracing::info!("network device name {}", self.net_env.device.clone());
match config.interface {
None => {}
Some(interface) => {
Expand Down Expand Up @@ -105,10 +107,10 @@ impl Proxy {

let rx = self.rx.take().unwrap();
self.task = Some(tokio::spawn(async move {
tracing::info!(target : "Proxy executor", "Starting proxy.");
tracing::info!("starting proxy");
let mut process = match proxy.stdin(Stdio::piped()).spawn() {
Ok(process) => {
tracing::info!(target : "Proxy executor", "Proxy is running.");
tracing::info!("proxy is running");
process
}
Err(e) => {
Expand All @@ -118,7 +120,7 @@ impl Proxy {
select! {
_ = process.wait() => {}
_ = rx => {
tracing::info!(target : "Proxy executor","killing sub process");
tracing::info!("killing sub process");
let id = process.id().unwrap() as i32;
unsafe {
libc::kill(id, libc::SIGINT);
Expand Down
20 changes: 6 additions & 14 deletions rs-tproxy-controller/src/proxy/net/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::net::Ipv4Addr;
use std::process::Command;

use crate::proxy::net::iptables::clear_ebtables;
use anyhow::{anyhow, Result};
use default_net;
use pnet::datalink::NetworkInterface;
use pnet::ipnetwork::{IpNetwork, Ipv4Network};
use std::net::Ipv4Addr;
use uuid::Uuid;

use crate::proxy::net::iptables::clear_ebtables;

#[derive(Debug, Clone)]
pub struct NetEnv {
pub netns: String,
Expand Down Expand Up @@ -161,18 +162,9 @@ impl NetEnv {
&self.netns,
vec!["sysctl", "-w", "net.ipv4.ip_nonlocal_bind=1"],
),
ip_netns(
&self.netns,
vec!["sysctl", "-w", &rp_filter_br2],
),
ip_netns(
&self.netns,
vec!["sysctl", "-w", &rp_filter_v2],
),
ip_netns(
&self.netns,
vec!["sysctl", "-w", &rp_filter_v3],
),
ip_netns(&self.netns, vec!["sysctl", "-w", &rp_filter_br2]),
ip_netns(&self.netns, vec!["sysctl", "-w", &rp_filter_v2]),
ip_netns(&self.netns, vec!["sysctl", "-w", &rp_filter_v3]),
ip_netns(
&self.netns,
vec!["sysctl", "-w", "net.ipv4.conf.lo.rp_filter=0"],
Expand Down
15 changes: 6 additions & 9 deletions rs-tproxy-controller/src/proxy/uds_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<T: serde::ser::Serialize> UdsDataServer<T> {
}

pub fn bind(&self) -> anyhow::Result<UnixListener> {
tracing::info!(target : "Uds listener try binding", "{:?}", &self.path);
tracing::info!("uds listener try binding {:?}", &self.path);
let listener = UnixListener::bind(self.path.clone())?;
Ok(listener)
}
Expand All @@ -26,29 +26,26 @@ impl<T: serde::ser::Serialize> UdsDataServer<T> {
}

pub async fn listen(&self, listener: UnixListener) -> anyhow::Result<()> {
tracing::info!(target : "Uds listener listening on", "{:?}", &self.path);
tracing::info!("uds listener listening on {:?}", &self.path);
loop {
match (&listener).accept().await {
Ok((mut stream, addr)) => {
let buf = bincode::serialize(&self.data)?;
let buf = serde_json::to_vec(&self.data)?;
tokio::spawn(async move {
return match stream.write_all(buf.as_slice()).await {
Ok(_) => {
tracing::info!(target : "Uds server" ,"Config successfully transferred.");
tracing::info!("uds server Config successfully transferred.");
Ok(())
}
Err(e) => {
tracing::error!(
"error : write_all raw config to {:?} failed",
addr
);
tracing::error!("write_all raw config to {:?} failed", addr);
Err(anyhow::anyhow!("{}", e))
}
};
});
}
Err(e) => {
tracing::error!("error : accept connection failed");
tracing::error!("accept connection failed");
return Err(anyhow::anyhow!("{}", e));
}
}
Expand Down
13 changes: 13 additions & 0 deletions rs-tproxy-plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "rs-tproxy-plugin"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
anyhow = "1.0"
http = "0.2.3"
log = "0.4"
Loading