CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 5505|回复: 56
打印 上一主题 下一主题

[疑问] 【更新#1】 node脚本生成CF Warp对应的WireGuard配置文件

  [复制链接]

9

主题

129

回帖

4222

积分

论坛元老

西城飘雪

Rank: 8Rank: 8

积分
4222
跳转到指定楼层
1#
发表于 2019-9-28 07:22:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 yyuueexxiinngg 于 2019-9-29 02:20 编辑

Warp之旅可以告一段落了


脚本地址:
  1. https://github.com/yyuueexxiinngg/some-scripts/blob/master/cloudflare/warp2wireguard.js
复制代码




更新记录:


#1 2019.09.29 02:13

- 支持免费无限流量版Warp (注册时不填写aff). 之后可以通过aff升级到Warp Plus
刷aff脚本地址: https://www.52.ht/thread-590354-1-1.html

鉴于Warp在国内沦为电话线, 这里就不放太详细的一步步教程了


使用方法:

在WireGuard中选择手动添加来生成一对密钥, 获取方法: https://i.loli.net/2019/09/28/8QTJzYjlpbg4noF.jpg
把Public Key和Private Key分别填入脚本中运行
然后把生成的配置文件导入WireGuard就行, Allow ip可以自行根据需要修改

warp-conf.json中保存了口令供之后重新获取配置和查询流量使用

怎样获取剩余流量? 运行
  1. node warp2wireguard.js q
复制代码
或者直接再次运行脚本



测试结果:

运行:


查询流量:


Windows版WireGuard测试:


iOS版WireGuard测试:


这里贴出中文版代码:
node warp2wireguard.js



  1. const publicKey = "Public Key复制到这里";
  2. const privateKey = "Private Key复制到这里";
  3. const referrer = "Referrer ID复制到这里获取1G流量直接启用Warp+";

  4. if (!publicKey.endsWith("=") || !privateKey.endsWith("=")) {
  5.   console.error("请正确填写密钥.");
  6.   process.exit(1);
  7. }

  8. const https = require("https");
  9. const zlib = require("zlib");
  10. const fs = require("fs");
  11. const util = require("util");

  12. let warpConf = null;

  13. async function run() {
  14.   let userData = {};

  15.   if (fs.existsSync("./warp-conf.json")) {
  16.     warpConf = JSON.parse(fs.readFileSync("./warp-conf.json").toString());
  17.   } else {
  18.     warpConf = {
  19.       id: null,
  20.       publicKey: publicKey, // WireGuard pubic key
  21.       token: null, // Cloudflare access token
  22.       isWarpPlusEnabled: null
  23.     };
  24.   }

  25.   if (!warpConf.id) {
  26.     console.log("未读取到保存信息, 正在注册新帐号中...");
  27.     userData = await reg();
  28.     console.log("注册成功:");
  29.     console.log(util.inspect(userData, false, null, true));
  30.   } else {
  31.     console.log("正在获取用户信息...");
  32.     const res = await getInfo(warpConf.id, warpConf.token);
  33.     userData = res.result;
  34.     if (
  35.       !warpConf.isWarpPlusEnablsed && // If saved record indicate using free version of Cloudflare Warp
  36.       userData.account &&
  37.       (userData.account.premium_data || data.account.warp_plus)
  38.     ) {
  39.       warpConf.isWarpPlusEnabled = true;
  40.       fs.writeFileSync("./warp-conf.json", JSON.stringify(warpConf));
  41.     }
  42.     console.log("成功获取用户信息:");
  43.     if (process.argv[2] && process.argv[2] === "q") {
  44.       if (warpConf.isWarpPlusEnabled) {
  45.         console.log(
  46.           "\x1b[36m%s\x1b[0m",
  47.           "WARP PLUS剩余流量:",
  48.           userData.account.quota / 1000000000,
  49.           "GB"
  50.         );
  51.       } else {
  52.         console.log(
  53.           "\x1b[36m%s\x1b[0m",
  54.           "您正在使用Cloudflare免费版Warp, 没有流量限制, 您可用自己的ID分享1.1.1.1来获取流量以升级到Warp Plus."
  55.         );
  56.       }
  57.       process.exit(0);
  58.     }
  59.     console.log(util.inspect(userData, false, null, true));
  60.   }

  61.   const wireGuardConf = `
  62. [Interface]
  63. PrivateKey = ${privateKey}
  64. # PublicKey = ${publicKey}
  65. Address = ${userData.config.interface.addresses.v4}
  66. # Address = ${userData.config.interface.addresses.v6}
  67. DNS = 1.1.1.1

  68. [Peer]
  69. PublicKey = ${userData.config.peers[0].public_key}
  70. Endpoint = ${userData.config.peers[0].endpoint.v4}
  71. # Endpoint = ${userData.config.peers[0].endpoint.v6}
  72. # Endpoint = ${userData.config.peers[0].endpoint.host}
  73. AllowedIPs = 0.0.0.0/0
  74. `;

  75.   console.log("Config: ");
  76.   console.log(wireGuardConf);
  77.   fs.writeFileSync("./wireguard-cloudflare-warp.conf", wireGuardConf);
  78.   console.log(
  79.     "Config saved, check wireguard-cloudflare-warp.conf in current dir."
  80.   );

  81.   if (warpConf.isWarpPlusEnabled) {
  82.     console.log(
  83.       "\x1b[36m%s\x1b[0m",
  84.       "WARP PLUS剩余流量:",
  85.       userData.account.quota / 1000000000,
  86.       "GB"
  87.     );
  88.   } else {
  89.     console.log(
  90.       "\x1b[36m%s\x1b[0m",
  91.       "您正在使用Cloudflare免费版Warp, 没有流量限制, 您可用自己的ID分享1.1.1.1来获取流量以升级到Warp Plus."
  92.     );
  93.   }
  94. }

  95. async function getInfo(id, token) {
  96.   return new Promise(async resolve => {
  97.     const result = await httpRequest({
  98.       hostname: "api.cloudflareclient.com",
  99.       port: 443,
  100.       path: `/v0i1909221500/reg/${id}`,
  101.       method: "GET",
  102.       headers: {
  103.         Accept: "*/*",
  104.         Authorization: `Bearer ${token}`,
  105.         Host: "api.cloudflareclient.com",
  106.         "Accept-Encoding": "gzip",
  107.         "Accept-Language": "Language",
  108.         "User-Agent": "1.1.1.1/1909221500.1 CFNetwork/978.0.7 Darwin/18.7.0"
  109.       }
  110.     });

  111.     if (result.success) {
  112.       const data = result.payload;
  113.       resolve(data);
  114.     } else {
  115.       console.error("获取用户信息失败.");
  116.       process.exit(1);
  117.     }
  118.   });
  119. }

  120. async function reg() {
  121.   return new Promise(async resolve => {
  122.     const install_id = genString(11);
  123.     const postData = {
  124.       key: publicKey,
  125.       install_id: install_id,
  126.       fcm_token: `${install_id}:APA91b${genString(134)}`,
  127.       referrer: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(
  128.         referrer
  129.       )
  130.         ? referrer
  131.         : "",
  132.       warp_enabled: true,
  133.       tos: new Date().toISOString().replace("Z", "+08:00"),
  134.       type: "Android",
  135.       locale: "en_US"
  136.     };

  137.     const result = await httpRequest(
  138.       {
  139.         hostname: "api.cloudflareclient.com",
  140.         port: 443,
  141.         path: "/v0a745/reg",
  142.         method: "POST",
  143.         headers: {
  144.           "Content-Type": "application/json",
  145.           Host: "api.cloudflareclient.com",
  146.           Connection: "Keep-Alive",
  147.           "Accept-Encoding": "gzip",
  148.           "User-Agent": "okhttp/3.12.1"
  149.         }
  150.       },
  151.       postData
  152.     );

  153.     if (result.success) {
  154.       const data = result.payload;
  155.       warpConf.id = data.id;
  156.       warpConf.token = data.token;

  157.       if (data.account && (data.account.premium_data || data.account.warp_plus))
  158.         warpConf.isWarpPlusEnabled = true;

  159.       fs.writeFileSync("./warp-conf.json", JSON.stringify(warpConf));

  160.       resolve(data);
  161.     } else {
  162.       console.error("注册帐号失败.");
  163.       process.exit(1);
  164.     }
  165.   });
  166. }

  167. function httpRequest(options, data = undefined) {
  168.   return new Promise(resolve => {
  169.     const bodyString = data ? JSON.stringify(data) : data;
  170.     const req = https.request(options, res => {
  171.       const gzip = zlib.createGunzip();
  172.       const buffer = [];
  173.       res.pipe(gzip);
  174.       gzip
  175.         .on("data", function(data) {
  176.           buffer.push(data.toString());
  177.         })
  178.         .on("end", function() {
  179.           const res = JSON.parse(buffer.join(""));
  180.           resolve({ success: true, payload: res });
  181.         })
  182.         .on("error", function(e) {
  183.           resolve({ success: false, payload: e });
  184.         });
  185.     });

  186.     req.on("error", e => {
  187.       resolve({ success: false, payload: e });
  188.     });

  189.     if (bodyString) req.write(bodyString);
  190.     req.end();
  191.   });
  192. }

  193. function genString(length) {
  194.   // https://gist.github.com/6174/6062387#gistcomment-2651745
  195.   return [...Array(length)]
  196.     .map(i => (~~(Math.random() * 36)).toString(36))
  197.     .join("");
  198. }

  199. run();
  200. // Original link: https://github.com/yyuueexxiinngg/some-scripts/blob/master/cloudflare/warp2wireguard.js
复制代码

117

主题

2874

回帖

5万

积分

论坛神仙

NOKIA--释放你活力

Rank: 9Rank: 9Rank: 9

积分
54635
推荐
发表于 2019-9-28 09:34:42 | 只看该作者
本帖最后由 Nokia 于 2019-9-28 09:37 编辑

已整理大佬的脚本
Cloudflare WARP+ 快速获取流量与生成WireGuard配置
  1. https://lib.im/share/cloudflare-warp
复制代码

点评

牛逼  发表于 2019-9-28 17:05

312

主题

2953

回帖

6098

积分

论坛元老

Rank: 8Rank: 8

积分
6098
2#
发表于 2019-9-28 07:30:11 来自手机 | 只看该作者
node怎么运行?

9

主题

129

回帖

4222

积分

论坛元老

西城飘雪

Rank: 8Rank: 8

积分
4222
3#
 楼主| 发表于 2019-9-28 07:36:41 | 只看该作者

要安装nodejs, 不过大佬给这个网站可以在线直接运行, 问题就是要手动保存配置文件和口令文件了
https://repl.it/languages/nodejs

312

主题

2953

回帖

6098

积分

论坛元老

Rank: 8Rank: 8

积分
6098
4#
发表于 2019-9-28 07:42:36 来自手机 | 只看该作者
thank you,是原创啊!魔败!大佬,需要帮提包不?

9

主题

129

回帖

4222

积分

论坛元老

西城飘雪

Rank: 8Rank: 8

积分
4222
5#
 楼主| 发表于 2019-9-28 07:53:41 | 只看该作者
ymcoming 发表于 2019-9-28 07:42
thank you,是原创啊!魔败!大佬,需要帮提包不?

提包? 推广的意思嘛, 没啥必要, 写给朋友用的而已

325

主题

1663

回帖

2393

积分

金牌会员

Rank: 6Rank: 6

积分
2393
6#
发表于 2019-9-28 07:58:33 | 只看该作者
大佬 ,首先非常感谢你的分享,我还是不会怎么弄,能不能把教程再写得详细点,以便像我这样的小白也能操作,谢谢

113

主题

2714

回帖

5989

积分

论坛元老

Rank: 8Rank: 8

积分
5989
7#
发表于 2019-9-28 08:04:55 | 只看该作者
WireGuard这东西第一次用 试试看

332

主题

6813

回帖

2万

积分

论坛神仙

。。。。。。

Rank: 9Rank: 9Rank: 9

积分
21803
8#
发表于 2019-9-28 08:09:32 | 只看该作者
Public Key和Private Key怎么获取的大佬

⚡️便宜又好用的机场 9.9元100G               ➡️自用高速图床

749

主题

8629

回帖

2万

积分

论坛神仙

Rank: 9Rank: 9Rank: 9

积分
20154
9#
发表于 2019-9-28 08:12:46 | 只看该作者
推荐人id怎么写

113

主题

2714

回帖

5989

积分

论坛元老

Rank: 8Rank: 8

积分
5989
10#
发表于 2019-9-28 08:13:23 | 只看该作者
不懂nodejs 这是什么错误啊
  1. /root/warp2wireguard.js:26
  2. async function run() {
  3.       ^^^^^^^^

  4. SyntaxError: Unexpected token function
  5.     at createScript (vm.js:56:10)
  6.     at Object.runInThisContext (vm.js:97:10)
  7.     at Module._compile (module.js:549:28)
  8.     at Object.Module._extensions..js (module.js:586:10)
  9.     at Module.load (module.js:494:32)
  10.     at tryModuleLoad (module.js:453:12)
  11.     at Function.Module._load (module.js:445:3)
  12.     at Module.runMain (module.js:611:10)
  13.     at run (bootstrap_node.js:394:7)
  14.     at startup (bootstrap_node.js:160:9)
复制代码
抱歉,管理员设置了本版块发表于 -30 天以前的主题自动关闭,不再接受新回复

本版积分规则

举报|Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-2-11 13:02 , Processed in 0.091314 second(s), 24 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表