blog-site

git clone git://git.lin.moe/blog-site.git

  1---
  2title: "V2ray 的内网访问策略"
  3date: 2021-01-16T20:53:43+08:00
  4draft: true
  5---
  6
  7## 用处
  8笔者有一些 web 服务是不便于或是不适合暴露在公网上的,包括
  91. 文件存储服务
 102. 密码管理器
 113. jupyter ( web 文件编辑器 )
 12
 13这些东西虽然都可以上密码,但是暴露在公网必然会存在一些风险。特别像是密码管理器,无论是服务漏洞,还是密码遭破解,都是致命性的事件。  
 14企业的解决方法一般是内网隔离,外部用 VPN 进入内网。为了方便,可能还会维护一个内网 dns 来提供解析内部域名。  
 15但这样会要额外维护一到两个套软件,故笔者考虑在已经搭建好的 v2ray 上写上路由,用 v2ray 来代替大部分 VPN 和内网 dns 的功能。  
 16这样只会需要将 v2ray 的端口暴露到公网。
 17
 18## 前提
 19* 本文默认读者熟悉 v2ray 的一般配置,参考[官方文档](https://guide.v2fly.org/)
 20* 在目标内网,需要一个能长期运行的 v2ray 服务
 21* 需要拥有一个公网 ip ,并能将 v2ray 的监听端口绑定到这个公网 ip 上。建议使用家庭带宽的动态公网 ip 。  
 22  虽然可以通过 [v2ray 的内网穿透功能](https://www.v2fly.org/config/reverse.html) 配合公网服务器来到达类似的功能,但配置更为复杂,且效率较低,故本文不介绍此种方式,如有需求,请自行配置解决。
 23
 24## v2ray 的 dns 分流
 25关于 v2ray 的 dns 分流,可参考 [这篇文章](https://medium.com/@TachyonDevel/%E6%BC%AB%E8%B0%88%E5%90%84%E7%A7%8D%E9%BB%91%E7%A7%91%E6%8A%80%E5%BC%8F-dns-%E6%8A%80%E6%9C%AF%E5%9C%A8%E4%BB%A3%E7%90%86%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8-62c50e58cbd0) 。  
 26简述如下  
 271. 添加 dns inbounds:
 28```
 29{
 30    "port": 53,
 31    "tag": "dns-in",
 32    "listen": "0.0.0.0",
 33    "protocol": "dokodemo-door",
 34    "settings": {
 35        "address": "233.5.5.5",
 36        "port": 53,
 37        "network": "tcp,udp"
 38    }
 39}
 40``` 
 412. 添加 dns outbounds
 42```
 43{
 44    "tag": "dns-out",
 45    "protocol": "dns",
 46    "streamSettings": {
 47        "sockopt": {
 48            "mark": 255
 49        }
 50    }
 51}
 52```
 533. 添加 dns 解析策略
 54```
 55"dns": {
 56    "hosts": {
 57        "pass.lin.moe": "172.233.233.1",
 58        "read.lin.moe": "172.233.233.1",
 59        "note.lin.moe": "172.233.233.1",
 60        "st.lin.moe": "172.233.233.1"
 61    },
 62    "servers": [
 63        "1.1.1.1",
 64        "8.8.8.8",
 65        // 以上两条 dns 会通过前置代理转发,以应对 dns 污染。如没有国外前置代理,请将上述两条替换为国内 dns
 66        {
 67        "address": "223.5.5.5",
 68        "port": 53,
 69        "domains": [
 70            "geosite:cn",
 71            "ntp.org"
 72        ]
 73        }
 74    ]
 75}
 76```
 774. 添加 dns 路由
 78```
 79{
 80    "type": "field",
 81    "inboundTag": [
 82    "dns-in"
 83    ],
 84    "outboundTag": "dns-out"
 85}
 86```
 87    
 88## 服务端入口
 89(这里的服务端指内网入口所提供的服务)  
 90服务端入口配置与一般的 v2ray inbound 配置无异,建议使用 vmess 等加密协议,而不是使用 http 或 socks 等,以免被恶意利用。  
 91示例如下:
 92```
 93{
 94    "port": 22233,
 95    "listen": "0.0.0.0",
 96    "protocol": "vmess",
 97    "settings": {
 98    "clients": [
 99        {
100        "id": "96ce0eea-ffa5-4e94-bb68-2e42cbacbd1d",
101        "level": 1,
102        "alterId": 16
103        }
104    ],
105    "disableInsecureEncryption": true
106    }
107}
108```
109之后将这个 inbound 入口端口(示例中为 22233)暴露到公网。  
110服务端的路由可以直接写一个 freedom 协议出口处理所有流量。  
111也可以通过 v2ray 自身提供的 geoip 和 geosite 文件改写路由,来满足访问国外网络等需求,这里不做过多赘述。
112
113
114## 客户端路由与 dns 配置
115一般情况下的 v2ray 会添加 nat ip 和国内 ip 直连的路由策略,示例如下
116```
117{
118    "type": "field",
119    "outboundTag": "direct",
120    "ip": [
121        "geoip:cn",
122        "geoip:private"
123    ]
124}
125```
126为了使 nat ip 也通过代理转发,在上条规则之前添加
127```
128{
129    "type": "field",
130    "outboundTag": "home",
131    // home 为客户端出口标签
132    "ip": [
133        "172.233.233.0/24"
134        // 这里改为需要通过代理内网 ip 网段 
135    ]
136}
137```
138之后客户端将 dns 服务器改为内网 dns
139```
140"dns": {
141    "hosts": {
142    },
143    "servers": [
144        "172.233.233.1"
145        // 这里怎么写请自行判断,全局 dns 转发可能会导致延迟增加
146    ]
147}
148```
149## 示例配置文件
150[服务端](https://lin.moe/attach/v2ray_nat/server.json)  
151[客户端](https://lin.moe/attach/v2ray_nat/client.json)
152> 注意:以上配置文件没由经过测试,如有错误,请自行更改