Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
bff92738d5 | |||
754a5af794 | |||
fc7a3038bd | |||
8a96de9666 | |||
0407f4b40c | |||
47be2568ba | |||
5944beb6a2 | |||
4363e3f76a | |||
ee9d0685b3 | |||
421ad8c979 |
39
.github/workflows/publish-binaries.yml
vendored
Normal file
39
.github/workflows/publish-binaries.yml
vendored
Normal 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
78
Cargo.lock
generated
@ -96,9 +96,19 @@ dependencies = [
|
||||
"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]]
|
||||
name = "fourth"
|
||||
version = "0.1.2"
|
||||
version = "0.1.4"
|
||||
dependencies = [
|
||||
"byte_string",
|
||||
"bytes 1.1.0",
|
||||
@ -110,6 +120,7 @@ dependencies = [
|
||||
"serde_yaml",
|
||||
"tls-parser",
|
||||
"tokio",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -241,6 +252,17 @@ dependencies = [
|
||||
"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]]
|
||||
name = "indexmap"
|
||||
version = "1.7.0"
|
||||
@ -318,6 +340,12 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.4.1"
|
||||
@ -453,6 +481,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||
|
||||
[[package]]
|
||||
name = "phf"
|
||||
version = "0.10.0"
|
||||
@ -721,6 +755,21 @@ dependencies = [
|
||||
"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]]
|
||||
name = "tls-parser"
|
||||
version = "0.11.0"
|
||||
@ -766,12 +815,39 @@ dependencies = [
|
||||
"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]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "fourth"
|
||||
version = "0.1.2"
|
||||
version = "0.1.4"
|
||||
edition = "2021"
|
||||
authors = ["LI Rui <lr_cn@outlook.com>"]
|
||||
license = "Apache-2.0"
|
||||
@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"] }
|
||||
serde_yaml = "0.8"
|
||||
futures = "0.3"
|
||||
tls-parser = "0.11"
|
||||
url = "2.2.2"
|
||||
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
|
||||
|
31
README-EN.md
31
README-EN.md
@ -4,6 +4,8 @@
|
||||
|
||||
[](https://crates.io/crates/fourth) [](https://github.com/KernelErr/fourth/actions/workflows/rust.yml)
|
||||
|
||||
**Under heavy development, version 0.1 may update frequently**
|
||||
|
||||
Fourth is a layer 4 proxy implemented by Rust to listen on specific ports and transfer TCP/KCP data to remote addresses(only TCP) according to configuration.
|
||||
|
||||
## Features
|
||||
@ -29,36 +31,35 @@ Or you can use Cargo to install Fourth:
|
||||
$ cargo install fourth
|
||||
```
|
||||
|
||||
Or you can download binary file form the Release page.
|
||||
|
||||
## 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
|
||||
version: 1
|
||||
log: info
|
||||
|
||||
servers:
|
||||
example_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:
|
||||
proxy_server:
|
||||
listen:
|
||||
- "127.0.0.1:8081"
|
||||
default: remote
|
||||
|
||||
upstream:
|
||||
nginx: "127.0.0.1:8080"
|
||||
proxy: "127.0.0.1:1024"
|
||||
other: "www.remote.example.com:8082" # proxy to remote address
|
||||
remote: "tcp://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
|
||||
|
||||
|
34
README.md
34
README.md
@ -6,6 +6,8 @@
|
||||
|
||||
[English](/README-EN.md)
|
||||
|
||||
**积极开发中,0.1版本迭代可能较快**
|
||||
|
||||
Fourth是一个Rust实现的Layer 4代理,用于监听指定端口TCP/KCP流量,并根据规则转发到指定目标(目前只支持TCP)。
|
||||
|
||||
## 功能
|
||||
@ -31,41 +33,35 @@ $ cargo build --release
|
||||
$ cargo install fourth
|
||||
```
|
||||
|
||||
或者您也可以直接从Release中下载二进制文件。
|
||||
|
||||
## 配置
|
||||
|
||||
Fourth使用yaml格式的配置文件,默认情况下会读取`/etc/fourth/config.yaml`,如下是一个示例配置。
|
||||
Fourth使用yaml格式的配置文件,默认情况下会读取`/etc/fourth/config.yaml`,您也可以设置自定义路径到环境变量`FOURTH_CONFIG`,如下是一个最小有效配置:
|
||||
|
||||
```yaml
|
||||
version: 1
|
||||
log: info
|
||||
|
||||
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:
|
||||
listen:
|
||||
- "127.0.0.1:8081"
|
||||
default: remote
|
||||
kcp_server:
|
||||
protocol: kcp # default TCP
|
||||
listen:
|
||||
- "127.0.0.1:8082"
|
||||
default: echo
|
||||
|
||||
upstream:
|
||||
nginx: "127.0.0.1:8080"
|
||||
proxy: "127.0.0.1:1024"
|
||||
other: "www.remote.example.com:8082" # proxy to remote address
|
||||
remote: "tcp://www.remote.example.com:8082" # proxy to remote address
|
||||
```
|
||||
|
||||
内置两个的upstream:ban(立即中断连接)、echo(返回读到的数据)。
|
||||
内置两个的upstream:ban(立即中断连接)、echo(返回读到的数据)。更详细的配置可以参考[示例配置](./example-config.yaml)。
|
||||
|
||||
## 性能测试
|
||||
|
||||
在4C2G的服务器上测试:
|
||||
|
||||
使用Fourth代理到Nginx(直连QPS 120000): ~70000req/s (测试命令:`wrk -t200 -c1000 -d120s --latency http://proxy-server:8081 `)
|
||||
|
||||
使用Fourth代理到本地iperf3:8Gbps
|
||||
|
||||
## io_uring?
|
||||
|
||||
|
@ -22,6 +22,6 @@ servers:
|
||||
default: echo
|
||||
|
||||
upstream:
|
||||
nginx: "127.0.0.1:8080"
|
||||
proxy: "127.0.0.1:1024"
|
||||
other: "www.remote.example.com:8082" # proxy to remote address
|
||||
nginx: "tcp://127.0.0.1:8080"
|
||||
proxy: "tcp://127.0.0.1:1024"
|
||||
remote: "tcp://www.remote.example.com:8082" # proxy to remote address
|
162
src/config.rs
162
src/config.rs
@ -1,12 +1,21 @@
|
||||
use log::debug;
|
||||
use log::{debug, warn};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::File;
|
||||
use std::io::{Error as IOError, Read};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
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)]
|
||||
@ -26,6 +35,20 @@ pub struct ServerConfig {
|
||||
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)]
|
||||
pub enum ConfigError {
|
||||
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 file = (File::open(path))?;
|
||||
(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(
|
||||
"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") {
|
||||
std::env::set_var("FOURTH_LOG", log_level.clone());
|
||||
pretty_env_logger::init_custom_env("FOURTH_LOG");
|
||||
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 {
|
||||
@ -87,7 +227,7 @@ mod tests {
|
||||
let config = Config::new("tests/config.yaml").unwrap();
|
||||
assert_eq!(config.base.version, 1);
|
||||
assert_eq!(config.base.log.unwrap(), "disable");
|
||||
assert_eq!(config.base.servers.len(), 4);
|
||||
assert_eq!(config.base.upstream.len(), 3);
|
||||
assert_eq!(config.base.servers.len(), 5);
|
||||
assert_eq!(config.base.upstream.len(), 3 + 2); // Add ban and echo upstreams
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,13 @@ mod servers;
|
||||
use crate::config::Config;
|
||||
use crate::servers::Server;
|
||||
|
||||
use std::env;
|
||||
use log::{debug, error};
|
||||
|
||||
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,
|
||||
Err(e) => {
|
||||
println!("Could not load config: {:?}", e);
|
||||
|
@ -5,13 +5,13 @@ use std::sync::Arc;
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
mod protocol;
|
||||
use crate::config::BaseConfig;
|
||||
use crate::config::{ParsedConfig, Upstream};
|
||||
use protocol::{kcp, tcp};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Server {
|
||||
pub proxies: Vec<Arc<Proxy>>,
|
||||
pub config: BaseConfig,
|
||||
pub config: ParsedConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -22,11 +22,11 @@ pub struct Proxy {
|
||||
pub tls: bool,
|
||||
pub sni: Option<HashMap<String, String>>,
|
||||
pub default: String,
|
||||
pub upstream: HashMap<String, String>,
|
||||
pub upstream: HashMap<String, Upstream>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub fn new(config: BaseConfig) -> Self {
|
||||
pub fn new(config: ParsedConfig) -> Self {
|
||||
let mut new_server = Server {
|
||||
proxies: Vec::new(),
|
||||
config: config.clone(),
|
||||
@ -53,6 +53,7 @@ impl Server {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let proxy = Proxy {
|
||||
name: name.clone(),
|
||||
listen: listen_addr,
|
||||
@ -103,7 +104,7 @@ impl Server {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
mod tests {
|
||||
use crate::plugins::kcp::{KcpConfig, KcpStream};
|
||||
use std::net::SocketAddr;
|
||||
use std::thread::{self, sleep};
|
||||
@ -117,13 +118,24 @@ mod test {
|
||||
async fn tcp_mock_server() {
|
||||
let server_addr: SocketAddr = "127.0.0.1:54599".parse().unwrap();
|
||||
let listener = TcpListener::bind(server_addr).await.unwrap();
|
||||
loop {
|
||||
let (mut stream, _) = listener.accept().await.unwrap();
|
||||
let mut buf = [0u8; 2];
|
||||
let mut n = stream.read(&mut buf).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]
|
||||
async fn test_tcp_proxy() {
|
||||
async fn test_proxy() {
|
||||
use crate::config::Config;
|
||||
let config = Config::new("tests/config.yaml").unwrap();
|
||||
let mut server = Server::new(config.base);
|
||||
@ -135,41 +147,26 @@ mod test {
|
||||
let _ = server.run();
|
||||
});
|
||||
sleep(Duration::from_secs(1)); // wait for server to start
|
||||
|
||||
// test TCP proxy
|
||||
let mut conn = TcpStream::connect("127.0.0.1:54500").await.unwrap();
|
||||
let mut buf = [0u8; 5];
|
||||
conn.write(b"hi").await.unwrap();
|
||||
conn.read(&mut buf).await.unwrap();
|
||||
assert_eq!(&buf, b"hello");
|
||||
conn.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_tcp_echo_server() {
|
||||
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 TCP echo
|
||||
let mut conn = TcpStream::connect("127.0.0.1:54956").await.unwrap();
|
||||
let mut buf = [0u8; 1];
|
||||
for i in 0..=255u8 {
|
||||
for i in 0..=10u8 {
|
||||
conn.write(&[i]).await.unwrap();
|
||||
conn.read(&mut buf).await.unwrap();
|
||||
assert_eq!(&buf, &[i]);
|
||||
}
|
||||
conn.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_kcp_echo_server() {
|
||||
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 KCP echo
|
||||
let kcp_config = KcpConfig::default();
|
||||
let server_addr: SocketAddr = "127.0.0.1:54959".parse().unwrap();
|
||||
let mut conn = KcpStream::connect(&kcp_config, server_addr).await.unwrap();
|
||||
@ -180,5 +177,15 @@ mod test {
|
||||
assert_eq!(&buf, &[i]);
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use crate::config::Upstream;
|
||||
use crate::plugins::kcp::{KcpConfig, KcpListener, KcpStream};
|
||||
use crate::servers::Proxy;
|
||||
use futures::future::try_join;
|
||||
@ -52,25 +53,30 @@ async fn accept(
|
||||
"No upstream named {:?} on server {:?}",
|
||||
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;
|
||||
}
|
||||
|
||||
async fn process(mut inbound: KcpStream, upstream: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if upstream == "ban" {
|
||||
async fn process(
|
||||
mut inbound: KcpStream,
|
||||
upstream: &Upstream,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
match upstream {
|
||||
Upstream::Ban => {
|
||||
let _ = inbound.shutdown();
|
||||
return Ok(());
|
||||
} else if upstream == "echo" {
|
||||
}
|
||||
Upstream::Echo => {
|
||||
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);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let outbound = TcpStream::connect(upstream).await?;
|
||||
Upstream::Custom(custom) => match custom.protocol.as_ref() {
|
||||
"tcp" => {
|
||||
let outbound = TcpStream::connect(custom.addr.clone()).await?;
|
||||
|
||||
let (mut ri, mut wi) = io::split(inbound);
|
||||
let (mut ro, mut wo) = io::split(outbound);
|
||||
@ -78,10 +84,16 @@ async fn process(mut inbound: KcpStream, upstream: &str) -> Result<(), Box<dyn s
|
||||
let inbound_to_outbound = copy(&mut ri, &mut wo);
|
||||
let outbound_to_inbound = copy(&mut ro, &mut wi);
|
||||
|
||||
let (bytes_tx, bytes_rx) = 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);
|
||||
|
||||
}
|
||||
_ => {
|
||||
error!("Reached unknown protocol: {:?}", custom.protocol);
|
||||
}
|
||||
},
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
use crate::config::Upstream;
|
||||
use crate::servers::protocol::tls::get_sni;
|
||||
use crate::servers::Proxy;
|
||||
use futures::future::try_join;
|
||||
@ -71,25 +72,30 @@ async fn accept(inbound: TcpStream, proxy: Arc<Proxy>) -> Result<(), Box<dyn std
|
||||
"No upstream named {:?} on server {:?}",
|
||||
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;
|
||||
}
|
||||
|
||||
async fn process(mut inbound: TcpStream, upstream: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if upstream == "ban" {
|
||||
async fn process(
|
||||
mut inbound: TcpStream,
|
||||
upstream: &Upstream,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
match upstream {
|
||||
Upstream::Ban => {
|
||||
let _ = inbound.shutdown();
|
||||
return Ok(());
|
||||
} else if upstream == "echo" {
|
||||
}
|
||||
Upstream::Echo => {
|
||||
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);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let outbound = TcpStream::connect(upstream).await?;
|
||||
Upstream::Custom(custom) => match custom.protocol.as_ref() {
|
||||
"tcp" => {
|
||||
let outbound = TcpStream::connect(custom.addr.clone()).await?;
|
||||
|
||||
let (mut ri, mut wi) = io::split(inbound);
|
||||
let (mut ro, mut wo) = io::split(outbound);
|
||||
@ -97,10 +103,16 @@ async fn process(mut inbound: TcpStream, upstream: &str) -> Result<(), Box<dyn s
|
||||
let inbound_to_outbound = copy(&mut ri, &mut wo);
|
||||
let outbound_to_inbound = copy(&mut ro, &mut wi);
|
||||
|
||||
let (bytes_tx, bytes_rx) = 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);
|
||||
|
||||
}
|
||||
_ => {
|
||||
error!("Reached unknown protocol: {:?}", custom.protocol);
|
||||
}
|
||||
},
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -11,21 +11,26 @@ servers:
|
||||
proxy.test.com: proxy
|
||||
www.test.com: web
|
||||
default: ban
|
||||
echo_server:
|
||||
listen:
|
||||
- "0.0.0.0:54956"
|
||||
default: echo
|
||||
tcp_server:
|
||||
listen:
|
||||
- "127.0.0.1:54500"
|
||||
default: tester
|
||||
tcp_echo_server:
|
||||
listen:
|
||||
- "0.0.0.0:54956"
|
||||
default: echo
|
||||
kcp_server:
|
||||
protocol: kcp
|
||||
listen:
|
||||
- "127.0.0.1:54958"
|
||||
default: tester
|
||||
kcp_echo_server:
|
||||
protocol: kcp
|
||||
listen:
|
||||
- "127.0.0.1:54959"
|
||||
default: echo
|
||||
|
||||
upstream:
|
||||
web: "127.0.0.1:8080"
|
||||
proxy: "www.example.com:1024"
|
||||
tester: "127.0.0.1:54599"
|
||||
web: "tcp://127.0.0.1:8080"
|
||||
proxy: "tcp://www.example.com:1024"
|
||||
tester: "tcp://127.0.0.1:54599"
|
Reference in New Issue
Block a user