Giter VIP home page Giter VIP logo

trojanrust's Introduction

trojan-rust

Build Stage

中文zh-CN

Trojan-rust is a rust implementation for Trojan protocol that is targeted to circumvent GFW. This implementation focus on performance and stability above everything else.

Why trojan-rust

  • Depends on tokio-rs to achieve high performance async io. Tokio io provides better async IO performance by using lightweight threads that is somewhat similar to the runtime environment of Golang.

  • Uses rustls to handle TLS protocol. rustls is an implemention written in native rust, and is considered to be more secure compared and performant compared to Openssl implementation.

  • Performance focused. This implementation only aims at a few mainstream proxy protocols like Trojan protocol, so that we have more capacity to improve the performance and bugfixes rather than keep adding useless features.

  • Easy to use/configure. Make this project beginner friendly, minimize the amount of configurations one needs to write.

How to compile

Currently there is no existing binary file that you can just download and use, and it is recommanded to compile and build yourself. To do so, first you need to set up the Rust environment, by installing through here https://www.rust-lang.org/. Once you have rust installed, you can simply go to command line and run,

cargo build --release

and it should generate a binary program under ./target/release/trojan-rust.

Alternatively, you can also run it directly through,

cargo run --release

To enable logs, on MacOs or Linux, run,

RUST_LOG=info cargo run --release

On windows powershell, run,

$Env:RUST_LOG = "info"
cargo run --release

Examples

Create Certificate

Quick short script for your convenience,

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes

Sample for Trojan

Server config

{
    "inbound": {
        "protocol": "TROJAN",
        "address": "0.0.0.0",
        "secret": "123123",
        "port": 8081,
        "mode": "TCP",
        "tls": {
            "cert_path": "./cert.pem",
            "key_path": "./key.pem"
        }
    },
    "outbound": {
        "protocol": "DIRECT"
    }
}

Client config

{
    "inbound": {
        "protocol": "SOCKS",
        "address": "0.0.0.0",
        "port": 8081
    },
    "outbound": {
        "protocol": "TROJAN",
        "address": "0.0.0.0",
        "port": 8082,
        "secret": "123123",
        "mode": "TCP",
        "tls": {
            "host_name": "example.com",
            "allow_insecure": true
        }
    }
}

For using GRPC as transport layer

Just add GRPC to transport under inbound or outbound

    "inbound": {
        "protocol": "TROJAN",
        "address": "0.0.0.0",
        "secret": "123123",
        "port": 8081,
        "tls": {
            "cert_path": "./cert.pem",
            "key_path": "./key.pem"
        },
        "mode": "GRPC"
    },
    "outbound": {
        "protocol": "DIRECT"
    }

Run the program

trojan-rust -h

Trojan Rust 0.0.1
Anonymous
Trojan Rust is a rust implementation of the trojan protocol to circumvent GFW

USAGE:
    trojan-rust [OPTIONS]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -c, --config <FILE>    Sets the config file, readers ./config/config.json by default

Run trojan-rust with specified config file

trojan-rust --config ./config.json

Roadmap

Beta stage 0.0.1 - 1.0.0(For developers)

  • Build up the framework for this project and support basic server side SOCKS5 protocol.

  • Support server side Trojan protocol for handling Trojan traffic.

  • Implement UDP over TCP for Trojan protocol on server side.

  • Implement client side Trojan protocol so that trojan-rust and be used as a Trojan client. - Work in progress.

    • Implement client side Trojan protocol with TCP
    • Implement client side Trojan protocol with TLS
    • -[Delayed After Beta] Implement client side Trojan protocol with UDP over TCP.
  • Performance profiling and bottleneck resolving. Will also include benchmarks versus other implementations. (Benchmark report coming up soon)

Official release 0.4.0 and above(For general users)

  • Improve client mode performance.

  • Implement gRPC for transporting data

  • +[Delayed After Beta] Implement client side Trojan protocol with UDP over TCP.

  • Build the package into kernel module release

  • Support other protocols, gRPC, websocket etc.

trojanrust's People

Contributors

cty123 avatar netheril96 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

trojanrust's Issues

Multiple secrets

Both trojan and trojan-go support multiple secret at least on the server. Will this support it too?

请问客户端用Clash怎么配置,用Trojan的模板好像不行

服务端运行起来了,客户端用Clash,按之前Trojan的模板测试不行,配置如下

  • name: "Trojan-RS"
    type: trojan
    server: ...
    port: 443
    password: "*"
    sni: "www.
    .com"
    skip-cert-verify: true

PS,服务端的配置sample少了个 mode参数,我配置成
“mode”: "TCP"
其它参照模板配置,不知道有没有影响

Other protocols config.json templates

Thanks for this, i was able to run client using Sagernet (Trojan) But would appreciate if you can expand more on grpc and websocket config.json templates.

thanks

discarding possible duplicate packet

我发现当使用QUIC + Trojan 一段时间后,总是会报警告:

[2023-02-20T06:27:40Z WARN quinn_proto::connection] discarding possible duplicate packet

出现这个警告时,延迟会高出许多,体验上感觉速度变慢了。并且这个警告一旦出现,每次发生数据传输都会
发生该警告。这个问题 leaf 也存在。见 eycorsican/leaf#349
ps: 我使用xray作为客户端

代码写的真漂亮,代码架构真优秀

在尝试把reqwest 与此进行结合,通过阅读理解其实现过程,发现代码写的如此简洁、架构设计的如此清晰,让我理解起来很顺畅,这代码很适合我这种新手(10年前后端开发经验)学习rust和网络编程,膜拜大神~

请问当前版本QUIC协议是否可用

你好,你代码写的真漂亮,尤其代码设计很清晰,请问当前版本QUIC协议还在完善中吗?我在mac中测试启动server 发现设置的端口未启动。配置如下:

{
    "inbound": {
        "protocol": "TROJAN",
        "address": "0.0.0.0",
        "secret": "123123",
        "port": 8032,
        "tls": {
            "cert_path": "./config/cert.pem",
            "key_path": "./config/key.pem"
        },
        "mode": "QUIC"
    },
    "outbound": {
        "mode": "DIRECT",
        "protocol": "DIRECT"
    }
}

alpn support

能否添加quic 服务端alpn的支持以兼容*ray,sing-box。现在使用xray的trojan + quic作为客户端,会报alpn错误:

[2023-02-12T17:01:39Z WARN rustls::conn] Sending fatal alert NoApplicationProtocol

抓包发现 *ray,sing-box的trojan + quic client 实现总是会有alpn拓展

小白问题,怎么作为client使用?

好奇使用这个的正确姿势是啥?比如服务商提供了trojan配置,对应这个的配置需要怎么配置?
可以使用这个的client模式直接连 服务商的trojan服务吗

编译ARM64版本失败

一、编译环境
Distributor ID: Ubuntu
Description: Ubuntu 22.04.2 LTS
Release: 22.04
Codename: jammy

Linux version 5.15.0-1030-oracle (buildd@bos02-arm64-061) (gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #36-Ubuntu SMP Wed Feb 15 05:57:14 UTC 2023

二、编译过程及错误信息
/tmp/TrojanRust# cargo build --release
Compiling serde_json v1.0.64
Compiling quinn v0.9.3
Compiling trojan-rust v0.7.2 (/tmp/TrojanRust)
Compiling constant_time_eq v0.2.4
error: failed to run custom build command for trojan-rust v0.7.2 (/tmp/TrojanRust)

Caused by:
process didn't exit successfully: /tmp/TrojanRust/target/release/build/trojan-rust-8bbe07b6d9abc75a/build-script-build (exit status: 101)
--- stdout
cargo:rerun-if-changed=proto/transport.proto
cargo:rerun-if-changed=proto

--- stderr
thread 'main' panicked at '
Could not find protoc installation and this build crate cannot proceed without
this knowledge. If protoc is installed and this crate had trouble finding
it, you can set the PROTOC environment variable with the specific path to your
installed protoc binary.

For more information: https://docs.rs/prost-build/#sourcing-protoc
', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/prost-build-0.11.0/src/lib.rs:1227:10
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...

Multiple secrets

Both trojan and trojan-go support multiple secret at least on the server. Will this support it too?

How do I set the “serviceName” of gRPC using XRAY as a proxy client?

Hello,

If I use this project as a proxy server and XRAY as a proxy client, how do I configure XRAY's serviceName to make it work?

grpcSettings
{
  "serviceName": "name",
  "multiMode": false,
  "idle_timeout": 60,
  "health_check_timeout": 20,
  "permit_without_stream": false,
  "initial_windows_size": 0
}

Thank you.

protoc

Could not find protoc installation and this build crate cannot proceed without
this knowledge. If protoc is installed and this crate had trouble finding
it, you can set the PROTOC environment variable with the specific path to your
installed protoc binary.

互操作问题

使用caddy-trojan搭建的服务端,客户端使用各种客户端连接,都很正常。
唯独TrojanRust无法连接上用caddy-trojan搭建的服务端。

Close connection faster to save memory

This project seems not to close connections fast enough, which could be important for this kind of servers.

In my server with modest (dozen) number of users the number of open files of trojan is high:
root@srv1667:~# lsof -s | grep trojan | wc returns 20913

The cpu usage is low but the memory usage is relatively high. I list the open files, most of ip address (each connected mobile device) have hundreds of open tcp connections (with different ports ofc). Most of them seems to be short lived tcp connections.
For a project like this it would be better to timeout connections faster to save resources, I believe most of this kind of servers had a short timeout.

bug report

  • Debian GNU/Linux 10 (buster) x86_64

  • Linux 201 5.10.0-0.bpo.12-amd64 SMP Debian 5.10.103-1~bpo10+1 (2022-03-08) x86_64 GNU/Linux

  • admin:~/trojanrust$ ./trojan_rust_linux_x86_64 -V
    Trojan Rust 0.7.1

  • only -V -h worked, -c or config not working

log

./trojan_rust_linux_x86_64 -c config.json thread 'main' panicked at 'Error parsing the config file: Custom { kind: InvalidData, error: Error("missing fieldmode", line: 15, column: 5) }', src/main.rs:29:48 stack backtrace: 0: rust_begin_unwind at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:584:5 1: core::panicking::panic_fmt at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/panicking.rs:142:14 2: core::result::unwrap_failed at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1785:5 3: std::sync::once::Once::call_once::{{closure}} 4: std::sync::once::Once::call_inner at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sync/once.rs:434:21 5: trojan_rust::main::{{closure}} 6: std::thread::local::LocalKey<T>::with 7: tokio::park::thread::CachedParkThread::block_on 8: tokio::runtime::thread_pool::ThreadPool::block_on 9: tokio::runtime::Runtime::block_on 10: trojan_rust::main note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.

./trojan_rust_linux_x86_64 thread 'main' panicked at 'Error parsing the config file: Custom { kind: InvalidData, error: Os { code: 2, kind: NotFound, message: "No such file or directory" } }', src/main.rs:29:48 stack backtrace: 0: rust_begin_unwind at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:584:5 1: core::panicking::panic_fmt at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/panicking.rs:142:14 2: core::result::unwrap_failed at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1785:5 3: std::sync::once::Once::call_once::{{closure}} 4: std::sync::once::Once::call_inner at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sync/once.rs:434:21 5: trojan_rust::main::{{closure}} 6: std::thread::local::LocalKey<T>::with 7: tokio::park::thread::CachedParkThread::block_on 8: tokio::runtime::thread_pool::ThreadPool::block_on 9: tokio::runtime::Runtime::block_on 10: trojan_rust::main note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.

./trojan_rust_linux_x86_64 thread 'main' panicked at 'Error parsing the config file: Custom { kind: InvalidData, error: Os { code: 2, kind: NotFound, message: "No such file or directory" } }', src/main.rs:29:48 stack backtrace: 0: 0x5637ff824cad - std::backtrace_rs::backtrace::libunwind::trace::h8e036432725b1c57 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x5637ff824cad - std::backtrace_rs::backtrace::trace_unsynchronized::h4f83092254c85869 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x5637ff824cad - std::sys_common::backtrace::_print_fmt::h9728b5e056a3ece3 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sys_common/backtrace.rs:66:5 3: 0x5637ff824cad - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h48bb4bd2928827d2 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sys_common/backtrace.rs:45:22 4: 0x5637ff846d2c - core::fmt::write::h909e69a2c24f44cc at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/fmt/mod.rs:1196:17 5: 0x5637ff81ef01 - std::io::Write::write_fmt::h7f4b8ab8af89e9ef at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/io/mod.rs:1654:15 6: 0x5637ff826485 - std::sys_common::backtrace::_print::hff4838ebf14a2171 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sys_common/backtrace.rs:48:5 7: 0x5637ff826485 - std::sys_common::backtrace::print::h2499280374189ad9 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sys_common/backtrace.rs:35:9 8: 0x5637ff826485 - std::panicking::default_hook::{{closure}}::h8b270fc55eeb284e at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:295:22 9: 0x5637ff8260f9 - std::panicking::default_hook::h3217e229d6e9d13c at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:314:9 10: 0x5637ff826a58 - std::panicking::rust_panic_with_hook::h9acb8048b738d2e0 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:698:17 11: 0x5637ff826907 - std::panicking::begin_panic_handler::{{closure}}::h70f3b839526af6dc at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:588:13 12: 0x5637ff825164 - std::sys_common::backtrace::__rust_end_short_backtrace::h1ecf2cee857fbe0a at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sys_common/backtrace.rs:138:18 13: 0x5637ff826639 - rust_begin_unwind at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:584:5 14: 0x5637ff351543 - core::panicking::panic_fmt::h9f8393e7fd56d655 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/panicking.rs:142:14 15: 0x5637ff351633 - core::result::unwrap_failed::h8b88eb424b1f8daa at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/result.rs:1785:5 16: 0x5637ff409be3 - std::sync::once::Once::call_once::{{closure}}::h3cb1026e9fb8bcc3 17: 0x5637ff3505ba - std::sync::once::Once::call_inner::h7e00b07cfc9177b0 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/sync/once.rs:434:21 18: 0x5637ff3b6af6 - trojan_rust::main::{{closure}}::h3633020e640aecfe 19: 0x5637ff42a73d - std::thread::local::LocalKey<T>::with::h1b1dd2cae74fc656 20: 0x5637ff4715da - tokio::park::thread::CachedParkThread::block_on::h6d70c2d359acf05f 21: 0x5637ff473c44 - tokio::runtime::thread_pool::ThreadPool::block_on::ha23a5f4056865696 22: 0x5637ff4cb0c3 - tokio::runtime::Runtime::block_on::he74b89146da0678b 23: 0x5637ff47ae3f - trojan_rust::main::he88ca6177664e157 24: 0x5637ff45d733 - std::sys_common::backtrace::__rust_begin_short_backtrace::h42c4215eb2aafe4b 25: 0x5637ff4b54ed - std::rt::lang_start::{{closure}}::hbdfb6706f1314abb 26: 0x5637ff819e3e - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h5f1ba3b861cac230 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/core/src/ops/function.rs:280:13 27: 0x5637ff819e3e - std::panicking::try::do_call::h4febfdb770eca39d at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:492:40 28: 0x5637ff819e3e - std::panicking::try::h8b0eac8a7d726dbf at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:456:19 29: 0x5637ff819e3e - std::panic::catch_unwind::h11b83c489c0d8394 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panic.rs:137:14 30: 0x5637ff819e3e - std::rt::lang_start_internal::{{closure}}::h63502d2988634103 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/rt.rs:128:48 31: 0x5637ff819e3e - std::panicking::try::do_call::h4be475cff12d2aae at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:492:40 32: 0x5637ff819e3e - std::panicking::try::h1a3b25b8eaf9ba31 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panicking.rs:456:19 33: 0x5637ff819e3e - std::panic::catch_unwind::h224588ada67b9b0b at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/panic.rs:137:14 34: 0x5637ff819e3e - std::rt::lang_start_internal::h2807b375c1959759 at /rustc/e092d0b6b43f2de967af0887873151bb1c0b18d3/library/std/src/rt.rs:128:20 35: 0x5637ff47af22 - main 36: 0x7f550da5209b - __libc_start_main at /build/glibc-6iIyft/glibc-2.28/csu/../csu/libc-start.c:308:16 37: 0x5637ff3517ee - _start 38: 0x0 - <unknown>

QUIC BBR support

能不能增加 QUIC 的 BBR 支持,TUIC 支持 QUIC BBR,使用起来速度很快。
如果 trojan + quic 将拥塞控制改为bbr应该也可以很快。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.