家用Nas中转v2ray并允许shadowsocks接入

为了防止2023年断更,特地水一篇文章。

最近在自己的 Nas 上搭建了 v2ray 并成功实现了免流的配置。既然已经安装了 v2ray,那为什么不用 Nas 作中转,这样不仅仅可以免流,还可以默认科学上网,说干就干。

Nas 搭建 v2ray

由于 v2ray 现在已经升级到 v2fly 了,但是我本身需要的功能很简单,并且由于 Nas 自带 docker,所以还是直接使用旧版本的 docker 来部署,这样省去再研究 v2fly 了,研究了下还是升级到v2fly,解决v2ray不稳定的问题。
由于群晖 docker 的注册表被墙了,但是还是可以用命令行来拉取镜像,所以 Nas 需要先开启 ssh,然后使用 ssh 登录到 Nas。
依次打开“控制面板” - “终端机和SNMP”,然后在“终端机”页勾选选择“启动 SSH 功能”,可以用默认22端口,如果修改了默认端口,在后面登录的时候记得加 -p 端口号

使用你的账号 ssh 登录到你的 Nas,如果你用的是 Windows,你可以下载 Putty这个小工具来登录,如果你用的是 macOS,你就可以直接用终端来登录。

ssh [email protected] 
# 提供输入密码,输入的字符会不显示,直接继续输入即可

拉取 v2ray 的镜像,我是用的旧版本的,根据我的观察,旧镜像停在 v4.22.1 版本,很多功能都已经有了,足够使用了,近期使用下来发现和最新的客户端会有一些不兼容的问题,所以推荐使用v2fly。

老版本v2ray:

sudo docker pull v2ray/official

新版本v2y:

sudo docker pull v2fly/v2fly-core

等待拉取 latest 镜像以后,你就可以在群晖自带的 docker 中看到它了,或者你也可以继续用命令行部署,为了方便手动把 geoip.dat 和 geosite.dat 这2个文件及时更新,我们也可以把他们映射出来。

老版本v2ray:

docker run -d --name v2ray --restart=always --network host \
-v /volume2/docker/v2ray/log/:/var/log/v2ray/ \
-v /volume2/docker/v2ray/config.json:/etc/v2ray/config.json \
-v /volume2/docker/v2ray/geoip.dat:/usr/bin/v2ray/geoip.dat \
-v /volume2/docker/v2ray/geosite.dat:/usr/bin/v2ray/geosite.dat \
v2ray/official:latest

新版本v2fly:

docker run -d --name v2ray --restart=always --network host \
-v /volume2/docker/v2ray/log/:/var/log/v2ray/ \
-v /volume2/docker/v2ray/config.json:/etc/v2ray/config.json \
-v /volume2/docker/v2ray/geoip.dat:/usr/local/share/v2ray/geoip.dat \
-v /volume2/docker/v2ray/geosite.dat:/usr/local/share/v2ray/geosite.dat \
v2fly/v2fly-core run -c /etc/v2ray/config.json

不管是通过群晖 docker 来部署,还是通过上述命令来部署,你都需要用到我提及的几个文件,可以下拉到文章最后自取。

此时你都可以在群晖中看到容器了。

其实在最终的 config.json 文件配置好之前,实际上我有折腾过好几个方案:

方案一:v2ray → v2ray → shadowsocks

由于之前已经有一台 Singapore 的 IPv6 VPS,所以本着少折腾的思路,就直接复用之前的 VPS,因为 v2ray 本身就支持中转。如果你也想最少改动,就可以按照我下面的这个 config.json 来,但是这个需要你的 VPS 对标准 shadowsocks 协议的支持:

{
    "log": {
        "access": "/var/log/v2ray/access.log",
        "error": "/var/log/v2ray/error.log",
        "loglevel": "error"
    },
    "dns": {
        "servers": [{
            "address": "119.29.29.29",
            "port": 53,
            "domains": ["geosite:cn"]
        }, {
            "address": "1.1.1.1",
            "port": 53,
            "domains": ["geosite:geolocation-!cn", "domain:raw.githubusercontent.com"]
        }]
    },
    "inbounds": [{
        "listen": "0.0.0.0",
        "port": 23456,                                        //和客户端设置的端口需要一致
        "protocol": "vmess",
        "settings": {
            "clients": [{
                "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", //和客户端设置的userId需要一致
                "alterId": 64
            }]
        },
        "streamSettings": {
            "network": "tcp",
            "tcpSettings": {
                "header": {
                    "type": "http",
                    "response": {
                        "version": "1.1",
                        "status": "200",
                        "reason": "OK",
                        "headers": {
                            "Content-Type": ["application/octet-stream", "video/mpeg"],
                            "Transfer-Encoding": ["chunked"],
                            "Connection": ["keep-alive"],
                            "Pragma": "no-cache"
                        }
                    }
                }
            }
        }
    }],
    "outbounds": [{
        "protocol": "shadowsocks",
        "tag": "proxy",
        "settings": {
            "servers": [{
                "address": "xxx.xxx.xxx.xxx",   //和VPS的IP地址需要一致
                "port": 12345,                  //和VPS的shadowsocks端口需要一致
                "method": "aes-256-cfb",        //和VPS的shadowsocks加密方式需要一致
                "password": "hello.world!",     //和VPS的shadowsocks密码需要一致
                "ota": false
            }]
        }
    }, {
        "protocol": "blackhole",
        "tag": "block",
        "settings": {}
    }, {
        "protocol": "freedom",
        "tag": "direct",
        "settings": {}
    }],
    "routing": {
        "strategy": "rules",
        "settings": {
            "rules": [{
                "type": "field",
                "outboundTag": "block",
                "domain": ["geosite:category-ads-all"]
            }, {
                "type": "field",
                "outboundTag": "direct",
                "domain": ["geosite:cn", "geosite:private", "geosite:apple-cn", "geosite:google-cn", "geosite:tld-cn", "geosite:geolocation-cn"]
            }, {
                "type": "field",
                "outboundTag": "proxy",
                "domain": ["geosite:geolocation-!cn"]
            }, {
                "type": "field",
                "outboundTag": "direct",
                "ip": ["geoip:cn", "geoip:private"]
            }]
        }
    }
}

方案二:shadowsocks → v2ray → v2ray

方案一运行了一段时间,实际上是勉强可用的,因为我原先是用的 shaodowsocks + kcptun 来实现加速的,因为单纯的 shadowsocks 速度实际上是非常慢的,如果按照方案一来跑的话,免流的目的是可以达到了,但是访问代理的速度还是很受限,后来翻看 v2ray 知道原来也支持 kcp 协议,所以就想到要么就彻底的把 VPS 也改为 v2ray 并在 Nas 和 VPS 之间使用 kcp 来加速,这样就满足了速度要求,同时为了兼容 shadowsocks 客户端的接入,我在 Nas 上中转配置中也加入了 shadowsocks 接入的支持,这样不仅仅支持 v2ray 的免流,可支持不需要免流的电脑来访问外网,因为电脑客户端 shadowsocks 还是非常方便的。

最终用到的参考文件:

1.Nas中转config.json

{
    "log": {
        "access": "/var/log/v2ray/access.log",
        "error": "/var/log/v2ray/error.log",
        "loglevel": "error"
    },
    "dns": {
        "servers": [{
            "address": "119.29.29.29",
            "port": 53,
            "domains": ["geosite:cn"]
        }, {
            "address": "1.1.1.1",
            "port": 53,
            "domains": ["geosite:geolocation-!cn", "domain:raw.githubusercontent.com"]
        }]
    },
    "inbounds": [{
        "port": 34567,                         //和客户端的shadowsocks端口需要一致
        "protocol": "shadowsocks",
        "settings": {
            "method": "aes-256-gcm",           //和客户端的shadowsocks加密方式需要一致
            "password": "hello.world!"         //和客户端的shadowsocks密码需要一致
        }
    }, {
        "listen": "0.0.0.0",
        "port": 23456,                                             //和客户端设置的端口需要一致
        "protocol": "vmess",
        "settings": {
            "clients": [{
                "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",      //和客户端设置的userId需要一致
                "alterId": 64
            }]
        },
        "streamSettings": {
            "network": "tcp",
            "tcpSettings": {
                "header": {
                    "type": "http",
                    "response": {
                        "version": "1.1",
                        "status": "200",
                        "reason": "OK",
                        "headers": {
                            "Content-Type": ["application/octet-stream", "video/mpeg"],
                            "Transfer-Encoding": ["chunked"],
                            "Connection": ["keep-alive"],
                            "Pragma": "no-cache"
                        }
                    }
                }
            }
        }
    }],
    "outbounds": [{
        "protocol": "vmess",
        "tag": "proxy",
        "settings": {
            "vnext": [{
                "address": "",
                "port": 12345,                                        //和VPS的端口需要一致
                "users": [{
                    "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",     //和VPS的userId需要一致
                    "alterId": 64
                }]
            }]
        },
        "streamSettings": {
            "network": "mkcp",
            "kcpSettings": {
                "uplinkCapacity": 5,
                "downlinkCapacity": 100,
                "congestion": true,
                "header": {
                    "type": "none"
                }
            }
        }
    }, {
        "protocol": "blackhole",
        "tag": "block",
        "settings": {}
    }, {
        "protocol": "freedom",
        "tag": "direct",
        "settings": {}
    }],
    "routing": {
        "strategy": "rules",
        "settings": {
            "rules": [{
                "type": "field",
                "outboundTag": "block",
                "domain": ["geosite:category-ads-all"]
            }, {
                "type": "field",
                "outboundTag": "direct",
                "domain": ["geosite:cn", "geosite:private", "geosite:apple-cn", "geosite:google-cn", "geosite:tld-cn", "geosite:geolocation-cn"]
            }, {
                "type": "field",
                "outboundTag": "proxy",
                "domain": ["geosite:geolocation-!cn"]
            }, {
                "type": "field",
                "outboundTag": "direct",
                "ip": ["geoip:cn", "geoip:private"]
            }]
        }
    }
}

2.VPS服务器config.json

{
    "inbounds": [{
        "port": 12345,                                         //和中转Nas的端口需要一致
        "protocol": "vmess",
        "settings": {
            "clients": [{
                "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",  //和中转Nas的userId需要一致
                "alterId": 64
            }]
        },
        "streamSettings": {
            "network": "mkcp",
            "kcpSettings": {
                "uplinkCapacity": 5,
                "downlinkCapacity": 100,
                "congestion": true,
                "header": {
                    "type": "none"
                }
            }
        }
    }],
    "outbounds": [{
        "protocol": "freedom",
        "settings": {}
    }, {
        "protocol": "blackhole",
        "settings": {},
        "tag": "blocked"
    }],
    "routing": {
        "rules": [{
            "type": "field",
            "ip": ["geoip:private"],
            "outboundTag": "blocked"
        }]
    }
}

3.geoip.dat
4.geosite.dat

1和2可以自行拷贝另存为文件,3和4都可以从github下载:
https://github.com/Loyalsoldier/v2ray-rules-dat/releases

标签:nas, shadowsocks, v2ray