Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

在 V2Ray 中加入自动选择延迟最小的 outbound 的负载均衡器

下载

https://github.com/eycorsican/v2ray-core/releases/tag/v4.13.0-latency-load-balancer-v3

从源码编译

首先 checkout 这两个分支

然后根据 这个页面 给出的步骤来编译,主要是下面这条命令:

bazel build --action_env=GOPATH=$GOPATH --action_env=PATH=$PATH //release:v2ray_linux_amd64_package

配置

    "routing": {
        "balancers": [
            {
                "tag": "proxy",
                "selector": [
                    "primary_proxy",
                    "backup_proxy"
                ],
                "strategy": "latency"
            }
        ],
        "rules": [
            {
                "network": "tcp,udp",
                "type": "field",
                "balancerTag": "proxy"
            }
        ]
    }

方法

启用 latency balancer 后,会每隔 30 秒对所有选中的 outbound 做速度测试,对每个 outbound 做 5 次测速,每次测速间隔 1 秒,最后记录下平均时间。每当需要代理请求时,就选择最后一次测速结果中延迟最低的 outbound 来用。

测速方式是使用对应 outbound 实际发出一个 HTTP 请求,目标地址是 www.google.com:80 ,请求内容如下:

HEAD / HTTP/1.1\r\n\r\n

服务器返回任何非空数据则视作连接没问题,并记录时间作为延迟时间。

使用场景

典型的使用场景是当你有一个速度很快的 VPS,但这个 VPS 偶尔会抽风一会,抽风期间你要切换使用另一个 VPS(可能是比较慢的 VPS),这时你就用得上这个功能了。

@laventofly

This comment has been minimized.

Copy link

laventofly commented Jan 25, 2019

测实际连接上的时延会不会更好,比如用cdn的情况,TCP只是跟cdn连接,可能速度很快,但是cdn到服务器还要一些时间。

@eycorsican

This comment has been minimized.

Copy link
Owner Author

eycorsican commented Jan 26, 2019

是的,实测某个代理请求,比如用对应 outbound 发个 HTTP 请求到 www.google.com ,记录返回数据所需的时间,这样会好很多,后面可能会实现下,暂时来说个人没太大需求。

@aliyuchang33

This comment has been minimized.

Copy link

aliyuchang33 commented Apr 2, 2019

能加上权重功能么,因为某些像azure这样流量计费的机子,虽然网络好,但是流量贵,大流量的比如视频走普通线路。目前直接domain rule,但是只要普通线路服务器挂了就没办法访问视频了。

@zenyanbo

This comment has been minimized.

@zenyanbo

This comment has been minimized.

Copy link

zenyanbo commented Jun 4, 2019

可以不用bazel编译吗?bazel各种问题搞不定

@zenyanbo

This comment has been minimized.

Copy link

zenyanbo commented Jun 4, 2019

bazel和直接编译,都显示包缺一些东西。比如go.starlark.net/starlark.net之类的。而用这两种方法我编译官方的包都成功了

@eycorsican

This comment has been minimized.

Copy link
Owner Author

eycorsican commented Jun 4, 2019

@zenyanbo

This comment has been minimized.

Copy link

zenyanbo commented Jun 4, 2019

意思是以后没有这功能了吗?

@eycorsican

This comment has been minimized.

Copy link
Owner Author

eycorsican commented Jun 4, 2019

@zenyanbo

This comment has been minimized.

Copy link

zenyanbo commented Jun 4, 2019

大佬,那可不可以放编译好的包?我觉得这功能真的很实用呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.