{"id":813,"date":"2025-07-17T20:34:02","date_gmt":"2025-07-17T12:34:02","guid":{"rendered":"https:\/\/www.zhzxs.xyz\/?p=813"},"modified":"2025-07-17T20:34:03","modified_gmt":"2025-07-17T12:34:03","slug":"1752755644","status":"publish","type":"post","link":"https:\/\/www.zhzxs.site\/index.php\/2025\/07\/17\/1752755644\/","title":{"rendered":"v2ray to clash python\u4ee3\u7801"},"content":{"rendered":"\n<p><strong>Python \u811a\u672c<\/strong>\uff0c\u53ef\u4ee5\u5c06 <code>vmess:\/\/<\/code> \u94fe\u63a5<strong>\u4e00\u952e\u8f6c\u6362\u4e3a Clash (Meta) \u7684 YAML \u914d\u7f6e\u6587\u4ef6<\/strong>\uff0c\u652f\u6301\u57fa\u672c\u5e38\u89c1\u5b57\u6bb5\uff0c<strong>\u4e0d\u4e0a\u4f20\u3001\u4e0d\u8054\u7f51\uff0c\u7eaf\u672c\u5730\u8f6c\u6362<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Python \u811a\u672c\uff1a\u4e00\u952e\u5c06 <code>vmess:\/\/<\/code> \u8f6c\u4e3a Clash YAML<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import base64\nimport json\nimport yaml\n\ndef decode_vmess_link(vmess_url):\n    if not vmess_url.startswith(\"vmess:\/\/\"):\n        raise ValueError(\"\u4e0d\u662f\u5408\u6cd5\u7684 vmess \u94fe\u63a5\")\n\n    base64_payload = vmess_url&#91;len(\"vmess:\/\/\"):]\n    padded_payload = base64_payload + \"=\" * (-len(base64_payload) % 4)  # \u8865\u9f50 Base64\n    json_data = base64.b64decode(padded_payload).decode('utf-8')\n    return json.loads(json_data)\n\ndef convert_to_clash(proxy_json):\n    clash_proxy = {\n        'name': proxy_json.get(\"ps\", \"vmess-node\"),\n        'type': 'vmess',\n        'server': proxy_json&#91;'add'],\n        'port': int(proxy_json&#91;'port']),\n        'uuid': proxy_json&#91;'id'],\n        'alterId': int(proxy_json.get('aid', 0)),\n        'cipher': 'auto',\n        'tls': proxy_json.get('tls', '').lower() == 'tls',\n        'skip-cert-verify': True,\n        'network': proxy_json.get('net', 'tcp'),\n    }\n\n    if clash_proxy&#91;'network'] == 'ws':\n        clash_proxy&#91;'ws-opts'] = {\n            'path': proxy_json.get('path', '\/'),\n        }\n        if proxy_json.get('host'):\n            clash_proxy&#91;'ws-opts']&#91;'headers'] = {\n                'Host': proxy_json&#91;'host']\n            }\n\n    return clash_proxy\n\ndef generate_yaml(proxy):\n    config = {\n        'proxies': &#91;proxy],\n        'proxy-groups': &#91;\n            {\n                'name': 'Proxy',\n                'type': 'select',\n                'proxies': &#91;proxy&#91;'name'], 'DIRECT']\n            }\n        ],\n        'rules': &#91;\n            'MATCH,Proxy'\n        ]\n    }\n    return yaml.dump(config, allow_unicode=True, sort_keys=False)\n\nif __name__ == \"__main__\":\n    vmess_input = input(\"\u8bf7\u8f93\u5165 vmess:\/\/ \u94fe\u63a5\uff1a\").strip()\n    proxy_json = decode_vmess_link(vmess_input)\n    clash_proxy = convert_to_clash(proxy_json)\n    yaml_output = generate_yaml(clash_proxy)\n\n    output_file = 'clash_config.yaml'\n    with open(output_file, 'w', encoding='utf-8') as f:\n        f.write(yaml_output)\n\n    print(f\"\u2705 \u5df2\u751f\u6210 Clash \u914d\u7f6e\u6587\u4ef6: {output_file}\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udce6 \u73af\u5883\u8981\u6c42\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3\uff08\u63a8\u8350 3.6+\uff09<\/li>\n\n\n\n<li>\u5b89\u88c5\u4f9d\u8d56\uff08\u5982\u679c\u6ca1\u6709 <code>pyyaml<\/code>\uff09\uff1a <code>pip install pyyaml<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u4f7f\u7528\u6b65\u9aa4\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u628a\u4e0a\u8ff0\u4ee3\u7801\u4fdd\u5b58\u4e3a <code>vmess2clash.py<\/code><\/li>\n\n\n\n<li>\u8fd0\u884c\u811a\u672c\uff1a <code>python vmess2clash.py<\/code><\/li>\n\n\n\n<li>\u8f93\u5165\u4f60\u7684 <code>vmess:\/\/<\/code> \u94fe\u63a5<\/li>\n\n\n\n<li>\u7a0b\u5e8f\u4f1a\u751f\u6210 <code>clash_config.yaml<\/code> \u6587\u4ef6\uff0c<strong>\u4f60\u5c31\u53ef\u4ee5\u5bfc\u5165 Clash.Meta For Android \u4f7f\u7528\u4e86<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u811a\u672c\uff0c\u53ef\u4ee5\u5c06 vmess:\/\/ \u94fe\u63a5\u4e00\u952e\u8f6c\u6362\u4e3a Clash (Meta) \u7684 YAML \u914d\u7f6e\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":117,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[154,155,156],"class_list":["post-813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-2","tag-clash","tag-v2ray","tag-vmess"],"_links":{"self":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts\/813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/comments?post=813"}],"version-history":[{"count":1,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts\/813\/revisions"}],"predecessor-version":[{"id":814,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts\/813\/revisions\/814"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/media\/117"}],"wp:attachment":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/media?parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/categories?post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/tags?post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}