Skip to content

Commit a7c97cd

Browse files
Add TLS support for http (#39)
* add TLS support on backends Signed-off-by: andrewmatilde <[email protected]> * support TLS Signed-off-by: andrewmatilde <[email protected]> * fix connect Signed-off-by: andrewmatilde <[email protected]> * fix tls config Signed-off-by: andrewmatilde <[email protected]> * fix client Signed-off-by: andrewmatilde <[email protected]> * fix client scheme Signed-off-by: andrewmatilde <[email protected]> * fix client authority Signed-off-by: andrewmatilde <[email protected]> * fix connector Signed-off-by: andrewmatilde <[email protected]> * delete some unnecessary code Signed-off-by: andrewmatilde <[email protected]> * fix-lock Signed-off-by: andrewmatilde <[email protected]> * try fix loop select Signed-off-by: andrewmatilde <[email protected]> * fmt Signed-off-by: andrewmatilde <[email protected]> * use span Signed-off-by: andrewmatilde <[email protected]> * fix BUG Signed-off-by: andrewmatilde <[email protected]> * fix duplicate code. Signed-off-by: andrewmatilde <[email protected]> * fix all Signed-off-by: andrewmatilde <[email protected]>
1 parent adb39a1 commit a7c97cd

File tree

15 files changed

+823
-336
lines changed

15 files changed

+823
-336
lines changed

Cargo.lock

Lines changed: 522 additions & 224 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ members = ["chaos-tproxy-controller", "chaos-tproxy-proxy", "tests"]
1717
anyhow = "1.0"
1818
clap = "2.33.3"
1919
futures = "0.3.10"
20-
http = "0.2.3"
20+
http = "0.2.7"
2121
humantime-serde = "1.0"
2222
hyper = {git = "https://github.com/Andrewmatilde/hyper.git", features = ["runtime", "client", "server", "http1", "http2", "stream", "error_return"]}
2323
iptables = "0.4"
@@ -44,6 +44,12 @@ bincode = "1.3.3"
4444
default-net = "0.9.0"
4545
system_gateway = {git="https://github.com/aruntomar/system_gateway"}
4646
base64 = "0.13.0"
47+
tokio-rustls = "0.23.4"
48+
rustls = "0.20.4"
49+
derivative = "2.2.0"
50+
rustls-pemfile = "1.0.0"
51+
webpki-roots = "0.22"
52+
hyper-rustls = { git = "https://github.com/Andrewmatilde/hyper-rustls.git", features = ["http2"] }
4753
rtnetlink = "0.9.1"
4854
iproute2-rs = {git="https://github.com/chaos-mesh/iproute2-rs.git"}
4955
futures-util = "0.3"

chaos-tproxy-controller/Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ path = "src/lib.rs"
1515
anyhow = "1.0"
1616
clap = "2.33.3"
1717
futures = "0.3.10"
18-
http = "0.2.3"
18+
http = "0.2.7"
1919
humantime-serde = "1.0"
20-
hyper = {version = "0.14.4", features = ["runtime", "client", "server", "http1", "http2", "stream"]}
20+
hyper = {git = "https://github.com/Andrewmatilde/hyper.git", features = ["runtime", "client", "server", "http1", "http2", "stream", "error_return"]}
2121
iptables = "0.4"
2222
libc = {version = "0.2.81", features = ["std"]}
2323
paw = "1.0"
@@ -45,4 +45,10 @@ pnet = "0.28.0"
4545
default-net = "0.9.0"
4646
rtnetlink = "0.9.1"
4747
iproute2-rs = {git="https://github.com/chaos-mesh/iproute2-rs.git"}
48-
system_gateway = {git="https://github.com/aruntomar/system_gateway"}
48+
system_gateway = {git="https://github.com/aruntomar/system_gateway"}
49+
tokio-rustls = "0.23.4"
50+
rustls = "0.20.4"
51+
derivative = "2.2.0"
52+
rustls-pemfile = "1.0.0"
53+
webpki-roots = "0.22"
54+
hyper-rustls = { git = "https://github.com/Andrewmatilde/hyper-rustls.git", features = ["http2"] }

chaos-tproxy-controller/src/proxy/config.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ impl TryFrom<RawConfig> for Config {
3232
Some(rules) => rules,
3333
None => vec![],
3434
},
35+
tls: raw.tls,
3536
},
3637
})
3738
}
@@ -76,6 +77,7 @@ mod tests {
7677
safe_mode: None,
7778
interface: None,
7879
rules: None,
80+
tls: None,
7981

8082
listen_port: None,
8183
proxy_mark: None,
@@ -92,7 +94,8 @@ mod tests {
9294
listen_port: get_free_port(None).unwrap(),
9395
safe_mode: false,
9496
interface: None,
95-
rules: vec![]
97+
rules: vec![],
98+
tls: None
9699
}
97100
}
98101
);
@@ -102,6 +105,7 @@ mod tests {
102105
safe_mode: Some(true),
103106
interface: Some("ens33".parse().unwrap()),
104107
rules: None,
108+
tls: None,
105109

106110
listen_port: None,
107111
proxy_mark: None,
@@ -118,7 +122,8 @@ mod tests {
118122
listen_port: 1027u16,
119123
safe_mode: true,
120124
interface: Some("ens33".parse().unwrap()),
121-
rules: vec![]
125+
rules: vec![],
126+
tls: None
122127
}
123128
}
124129
);

chaos-tproxy-controller/src/proxy/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::path::PathBuf;
33
use std::process::Stdio;
44

55
use anyhow::Error;
6-
use rtnetlink::{Handle, new_connection};
76
use chaos_tproxy_proxy::raw_config::RawConfig as ProxyRawConfig;
7+
use rtnetlink::{new_connection, Handle};
88
use tokio::process::Command;
99
use tokio::select;
1010
use tokio::sync::oneshot::{channel, Receiver, Sender};

chaos-tproxy-controller/src/proxy/net/bridge.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use default_net;
55
use default_net::Gateway;
66
use pnet::datalink::NetworkInterface;
77
use pnet::ipnetwork::{IpNetwork, Ipv4Network};
8-
use rtnetlink::Handle;
98
use rtnetlink::packet::route::Nla;
109
use rtnetlink::packet::RouteMessage;
10+
use rtnetlink::Handle;
1111
use uuid::Uuid;
12-
use crate::proxy::net::iptables::clear_ebtables;
1312

13+
use crate::proxy::net::iptables::clear_ebtables;
1414
use crate::proxy::net::routes::{del_routes_noblock, get_routes_noblock, load_routes};
1515

1616
#[derive(Debug, Clone)]
@@ -31,7 +31,7 @@ pub struct NetEnv {
3131
}
3232

3333
impl NetEnv {
34-
pub async fn new(handle:&Handle) -> Self {
34+
pub async fn new(handle: &Handle) -> Self {
3535
let interfaces = pnet::datalink::interfaces();
3636
let prefix = loop {
3737
let key = Uuid::new_v4().to_string()[0..13].to_string();
@@ -194,12 +194,10 @@ impl NetEnv {
194194
.mac
195195
.context(format!("mac {} not found", self.veth4.clone()))?
196196
.to_string();
197-
execute_all(vec![
198-
ip_netns(
199-
&self.netns,
200-
arp_set(&net.ip().to_string(), &veth4_mac, &self.bridge2),
201-
),
202-
])?;
197+
execute_all(vec![ip_netns(
198+
&self.netns,
199+
arp_set(&net.ip().to_string(), &veth4_mac, &self.bridge2),
200+
)])?;
203201

204202
let all_routes = get_routes_noblock(handle).await?;
205203

@@ -226,7 +224,7 @@ impl NetEnv {
226224
Ok(())
227225
}
228226

229-
pub async fn clear_bridge(&self, handle:&mut Handle) -> Result<()> {
227+
pub async fn clear_bridge(&self, handle: &mut Handle) -> Result<()> {
230228
let restore_dns = "cp /etc/resolv.conf.bak /etc/resolv.conf";
231229

232230
let cmdvv = vec![
@@ -243,9 +241,11 @@ impl NetEnv {
243241
vec![]
244242
});
245243

246-
del_routes_noblock(handle, routes).await.unwrap_or_else(|e| {
247-
tracing::error!("clear routes del_routes_noblock with error {}", e);
248-
});
244+
del_routes_noblock(handle, routes)
245+
.await
246+
.unwrap_or_else(|e| {
247+
tracing::error!("clear routes del_routes_noblock with error {}", e);
248+
});
249249

250250
load_routes(handle, self.save_routes.clone())
251251
.await

chaos-tproxy-controller/src/proxy/net/routes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use anyhow::{anyhow, Result};
22
use futures_util::future::join_all;
33
use iproute2_rs::ip::iproute::{del_routes, get_routes, Action, IPRoute};
44
use rtnetlink::packet::RouteMessage;
5-
use rtnetlink::{IpVersion, Handle};
6-
5+
use rtnetlink::{Handle, IpVersion};
76

87
pub async fn get_routes_noblock(handle: &Handle) -> Result<Vec<RouteMessage>> {
98
let routes = get_routes(handle, IpVersion::V4).await?;
@@ -45,6 +44,7 @@ pub async fn load_routes(handle: &mut Handle, msgs: Vec<RouteMessage>) -> Result
4544
mod test {
4645
use rtnetlink::new_connection;
4746
use tokio::spawn;
47+
4848
use crate::proxy::net::routes::{del_routes_noblock, get_routes_noblock, load_routes};
4949

5050
#[ignore]

chaos-tproxy-controller/src/proxy/net/set_net.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::option::Option::Some;
2+
23
use rtnetlink::Handle;
34

45
use crate::proxy::net::bridge::{bash_c, execute, execute_all, get_interface, NetEnv};

chaos-tproxy-controller/src/raw_config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chaos_tproxy_proxy::raw_config::RawRule;
1+
use chaos_tproxy_proxy::raw_config::{RawRule, TLSRawConfig};
22
use serde::{Deserialize, Serialize};
33

44
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize, Default)]
@@ -8,6 +8,7 @@ pub struct RawConfig {
88
pub safe_mode: Option<bool>,
99
pub interface: Option<String>,
1010
pub rules: Option<Vec<RawRule>>,
11+
pub tls: Option<TLSRawConfig>,
1112

1213
// Useless options now. Keep these options for upward compatible.
1314
pub listen_port: Option<u16>,

chaos-tproxy-proxy/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2018"
99
anyhow = "1.0"
1010
clap = "2.33.3"
1111
futures = "0.3.10"
12-
http = "0.2.3"
12+
http = "0.2.7"
1313
humantime-serde = "1.0"
1414
hyper = {git = "https://github.com/Andrewmatilde/hyper.git", features = ["runtime", "client", "server", "http1", "http2", "stream", "error_return"]}
1515
iptables = "0.4"
@@ -34,5 +34,11 @@ bincode = "1.3.3"
3434
tempfile = "3.2.0"
3535
uuid = { version = "0.8", features = ["serde", "v4"] }
3636
base64 = "0.13.0"
37+
tokio-rustls = "0.23.4"
38+
rustls = "0.20.4"
39+
derivative = "2.2.0"
40+
rustls-pemfile = "1.0.0"
41+
webpki-roots = "0.22"
42+
hyper-rustls = { git = "https://github.com/Andrewmatilde/hyper-rustls.git", features = ["http2"] }
3743
rtnetlink = "0.9.1"
3844
futures-util = "0.3"

0 commit comments

Comments
 (0)