[OpenWrt]V2Ray定制构建

Author Avatar
秋のかえで 5月 01, 2019
  • 在其它设备中阅读本文章

V2Ray这么一款爱国上网利器大家一定不陌生,苦在它是用Golang写的,编译出来的大小着实惊人。对于嵌入式设备来说,18M的它,几乎是没法放进小小的ROM。就算经过UPX压缩,也有6M的大小。

这时候,我们就可以通过定制自己的V2Ray来达到精简的目的了qwq

构建环境

OS:Linux/Mac OS/BSD(不推荐Windows)
Arch:x86_64
Depend:Golang Bazel
Network:能够科学地上网

安装Go和Bazel

不需要讲了,不会的可以左转
Golang
Bazel

拉取代码

go get -u v2ray.com/core/...

定制

修改$GOPATH/src/v2ray.com/core/main/distro/all/all.go,你可以参照我的代码进行修改。

package all

import (
    // The following are necessary as they register handlers in their init functions.

    // Required features. Can't remove unless there is replacements.
    _ "v2ray.com/core/app/dispatcher"
    _ "v2ray.com/core/app/proxyman/inbound"
    _ "v2ray.com/core/app/proxyman/outbound"

    // Default commander and all its services. This is an optional feature.
    _ "v2ray.com/core/app/commander"
    _ "v2ray.com/core/app/log/command"
    _ "v2ray.com/core/app/proxyman/command"
    _ "v2ray.com/core/app/stats/command"

    // Other optional features.
    // _ "v2ray.com/core/app/dns"
    // _ "v2ray.com/core/app/log"
    // _ "v2ray.com/core/app/policy"
    _ "v2ray.com/core/app/reverse"
    // _ "v2ray.com/core/app/router"
    // _ "v2ray.com/core/app/stats"

    // Inbound and outbound proxies.
    // _ "v2ray.com/core/proxy/blackhole"
    // _ "v2ray.com/core/proxy/dns"
    _ "v2ray.com/core/proxy/dokodemo"
    _ "v2ray.com/core/proxy/freedom"
    _ "v2ray.com/core/proxy/http"
    // _ "v2ray.com/core/proxy/mtproto"
    // _ "v2ray.com/core/proxy/shadowsocks"
    _ "v2ray.com/core/proxy/socks"
    _ "v2ray.com/core/proxy/vmess/inbound"
    _ "v2ray.com/core/proxy/vmess/outbound"

    // Transports
    _ "v2ray.com/core/transport/internet/domainsocket"
    _ "v2ray.com/core/transport/internet/http"
    _ "v2ray.com/core/transport/internet/kcp"
    _ "v2ray.com/core/transport/internet/quic"
    _ "v2ray.com/core/transport/internet/tcp"
    _ "v2ray.com/core/transport/internet/tls"
    _ "v2ray.com/core/transport/internet/udp"
    _ "v2ray.com/core/transport/internet/websocket"

    // Transport headers
    _ "v2ray.com/core/transport/internet/headers/http"
    _ "v2ray.com/core/transport/internet/headers/noop"
    // _ "v2ray.com/core/transport/internet/headers/srtp"
    _ "v2ray.com/core/transport/internet/headers/tls"
    _ "v2ray.com/core/transport/internet/headers/utp"
    _ "v2ray.com/core/transport/internet/headers/wechat"
    _ "v2ray.com/core/transport/internet/headers/wireguard"

    // JSON config support. Choose only one from the two below.
    // The following line loads JSON from v2ctl
    // _ "v2ray.com/core/main/json"
    // The following line loads JSON internally
    _ "v2ray.com/core/main/jsonem"

    // Load config from file or http(s)
    _ "v2ray.com/core/main/confloader/external"
)

值得注意的是如果你不需要WebSocket可以将它精简掉,这个东西非常占空间。
最后你可以修改$GOPATH/src/v2ray.com/core/core.go来修改一些信息,比如我把build改成了”AkinoKaede’s custom build”。

构建

格式为v2ray_os_isa_package,比如AMD64(x86_64)的Linux是v2ray_linux_amd64_package,但是mipsel的Linux却是v2ray_linux_mipsle_package

cd $GOPATH/src/v2ray.com/core
bazel build --action_env=GOPATH=$GOPATH --action_env=PATH=$PATH //release:v2ray_linux_amd64_package

最后会输出在bazel-bin/release/v2ray-linux-64.zip
将其解压出来,如果你引用了jsonem而不是json的话,不需要v2ctl,反之。mips(mipsel)架构有softfloat,部分没有FPU的架构建议使用这个(如mipsel_1004kc(如MT7621) mipsel_24kc(如MT7620))

UPX压缩

可极大减小体积,部分指令集可能不支持。

# 添加写执行权限
chmod +wx ./v2ray
upx --ultra-brute ./v2ray

最后就能愉快地食用啦

在无特别说明的情况下,本站文章均遵循 CC BY-NC-SA 4.0
本文链接:https://blog.iknet.top/post/v2ray-reduce-build.html