-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
375 lines (347 loc) · 11.4 KB
/
main.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
const fs=require("fs")
const url = require('url');
const path=require("path")
const util=require("util")
const zlib=require("zlib")
const http=require("http")
const http2=require("http2")
const child=require("child_process")
const mimetype={
".html":"text/html",
".css":"text/css",
".js":"application/javascript",
".json":"application/json",
".svg":"image/svg+xml",
".png":"image/png",
".jpg":"image/jpeg",
".jpeg":"image/jpg",
".gif":"image/gif",
".ico":"image/vnd.microsoft.icon",
".rss":"application/rss+xml"
}
const defaultErrorMessage={
400:"pikaServiceError: bad request\n",
404:"pikaServiceError: not found\n",
500:"pikaServiceError: internal server error\n"
}
const serverStartTime=new Date()
const getConfig=()=>{
let config={}
try{
let configPath=process.argv[2]?path.resolve(process.cwd(),process.argv[2]):"./config.json"
config=require(process.argv[2]||"./config.json")
}catch(err){
console.log(`Warning: ${err.message}`)
console.log(`Warning: use default config`)
}
return config
}
const config=getConfig()
const{host="::",port:httpPort="80",defaultDomain="domain.example",basePath="/home/user/static/",https={},actions:actionsSchema=[],plugins=[],errorMessage,errorPage}=config
const{open:httpsOpen=false,hsts=true,port:httpsPort="443",key="",cert="",renew={}}=https
const{open:renewOpen=false,period:renewPeriod=1728000000,command:renewCommand="",commandArgs:renewArgs=[]}=renew
const encoders=[{name:"gzip",builder:()=>zlib.createGzip()},{name:"deflate",builder:()=>zlib.createDeflate()}]
const serveStream=async(context,stream,code)=>{
const encode=(context.request.headers["accept-encoding"]||"").split(",").map((str)=>str.trim())
console.log(`---- [${context.reqId}] supported encoding: ${encode}`)
for(const encoder of encoders){
if(encode.includes(encoder.name)){
context.respond.writeHead(code,{"Content-Encoding":encoder.name})
console.log(`---- [${context.reqId}] use encoding: ${encoder.name}`)
stream.pipe(encoder.builder()).pipe(context.respond)
return
}
}
context.respond.writeHead(code)
stream.pipe(context.respond)
}
const serveConfirmedFile=async(context,filepath,code,checkTime)=>{
if(checkTime&&(context.stat!==undefined)){
let mime=mimetype[path.extname(filepath)]
if(mime){
context.respond.setHeader("Content-Type",mimetype[path.extname(filepath)])
}
let stat=context.stat
let mtime=stat.mtime.getTime()<serverStartTime.getTime()?serverStartTime:stat.mtime
context.respond.setHeader("Last-Modified",mtime.toUTCString())
let ifModifiedAfter=Date.parse(context.request.headers["if-modified-since"])
if(!isNaN(ifModifiedAfter)){
if(path.extname(filepath)!==".html"){
console.log(`---- [${context.reqId}] detected header if-modified-since: ${new Date(ifModifiedAfter).toUTCString()}(${ifModifiedAfter})`)
if(mtime.getTime()-ifModifiedAfter<=999){
console.log(`---- [${context.reqId}] file is not modified, send 304`)
context.respond.writeHead(304)
context.respond.end()
return
}
}
}
}
await serveStream(context,fs.createReadStream(filepath),code)
}
const serveFile=async(context,filepath,code=200,checkTime=true,throwInsteadOf404=false)=>{
console.log(`---- [${context.reqId}] ask for ${filepath}`)
try{
await util.promisify(fs.access)(filepath,fs.constants.R_OK)
}catch(err){
console.log(`---- [${context.reqId}] not find`)
if(throwInsteadOf404){
throw new Error("file not found")
}else{
serveError(context,404)
return
}
}
const stat=await util.promisify(fs.stat)(filepath)
if(stat.isDirectory()){
console.log(`---- [${context.reqId}] requested file is a directory`)
if((context.processedPath[context.processedPath-1]!=="/")&&(context.url.pathname[context.url.pathname.length-1]!=="/")){
console.log(`---- [${context.reqId}] requested path do not end with "/", 301 redirect`)
context.respond.writeHead(301,{Location:`${context.url.pathname}/`})
context.respond.end()
}else{
console.log(`---- [${context.reqId}] try /index.html`)
const indexpath=path.join(filepath,"/index.html")
try{
await util.promisify(fs.access)(indexpath,fs.constants.R_OK)
const stat=(await util.promisify(fs.stat)(indexpath))
if(stat.isDirectory()){
throw new Error("index is directory")
}
context.stat=stat
}catch(err){
console.log(`---- [${context.reqId}] index not find`)
if(throwInsteadOf404){
throw new Error("file not found")
}else{
serveError(context,404)
return
}
}
await serveConfirmedFile(context,indexpath,code,checkTime)
}
}else{
context.stat=stat
await serveConfirmedFile(context,filepath,code,checkTime)
}
}
const serveError=async(context,errorCode)=>{
let customErrorPage=context.errorPage
if(customErrorPage!==undefined){
if(errorCode in customErrorPage){
let filepath=path.resolve(context.baseErrorPath,customErrorPage[errorCode])
try{
await serveFile(context,filepath,errorCode,false,true)
return
}catch(err){
console.log(`---- [${context.reqId}] error page for ${errorCode} (${filepath}) not found`)
}
}
}
let customErrorMessage=context.errorMessage
if(customErrorMessage!==undefined){
if(errorCode in customErrorMessage){
context.respond.writeHead(errorCode)
context.respond.end(customErrorMessage[errorCode])
return
}
}
if(errorCode in errorPage){
let filepath=path.resolve(context.baseErrorPath,errorPage[errorCode])
try{
await serveFile(context,filepath,errorCode,false,true)
return
}catch(err){
console.log(`---- [${context.reqId}] error page for ${errorCode} (${filepath}) not found`)
}
}
context.respond.writeHead(errorCode)
context.respond.end((errorCode in errorMessage)?(errorMessage[errorCode]):(defaultErrorMessage[errorCode]||"pikaServiceError: unknown error\n"))
}
const defaultAction=async(context)=>{
await serveFile(context,path.join(context.basePath,context.processedPath))
return true
}
const rangeBlockBuilder=(schema)=>async(context)=>{
if(path.relative(schema.path,context.processedPath)[0]!=="."){
console.log(`---- [${context.reqId}] blocked: child of "${schema.path}"`)
await serveError(context,404)
return true
}
return false
}
const pathRewriteBuilder=(schema)=>async(context)=>{
if(context.processedPath===schema.from){
console.log(`---- [${context.reqId}] rewrited: from "${schema.from}" to "${schema.to}"`)
context.processedPath=schema.to
}
return false
}
const rangePathRewriteBuilder=(schema)=>async(context)=>{
if(path.relative(schema.from,context.processedPath)[0]!=="."){
console.log(`---- [${context.reqId}] rewrited: from child of "${schema.from}" to "${schema.to}"`)
context.processedPath=schema.to
}
return false
}
const serveFileBuilder=(schema)=>async(context)=>{
if(context.processedPath===schema.path){
console.log(`---- [${context.reqId}] hit: path "${schema.path}", send file "${schema.file}"`)
await serveFile(context,path.resolve(context.basePath,schema.file))
return true
}
return false
}
const rangeServeFileBuilder=(schema)=>async(context)=>{
let relative=path.relative(schema.path,context.processedPath)
if(relative[0]!=="."){
console.log(`---- [${context.reqId}] hit: path "${schema.path}", reset base to "${schema.base}"`)
await serveFile(context,path.join(path.resolve(context.basePath,schema.base),relative))
return true
}
return false
}
let actionBuilders={
rangeBlock:rangeBlockBuilder,
pathRewrite:pathRewriteBuilder,
rangePathRewrite:rangePathRewriteBuilder,
serveFile:serveFileBuilder,
rangeServeFile:rangeServeFileBuilder
}
const actionBuilder=(schema)=>{
if(schema.type in actionBuilders){
return actionBuilders[schema.type](schema)
}
console.log(`Warning: schema "${schema}" is not recognized`)
return async(context)=>false
}
const builderUtil={
actionBuilder:actionBuilder,
serveError:serveError,
serveFile:serveFile,
serveConfirmedFile:serveConfirmedFile,
serveStream:serveStream
}
const registerPlugin=(identifier)=>{
try{
const{builder,type}=require(identifier)(config,builderUtil)
actionBuilders[type]=builder
}catch(err){
console.log(`Warning: ${err.message}`)
console.log(`Warning: plugin "${identifier}" is not registered`)
}
}
plugins.forEach(registerPlugin)
const actions=actionsSchema.map(actionBuilder)
let reqNum=0
const handler=async(req,res)=>{
const reqId=Date.now()+reqNum
reqNum++
console.log(`-- [${reqId}] request heared at ${new Date()}`)
const rawDomain=("host" in req.headers)?url.domainToUnicode(req.headers.host):defaultDomain
const domain=rawDomain===""?defaultDomain:rawDomain
const reqUrl=new URL(req.url,`http://${domain}`)
const processedPath=(()=>{
try{
return path.posix.normalize(path.posix.join(path.posix.sep,decodeURIComponent(reqUrl.pathname)))
}catch{
return null
}
})()
console.log(`---- [${reqId}] ask for "${processedPath}" under "${domain}" with search "<${reqUrl.search}>"`)
const context={request:req,respond:res,domain:domain,url:reqUrl,processedPath:processedPath,reqId:reqId,basePath:basePath,baseErrorPath:basePath}
if(processedPath===null){
console.log(`---- [${reqId}] bad request: URI malformed`)
await serveError(context,400)
return
}
for(const action of actions){
if(await action(context)){
return
}
}
try{
console.log(`---- [${reqId}] fallback to default action, try serve static file`)
await defaultAction(context)
}catch(err){
let code=err.code
if(!(Number.isInteger(code)&&code<600&&code>=100)){
code=500
}
console.log(`---- [${reqId}] error when handling request, code=${code}, err:${err}:\n${err.stack}`)
await serveError(context,code)
}
}
let httpServer=null
let httpsServer=null
const setupHttpServer=()=>new Promise((res,rej)=>{
httpServer=http.createServer((req,res)=>{
if(httpsOpen&&hsts&&httpsServer){
res.setHeader("Strict-Transport-Security","max-age=7776000")
res.writeHead(302,{Location:`https://${req.headers.host}${req.url}`})
res.end()
return
}
handler(req,res)
})
httpServer.listen(httpPort,host,(err)=>{
if(err){
console.log(`-- pikaService(http/1.1 over TCP) fail to start`)
httpServer=null
rej(err)
}
console.log(`-- pikaService(http/1.1 over TCP) running at http://${host}:${httpPort}/`)
res()
})
})
const setupHttpsServer=()=>new Promise((res,rej)=>{
httpsServer=http2.createSecureServer({allowHTTP1:true,key:fs.readFileSync(key),cert:fs.readFileSync(cert)},(req,res)=>{
if(hsts){
res.setHeader("Strict-Transport-Security","max-age=7776000")
}
handler(req,res)
})
httpsServer.listen(httpsPort,host,(err)=>{
if(err){
console.log(`-- pikaService(http/1.1&http/2 over TLS) fail to start`)
httpsServer=null
rej(err)
}
console.log(`-- pikaService(http/1.1&http/2 over TLS) running at http://[${host}]:${httpsPort}/`)
res()
})
})
const updateHttpsServer=()=>new Promise((res,rej)=>{
console.log(`-- trying to update the cert`)
child.spawn(renewCommand,renewArgs,{stdio:["ignore","inherit","inherit"]}).on("close",(err)=>{
if(err){
rej(err)
}else{
if(httpsServer===null){
setupHttpsServer().then(res)
}else{
console.log(`-- update cert pikaService(http/1.1&http/2 over TLS)`)
httpsServer.setSecureContext({key:fs.readFileSync(key),cert:fs.readFileSync(cert)})
}
}
})
})
const scheduleRenewJob=()=>{
console.log(`-- next cert update scheduled`)
setTimeout(()=>{
updateHttpsServer()
scheduleRenewJob();
},renewPeriod)
}
process.on("unhandledRejection",(err,promise)=>{
console.log(`Unhandled Rejection at: ${promise}\nreason: ${err}`)
process.exitCode=-1
})
setupHttpServer()
if(httpsOpen){
if(renewOpen){
updateHttpsServer().then(scheduleRenewJob)
}else{
setupHttpsServer()
}
}