vernesong / OpenClash

A Clash Client For OpenWrt
MIT License
16.9k stars 3.11k forks source link

第三方规则集与策略组管理,保存后config文件中出现^M换行符,导致Clash启动失败 #700

Closed ghost closed 4 years ago

ghost commented 4 years ago

其实就是 https://github.com/vernesong/OpenClash/issues/693 的继续深挖。

现在可以确定就是/etc/config/openclash设置文件中,rule_provider_config部分出现了windows下的换行符,导致最终生成的clash config文件格式错误,继而导致clash启动失败。

config rule_provider_config
        option enabled '1'
        option interval '86400'
        option config 'all'
        option group 'DIRECT'
        option position '0'
        list rule_name '国内IP白名单^M
'

正确的格式应该是这样的:

config rule_provider_config
        option enabled '1'
        option interval '86400'
        option config 'all'
        option group 'DIRECT'
        option position '0'
        list rule_name '国内IP白名单'

手工修改config文件是可以暂时解决问题,但没找到问题根源。

为了避免其他因素的影响,我找了台x86软路由重新刷过重新编译纯净openwrt 19.7.3系统,手动用ipk文件安装了openclash最近的v0.39.8版本。

然后在第三方规则集与策略组管理中,随便启用一个第三方规则集,点击保存后,查看/etc/config/openclash文件,就出现了上面的问题。

之前没用过这个功能,不知道是不是最近引入的还是一直都有。感觉调试日志也没什么帮助,但还是帖一下。

OpenClash 调试日志

生成时间: 2020-08-30 00:55:54
插件版本: v0.39.8-beta

#===================== 系统信息 =====================#
主机型号: To be filled by O.E.M. To be filled by O.E.M.
固件版本: OpenWrt 19.07.3 r11063-85e04e9f46
LuCI版本: git-20.136.49537-fb2f363-1
内核版本: 4.14.180
处理器架构: x86_64

#此项在使用Tun模式时应为ACCEPT
防火墙转发: ACCEPT

#此项有值时建议到网络-接口-lan的设置中禁用IPV6的DHCP
IPV6-DHCP: 

#此项结果应仅有配置文件的DNS监听地址
Dnsmasq转发设置: 

#===================== 依赖检查 =====================#
dnsmasq-full: 已安装
coreutils: 已安装
coreutils-nohup: 已安装
bash: 已安装
curl: 已安装
jsonfilter: 已安装
ca-certificates: 已安装
ipset: 已安装
ip-full: 已安装
iptables-mod-tproxy: 已安装
kmod-tun(TUN模式): 已安装
luci-compat(Luci-19.07): 已安装

#===================== 内核检查 =====================#
运行状态: 未运行
已选择的架构: linux-amd64

#下方无法显示内核版本号时请确认您的内核版本是否正确或者有无权限
Tun内核版本: 2020.08.26.g3803f5f
Tun内核文件: 存在
Tun内核运行权限: 正常

Game内核版本: v0.17.0-184-g384eb6c
Game内核文件: 存在
Game内核运行权限: 正常

Dev内核版本: v1.1.0-5-gc9735ef
Dev内核文件: 存在
Dev内核运行权限: 正常

#===================== 插件设置 =====================#
当前配置文件: /etc/openclash/config/config.yaml
运行模式: fake-ip-mix
默认代理模式: rule
UDP流量转发: 停用
DNS劫持: 启用
自定义DNS: 停用
IPV6-DNS解析: 停用
禁用Dnsmasq缓存: 停用
自定义规则: 停用
仅允许内网: 停用
仅代理命中规则流量: 停用

#启动异常时建议关闭此项后重试
保留配置: 停用
第三方规则: 停用

#===================== 配置文件 =====================#
#===================== Clash-DNS-Settings =====================#
redir-port: 7892
port: 7890
socks-port: 7891
ipv6: false
mode: rule
log-level: silent
external-controller: 0.0.0.0:9090
secret: "123456"
allow-lan: true
bind-address: "*"
external-ui: "/usr/share/openclash/dashboard"
tun:
  enable: true
  stack: system
  dns-hijack:
  - tcp://8.8.8.8:53
  - tcp://8.8.4.4:53
dns:
  listen: 127.0.0.1:7874

  enable: true
  ipv6: false
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16
  fake-ip-filter:
##Custom fake-ip-filter##
  - '*.lan'
  - 'time.windows.com'
  - 'time.nist.gov'
  - 'time.apple.com'
  - 'time.asia.apple.com'
  - '*.ntp.org.cn'
  - '*.openwrt.pool.ntp.org'
  - 'time1.cloud.tencent.com'
  - 'time.ustc.edu.cn'
  - 'pool.ntp.org'
  - 'ntp.ubuntu.com'
  - 'ntp.aliyun.com'
  - 'ntp1.aliyun.com'
  - 'ntp2.aliyun.com'
  - 'ntp3.aliyun.com'
  - 'ntp4.aliyun.com'
  - 'ntp5.aliyun.com'
  - 'ntp6.aliyun.com'
  - 'ntp7.aliyun.com'
  - 'time1.aliyun.com'
  - 'time2.aliyun.com'
  - 'time3.aliyun.com'
  - 'time4.aliyun.com'
  - 'time5.aliyun.com'
  - 'time6.aliyun.com'
  - 'time7.aliyun.com'
  - '*.time.edu.cn'
  - 'time1.apple.com'
  - 'time2.apple.com'
  - 'time3.apple.com'
  - 'time4.apple.com'
  - 'time5.apple.com'
  - 'time6.apple.com'
  - 'time7.apple.com'
  - 'time1.google.com'
  - 'time2.google.com'
  - 'time3.google.com'
  - 'time4.google.com'
  - 'music.163.com'
  - '*.music.163.com'
  - '*.126.net'
  - 'musicapi.taihe.com'
  - 'music.taihe.com'
  - 'songsearch.kugou.com'
  - 'trackercdn.kugou.com'
  - '*.kuwo.cn'
  - 'api-jooxtt.sanook.com'
  - 'api.joox.com'
  - 'joox.com'
  - 'y.qq.com'
  - '*.y.qq.com'
  - 'streamoc.music.tc.qq.com'
  - 'mobileoc.music.tc.qq.com'
  - 'isure.stream.qqmusic.qq.com'
  - 'dl.stream.qqmusic.qq.com'
  - 'aqqmusic.tc.qq.com'
  - 'amobile.music.tc.qq.com'
  - '*.xiami.com'
  - '*.music.migu.cn'
  - 'music.migu.cn'
  - '*.msftconnecttest.com'
  - '*.msftncsi.com'
  - 'localhost.ptlogin2.qq.com'
  - '*.*.*.srv.nintendo.net'
  - '*.*.stun.playstation.net'
  - 'xbox.*.*.microsoft.com'
  - '*.*.xboxlive.com'
##Custom fake-ip-filter END##
  nameserver:
  - 119.29.29.29
  - 1.2.4.8

#===================== Clash-Node-Settings =====================#

#===================== 防火墙设置 =====================#

#NAT chain

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    prerouting_rule  all  --  0.0.0.0/0            0.0.0.0/0            /* !fw3: Custom prerouting rule chain */
2    zone_lan_prerouting  all  --  0.0.0.0/0            0.0.0.0/0            /* !fw3 */
3    zone_wan_prerouting  all  --  0.0.0.0/0            0.0.0.0/0            /* !fw3 */
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

#Mangle chain

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

#===================== 路由表状态 =====================#
#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.10.1    0.0.0.0         UG    0      0        0 eth0
10.242.0.0      0.0.0.0         255.255.0.0     U     0      0        0 ztzlgmpcam
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.55.0    10.242.100.110  255.255.255.0   UG    0      0        0 ztzlgmpcam
192.168.66.0    10.242.100.119  255.255.255.0   UG    0      0        0 ztzlgmpcam
192.168.77.0    10.242.100.179  255.255.255.0   UG    0      0        0 ztzlgmpcam
192.168.88.0    10.242.100.216  255.255.255.0   UG    0      0        0 ztzlgmpcam
192.168.99.0    0.0.0.0         255.255.255.0   U     0      0        0 br-lan
#ip route list
default via 192.168.10.1 dev eth0 proto static src 192.168.10.248 
10.242.0.0/16 dev ztzlgmpcam proto kernel scope link src 10.242.100.121 
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.248 
192.168.55.0/24 via 10.242.100.110 dev ztzlgmpcam 
192.168.66.0/24 via 10.242.100.119 dev ztzlgmpcam 
192.168.77.0/24 via 10.242.100.179 dev ztzlgmpcam 
192.168.88.0/24 via 10.242.100.216 dev ztzlgmpcam 
192.168.99.0/24 dev br-lan proto kernel scope link src 192.168.99.1 
#ip rule show
0:  from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default 

#===================== Tun设备状态 =====================#
ztzlgmpcam: tap

#===================== 端口占用状态 =====================#

#===================== 测试本机DNS查询 =====================#
Server:     127.0.0.1
Address:    127.0.0.1#53

Name:      www.baidu.com
Address 1: 198.18.0.167
*** Can't find www.baidu.com: No answer

#===================== resolv.conf.auto =====================#
# Interface wan
nameserver 192.168.10.1
search COMFAST

#===================== 测试本机网络连接 =====================#
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Sat, 29 Aug 2020 16:55:55 GMT
Etag: "575e1f7c-115"
Last-Modified: Mon, 13 Jun 2016 02:50:36 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

#===================== 测试本机网络下载 =====================#
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 78
Cache-Control: max-age=300
Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
Content-Type: text/plain; charset=utf-8
ETag: "ef50130dcb4f80be197dede1c4d92defd39808f18c42543560fcb4df97dfb683"
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
Via: 1.1 varnish (Varnish/6.0)
X-GitHub-Request-Id: 2702:1690:1C18B:24AFA:5F4A7F91
Accept-Ranges: bytes
Date: Sat, 29 Aug 2020 16:55:55 GMT
Via: 1.1 varnish
X-Served-By: cache-hkg17927-HKG
X-Cache: MISS, HIT
X-Cache-Hits: 0, 3
X-Timer: S1598720156.686792,VS0,VE0
Vary: Authorization,Accept-Encoding
Access-Control-Allow-Origin: *
X-Fastly-Request-ID: 14dedcddaba588f1bdbcca136c1eb2163ff72b5f
Expires: Sat, 29 Aug 2020 17:00:55 GMT
Source-Age: 215

#===================== 最近运行日志 =====================#
2020-08-30 00:43:34 Config Not Found
2020-08-30 00:52:30 OpenClash 【Dev】 Core Update Successful
2020-08-30 00:54:19 OpenClash 【Tun】 Core Update Successful
2020-08-30 00:54:47 OpenClash 【Game】 Core Update Error
2020-08-30 00:54:57 OpenClash 【Game】 Core Update Successful
time="2020-08-29T16:55:29Z" level=fatal msg="Parse config error: yaml: line 132: did not find expected key"
2020-08-30 00:55:23 OpenClash Can Not Start, Please Check The Error Info And Try Again
vernesong commented 4 years ago

你的ipk是哪下载的?你可以尝试删除/etc/openclash/rule_providers.list里的换行符

ghost commented 4 years ago

你的ipk是哪下载的?你可以尝试删除/etc/openclash/rule_providers.list里的换行符

就github这里下载的啊。。 /etc/openclash/rule_providers.list没有看到这个windows换行符,用dos2unix处理过一遍也没解决,很奇怪。 我还有台r2s pi,刷的friendlywrt(op 19.07.1),装的是openclash 0.39.4版本,也有一样的问题。

vernesong commented 4 years ago

image 你点这个自定义删除最后的字符试试

ghost commented 4 years ago

用自定义的方式手工写入规则集名称是可以的,但从列表选择就有上述的问题。

ghost commented 4 years ago

还是没找到原因,但有办法解决。

我不太会lua,但是我试着在rule-providers-settings.lua添加了一行代码,读rule_providers.list的时候强制性的把换行符替换掉(虽然我确实没看到rule_providers.list里有换行符),然后就可以了,大概在150行的位置:

    if IsYamlFile(e[t].filename) or IsYmlFile(e[t].filename) then
       e[t].name=luci.sys.exec(string.format("grep -F ',%s' /etc/openclash/rule_providers.list |awk -F ',' '{print $1}' 2>/dev/null",e[t].filename))
       if e[t].name ~= "" and e[t].name ~= nil then
          //加入下面这行,强制做一次换行符替换操作
          e[t].name=string.gsub(e[t].name, "[\r\n]", "")
          o:value(e[t].name)
       end
    end
  end
end

可能game rule provider那里也要做一样的处理。 这个完全是我现搜现学写的,就不发PR现眼了,请作者帮忙完善下合并进去吧,谢谢!

另外,第三方规则集列表管理页(rule-providers-manage)里: Apple SoftwareUpdate 和Twitch 这两条规则好像删不掉,下载也是失败的。

vernesong commented 4 years ago

主要是我不能复现,原因没找到

ghost commented 4 years ago

嗯,我再重刷几个干净的系统试试吧,确实没发现代码哪里会有换行符引入。

-- Sent from Canary (https://canarymail.io)

On 星期一, 8月 31, 2020 at 5:31 下午, vernesong <notifications@github.com (mailto:notifications@github.com)> wrote:

主要是我不能复现,原因没找到

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub (https://github.com/vernesong/OpenClash/issues/700#issuecomment-683672634), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AAI56YSO5EFRYPKCEM3ETX3SDNUXXANCNFSM4QPC37EQ).

ghost commented 4 years ago

我可能找到问题的症结所在了,这个估计是openwrt版本的坑。 我分别用x86软路由,rockchip的r2s pi,qca8563的gl-inet ar750s三台机器测试。

r2s用官方的friendlywrt(基于op 19.07.1)时,有上述问题, 用https://github.com/klever1988/nanopi-openwrt 这里的现成固件(用的lean的lede,应该是基于op 18.06),没有上述问题。

x86软路由和ar750s,分别用lean的代码和op 19.07.3的代码纯净编译,不做任何特殊设置和打包任何配置或数据文件,只打包编译最新版的openclash。 用lean的代码编译的都没问题,用op19.07.3编译的都有这个换行符问题。整个编译和刷机过程就没用过windows电脑。

当然,你要说你的也是19.07的op但是没这问题。。。。我这就算白干了,反正目前来看是和op的版本高度相关。

就跟之前我说geoip 数据在op19.07下无法下载一样,也不知道具体哪里的问题,但问题确实就有,后面也不知道是openclash更新还是op本身更新后问题自己消失了。

强制替换换行符这个,你评估下看要不要加进去吧,应该也没什么太大影响,不行我就自己手改我本地代码先顶着,鬼知道会不会op哪天更新下就好了。