Skip to content

Instantly share code, notes, and snippets.

@socram8888
Last active December 18, 2023 12:00
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save socram8888/18390775c8d6d97186e774abf7ead0b5 to your computer and use it in GitHub Desktop.
Save socram8888/18390775c8d6d97186e774abf7ead0b5 to your computer and use it in GitHub Desktop.
Refresh script for CloudFlare Warp on OpenWRT devices
#!/bin/ash
set -euo pipefail
cd $(dirname "$0")
CF_API=https://api.cloudflareclient.com/v0i1909051800
WG_IFACE=cfwarp
regen=true
if [ -e keys.cfg ]; then
. ./keys.cfg
echo "Using pre-existing keys"
reg=$(curl -s -H 'user-agent: okhttp/3.12.1' --tlsv1.2 -H 'content-type: application/json' -H "authorization: Bearer ${token}" -X "GET" "${CF_API}/reg/${regid}")
if [ "$(echo $reg | jq .success)" == true ]; then
echo "Succeeded"
regen=false
else
echo "Failed"
fi
fi
if $regen; then
privkey=$(wg genkey)
pubkey=$(echo $privkey | wg pubkey)
echo "Generated public key: ${pubkey}"
reg=$(curl -s -H 'user-agent: okhttp/3.12.1' --tlsv1.2 -H 'content-type: application/json' -X "POST" -d '{"install_id":"","tos":"'$(date -u +%FT%T.000Z)'","key":"'${pubkey}'","fcm_token":"","type":"ios","locale":"en_US"}' ${CF_API}/reg)
if [ "$(echo $reg | jq .success)" != true ]; then
echo "Registration failed" >&2
echo "===" >&2
echo "$reg" >&2
echo "===" >&2
exit 1
fi
regid=$(echo $reg | jq -r .result.id)
echo "Registration ID: $regid"
token=$(echo $reg | jq -r .result.token)
echo "Token: $token"
enable=$(curl -s -H 'user-agent: okhttp/3.12.1' --tlsv1.2 -H 'content-type: application/json' -H "authorization: Bearer ${token}" -X "PATCH" -d '{"warp_enabled":true}' ${CF_API}/reg/${regid})
if [ "$(echo $enable | jq .success)" != true ]; then
echo "Enable failed" >&2
echo "===" >&2
echo "$enable" >&2
echo "===" >&2
exit 1
fi
fi
cat <<EOF >keys.cfg
privkey=${privkey}
token=${token}
regid=${regid}
EOF
if $regen; then
ipv4=$(echo $reg | jq -r ".result.config.interface.addresses.v4")
ipv6=$(echo $reg | jq -r ".result.config.interface.addresses.v6")
uci delete network.${WG_IFACE}.addresses
uci add_list network.${WG_IFACE}.addresses=$ipv4
uci add_list network.${WG_IFACE}.addresses=$ipv6
uci set network.${WG_IFACE}.private_key=$privkey
uci commit
ifdown ${WG_IFACE}
ifup ${WG_IFACE}
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment