-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
140 lines (104 loc) · 3.24 KB
/
index.ts
File metadata and controls
140 lines (104 loc) · 3.24 KB
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
import { ClientHttp2Session, connect, constants } from "node:http2";
import { createServer } from "node:http";
import { env } from "node:process";
let client2: Record<string, ClientHttp2Session> = {};
function getClient2(key: string) {
if (client2[key] && !client2[key].destroyed) {
return client2[key];
}
client2[key] = connect(key);
return client2[key];
}
function ensureSingle<T>(header: T | T[] | undefined) {
return Array.isArray(header) ? header[0] : header;
}
function ensureMany<T>(header: T | T[] | undefined) {
return header == null ? [] : Array.isArray(header) ? header : [header];
}
let cookies: string;
let last = 0;
function getCookies() {
const now = Date.now();
if (now - last < 240000) {
return;
}
last = now;
console.log("Reading cookies");
const clientStream = getClient2("https://www.strava.com").request({
[constants.HTTP2_HEADER_METHOD]: constants.HTTP2_METHOD_POST,
[constants.HTTP2_HEADER_PATH]: "/api/next/refresh-cookies",
[constants.HTTP2_HEADER_CONTENT_LENGTH]: "0",
[constants.HTTP2_HEADER_COOKIE]: "_strava4_session=" + env.SP_SESSION,
});
clientStream.on("response", (headers) => {
const status = Number(ensureSingle(headers[constants.HTTP2_HEADER_STATUS]));
if (status !== 200) {
console.error("invalid response status");
process.exit(1);
}
const latestCookies = new Map<string, string>();
for (const cookie of ensureMany(
headers[constants.HTTP2_HEADER_SET_COOKIE],
)) {
const [pair] = cookie.split(";", 1);
const [name] = pair.split("=", 1);
latestCookies.set(name, pair);
}
cookies = Array.from(latestCookies.values()).join("; ");
clientStream.close();
console.log("Cookies read", cookies);
});
}
getCookies();
setInterval(() => getCookies(), 6 * 60 * 60_000);
createServer((req, res) => {
const clientStream = getClient2("https://content-a.strava.com").request({
[constants.HTTP2_HEADER_METHOD]: constants.HTTP2_METHOD_GET,
[constants.HTTP2_HEADER_PATH]: "/identified/globalheat" + req.url,
[constants.HTTP2_HEADER_COOKIE]: cookies,
});
clientStream.on("response", (headers) => {
const status = Number(ensureSingle(headers[constants.HTTP2_HEADER_STATUS]));
if (status === 403) {
getCookies();
}
console.log(
status +
" | " +
req.headers["x-forwarded-for"] +
" | " +
req.headers["referer"] +
" | " +
req.headers["user-agent"] +
" | " +
req.url,
);
const ct = ensureSingle(headers[constants.HTTP2_HEADER_CONTENT_TYPE]);
if (status === 200 && ct && ct?.startsWith("image/")) {
res.setHeader("Content-Type", ct);
} else if (status === 404) {
res.writeHead(404).end();
} else {
console.warn(
Object.entries(headers)
.map(([k, v]) => `${k}: ${v}`)
.join("|"),
);
res.writeHead(500).end();
}
});
clientStream.on("error", (err) => {
console.error(err);
if (!res.headersSent) {
res.writeHead(500);
}
res.end();
});
clientStream.on("data", (chunk) => {
res.write(chunk);
});
clientStream.on("end", () => {
res.end();
});
clientStream.end();
}).listen(Number(env.SP_PORT || "8080"));