请教 Clash 透明代理问题, iPhone 的哔哩哔哩、微博 app 无法联网,但是网页正常

2021-11-06 21:13:06 +08:00
 AllenHua

和这个 issue 类似,但是我没找到原因在哪,求助攻。

clash 透明代理跑在局域网 n1 这台设备上。

clash config.yaml 配置

redir-port: 7892
mixed-port: 7890
# Transparent proxy server port for Linux (TProxy TCP and TProxy UDP)
#tproxy-port: 7893
allow-lan: true
mode: rule 
# 设置输出日志的等级 (默认为 info)
# silent / info / warning / error / debug
log-level: silent 
external-controller: 0.0.0.0:9090
secret: ""
external-ui: dashboard
# hosts 设置,只有当 DNS 模式为 redir-host 时生效
hosts:
  # '*.clash.dev': 127.0.0.1
  # '.dev': 127.0.0.1
  # 'alpha.clash.dev': '::1'

dns:
  enable: true # 启用自定义 DNS
  ipv6: false # default is false
  listen: 0.0.0.0:53
  enhanced-mode: fake-ip  #DNS 模式,这里推荐使用 fake-ip ,因为后续的 iptables 规则是根据 fake-ip 做的
  fake-ip-range: 198.18.0.1/16 # if you don't know what it is, don't change it
  default-nameserver:
    - 180.76.76.76
    - 119.29.29.29
    - 223.5.5.5
  nameserver:
    - https://doh.pub/dns-query
    - https://dns.alidns.com/dns-query

其中 fallback 相关的配置已经注释掉了

  fallback:
    - 101.6.6.6:5353
  fallback-filter:
    geoip: true
  ipcidr:
    - 240.0.0.0/4

clash debug 级别的日志

[TCP] dial 🎯 全球直连 (match GeoIP/CN) to 202.113.3.108:43388 error: dial tcp4 202.113.3.108:43388: i/o timeout

[TCP] dial 🎯 全球直连 (match GeoIP/CN) to 101.229.42.233:51413 error: dial tcp4 101.229.42.233:51413: connect: connection refused

find process name Process error: no such process

感觉还是 dns 的问题,可以看本文开头那个 issue

已经查了一些资料

12106 次点击
所在节点    问与答
37 条回复
bigbyto
2021-11-06 21:43:16 +08:00
我感觉 clash 的 fakeip 不好用。我的透明代理 dns 方案是 dnsmasq+dnscrypt-proxy ,前者分流国内域名,后者设置 socks5 代理用 google 的 doh 解决污染问题。
AllenHua
2021-11-06 21:49:26 +08:00
@bigbyto #1 可以分享一下吗

我也搜到了很多说法,clash 这种方案做透明代理都不太适合
Love4Taylor
2021-11-06 21:59:06 +08:00
fakeip 的话等等支持持久化的版本发布吧

https://github.com/Dreamacro/clash/pull/1662
yanyumihuang
2021-11-06 22:12:31 +08:00
你是 openclash ,还是 clash 的 tun 模式。前者不懂,后者用 iptables 将 dns 指向 clash 监听的端口
yanyumihuang
2021-11-06 22:14:18 +08:00
另外用不到 v6 就把 ipv6 关了,设备会优先使用 ipv6 ,这样会出现问题
bigbyto
2021-11-06 22:15:36 +08:00
@AllenHua clash 可以做透明代理,不过别用它的 fakeip ,你可以参考我那个 dns 方案,我家里正在用的,很稳定。
AllenHua
2021-11-06 22:15:48 +08:00
@yanyumihuang #4 运行在 armbian 上,是 clash core 。

iptables 应该没什么问题

```
iptables -t nat -N clash
iptables -t nat -N clash_dns

iptables -t nat -A PREROUTING -p tcp --dport 53 -d 198.18.0.0/24 -j clash_dns
iptables -t nat -A PREROUTING -p udp --dport 53 -d 198.18.0.0/24 -j clash_dns
iptables -t nat -A PREROUTING -p tcp -j clash

iptables -t nat -A clash_dns -p udp --dport 53 -d 198.18.0.0/24 -j DNAT --to-destination 10.10.10.4:53
iptables -t nat -A clash_dns -p tcp --dport 53 -d 198.18.0.0/24 -j DNAT --to-destination 10.10.10.4:53

iptables -t nat -A clash -d 1.0.0.0/8 -j ACCEPT
iptables -t nat -A clash -d 10.0.0.0/8 -j ACCEPT
iptables -t nat -A clash -d 100.64.0.0/10 -j ACCEPT
iptables -t nat -A clash -d 127.0.0.0/8 -j ACCEPT
iptables -t nat -A clash -d 169.254.0.0/16 -j ACCEPT
iptables -t nat -A clash -d 172.16.0.0/12 -j ACCEPT
iptables -t nat -A clash -d 192.168.0.0/16 -j ACCEPT
iptables -t nat -A clash -d 224.0.0.0/4 -j ACCEPT
iptables -t nat -A clash -d 240.0.0.0/4 -j ACCEPT
iptables -t nat -A clash -d 192.168.1.2/32 -j ACCEPT

iptables -t nat -A clash -p tcp --dport 22 -d 10.10.10.4/32 -j ACCEPT

iptables -t nat -A clash -p tcp -j REDIRECT --to-ports 7892
```

注:n1 的 ip 是 10.10.10.4
AllenHua
2021-11-06 22:17:18 +08:00
@yanyumihuang #5 是的 我的 ipv6 已经全部关掉了。主路由是个 x86 openwrt


@bigbyto #6 所以有什么教程可以分享的吗 刚搜了一下 dnscrypt-proxy 部署的记录文章都很少
hronro
2021-11-06 23:13:12 +08:00
@bigbyto #1 我之前也是没用 clash 内置的 DNS ,但后来发现如果这样 clash 很多域名匹配的规则就无法生效了,因为 clash 拿不到域名的信息。请问你是如何解决这个问题的?
bigbyto
2021-11-06 23:19:16 +08:00
@hronro 我的方案主要就是用 dnscrypt-proxy 解决 dns 污染。家里有点事在处理,晚点详细说下。
bigbyto
2021-11-06 23:20:16 +08:00
@AllenHua 官方文档挺详细的,有空我整理一下我的配置
AllenHua
2021-11-06 23:36:28 +08:00
@bigbyto #11 很期待你的分享。我刚参考了一下这个 https://github.com/XXpE3/clash_raspberrypi/blob/master/dnscrypt-proxy.md 但是提示 `[FATAL] Unsupported key in configuration file: [fallback_resolver]` 我明明用了更新的版本。注释了这一行再运行发现提示 `[FATAL] No servers configured` 官方指导性的 toml config file 800+ lines ,的确有些头疼啊。我还原了 iptables 暂时先用着 clash 内置 dns 了
Kinnice
2021-11-07 00:05:13 +08:00
最好不要使用 fakeip
xenme
2021-11-07 06:56:05 +08:00
dns 加 ip 分流,clash 只做透明代理比较稳定
yanyumihuang
2021-11-07 08:32:28 +08:00
@AllenHua 昨晚睡了,我使用的 clash 的 tun 版本,fake ip 模式,这是目前我在用的 iptables ,只要 dns 和网关指定为相应 IP ,就能透明上网。没关 ipV6 时,因为抢答,手机总是不能透明代理,关了 ipv6 ,发现国内 app 巨慢,加上下面的 iptablles 就 OK 了。这个还解决了 Android 微信视频别人听不到你讲话的问题。因为我用的是 tun 版本,所以不需要最后将所有流量转向 7892 端口,直接就能处理了
sudo iptables -t nat -N clash
sudo iptables -t nat -N clash_dns

sudo iptables -t nat -A PREROUTING -j clash_dns
sudo iptables -t nat -A PREROUTING -j clash

sudo iptables -t nat -A POSTROUTING -j SNAT --to-source 192.168.2.2

sudo iptables -t nat -A clash_dns -p udp --dport 53 -j DNAT --to-destination 192.168.2.2:53
sudo iptables -t nat -A clash_dns -p tcp --dport 53 -j DNAT --to-destination 192.168.2.2:53
sudo iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
sudo iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
sudo iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
sudo iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
sudo iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
AllenHua
2021-11-07 08:32:40 +08:00
另外我还有几个问题

1. Chrome 和 Alook 没有给 “本地网络” 的权限但是可以正常使用代理。我是给 wifi 手动设置代理的,代理 ip 就是 n1 的 ip 10.10.10.4 ,为什么 Chrome 和 Alook 不需要这个权限呢?


Firefox 和 Edge 没有这个设置。

2. Transparent proxy 可以代理哪些协议? 看 https://lancellc.gitbook.io/clash/clash-config-file/an-example-configuration-file

```
# Transparent proxy server port for Linux and macOS (Redirect TCP and TProxy UDP)
# redir-port: 7892
# Transparent proxy server port for Linux (TProxy TCP and TProxy UDP)
# tproxy-port: 7893
```

为什么看到的透明代理教程都是说设置好了,将局域网客户端的 default gateway 和 dns 都指向透明代理设备的 IP 然后就可以无感翻强了。不太明白这里的实践,这是怎么实现的?我为什么不行,还是需要设置系统代理,或者手动对浏览器、终端设置代理才可以帆樯。telegram 倒是可以“无感翻强”……

求解
AllenHua
2021-11-07 08:34:53 +08:00
@yanyumihuang #15 谢谢分享,我回头用用 tun ,这个是在 clash premium 里?听说没有开源(不像基础 clash core 开源)
yanyumihuang
2021-11-07 08:43:30 +08:00
@AllenHua 嗯,和开源的版本是共用代码,然后另外增加了一些功能。搞透明代理建议用 tun ,简单方便,用 root 模式跑起来后,执行上面我发的 iptables 后将设备的 dns 和网关设置为运行 clash 的设备的 IP 就好。tun 版本有个缺点,ip 直连类无法处理。比如 telegram 需要手动设置代理外。想解决这个问题,就再加上一个 iptables
sudo iptables -t nat -A clash -p tcp -j REDIRECT --to-ports 7892
即可
hronro
2021-11-07 12:33:41 +08:00
@AllenHua #16
「将局域网客户端的 default gateway 和 dns 都指向透明代理设备的 IP 然后就可以无感翻强了。不太明白这里的实践,这是怎么实现的?」
把你路由器上 DHCP server 的配置里面的 gateway 和 DNS 改成你想要的 IP 就行了。
AllenHua
2021-11-07 12:51:09 +08:00
@hronro #19 我的确是这样做的 可以看这个 https://www.v2ex.com/t/813612
这样「无感帆樯」只有 telegram 可以做到,chrome terminal 依然要 set proxy

比如 terminal

```
export http_proxy=http://10.10.10.4:7890
export https_proxy=http://10.10.10.4:7890
```

比如 chrome 的 switchy omega 里配置一个 profile 使用 10.10.10.4 的 socks 、http(s) 代理

终端和浏览器这样配置之后才可以帆樯。我想象的无感是局域网设备不需要做任何配置的。如果我理解的有误,还请指正。

```
# Transparent proxy server port for Linux and macOS (Redirect TCP and TProxy UDP)
redir-port: 7892
```

7892 这个端口 不只能 Redirect TCP and TProxy UDP 么

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/813550

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX