Giter Site home page Giter Site logo

trojan-r's Introduction

Trojan-R

高性能的 Trojan 代理,使用 Rust 实现。为嵌入式设备或低性能机器设计。R 意为 Rust / Rapid。

Trojan-R 目前为实验性项目,仍处于重度开发中,协议、接口和配置文件格式均可能改变,请勿用于任何生产环境。

特性

  • 极致性能

    牺牲部分灵活性,采用激进的性能优化策略以极力减少不必要的开销。采用更高效rustls (相较 openssl)建立 TLS 隧道以提升加解密的性能表现。

    使用 tokio 异步运行时,允许 Trojan-R 同时使用所有 CPU 核心,保证低时延和高效的吞吐能力。

    需要更多 benchmark 数据和更多优化

  • 低内存占用

    Rust 无 GC 机制,内存占用可被预计。简化的握手和连接流程,仅使用极少的堆内存和复制。

    需要更多 benchmark 数据和更多优化

  • 简易配置

    使用 toml 格式配置,仅需数行配置即可启动完整客户端或服务器。

  • 内存安全

    使用 Rust 语言实现,可证明的内存安全性。在语法层面保证所有内存操作安全可靠。无竞争条件,无悬挂指针,无 UAF,无 Double Free。

  • 密码学安全

    使用 rustls 建立 TLS 加密安全信道,过时的或不安全的密码学套件均被禁用Trojan-R 强制开启服务器证书校验以防止中间人攻击。

  • 隐蔽传输

    Trojan-R 使用 TLS 建立代理隧道,难以从正常 TLS 流量中被区分。支持协议回落,在遭到主动探测时将与普通 TLS 服务器表现一致。

  • 跨平台支持

    Trojan-R 可被交叉编译,支持 Android, Linux,Windows 和 MacOS 等操作系统,以及 x86,x86_64,armv7,aarch64 等硬件平台。

非特性

由于与项目的设计原则冲突,下列特性不计划实现

  • 统计功能,包括 API 和数据库对接等

  • 路由功能

  • 用户自定义协议栈

  • 透明代理

如果需要实现上述功能,请使用其他类似工具与 Trojan-R 组合实现。

设计原则

  • 安全性

    Trojan-R 不涉及底层操作,且目前的性能瓶颈与其无关,无使用 unsafe rust 的必要。协议回落和 TLS 配置等安全敏感代码经过仔细考虑和审计,同时也欢迎更多来自开源社区的安全审计。

    目前 Trojan-R 使用 #![forbid(unsafe_code)] 禁用 unsafe rust。如未来有必要使用 unsafe rust 时,必须经过严格审计和测试。

  • 使用静态分发而非动态分发

    协议实现使用统一的 trait。协议嵌套使用静态分发,以保证嵌套协议栈的函数调用关系在编译时被确定,使编译器可以进行内联和更好的优化。

  • 低内存分配

    减少热点代码的内存分配,用引用替换复制,以实现更高的性能和更低的内存开销。

  • 简洁

    保持最简洁干净的实现,以保证最低的代码复杂度,尽可能少的性能开销,并增加可靠性和减少攻击面。

部署和使用

Trojan-R 使用 toml 进行配置,参考 config 文件夹下配置文件。

编译

cargo build --release

交叉编译基于 cross 完成,编译前请确认已经安装 cross (cargo install cross)

make armv7-unknown-linux-musleabihf

编译默认开启链接时优化,以提升性能并减小可执行文件体积,因此编译耗时可能较其他项目更长。

编译完成后可以使用 strip 去除调试符号表以减少文件体积。

TODOs

  • 更完善的交互接口和文档

  • 更多的单元测试和集成测试

  • 性能调优

  • 可复现的 benchmark 环境

  • 实现 lib.rs 和导出函数

  • 分离客户端和服务端 features

  • Github Actions

致谢

trojan-r's People

Contributors

p4gefau1t 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

trojan-r's Issues

使用mux后udp请求错误

r版服务端日志
[2021-03-04T02:08:15Z ERROR trojan_r::proxy] failed to relay tcp connection to UDP_CONN:0: failed to lookup address information: Temporary failure in name resolution
[2021-03-04T02:08:18Z WARN trojan_r::protocol::mux] invalid frame recvd, stream_id=8
tcp能正常

relay_tcp err: Connection reset by peer (os error 104)

服务端:trojan-r
客户端: ios - shadowsokets
没开 mux

direct-connector 在第二次发起目标服务器请求时(target) 复用了 session?
目标服务器已经关闭了连接,此时做任何 read write 的操作, 都会引发 104错误(socket 已关闭)
或者说? 客户端的 stream被干扰了??

但同一台服务器, trojan-go 的流量正常, trojan-r 的流量不正常,

2021-04-13T17:57:53Z INFO  trojan_r::protocol::tls::acceptor] tcp connection from 120.229.21.18:6260
[2021-04-13T17:57:53Z DEBUG rustls::server::hs] decided upon suite SupportedCipherSuite { suite: TLS_ECDHE_ECDSA_WITH_CHACHA2                                   0_POLY1305_SHA256, kx: ECDHE, bulk: CHACHA20_POLY1305, hash: SHA256, sign: Some([ED25519, ECDSA_NISTP521_SHA512, ECDSA_NISTP3                                   84_SHA384, ECDSA_NISTP256_SHA256]), enc_key_len: 32, fixed_iv_len: 12, explicit_nonce_len: 0 }
[2021-04-13T17:57:53Z DEBUG rustls::server::hs] Resuming session
[2021-04-13T17:57:54Z INFO  trojan_r::protocol::trojan::acceptor] trojan tcp stream www.youtube.com:443
[2021-04-13T17:57:54Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to www.youtube.com:443
[2021-04-13T17:57:54Z INFO  trojan_r::proxy] relaying tcp stream to www.youtube.com:443
[2021-04-13T17:57:54Z DEBUG trojan_r::proxy] relay_tcp err: Connection reset by peer (os error 104)
[2021-04-13T17:57:54Z DEBUG rustls::session] Sending warning alert CloseNotify
[2021-04-13T17:57:54Z INFO  trojan_r::proxy] tcp session ends

[2021-04-13T17:57:31Z INFO  trojan_r::protocol::tls::acceptor] tcp connection from 120.229.21.18:6247
[2021-04-13T17:57:31Z DEBUG rustls::server::hs] decided upon suite SupportedCipherSuite { suite: TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, kx: ECDHE, bulk: CHACHA20_POLY1305, hash: SHA256, sign: Some([ED25519, ECDSA_NISTP521_SHA512, ECDSA_NISTP384_SHA384, ECDSA_NISTP256_SHA256]), enc_key_len: 32, fixed_iv_len: 12, explicit_nonce_len: 0 }
[2021-04-13T17:57:31Z DEBUG rustls::server::tls12] Session saved
[2021-04-13T17:57:31Z INFO  trojan_r::protocol::trojan::acceptor] trojan tcp stream play.googleapis.com:443
[2021-04-13T17:57:31Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to play.googleapis.com:443
[2021-04-13T17:57:31Z INFO  trojan_r::proxy] relaying tcp stream to play.googleapis.com:443
[2021-04-13T17:57:32Z DEBUG trojan_r::proxy] relay_tcp err: Connection reset by peer (os error 104)
[2021-04-13T17:57:32Z DEBUG rustls::session] Sending warning alert CloseNotify
[2021-04-13T17:57:32Z INFO  trojan_r::proxy] tcp session ends

内存泄漏

本地回环压力测试,server和clinet选择log等级不一样,有概率出现内存泄漏
TIM截图20201217002813
TIM截图20201217002827
复现成功概率大概30%左右吧

交叉编译出错

交叉编译 ar71xx 芯片的openwrt 系统路由器用trojan( Go语言的太大了,看介绍这个小巧),结果无法通过。
错误如下:
error: failed to run custom build command for ring v0.16.15
....
thread 'main' panicked at 'called Option::unwrap() on a None value', /home/root/.cargo/registry/src/github.com-1ecc6299db9ec823/ring-0.16.15/build.rs:378:10
不知如何解决。

服务端无法处理rtmp流数据

作为服务端无法中继twitch的rtmp流

[2021-04-01T10:20:32Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to 52.223.247.225:1935
[2021-04-01T10:20:32Z INFO  trojan_r::proxy] relaying tcp stream to [2607:f8b0:4007:80c::200c]:1935
[2021-04-01T10:20:32Z INFO  trojan_r::proxy] relaying tcp stream to 52.223.247.225:1935
[2021-04-01T10:20:36Z DEBUG trojan_r::protocol::mux::acceptor] new mux stream 6 accepted
[2021-04-01T10:20:36Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to 91.108.56.176:80
[2021-04-01T10:20:36Z INFO  trojan_r::proxy] relaying tcp stream to 91.108.56.176:80
[2021-04-01T10:20:39Z DEBUG trojan_r::protocol::mux::acceptor] new mux stream 7 accepted
[2021-04-01T10:20:39Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to [2607:f8b0:4023:c03::bc]:443
[2021-04-01T10:20:39Z INFO  trojan_r::proxy] relaying tcp stream to [2607:f8b0:4023:c03::bc]:443
[2021-04-01T10:20:42Z DEBUG trojan_r::protocol::mux::acceptor] new mux stream 8 accepted
[2021-04-01T10:20:42Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to 91.108.56.176:80
[2021-04-01T10:20:42Z INFO  trojan_r::proxy] relaying tcp stream to 91.108.56.176:80
[2021-04-01T10:20:43Z DEBUG trojan_r::protocol::mux::acceptor] new mux stream 9 accepted
[2021-04-01T10:20:43Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to 52.88.36.248:443
[2021-04-01T10:20:43Z INFO  trojan_r::proxy] relaying tcp stream to 52.88.36.248:443
[2021-04-01T10:21:03Z INFO  trojan_r::proxy] tcp session ends
[2021-04-01T10:21:03Z DEBUG trojan_r::protocol::mux] local shutdown stream 4
[2021-04-01T10:21:06Z DEBUG trojan_r::proxy] relay_tcp err: Connection reset by peer (os error 104)
[2021-04-01T10:21:06Z INFO  trojan_r::proxy] tcp session ends
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] frame recvd but the stream 5 is closed
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] echo finish frame 5
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 4
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 5
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 4
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 5
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 4
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 5
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 4
[2021-04-01T10:21:06Z DEBUG trojan_r::protocol::mux] invalid frame recvd, stream_id = 5

无mux:

[2021-04-01T10:33:45Z DEBUG tungstenite::handshake::server] Server handshake done.
[2021-04-01T10:33:45Z INFO  trojan_r::protocol::trojan::acceptor] trojan tcp stream 52.223.247.183:1935
[2021-04-01T10:33:45Z DEBUG trojan_r::protocol::direct::connector] direct: connecting to 52.223.247.183:1935
[2021-04-01T10:33:45Z INFO  trojan_r::proxy] relaying tcp stream to 52.223.247.183:1935
[2021-04-01T10:33:47Z DEBUG tungstenite::handshake::server] Server handshake done.

开mux无法正常推流 关闭mux会间接性中断
image

请问目前trojan-r是否不支持ECC算法的TLS证书?

如题,请问目前是否不支持ECC算法的TLS证书,在服务器侧运行时,提示
failed to launch proxy: tls: no valid key found
如果使用早期申请的普通算法证书,则trojan-r可以正常运行。
同时该ECC算法的证书,可以在trojan-go和xray下使用
所以怀疑是否trojan-r暂时不支持ECC算法的TLS证书?

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.