无论是在国内还是俄罗斯,想要正常的访问国际互联网,都需要借助代理工具;本文中使用的是 Clash for Windows 的 MacOS 客户端。
而大多数机场提供的 Clash 订阅链接都包含了 Rules 规则 —— 这是为了方便国内用户,减少用户的配置负担;但是如果按照机场给定的 Rules 代理,很多在俄罗斯本没有必要代理的网站也会使用走代理,这不但会浪费流量,很多情况下也会减慢网速。
CFW 提供了名为 Parser 的功能用于解决这个问题。
在 Settings - Parsers 中编写如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   | parsers:   - url: # Your subscription address     code: |       module.exports.parse = async (raw, { axios, yaml, notify, console }, { name, url, interval, selected }) => {         const obj = yaml.parse(raw);         const address = "https://raw.githubusercontent.com/guiqiqi/storage/master/rules.yaml";                           obj.dns['default_nameserver'] = [           '8.8.8.8',            '1.1.1.1'         ];         obj.dns.nameserver = [           'tls://8.8.4.4:853',           'https://dns.cloudflare.com/dns-query'         ];         obj.dns.fallback = [           'https://doh.pub/dns-query',           'https://dns.alidns.com/dns-query',           'https://doh.dns.sb/dns-query',           'https://dns.twnic.tw/dns-query'         ];         obj.dns['enhanced-mode'] = 'fake-ip';
                   const proxies = [];         for (const proxy of obj.proxies) {           proxies.push(proxy.name);         }         obj['proxy-groups'] = [           {             name: 'Proxy',             type: 'url-test',             proxies: proxies,             url: 'http://www.gstatic.com/generate_204',             interval: interval           }         ];         const rules = await axios.get(address).then(function (response) {           return yaml.parse(response.data).rules;         });         obj.rules = rules;
                   const title = "更新成功";         const message = "更新并预处理了订阅文件: " + name;         notify(title, message);         return yaml.stringify(obj);       }
   | 
 
在 url 处将自己的订阅链接填入,这样当 CFW 定时更新订阅时,code 里的 Javascript 代码就会被执行。
回调函数的具体参数如下:
raw:获取到的订阅文件内容,一般都需要用 yaml 库解析; 
{ axios, yaml, notify, console }:会用到的工具,这里使用 axios 从 GitHub 上动态拉取了一份我维护的俄罗斯政府屏蔽网站的列表; 
{ name, url, interval, selected }:订阅的属性信息。 
下面的 Javascript 代码主要是解析了获取到的 YAML 文件并替换了一些内容(包括 DNS 配置,订阅组之类的),最后使用 yaml 重新格式化后返回。
同时,我还使用了 notify 在 macOS 上给了一个通知,最后的结果如下:

如有需要可以自行更改代码内容。
     
    
    
        
            Author: 桂小方
        
        
            
                Permalink: 
            
            https://init.blog/cfw-update-with-parser/
        
        
            
                文章许可协议: 
            
        
            
                如果你觉得文章对你有帮助,可以 支持我
            
        
     
 
     
    
Comments