forked from ViRb3/cloudflare-warp-wireguard-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wgcf.py
284 lines (207 loc) · 9.49 KB
/
wgcf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from pathlib import Path
from datetime import datetime, timezone
import requests
import dataclasses
import json
import subprocess
import shutil
import sys
from typing import Optional
api_version = "v0a884"
api = f"https://api.cloudflareclient.com/{api_version}"
reg_url = f"{api}/reg"
status_url = f"{api}/client_config"
terms_of_service_url = "https://www.cloudflare.com/application/terms/"
data_path = Path(".")
identity_path = data_path.joinpath("wgcf-identity.json")
config_path = data_path.joinpath("wgcf-profile.conf")
default_headers = {"Accept-Encoding": "gzip",
"User-Agent": "okhttp/3.12.1"}
# toggle to allow sniffing traffic
debug = False
def get_verify() -> bool:
return not debug
def get_config_url(account_id: str) -> str:
return f"{reg_url}/{account_id}"
def get_account_url(account_id: str) -> str:
return f"{get_config_url(account_id)}/account"
def get_devices_url(account_id: str) -> str:
return f"{get_account_url(account_id)}/devices"
def get_account_reg_url(account_id: str, device_token: str) -> str:
return f"{get_account_url(account_id)}/reg/{device_token}"
@dataclasses.dataclass
class AccountData:
account_id: str
access_token: str
private_key: str
license_key: str
@dataclasses.dataclass
class ConfigurationData:
local_address_ipv4: str
local_address_ipv6: str
endpoint_address_host: str
endpoint_address_ipv4: str
endpoint_address_ipv6: str
endpoint_public_key: str
warp_enabled: bool
account_type: str
warp_plus_enabled: bool
license_key_updated: bool
def get_timestamp() -> str:
# SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss", Locale.US)
timestamp = datetime.now(tz=timezone.utc).astimezone(None).strftime("%Y-%m-%dT%H:%M:%S.%f%z")
# trim microseconds to 2 digits
timestamp = timestamp[:-10] + timestamp[-6:]
# separate timezone offset
timestamp = timestamp[:-2] + ":" + timestamp[-2:]
return timestamp
def gen_private_key() -> str:
result: str = subprocess.run(["wg", "genkey"], capture_output=True).stdout.decode('utf-8')
return result.strip()
def gen_public_key(private_key: str) -> str:
result: str = subprocess.run(["wg", "pubkey"], input=bytes(private_key, 'utf-8'),
capture_output=True).stdout.decode('utf-8')
return result.strip()
def do_register() -> AccountData:
timestamp = get_timestamp()
private_key = gen_private_key()
public_key = gen_public_key(private_key)
data = {"install_id": "", "tos": timestamp, "key": public_key, "fcm_token": "", "type": "Android",
"model": "PC", "locale": "en_US"}
headers = default_headers.copy()
headers["Content-Type"] = "application/json; charset=UTF-8"
response = requests.post(reg_url, json=data, headers=headers, verify=get_verify())
response.raise_for_status()
response = json.loads(response.content)
return AccountData(response["id"], response["token"], private_key, response["account"]["license"])
def save_identitiy(account_data: AccountData):
with open(identity_path, "w") as f:
f.write(json.dumps(dataclasses.asdict(account_data), indent=4))
def load_identity() -> AccountData:
with open(identity_path, "r") as f:
account_data = AccountData(**json.loads(f.read()))
return account_data
def enable_warp(account_data: AccountData):
data = {"warp_enabled": True}
headers = default_headers.copy()
headers["Authorization"] = f"Bearer {account_data.access_token}"
headers["Content-Type"] = "application/json; charset=UTF-8"
response = requests.patch(get_config_url(account_data.account_id), json=data, headers=headers, verify=get_verify())
response.raise_for_status()
response = json.loads(response.content)
assert response["warp_enabled"] is True
def get_server_conf(account_data: AccountData) -> ConfigurationData:
headers = default_headers.copy()
headers["Authorization"] = f"Bearer {account_data.access_token}"
response = requests.get(get_config_url(account_data.account_id), headers=headers, verify=get_verify())
response.raise_for_status()
response = json.loads(response.content)
addresses = response["config"]["interface"]["addresses"]
peer = response["config"]["peers"][0]
endpoint = peer["endpoint"]
account = response["account"] if "account" in response else ""
account_type = account["account_type"] if account != "" else "free"
warp_plus = account["warp_plus"] if account != "" else False
license_key_updated = response["account"]["license"].strip() != account_data.license_key.strip()
return ConfigurationData(addresses["v4"], addresses["v6"], endpoint["host"], endpoint["v4"],
endpoint["v6"], peer["public_key"], response["warp_enabled"],
account_type, warp_plus, license_key_updated)
def update_license_key(account_data: AccountData, conf_data: ConfigurationData) -> bool:
if conf_data.account_type == "free" and account_data.license_key != "":
headers = default_headers.copy()
headers["Authorization"] = f"Bearer {account_data.access_token}"
data = {"license": account_data.license_key}
url = get_account_url(account_data.account_id)
activation_resp = requests.put(url, json=data, headers=headers, verify=get_verify())
activation_resp.raise_for_status()
activation_resp = json.loads(activation_resp.content)
return activation_resp["warp_plus"]
elif conf_data.account_type == "unlimited":
# already has unlimited subscription
return True
return False
def get_device_active(account_data: AccountData) -> Optional[bool]:
headers = default_headers.copy()
headers["Authorization"] = f"Bearer {account_data.access_token}"
url = get_devices_url(account_data.account_id)
activation_resp = requests.get(url, headers=headers, verify=get_verify())
activation_resp.raise_for_status()
activation_resp = json.loads(activation_resp.content)
my_device = next((x for x in activation_resp if x["id"] == account_data.account_id), None)
return None if my_device is None else my_device["active"]
def set_device_active(account_data: AccountData, status: bool) -> bool:
headers = default_headers.copy()
headers["Authorization"] = f"Bearer {account_data.access_token}"
data = {"active": status}
url = get_account_reg_url(account_data.account_id, account_data.account_id)
activation_resp = requests.patch(url, json=data, headers=headers, verify=get_verify())
activation_resp.raise_for_status()
activation_resp = json.loads(activation_resp.content)
my_device = next(x for x in activation_resp if x["id"] == account_data.account_id)
return my_device["active"]
def get_wireguard_conf(private_key: str, address_1: str, address_2: str, public_key: str, endpoint: str) -> str:
return f"""
[Interface]
PrivateKey = {private_key}
DNS = 1.1.1.1
Address = {address_1}
Address = {address_2}
[Peer]
PublicKey = {public_key}
AllowedIPs = 0.0.0.0/0
AllowedIPs = ::/0
Endpoint = {endpoint}
"""[1:-1]
def create_conf(account_data: AccountData, conf_data: ConfigurationData):
with open(config_path, "w") as f:
f.write(
get_wireguard_conf(account_data.private_key, conf_data.local_address_ipv4,
conf_data.local_address_ipv6, conf_data.endpoint_public_key,
conf_data.endpoint_address_host))
if __name__ == "__main__":
if shutil.which("wg") is None:
print("Error: 'wg' must be installed and added to PATH")
print("More information: https://www.wireguard.com/install/")
sys.exit(1)
data_path.mkdir(exist_ok=True)
account_data: AccountData
if not identity_path.exists():
print("This project is in no way affiliated with Cloudflare!")
print(f"Cloudflare's Terms of Service: {terms_of_service_url}")
if not input("Do you agree? (y/N): ").lower() == "y":
sys.exit(2)
print(f"Creating new identity...")
account_data = do_register()
save_identitiy(account_data)
else:
print(f"Loading existing identity...")
account_data = load_identity()
print(f"Getting configuration...")
conf_data = get_server_conf(account_data)
if conf_data.license_key_updated:
print("Updating account license key...")
result = update_license_key(account_data, conf_data)
if result:
conf_data = get_server_conf(account_data)
device_status = get_device_active(account_data)
if device_status is None:
print("This device not registered to the account!")
sys.exit(3)
if conf_data.warp_plus_enabled and not device_status:
print("This device is registered to the account, but not activated. It is highly recommended to activate it "
"before using Warp+.")
if input("Would you like to activate this device? (y/N): ").lower() == "y":
device_status = set_device_active(account_data, True)
if not conf_data.warp_enabled:
print(f"Enabling Warp...")
enable_warp(account_data)
conf_data.warp_enabled = True
print(f"Warp+ enabled: {conf_data.warp_plus_enabled}")
print(f"Device activated: {device_status}")
print(f"Account type: {conf_data.account_type}")
print(f"Warp+ enabled: {conf_data.warp_plus_enabled}")
print("Creating WireGuard configuration...")
create_conf(account_data, conf_data)
print("All done! Find your files here:")
print(identity_path.absolute())
print(config_path.absolute())