Skip to content

Instantly share code, notes, and snippets.

@xrip
Last active May 3, 2023 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xrip/5810acb5840e1c14a4ff7e22803affb5 to your computer and use it in GitHub Desktop.
Save xrip/5810acb5840e1c14a4ff7e22803affb5 to your computer and use it in GitHub Desktop.
Warp.conf create fresh wireguard conf from cloudflare warp
const nacl = require('tweetnacl');
const genString = length => [...Array(length)].map(() => (~~(Math.random() * 36)).toString(36)).join('');
const clampSecret = secret => {
secret[0] &= 248;
secret[31] = (secret[31] & 127) | 64;
return secret;
};
const genKeys = async () => {
const key = clampSecret(nacl.randomBytes(32));
const keyPair = nacl.box.keyPair.fromSecretKey(key);
return {
privateKey: Buffer.from(key).toString('base64'),
publicKey: Buffer.from(keyPair.publicKey).toString('base64'),
};
};
const register = async ({ publicKey, referrer = `` }) => {
const install_id = genString(11);
return fetch('https://api.cloudflareclient.com/v0a977/reg',
{
method: 'POST',
headers: {
'User-Agent': 'okhttp/3.12.1',
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({
key: publicKey,
install_id,
fcm_token: `${install_id}:APA91b${genString(134)}`,
referrer,
warp_enabled: true,
tos: new Date().toISOString().replace('Z', '+08:00'),
model: 'Xiaomi POCO X2',
type: 'Android',
locale: 'en_US',
}),
}).then(r => r.json());
};
const getConfig = async ({ id, token }) => fetch(`https://api.cloudflareclient.com/v0a977/reg/${id}`,
{
headers: {
'User-Agent': 'okhttp/3.12.1',
Authorization: `Bearer ${token}`,
},
}).then(r => r.json());
const createConf = ({ privateKey, publicKey, config }) => `[Interface]
PrivateKey = ${privateKey}
Address = ${config.interface.addresses.v4}/32
Address = ${config.interface.addresses.v6}/128
DNS = 1.1.1.1
[Peer]
PublicKey = ${config.peers[0].public_key}
Endpoint = ${config.peers[0].endpoint.host}
AllowedIPs = 0.0.0.0/0
AllowedIPs = ::/0
`;
void async function(){
const keys = await genKeys();
const config = await getConfig(await register(keys));
console.log(createConf({ ...keys, ...config }));
}();
/*
require('http').createServer(async (request, response) => {
const keys = await genKeys();
const config = await getConfig(await register(keys));
return response.writeHead(200, { 'Content-Type': 'application/text' }).end(createConf({ ...keys, ...config }));
}).listen(3000);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment