-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
288 lines (253 loc) · 9.38 KB
/
Copy pathindex.js
File metadata and controls
288 lines (253 loc) · 9.38 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
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
285
286
287
288
/**
* CLOUDFLARE WORKER: GITHUB MIRROR (High Speed + UI)
*
* Features:
* 1. Edge Caching (Fast downloads for popular files)
* 2. Streaming (Low memory usage)
* 3. Range Requests (Supports Resume/Download Managers)
* 4. UI with "Copy to Clipboard"
*/
const GITHUB_URL = 'https://github.com';
const CACHE_TTL = 31536000; // 1 Year Cache (since release files don't change)
const HTML_PAGE = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>High Speed GitHub Mirror</title>
<style>
:root {
--primary: #f6821f;
--bg: #1a1a1a;
--card: #2c2c2c;
--text: #ffffff;
--input-bg: #383838;
--success: #00ff9d;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--bg);
color: var(--text);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
box-sizing: border-box;
}
.container {
background-color: var(--card);
padding: 2.5rem;
border-radius: 16px;
box-shadow: 0 10px 40px rgba(0,0,0,0.4);
width: 100%;
max-width: 550px;
text-align: center;
}
h1 { margin-top: 0; color: var(--primary); font-size: 1.8rem; }
p { color: #aaa; margin-bottom: 2rem; line-height: 1.5; }
.input-group { margin-bottom: 1.5rem; text-align: left; }
label { display: block; margin-bottom: 0.5rem; font-size: 0.9rem; font-weight: 600; color: #ddd; }
input {
width: 100%;
padding: 14px;
border: 1px solid #444;
border-radius: 8px;
background-color: var(--input-bg);
color: white;
box-sizing: border-box;
font-size: 1rem;
transition: border 0.2s;
}
input:focus { outline: none; border-color: var(--primary); }
.btn-main {
background-color: var(--primary);
color: white;
border: none;
padding: 14px 24px;
border-radius: 8px;
font-size: 1rem;
font-weight: bold;
cursor: pointer;
width: 100%;
transition: transform 0.1s, opacity 0.2s;
}
.btn-main:hover { opacity: 0.9; }
.btn-main:active { transform: scale(0.98); }
/* Result Area */
#result-area {
margin-top: 2rem;
display: none;
background: #222;
padding: 1.5rem;
border-radius: 12px;
border: 1px solid #333;
animation: fadeIn 0.3s ease;
}
.link-container {
display: flex;
align-items: center;
background: #111;
padding: 8px;
border-radius: 6px;
margin-top: 10px;
border: 1px solid #333;
}
.link-text {
flex-grow: 1;
color: var(--success);
text-decoration: none;
font-family: monospace;
font-size: 0.9rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 0 10px;
text-align: left;
}
.link-text:hover { text-decoration: underline; }
.btn-copy {
background-color: #444;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
font-size: 0.85rem;
cursor: pointer;
font-weight: 600;
white-space: nowrap;
}
.btn-copy:hover { background-color: #555; }
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 2ri4e Mirror</h1>
<p>Enter a GitHub Release URL below to generate a high-speed, cached mirror link.</p>
<div class="input-group">
<label for="ghUrl">GitHub Release URL</label>
<input type="text" id="ghUrl" placeholder="https://github.com/username/repo/releases/download/v1.0/app.zip" autocomplete="off">
</div>
<button class="btn-main" onclick="generateLink()">Generate Fast Link</button>
<div id="result-area">
<label style="text-align: left">Your Mirror Link:</label>
<div class="link-container">
<a id="mirrorLink" class="link-text" href="#" target="_blank"></a>
<button class="btn-copy" onclick="copyToClipboard()" id="copyBtn">Copy</button>
</div>
</div>
</div>
<script>
function generateLink() {
const input = document.getElementById('ghUrl').value.trim();
const resultArea = document.getElementById('result-area');
const linkDisplay = document.getElementById('mirrorLink');
if (!input) {
alert("Please enter a URL");
return;
}
try {
const urlObj = new URL(input);
if (!urlObj.hostname.includes('github.com')) {
alert("Only github.com URLs are allowed");
return;
}
// Replace github.com with the current worker hostname
const workerHost = window.location.host;
const newUrl = input.replace(urlObj.hostname, workerHost);
linkDisplay.href = newUrl;
linkDisplay.textContent = newUrl;
resultArea.style.display = 'block';
// Reset copy button text
const btn = document.getElementById('copyBtn');
btn.textContent = "Copy";
btn.style.backgroundColor = "#444";
} catch (e) {
alert("Invalid URL format");
}
}
function copyToClipboard() {
const link = document.getElementById('mirrorLink').textContent;
const btn = document.getElementById('copyBtn');
navigator.clipboard.writeText(link).then(() => {
btn.textContent = "Copied!";
btn.style.backgroundColor = "#28a745"; // Green success color
setTimeout(() => {
btn.textContent = "Copy";
btn.style.backgroundColor = "#444";
}, 2000);
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
</script>
</body>
</html>
`;
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// 1. SERVE UI
if (url.pathname === '/' || url.pathname === '') {
return new Response(HTML_PAGE, {
headers: { 'Content-Type': 'text/html;charset=UTF-8' }
});
}
// 2. CHECK CACHE (Speed Boost)
const cache = caches.default;
let response = await cache.match(request);
if (response) {
// HIT: Return cached file
let newHeaders = new Headers(response.headers);
newHeaders.set('X-Worker-Cache', 'HIT');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders
});
}
// 3. FETCH FROM ORIGIN (GitHub)
const ghUrl = new URL(GITHUB_URL + url.pathname);
try {
const upstreamReq = new Request(ghUrl, {
method: request.method,
headers: {
'User-Agent': 'Cloudflare-Worker-Mirror',
'Range': request.headers.get('Range') || '' // Support Resume
}
});
const upstreamRes = await fetch(upstreamReq);
// If upstream failed or partial content (206), pass through without caching logic for now
// (Caching 206 ranges is complex, simple caching works best on full 200s)
if (!upstreamRes.ok && upstreamRes.status !== 304) {
return upstreamRes;
}
// 4. PREPARE RESPONSE
let resHeaders = new Headers(upstreamRes.headers);
resHeaders.set('Access-Control-Allow-Origin', '*'); // Allow downloads from anywhere
resHeaders.set('X-Worker-Cache', 'MISS');
// Force browser and Cloudflare to cache this for a long time
if (upstreamRes.status === 200) {
resHeaders.set('Cache-Control', `public, max-age=${CACHE_TTL}`);
}
response = new Response(upstreamRes.body, {
status: upstreamRes.status,
statusText: upstreamRes.statusText,
headers: resHeaders
});
// 5. SAVE TO CACHE (if full file)
if (upstreamRes.status === 200) {
ctx.waitUntil(cache.put(request, response.clone()));
}
return response;
} catch (e) {
return new Response("Error: " + e.message, { status: 500 });
}
}
};