This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.js
251 lines (211 loc) · 9.13 KB
/
index.js
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
#!/bin/env node
"use strict"
let favicon = require('zlib').gzipSync(require('fs').readFileSync('website/favicon.ico')), //load the favicon into memory, and gzip it. the memory footprint is small, and it saves disk reads
express = require('express'),
fs = require('fs'),
app = express(),
request = require('request'),
cors = require('cors')(),
port = process.env.PORT || 8081,
/*Website Static Content Serving*/
staticContent = express.static(process.cwd() + '/website'),
staticContentAbout = express.static(process.cwd() + '/website/about'),
staticContentDonate = express.static(process.cwd() + '/website/donate'),
staticContentFAQ = express.static(process.cwd() + '/website/faq'),
staticContentCDN = express.static(process.cwd() + '/website/cdn'),
staticContentBlog = express.static(process.cwd() + '/website/blog'),
staticContentSource = express.static(process.cwd() + '/website/open-source'),
http = require('https'),
mime = require('mime'),
rawURL = 'https://raw.githubusercontent.com',
gistURL = 'https://gist.githubusercontent.com',
cdnURL = 'cdn.gitcdn.link',
cache = {},
blacklist = [],
tempBlacklist = [],
strikes = {},
collectGarbageInterval = 15000,
charsetOverrides = {
'application/javascript': '; charset=utf-8',
'text/css': '; charset=utf-8',
'text/html': '; charset=utf-8',
'text/plain': '; charset=utf-8',
'application/json': '; charset=utf-8'
}
//Load the cache file, if it exists
try {
blacklist = JSON.parse(fs.readFileSync('blacklist.json'))
}
catch(e) {
console.log("Error: blacklist.json missing")
}
//Start the garbage collection enforcer
setInterval(collectGarbage, collectGarbageInterval)
//create the return url
function createRedirectUrl (headers, meta, sha) {
let scheme = (headers['cf-visitor'] && headers['cf-visitor'].scheme ? headers['cf-visitor'].scheme : 'https'),
host = headers.host || cdnURL
return `${scheme}://${host}/cdn/${meta.user}/${meta.repo}/${sha}/${meta.filePath}`
}
//Used for debugging during development
function debugFunc (req, res, next) {
next()
}
//Serves the favicon accounting for it being pre gzipped
function faviconFunc (req, res) {
res.setHeader('Content-Encoding', 'gzip')
res.setHeader('Content-Type', 'image/x-icon')
res.send(favicon)
}
//Serves the cdn route
function cdnFunc (req, res) {
//Gets the path data
let t = req.originalUrl.substr(4),
blacktlistTests = []
for (var i in blacklist) {
blacktlistTests.push(t.indexOf(blacklist[i]) > -1)
}
if (blacktlistTests.indexOf(true) > -1) {
res.status(403).send("Forbidden - This repo/gist is on the blacklist. If you wish to appeal, please open an issue here: https://github.com/schme16/gitcdn.xyz/issues, with why you feel this repo should not be on the blacklist.")
return false
}
else {
req.pipe(http.request((t.split('/')[3] === 'raw' ? gistURL : rawURL) + t, function(newRes) {
let mimeType = mime.lookup(t)
res.setHeader('Content-Type', mimeType + (charsetOverrides[mimeType] || ''))
res.setHeader("Cache-Control", "public, max-age=2592000");
res.setHeader("Expires", new Date(Date.now() + 2592000000).toUTCString());
newRes.pipe(res)
}).on('error', function(err) {
res.statusCode = 500
res.end()
console.log(new Error('Status 500: couldn\'t pipe file to client || ' + meta.user + '/' + meta.repo + '/' + body.sha + '/' + meta.filePath))
}))
}
}
//Serves the repo route
function repoFunc (req, res) {
let meta = {},
refreshCache = false,
options = {
headers: {
'User-Agent': 'request'
}
}
/*Define the meta data*/
meta.t = req.originalUrl.substr(6)
meta.raw = meta.t.split('/')
meta.user = meta.raw.shift()
meta.repo = meta.raw.shift()
meta.gist = (meta.raw[0] === 'raw' ? true : false)
if (meta.gist) meta.raw.shift()
meta.branch = meta.raw.shift()
meta.filePath = meta.raw.join('/')
let blacktlistTests = []
for (var i in blacklist) {
blacktlistTests.push(meta.user.indexOf(blacklist[i]) > -1)
blacktlistTests.push(meta.user.indexOf(blacklist[i]) > -1)
blacktlistTests.push(meta.repo.indexOf(blacklist[i]) > -1)
blacktlistTests.push(meta.user.indexOf(blacklist[i] + '/' + meta.repo) > -1)
blacktlistTests.push(meta.raw.indexOf(blacklist[i]) > -1)
blacktlistTests.push(meta.branch.indexOf(blacklist[i]) > -1)
blacktlistTests.push(meta.filePath.indexOf(blacklist[i]) > -1)
}
if (blacktlistTests.indexOf(true) > -1) {
res.status(403).send("Forbidden - This repo/gist is on the blacklist. If you wish to appeal, please open an issue here: https://github.com/schme16/gitcdn.xyz/issues, with why you feel this repo should not be on the blacklist.")
return false
}
else if ((!meta.repo && !meta.user) && !meta.gist) {
res.sendStatus(404)
return false
}
else {
/*Set the */
options.url = 'https://api.github.com/' + (meta.gist ? 'gists' : 'repos') + '/' + (meta.gist ? '' : meta.user + '/') + meta.repo + (meta.gist ? '' : '/commits/' + meta.branch + '?client_id=' + process.env.gitcdn_clientid + '&client_secret=' + process.env.gitcdn_clientsecret)
/*if the repo is cached, just send that back, and update it for next time*/
if (cache[meta.user + '/' + meta.repo + (meta.gist ? '' : '/' + meta.branch)]) {
refreshCache = true
lastCall(meta, cache[meta.user + '/' + meta.repo + (meta.gist ? '' : '/' + meta.branch)], req, res)
}
/*Update the repo, and cache it*/
request.get(options, function (err, r, rawBody) {
let body
if (rawBody) {
try {
body = JSON.parse(rawBody)
}
catch (e) {
console.log("Error: ", e)
}
if (meta.gist) meta.repo += '/raw'
if (body && (body.sha || (body.history && body.history[0] && body.history[0].version))) {
lastCall(meta, body.sha || body.history[0].version, req, res, refreshCache)
}
else { //Error
if (!refreshCache) res.sendStatus(500)
console.log("Error: " + 'SHA1 hash is missing in /repo -> request: ' + req.originalUrl + ' JSON=' + JSON.stringify(body))
strikes[meta.filePath] = strikes[meta.filePath] || 0
if (strikes[meta.filePath] >= 10) {
//tempBlacklist.push(meta.filePath)//meta.user + '/' + meta.repo)
}
else {
strikes[meta.filePath]++
}
}
}
else { //Error
if (!refreshCache) res.sendStatus(500)
console.log("Error: " + 'Status 500: ' + meta.user + '/' + meta.repo + '/' + body.sha + '/' + meta.filePath)
}
meta = null
options = null
})
}
}
//Handles redirection and cacheing
function lastCall (meta, sha, req, res, cacheing) {
if (sha && !cacheing) {
let newUrl = createRedirectUrl(req.headers, meta, sha)
cache[meta.user + '/' + meta.repo + (meta.gist ? '' : '/' + meta.branch)] = sha
res.redirect(301, newUrl)
}
else if (!!cacheing) {
cache[meta.user + '/' + meta.repo + (meta.gist ? '' : '/' + meta.branch)] = sha
}
else {
if (!cacheing) res.sendStatus(500)
console.log("Error: " + 'Status 500: SHA1 hash is missing in lastCall() || ' + meta.user + '/' + meta.repo + '/' + sha + '/' + meta.filePath)
//tempBlacklist.push(meta.filePath)//meta.user + '/' + meta.repo)
}
}
//Does mandatory garbage collection at predefined intervals
function collectGarbage () {
if (global.gc) global.gc()
}
//Experimental - Hopeing to reduce the downtime posibly casued by memory limits
setTimeout(function () {
cache = {}
collectGarbage()
setTimeout(collectGarbage, 10000)
}, 4.32e+7)
//Set up the exprtess routes
if (process.env.NODE_ENV === 'development') app.use(debugFunc)
app.set('etag', 'strong') // use strong etags
app.use('/favicon.ico', faviconFunc)//Serve the site icon
app.use('/', staticContent)
app.use('/About', staticContentAbout)
app.use('/FAQ', staticContentFAQ)
app.use('/Donate', staticContentDonate)
app.use('/Blog', staticContentBlog)
app.use('/CDN', staticContentCDN)
app.use('/Open-Source', staticContentSource)
app.use('/about', staticContentAbout)
app.use('/faq', staticContentFAQ)
app.use('/donate', staticContentDonate)
app.use('/blog', staticContentBlog)
app.use('/cdn', staticContentCDN)
app.use('/open-source', staticContentSource)
app.use(cors)
app.get('/cdn/*', cdnFunc)
app.use('/repo/*', repoFunc)
app.listen(port)