7 Commits

Author SHA1 Message Date
KernelErr
bff92738d5 Allow config path from FOURTH_CONFIG 2021-11-01 16:06:47 +08:00
KernelErr
754a5af794 Add publish CI and run fmt 2021-11-01 15:56:57 +08:00
KernelErr
fc7a3038bd Add unknown protocol error 2021-11-01 15:32:08 +08:00
KernelErr
8a96de9666 Update README and minor refactor 2021-11-01 15:25:12 +08:00
KernelErr
0407f4b40c Add config validation 2021-11-01 13:45:47 +08:00
KernelErr
47be2568ba Add upstream scheme support
Need to implement TCP and UDP upstream support.
2021-10-31 19:21:32 +08:00
KernelErr
5944beb6a2 Combine TCP and KCP tests 2021-10-27 08:36:24 +08:00
12 changed files with 410 additions and 135 deletions

39
.github/workflows/publish-binaries.yml vendored Normal file
View File

@@ -0,0 +1,39 @@
on:
release:
types: [published]
name: Publish binaries to release
jobs:
publish:
name: Publish for ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: ubuntu-latest
artifact_name: fourth
asset_name: fourth-linux-amd64
- os: macos-latest
artifact_name: fourth
asset_name: fourth-macos-amd64
- os: windows-latest
artifact_name: fourth.exe
asset_name: fourth-windows-amd64.exe
steps:
- uses: hecrj/setup-rust-action@master
with:
rust-version: stable
- uses: actions/checkout@v2
- name: Build
run: cargo build --release --locked
- name: Publish
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.PUBLISH_TOKEN }}
file: target/release/${{ matrix.artifact_name }}
asset_name: ${{ matrix.asset_name }}
tag: ${{ github.ref }}

78
Cargo.lock generated
View File

@@ -96,9 +96,19 @@ dependencies = [
"termcolor", "termcolor",
] ]
[[package]]
name = "form_urlencoded"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191"
dependencies = [
"matches",
"percent-encoding",
]
[[package]] [[package]]
name = "fourth" name = "fourth"
version = "0.1.3" version = "0.1.4"
dependencies = [ dependencies = [
"byte_string", "byte_string",
"bytes 1.1.0", "bytes 1.1.0",
@@ -110,6 +120,7 @@ dependencies = [
"serde_yaml", "serde_yaml",
"tls-parser", "tls-parser",
"tokio", "tokio",
"url",
] ]
[[package]] [[package]]
@@ -241,6 +252,17 @@ dependencies = [
"quick-error", "quick-error",
] ]
[[package]]
name = "idna"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
dependencies = [
"matches",
"unicode-bidi",
"unicode-normalization",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.7.0" version = "1.7.0"
@@ -318,6 +340,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "matches"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.4.1" version = "2.4.1"
@@ -453,6 +481,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "percent-encoding"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]] [[package]]
name = "phf" name = "phf"
version = "0.10.0" version = "0.10.0"
@@ -721,6 +755,21 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "tinyvec"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83b2a3d4d9091d0abd7eba4dc2710b1718583bd4d8992e2190720ea38f391f7"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]] [[package]]
name = "tls-parser" name = "tls-parser"
version = "0.11.0" version = "0.11.0"
@@ -766,12 +815,39 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "unicode-bidi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f"
[[package]]
name = "unicode-normalization"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9"
dependencies = [
"tinyvec",
]
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.2" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "url"
version = "2.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
dependencies = [
"form_urlencoded",
"idna",
"matches",
"percent-encoding",
]
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.3" version = "0.9.3"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "fourth" name = "fourth"
version = "0.1.3" version = "0.1.4"
edition = "2021" edition = "2021"
authors = ["LI Rui <lr_cn@outlook.com>"] authors = ["LI Rui <lr_cn@outlook.com>"]
license = "Apache-2.0" license = "Apache-2.0"
@@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8" serde_yaml = "0.8"
futures = "0.3" futures = "0.3"
tls-parser = "0.11" tls-parser = "0.11"
url = "2.2.2"
tokio = { version = "1.0", features = ["full"] } tokio = { version = "1.0", features = ["full"] }

View File

@@ -31,36 +31,35 @@ Or you can use Cargo to install Fourth:
$ cargo install fourth $ cargo install fourth
``` ```
Or you can download binary file form the Release page.
## Configuration ## Configuration
Fourth will read yaml format configuration file from `/etc/fourth/config.yaml`, here is an example: Fourth will read yaml format configuration file from `/etc/fourth/config.yaml`, and you can set custom path to environment variable `FOURTH_CONFIG`, here is an minimal viable example:
```yaml ```yaml
version: 1 version: 1
log: info log: info
servers: servers:
example_server: proxy_server:
listen:
- "0.0.0.0:443"
- "[::]:443"
tls: true # Enable TLS features like SNI
sni:
proxy.example.com: proxy
www.example.com: nginx
default: ban
relay_server:
listen: listen:
- "127.0.0.1:8081" - "127.0.0.1:8081"
default: remote default: remote
upstream: upstream:
nginx: "127.0.0.1:8080" remote: "tcp://www.remote.example.com:8082" # proxy to remote address
proxy: "127.0.0.1:1024"
remote: "www.remote.example.com:8082" # proxy to remote address
``` ```
Built-in two upstreams: ban(terminate connection immediately), echo Built-in two upstreams: ban(terminate connection immediately), echo. For detailed configuration, check [this example](./example-config.yaml).
## Performance Benchmark
Tested on 4C2G server:
Use fourth to proxy to Nginx(QPS of direct connection: ~120000): ~70000 req/s (Command: `wrk -t200 -c1000 -d120s --latency http://proxy-server:8081`)
Use fourth to proxy to local iperf3: 8Gbps
## Thanks ## Thanks

View File

@@ -33,41 +33,35 @@ $ cargo build --release
$ cargo install fourth $ cargo install fourth
``` ```
或者您也可以直接从Release中下载二进制文件。
## 配置 ## 配置
Fourth使用yaml格式的配置文件默认情况下会读取`/etc/fourth/config.yaml`,如下是一个示例配置 Fourth使用yaml格式的配置文件默认情况下会读取`/etc/fourth/config.yaml`您也可以设置自定义路径到环境变量`FOURTH_CONFIG`如下是一个最小有效配置
```yaml ```yaml
version: 1 version: 1
log: info log: info
servers: servers:
example_server:
listen:
- "0.0.0.0:443"
- "[::]:443"
tls: true # Enable TLS features like SNI filtering
sni:
proxy.example.com: proxy
www.example.com: nginx
default: ban
proxy_server: proxy_server:
listen: listen:
- "127.0.0.1:8081" - "127.0.0.1:8081"
default: remote default: remote
kcp_server:
protocol: kcp # default TCP
listen:
- "127.0.0.1:8082"
default: echo
upstream: upstream:
nginx: "127.0.0.1:8080" remote: "tcp://www.remote.example.com:8082" # proxy to remote address
proxy: "127.0.0.1:1024"
remote: "www.remote.example.com:8082" # proxy to remote address
``` ```
内置两个的upstreamban立即中断连接、echo返回读到的数据 内置两个的upstreamban立即中断连接、echo返回读到的数据更详细的配置可以参考[示例配置](./example-config.yaml)。
## 性能测试
在4C2G的服务器上测试
使用Fourth代理到Nginx直连QPS 120000: ~70000req/s (测试命令:`wrk -t200 -c1000 -d120s --latency http://proxy-server:8081 `
使用Fourth代理到本地iperf38Gbps
## io_uring? ## io_uring?

View File

@@ -22,6 +22,6 @@ servers:
default: echo default: echo
upstream: upstream:
nginx: "127.0.0.1:8080" nginx: "tcp://127.0.0.1:8080"
proxy: "127.0.0.1:1024" proxy: "tcp://127.0.0.1:1024"
remote: "www.remote.example.com:8082" # proxy to remote address remote: "tcp://www.remote.example.com:8082" # proxy to remote address

View File

@@ -1,12 +1,21 @@
use log::debug; use log::{debug, warn};
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::fs::File; use std::fs::File;
use std::io::{Error as IOError, Read}; use std::io::{Error as IOError, Read};
use url::Url;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct Config {
pub base: BaseConfig, pub base: ParsedConfig,
}
#[derive(Debug, Default, Deserialize, Clone)]
pub struct ParsedConfig {
pub version: i32,
pub log: Option<String>,
pub servers: HashMap<String, ServerConfig>,
pub upstream: HashMap<String, Upstream>,
} }
#[derive(Debug, Default, Deserialize, Clone)] #[derive(Debug, Default, Deserialize, Clone)]
@@ -26,6 +35,20 @@ pub struct ServerConfig {
pub default: Option<String>, pub default: Option<String>,
} }
#[derive(Debug, Clone, Deserialize)]
pub enum Upstream {
Ban,
Echo,
Custom(CustomUpstream),
}
#[derive(Debug, Clone, Deserialize)]
pub struct CustomUpstream {
pub name: String,
pub addr: String,
pub protocol: String,
}
#[derive(Debug)] #[derive(Debug)]
pub enum ConfigError { pub enum ConfigError {
IO(IOError), IO(IOError),
@@ -41,29 +64,146 @@ impl Config {
} }
} }
fn load_config(path: &str) -> Result<BaseConfig, ConfigError> { fn load_config(path: &str) -> Result<ParsedConfig, ConfigError> {
let mut contents = String::new(); let mut contents = String::new();
let mut file = (File::open(path))?; let mut file = (File::open(path))?;
(file.read_to_string(&mut contents))?; (file.read_to_string(&mut contents))?;
let parsed: BaseConfig = serde_yaml::from_str(&contents)?; let base: BaseConfig = serde_yaml::from_str(&contents)?;
if parsed.version != 1 { if base.version != 1 {
return Err(ConfigError::Custom( return Err(ConfigError::Custom(
"Unsupported config version".to_string(), "Unsupported config version".to_string(),
)); ));
} }
let log_level = parsed.log.clone().unwrap_or_else(|| "info".to_string()); let log_level = base.log.clone().unwrap_or_else(|| "info".to_string());
if !log_level.eq("disable") { if !log_level.eq("disable") {
std::env::set_var("FOURTH_LOG", log_level.clone()); std::env::set_var("FOURTH_LOG", log_level.clone());
pretty_env_logger::init_custom_env("FOURTH_LOG"); pretty_env_logger::init_custom_env("FOURTH_LOG");
debug!("Set log level to {}", log_level); debug!("Set log level to {}", log_level);
} }
debug!("Config version {}", parsed.version); debug!("Config version {}", base.version);
Ok(parsed) let mut parsed_upstream: HashMap<String, Upstream> = HashMap::new();
for (name, upstream) in base.upstream.iter() {
let upstream_url = match Url::parse(upstream) {
Ok(url) => url,
Err(_) => {
return Err(ConfigError::Custom(format!(
"Invalid upstream url {}",
upstream
)))
}
};
let upstream_host = match upstream_url.host_str() {
Some(host) => host,
None => {
return Err(ConfigError::Custom(format!(
"Invalid upstream url {}",
upstream
)))
}
};
let upsteam_port = match upstream_url.port_or_known_default() {
Some(port) => port,
None => {
return Err(ConfigError::Custom(format!(
"Invalid upstream url {}",
upstream
)))
}
};
if upstream_url.scheme() != "tcp" {
return Err(ConfigError::Custom(format!(
"Invalid upstream scheme {}",
upstream
)));
}
parsed_upstream.insert(
name.to_string(),
Upstream::Custom(CustomUpstream {
name: name.to_string(),
addr: format!("{}:{}", upstream_host, upsteam_port),
protocol: upstream_url.scheme().to_string(),
}),
);
}
parsed_upstream.insert("ban".to_string(), Upstream::Ban);
parsed_upstream.insert("echo".to_string(), Upstream::Echo);
let parsed = ParsedConfig {
version: base.version,
log: base.log,
servers: base.servers,
upstream: parsed_upstream,
};
verify_config(parsed)
}
fn verify_config(config: ParsedConfig) -> Result<ParsedConfig, ConfigError> {
let mut used_upstreams: HashSet<String> = HashSet::new();
let mut upstream_names: HashSet<String> = HashSet::new();
let mut listen_addresses: HashSet<String> = HashSet::new();
// Check for duplicate upstream names
for (name, _) in config.upstream.iter() {
if upstream_names.contains(name) {
return Err(ConfigError::Custom(format!(
"Duplicate upstream name {}",
name
)));
}
upstream_names.insert(name.to_string());
}
for (_, server) in config.servers.clone() {
// check for duplicate listen addresses
for listen in server.listen {
if listen_addresses.contains(&listen) {
return Err(ConfigError::Custom(format!(
"Duplicate listen address {}",
listen
)));
}
listen_addresses.insert(listen.to_string());
}
if server.tls.unwrap_or_default() && server.sni.is_some() {
for (_, val) in server.sni.unwrap() {
used_upstreams.insert(val.to_string());
}
}
if server.default.is_some() {
used_upstreams.insert(server.default.unwrap().to_string());
}
for key in &used_upstreams {
if !config.upstream.contains_key(key) {
return Err(ConfigError::Custom(format!("Upstream {} not found", key)));
}
}
}
for key in &upstream_names {
if !used_upstreams.contains(key) {
warn!("Upstream {} not used", key);
}
}
Ok(config)
} }
impl From<IOError> for ConfigError { impl From<IOError> for ConfigError {
@@ -88,6 +228,6 @@ mod tests {
assert_eq!(config.base.version, 1); assert_eq!(config.base.version, 1);
assert_eq!(config.base.log.unwrap(), "disable"); assert_eq!(config.base.log.unwrap(), "disable");
assert_eq!(config.base.servers.len(), 5); assert_eq!(config.base.servers.len(), 5);
assert_eq!(config.base.upstream.len(), 3); assert_eq!(config.base.upstream.len(), 3 + 2); // Add ban and echo upstreams
} }
} }

View File

@@ -5,10 +5,13 @@ mod servers;
use crate::config::Config; use crate::config::Config;
use crate::servers::Server; use crate::servers::Server;
use std::env;
use log::{debug, error}; use log::{debug, error};
fn main() { fn main() {
let config = match Config::new("/etc/fourth/config.yaml") { let config_path = env::var("FOURTH_CONFIG").unwrap_or_else(|_| "/etc/fourth/config.yaml".to_string());
let config = match Config::new(&config_path) {
Ok(config) => config, Ok(config) => config,
Err(e) => { Err(e) => {
println!("Could not load config: {:?}", e); println!("Could not load config: {:?}", e);

View File

@@ -5,13 +5,13 @@ use std::sync::Arc;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
mod protocol; mod protocol;
use crate::config::BaseConfig; use crate::config::{ParsedConfig, Upstream};
use protocol::{kcp, tcp}; use protocol::{kcp, tcp};
#[derive(Debug)] #[derive(Debug)]
pub struct Server { pub struct Server {
pub proxies: Vec<Arc<Proxy>>, pub proxies: Vec<Arc<Proxy>>,
pub config: BaseConfig, pub config: ParsedConfig,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -22,11 +22,11 @@ pub struct Proxy {
pub tls: bool, pub tls: bool,
pub sni: Option<HashMap<String, String>>, pub sni: Option<HashMap<String, String>>,
pub default: String, pub default: String,
pub upstream: HashMap<String, String>, pub upstream: HashMap<String, Upstream>,
} }
impl Server { impl Server {
pub fn new(config: BaseConfig) -> Self { pub fn new(config: ParsedConfig) -> Self {
let mut new_server = Server { let mut new_server = Server {
proxies: Vec::new(), proxies: Vec::new(),
config: config.clone(), config: config.clone(),
@@ -53,6 +53,7 @@ impl Server {
continue; continue;
} }
}; };
let proxy = Proxy { let proxy = Proxy {
name: name.clone(), name: name.clone(),
listen: listen_addr, listen: listen_addr,
@@ -103,7 +104,7 @@ impl Server {
} }
#[cfg(test)] #[cfg(test)]
mod test { mod tests {
use crate::plugins::kcp::{KcpConfig, KcpStream}; use crate::plugins::kcp::{KcpConfig, KcpStream};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::thread::{self, sleep}; use std::thread::{self, sleep};
@@ -117,16 +118,24 @@ mod test {
async fn tcp_mock_server() { async fn tcp_mock_server() {
let server_addr: SocketAddr = "127.0.0.1:54599".parse().unwrap(); let server_addr: SocketAddr = "127.0.0.1:54599".parse().unwrap();
let listener = TcpListener::bind(server_addr).await.unwrap(); let listener = TcpListener::bind(server_addr).await.unwrap();
let (mut stream, _) = listener.accept().await.unwrap(); loop {
let mut buf = [0u8; 1024]; let (mut stream, _) = listener.accept().await.unwrap();
let n = stream.read(&mut buf).await.unwrap(); let mut buf = [0u8; 2];
if n > 0 { let mut n = stream.read(&mut buf).await.unwrap();
stream.write(b"hello").await.unwrap(); while n > 0 {
stream.write(b"hello").await.unwrap();
if buf.eq(b"by") {
stream.shutdown().await.unwrap();
break;
}
n = stream.read(&mut buf).await.unwrap();
}
stream.shutdown().await.unwrap();
} }
} }
#[tokio::test] #[tokio::test]
async fn test_tcp_proxy() { async fn test_proxy() {
use crate::config::Config; use crate::config::Config;
let config = Config::new("tests/config.yaml").unwrap(); let config = Config::new("tests/config.yaml").unwrap();
let mut server = Server::new(config.base); let mut server = Server::new(config.base);
@@ -139,15 +148,15 @@ mod test {
}); });
sleep(Duration::from_secs(1)); // wait for server to start sleep(Duration::from_secs(1)); // wait for server to start
// // test proxy // test TCP proxy
// let mut conn = TcpStream::connect("127.0.0.1:54500").await.unwrap(); let mut conn = TcpStream::connect("127.0.0.1:54500").await.unwrap();
// let mut buf = [0u8; 5]; let mut buf = [0u8; 5];
// conn.write(b"hi").await.unwrap(); conn.write(b"hi").await.unwrap();
// conn.read(&mut buf).await.unwrap(); conn.read(&mut buf).await.unwrap();
// assert_eq!(&buf, b"hello"); assert_eq!(&buf, b"hello");
// conn.shutdown().await.unwrap(); conn.shutdown().await.unwrap();
// test echo // test TCP echo
let mut conn = TcpStream::connect("127.0.0.1:54956").await.unwrap(); let mut conn = TcpStream::connect("127.0.0.1:54956").await.unwrap();
let mut buf = [0u8; 1]; let mut buf = [0u8; 1];
for i in 0..=10u8 { for i in 0..=10u8 {
@@ -156,28 +165,8 @@ mod test {
assert_eq!(&buf, &[i]); assert_eq!(&buf, &[i]);
} }
conn.shutdown().await.unwrap(); conn.shutdown().await.unwrap();
}
#[tokio::test] // test KCP echo
async fn test_kcp_proxy() {
use crate::config::Config;
let config = Config::new("tests/config.yaml").unwrap();
let mut server = Server::new(config.base);
thread::spawn(move || {
let _ = server.run();
});
sleep(Duration::from_secs(1)); // wait for server to start
// test proxy
let kcp_config = KcpConfig::default();
let server_addr: SocketAddr = "127.0.0.1:54958".parse().unwrap();
let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap();
let mut buf = [0u8; 5];
conn.write(b"hi").await.unwrap();
conn.read(&mut buf).await.unwrap();
assert_eq!(&buf, b"hello");
// test echo
let kcp_config = KcpConfig::default(); let kcp_config = KcpConfig::default();
let server_addr: SocketAddr = "127.0.0.1:54959".parse().unwrap(); let server_addr: SocketAddr = "127.0.0.1:54959".parse().unwrap();
let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap(); let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap();
@@ -188,5 +177,15 @@ mod test {
assert_eq!(&buf, &[i]); assert_eq!(&buf, &[i]);
} }
conn.shutdown().await.unwrap(); conn.shutdown().await.unwrap();
// test KCP proxy and close mock server
let kcp_config = KcpConfig::default();
let server_addr: SocketAddr = "127.0.0.1:54958".parse().unwrap();
let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap();
let mut buf = [0u8; 5];
conn.write(b"by").await.unwrap();
conn.read(&mut buf).await.unwrap();
assert_eq!(&buf, b"hello");
conn.shutdown().await.unwrap();
} }
} }

View File

@@ -1,3 +1,4 @@
use crate::config::Upstream;
use crate::plugins::kcp::{KcpConfig, KcpListener, KcpStream}; use crate::plugins::kcp::{KcpConfig, KcpListener, KcpStream};
use crate::servers::Proxy; use crate::servers::Proxy;
use futures::future::try_join; use futures::future::try_join;
@@ -52,36 +53,47 @@ async fn accept(
"No upstream named {:?} on server {:?}", "No upstream named {:?} on server {:?}",
proxy.default, proxy.name proxy.default, proxy.name
); );
return process(inbound, &proxy.default).await; return process(inbound, proxy.upstream.get(&proxy.default).unwrap()).await;
// ToDo: Remove unwrap and check default option
} }
}; };
return process(inbound, upstream).await; return process(inbound, upstream).await;
} }
async fn process(mut inbound: KcpStream, upstream: &str) -> Result<(), Box<dyn std::error::Error>> { async fn process(
if upstream == "ban" { mut inbound: KcpStream,
let _ = inbound.shutdown(); upstream: &Upstream,
return Ok(()); ) -> Result<(), Box<dyn std::error::Error>> {
} else if upstream == "echo" { match upstream {
let (mut ri, mut wi) = io::split(inbound); Upstream::Ban => {
let inbound_to_inbound = copy(&mut ri, &mut wi); let _ = inbound.shutdown();
let bytes_tx = inbound_to_inbound.await; }
debug!("Bytes read: {:?}", bytes_tx); Upstream::Echo => {
return Ok(()); let (mut ri, mut wi) = io::split(inbound);
} let inbound_to_inbound = copy(&mut ri, &mut wi);
let bytes_tx = inbound_to_inbound.await;
debug!("Bytes read: {:?}", bytes_tx);
}
Upstream::Custom(custom) => match custom.protocol.as_ref() {
"tcp" => {
let outbound = TcpStream::connect(custom.addr.clone()).await?;
let outbound = TcpStream::connect(upstream).await?; let (mut ri, mut wi) = io::split(inbound);
let (mut ro, mut wo) = io::split(outbound);
let (mut ri, mut wi) = io::split(inbound); let inbound_to_outbound = copy(&mut ri, &mut wo);
let (mut ro, mut wo) = io::split(outbound); let outbound_to_inbound = copy(&mut ro, &mut wi);
let inbound_to_outbound = copy(&mut ri, &mut wo); let (bytes_tx, bytes_rx) =
let outbound_to_inbound = copy(&mut ro, &mut wi); try_join(inbound_to_outbound, outbound_to_inbound).await?;
let (bytes_tx, bytes_rx) = try_join(inbound_to_outbound, outbound_to_inbound).await?;
debug!("Bytes read: {:?} write: {:?}", bytes_tx, bytes_rx);
debug!("Bytes read: {:?} write: {:?}", bytes_tx, bytes_rx);
}
_ => {
error!("Reached unknown protocol: {:?}", custom.protocol);
}
},
};
Ok(()) Ok(())
} }

View File

@@ -1,3 +1,4 @@
use crate::config::Upstream;
use crate::servers::protocol::tls::get_sni; use crate::servers::protocol::tls::get_sni;
use crate::servers::Proxy; use crate::servers::Proxy;
use futures::future::try_join; use futures::future::try_join;
@@ -71,36 +72,47 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn std
"No upstream named {:?} on server {:?}", "No upstream named {:?} on server {:?}",
proxy.default, proxy.name proxy.default, proxy.name
); );
return process(inbound, &proxy.default).await; return process(inbound, proxy.upstream.get(&proxy.default).unwrap()).await;
// ToDo: Remove unwrap and check default option
} }
}; };
return process(inbound, upstream).await; return process(inbound, upstream).await;
} }
async fn process(mut inbound: TcpStream, upstream: &str) -> Result<(), Box<dyn std::error::Error>> { async fn process(
if upstream == "ban" { mut inbound: TcpStream,
let _ = inbound.shutdown(); upstream: &Upstream,
return Ok(()); ) -> Result<(), Box<dyn std::error::Error>> {
} else if upstream == "echo" { match upstream {
let (mut ri, mut wi) = io::split(inbound); Upstream::Ban => {
let inbound_to_inbound = copy(&mut ri, &mut wi); let _ = inbound.shutdown();
let bytes_tx = inbound_to_inbound.await; }
debug!("Bytes read: {:?}", bytes_tx); Upstream::Echo => {
return Ok(()); let (mut ri, mut wi) = io::split(inbound);
} let inbound_to_inbound = copy(&mut ri, &mut wi);
let bytes_tx = inbound_to_inbound.await;
debug!("Bytes read: {:?}", bytes_tx);
}
Upstream::Custom(custom) => match custom.protocol.as_ref() {
"tcp" => {
let outbound = TcpStream::connect(custom.addr.clone()).await?;
let outbound = TcpStream::connect(upstream).await?; let (mut ri, mut wi) = io::split(inbound);
let (mut ro, mut wo) = io::split(outbound);
let (mut ri, mut wi) = io::split(inbound); let inbound_to_outbound = copy(&mut ri, &mut wo);
let (mut ro, mut wo) = io::split(outbound); let outbound_to_inbound = copy(&mut ro, &mut wi);
let inbound_to_outbound = copy(&mut ri, &mut wo); let (bytes_tx, bytes_rx) =
let outbound_to_inbound = copy(&mut ro, &mut wi); try_join(inbound_to_outbound, outbound_to_inbound).await?;
let (bytes_tx, bytes_rx) = try_join(inbound_to_outbound, outbound_to_inbound).await?;
debug!("Bytes read: {:?} write: {:?}", bytes_tx, bytes_rx);
debug!("Bytes read: {:?} write: {:?}", bytes_tx, bytes_rx);
}
_ => {
error!("Reached unknown protocol: {:?}", custom.protocol);
}
},
};
Ok(()) Ok(())
} }

View File

@@ -31,6 +31,6 @@ servers:
default: echo default: echo
upstream: upstream:
web: "127.0.0.1:8080" web: "tcp://127.0.0.1:8080"
proxy: "www.example.com:1024" proxy: "tcp://www.example.com:1024"
tester: "127.0.0.1:54599" tester: "tcp://127.0.0.1:54599"