Last active
February 12, 2019 03:56
-
-
Save LeiHao0/86b237db513a0f8ee8e599efb07cd182 to your computer and use it in GitHub Desktop.
Auto convert surge conf to clash yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 2019-02-11 | |
| import os | |
| import re | |
| os.system('curl https://xxx -o proxy.conf') | |
| os.system('curl https://r0uter.github.io/ss.conf-for-surge/geoip_whitelist.conf -o geoip_whitelist.conf') | |
| DNS = 'dns:\n enable: true\n ipv6: false\n nameserver:\n - 1.2.4.8\n - 114.114.114.114\n - 223.5.5.5\n - tls://dns.rubyfish.cn:853\n fallback:\n - 1.1.1.1\n - tls://dns.rubyfish.cn:853\n - tls://dns.google' | |
| ExtralList = '# Apple\n- DOMAIN,hls.itunes.apple.com,Proxy\n- DOMAIN,itunes.apple.com,Proxy\n- DOMAIN-SUFFIX,digicert.com,DIRECT\n- DOMAIN-SUFFIX,icloud.com,DIRECT\n- DOMAIN-SUFFIX,icloud-content.com,DIRECT\n- DOMAIN-SUFFIX,mzstatic.com,DIRECT\n- DOMAIN-SUFFIX,akadns.net,DIRECT\n- DOMAIN-SUFFIX,aaplimg.com,DIRECT\n- DOMAIN-SUFFIX,cdn-apple.com,DIRECT\n- DOMAIN-SUFFIX,apple.com,DIRECT\n# LAN\n- DOMAIN-SUFFIX,local,DIRECT\n- IP-CIDR,127.0.0.0/8,DIRECT\n- IP-CIDR,172.16.0.0/12,DIRECT\n- IP-CIDR,192.168.0.0/16,DIRECT\n- IP-CIDR,10.0.0.0/8,DIRECT\n- IP-CIDR,17.0.0.0/8,DIRECT\n- IP-CIDR,100.64.0.0/10,DIRECT' | |
| geoip_whitelist = open('geoip_whitelist.conf', 'r').read().split('[Rule]')[1].replace('//', '#').replace(', no-resolve', '').replace(',no-resolve', '') | |
| geoip_whitelist = re.sub(r'(?m)^(\w)', r'- \1', geoip_whitelist) | |
| geoip_whitelist = f'{ExtralList}\n{geoip_whitelist}\n- MATCH,Proxy' | |
| ProxyGroup = [] | |
| Proxy = '' | |
| Fallback = '' | |
| Auto = '' | |
| s = open('proxy.conf', 'r').read().split('DIRECT = direct')[1].split('[Proxy Group]')[0].splitlines() | |
| for l in s: | |
| if l: | |
| a = l.split(' = custom,') | |
| n = f'"{a[0]}"' | |
| ProxyGroup.append(n) | |
| a = a[1].split(',') | |
| Proxy += f'- {{ name: {n}, type: ss, server: {a[0]}, port: {a[1]}, cipher: {a[2]}, password: "{a[3]}", obfs: http, obfs-host: download.windowsupdate.com }}\n' | |
| Fallback = [s for s in ProxyGroup if ('(1.' in s or '(0.' in s) and 'SS - ' not in s] | |
| Fallback = ', '.join(Fallback) | |
| Fallback = f'- {{ name: "fallback-auto", type: fallback, proxies: [{Fallback}], url: "https://www.google.com/ncr", interval: 300 }}' | |
| Auto = [s for s in ProxyGroup if ('(0.' in s or '倍率' not in s) and 'SS - ' not in s] | |
| Auto = ', '.join(Auto) | |
| Auto = f'- {{ name: "auto", type: url-test, proxies: [{Auto}], url: "https://www.google.com/ncr", interval: 300 }}' | |
| ProxyGroup = ', '.join(ProxyGroup) | |
| ProxyGroup = f'- {{ name: "Proxy", type: select, proxies: ["auto", "fallback-auto", {ProxyGroup} ] }}' | |
| text = f'port: 1080\nsocks-port: 1081\nallow-lan: false\nmode: Rule\nlog-level: info\nexternal-controller: 127.0.0.1:1082\n{DNS}\n\nProxy:\n{Proxy}\n\nProxy Group:\n{Auto}\n{Fallback}\n{ProxyGroup}\n\nRule:\n{geoip_whitelist}' | |
| f = open('auto.yml', 'w') | |
| f.write(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment