forked from On1x/HiddenEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (194 loc) · 5.32 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
#!/usr/bin/env node
var express=require('express');
var app=express();
var zlib=require('zlib');
var http=require('http');
var https=require('https');
var bodyparser=require('body-parser');
var fs=require('fs');
global.he=require('./class/global.js');
var he_watchers=require('./class/watchers.js');
var watchers;
watchers=new he_watchers();
var url_reg = new RegExp('^[a-z0-9A-Z\-_!~@\$\% \+\=\*\&:;,\.]+$');
var he_template=require('./class/template.js');
app.use(bodyparser.urlencoded({extended:true}));
app.use(bodyparser.json());
function parse_cookies(request){
var list = {},
rc = request.headers.cookie;
rc && rc.split(';').forEach(function( cookie ) {
var parts = cookie.split('=');
list[parts.shift().trim()] = decodeURI(parts.join('='));
});
return list;
}
app.all('*',function(req,response){
if(!req.secure){
response.redirect(302,'https://'+req.hostname+'/');
response.end();
}
else{
var _GET={};
var _POST={};
var cookies={};
var path_query='';
var path='';
var path_array=[];
var content='';
var replace={};
var session={};
_POST=req.body;
cookies=parse_cookies(req);
if(-1!=req.url.indexOf('?')){
var path_query_arr=req.url.split('?');
path=''+path_query_arr[0];
path_query=''+path_query_arr[1];
if(-1!=path_query.indexOf('&')){
path_query_arr=path_query.split('&');
for(i=0;i<path_query_arr.length;i++){
if(-1!=path_query_arr[i].indexOf('=')){
var buf_arr=path_query_arr[i].split('=');
if(buf_arr[0]){
_GET[buf_arr[0]]=buf_arr[1];
}
}
else{
_GET[path_query_arr[i]]='';
}
}
}
else{
if(-1!=path_query.indexOf('=')){
var buf_arr=path_query.split('=');
if(buf_arr[0]){
_GET[buf_arr[0]]=buf_arr[1];
}
}
else{
_GET[path_query]='';
}
}
}
else{
path=req.url;
path_query='';
}
path_array=path.split('/');
if(('public'==path_array[1])||('uploads'==path_array[1])){
path=path.replace('..','');
fs.stat(__dirname+path,function(err,stat){
if(stat.isDirectory()){
response.status(403).end('Forbidden');
}
else{
if(fs.existsSync(__dirname+path)){
response.sendFile(__dirname+path);
}
else{
response.status(404).end('Not Found');
}
}
});
}
else{
var module_name='prepare';
var module_file='./module/'+module_name+'.js';
if(fs.existsSync(module_file)){
var he_module=require(module_file);
var module=new he_module({
'content':content,
'replace':replace,
'session':session,
'path_array':path_array,
'_GET':_GET,
'_POST':_POST,
'cookies':cookies,
'response':response,
}).then(module_result=>{
content=module_result.content;
replace=module_result.replace;
session=module_result.session;
var path_array=module_result.path_array;
var _GET=module_result._GET;
var _POST=module_result._POST;
var cookies=module_result.cookies;
var response=module_result.response;
var module_name='index';
if(''!=path_array[1]){
if(url_reg.test(path_array[1])){
module_name=path_array[1];
}
}
var module_file='./module/'+module_name+'.js';
if(fs.existsSync(module_file)){
var he_module=require(module_file);
var module=new he_module({
'content':content,
'replace':replace,
'session':session,
'path_array':path_array,
'_GET':_GET,
'_POST':_POST,
'cookies':cookies,
'response':response,
}).then(module_result=>{
content=module_result.content;
replace=module_result.replace;
session=module_result.session;
var path_array=module_result.path_array;
var _GET=module_result._GET;
var _POST=module_result._POST;
var cookies=module_result.cookies;
var response=module_result.response;
var result='';
var t=new he_template('./templates/');
global.he.watch_manager.save_global=1;
if(!session.redirect){
t.open(session.change_template,'content',function(){
replace.content=content;
Object.keys(replace).forEach(function(key){
t.assign(key,replace[key]);
});
if(''!=replace.content){
result=t.get();
var buf = new Buffer(result, 'utf-8');
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8', 'Content-Encoding': 'gzip'});
zlib.gzip(buf, function (_, result) {
response.end(result);
});
}
else{
response.status(404).end('Not Found');
}
});
}
else{
response.end();
}
});
}
});
}
}
}
});
var options={
key: fs.readFileSync(__dirname+'/ssl/ssl.key'),
cert: fs.readFileSync(__dirname+'/ssl/ssl.crt')
};
var http_server=http.createServer(app).listen(80);
var https_server=https.createServer(options, app).listen(443);
process.on('SIGTERM', he_shutdown);
process.on('SIGINT', he_shutdown);
function he_shutdown(){
console.log('Received kill signal, shutting down gracefully');
http_server.close(()=>{
console.log('HTTP server: Closed out remaining connections');
https_server.close(()=>{
console.log('HTTPS server: Closed out remaining connections');
watchers.process_destroy(process);
setTimeout(function(){console.log('Watchers: Timeout... Exit...');process.exit(0);},5000);
});
});
}