Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

V2Ray Router

基于 Loyalsoldier/v2ray-rules-dat 的 Rust 分流路由器库。

功能

  • 域名分流(代理/直连/拒绝)
  • IP 地理位置分流
  • 支持从文件路径或字节切片加载数据
  • 使用 Loyalsoldier 的内置分类规则

安装

[dependencies]
v2ray-router = "0.1"

或使用 git:

git clone https://github.com/your-repo/v2ray-router.git
cd v2ray-router
cargo build --release

使用方法

方法 1: 从文件路径加载

use v2ray_router::Router;

// 从文件路径创建路由器
let router = Router::from_paths("data/geosite.dat", "data/geoip.dat")?;

// 查询域名
let action = router.query_domain("www.google.com");
if action.is_proxy() {
    println!("走代理");
}

// 查询 IP
let ip: std::net::IpAddr = "8.8.8.8".parse()?;
let action = router.query_ip(ip);
match action {
    v2ray_router::RouteAction::Proxy => println!("代理"),
    v2ray_router::RouteAction::Direct => println!("直连"),
    v2ray_router::RouteAction::Reject => println!("拒绝"),
}

方法 2: 从字节数据加载

use v2ray_router::Router;

// 读取文件内容
let geosite_bytes = std::fs::read("data/geosite.dat")?;
let geoip_bytes = std::fs::read("data/geoip.dat")?;

// 从字节数据创建路由器
let router = Router::from_bytes(&geosite_bytes, &geoip_bytes)?;

// 使用路由器
let action = router.query_domain("www.youtube.com");

下载规则文件

Loyalsoldier/v2ray-rules-dat 下载:

# 使用提供的下载脚本
./download_rules.sh

# 或手动下载
wget https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat -O data/geosite.dat
wget https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat -O data/geoip.dat

路由规则

分类 动作 说明
CATEGORY-ADS-ALL, *ADS* 🚫 拒绝 广告域名
CN, GEOLOCATION-CN 🏠 直连 国内站点
GEOLOCATION-!CN 🔐 代理 国外站点
私有 IP 🏠 直连 内网地址

API 文档

Router

pub struct Router { }

impl Router {
    // 从文件路径创建路由器
    pub fn from_paths(geosite_path: &str, geoip_path: &str) -> Result<Self, Error>;

    // 从字节数据创建路由器
    pub fn from_bytes(geosite_bytes: &[u8], geoip_bytes: &[u8]) -> Result<Self, Error>;

    // 查询域名路由
    pub fn query_domain(&self, domain: &str) -> RouteAction;

    // 查询 IP 路由
    pub fn query_ip(&self, ip: IpAddr) -> RouteAction;
}

RouteAction

pub enum RouteAction {
    Proxy,   // 走代理
    Direct,  // 走直连
    Reject,  // 拒绝连接
}

impl RouteAction {
    pub fn is_proxy(&self) -> bool;
    pub fn is_direct(&self) -> bool;
    pub fn is_reject(&self) -> bool;
}

运行示例

# 运行演示程序
cargo run --bin dat-demo

# 运行使用示例
cargo run --example usage
cargo run --example from_bytes

# 构建库
cargo build --release

项目结构

v2ray-router/
├── src/
│   ├── lib.rs              # 库入口
│   ├── main.rs             # 演示程序
│   ├── proto/v2ray.proto   # Protobuf 定义
│   └── bin/                # 辅助工具
├── examples/               # 使用示例
├── data/                   # 数据文件目录
├── build.rs                # 构建脚本
└── Cargo.toml              # 项目配置

许可证

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages