-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
354 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
var fs=require('fs'); | ||
module.exports=class he_template{ | ||
constructor(template_dir){ | ||
this.file_root='./'; | ||
this.file_buf={}; | ||
this.last=''; | ||
if(typeof template_dir !== 'undefined'){ | ||
this.dir(template_dir); | ||
} | ||
} | ||
dir(template_dir){ | ||
this.file_root=template_dir; | ||
} | ||
open(filename,name){ | ||
if(typeof name !== 'undefined'){ | ||
this.last=name; | ||
} | ||
var file_buf=this.file_buf; | ||
var last=this.last; | ||
fs.readFile(this.file_root+filename,function read(err,data){ | ||
if(typeof data !== 'undefined'){ | ||
file_buf[last]=data; | ||
} | ||
}); | ||
} | ||
get(name){ | ||
if(typeof name !== 'undefined'){ | ||
this.last=name; | ||
} | ||
return this.file_buf[this.last]; | ||
} | ||
set(name,value){ | ||
if(typeof name !== 'undefined'){ | ||
this.last=name; | ||
this.file_buf[this.last]=value; | ||
} | ||
} | ||
assign(tag,value,name){ | ||
if(typeof name !== 'undefined'){ | ||
this.last=name; | ||
} | ||
if(typeof this.file_buf[this.last] !== 'undefined'){ | ||
this.file_buf[this.last]=this.file_buf[this.last].toString().replace('{'+tag+'}',value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#!/usr/bin/env node | ||
var app_port=3000; | ||
var zlib = require('zlib'); | ||
var http=require('http'); | ||
var express=require('express'); | ||
var bodyparser=require('body-parser'); | ||
var fs=require('fs'); | ||
var app=express(); | ||
var _GET={}; | ||
var _POST={}; | ||
var cookies={}; | ||
var path_query=''; | ||
var path=''; | ||
var path_array=[]; | ||
var result=''; | ||
var content=''; | ||
var replace={}; | ||
var global={}; | ||
var url_reg = new RegExp('^[a-z0-9A-Z\-_!~@\$\% \+\=\*\&:;,\.]+$'); | ||
|
||
var he_template=require('./class/template.js'); | ||
var t=new he_template('./templates/'); | ||
t.open('index.tpl','content'); | ||
|
||
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; | ||
} | ||
|
||
function parse_query(el,i,arr){ | ||
var buf_arr=el.split('='); | ||
if(buf_arr[0]){ | ||
_GET[buf_arr[0]]=buf_arr[1]; | ||
} | ||
} | ||
|
||
function parse_url(url){ | ||
var find_query=url.indexOf('?'); | ||
if(typeof find_query !== 'undefined'){ | ||
var path_query_arr=url.split('?'); | ||
path=''+path_query_arr[0]; | ||
path_query=''+path_query_arr[1]; | ||
var find_query_delimiter=path_query.indexOf('&'); | ||
if(typeof find_query_delimiter !== 'undefined'){ | ||
path_query_arr=path_query.split('&'); | ||
path_query_arr.forEach(parse_query); | ||
} | ||
else{ | ||
parse_query(path_query,0,_GET); | ||
} | ||
} | ||
path_array=path.split('/'); | ||
} | ||
|
||
function fresh_query(req){ | ||
t.open('index.tpl','content'); | ||
_GET={}; | ||
_POST={}; | ||
cookies={}; | ||
path_query=''; | ||
path=''; | ||
path_array=[]; | ||
result=''; | ||
content=''; | ||
replace={}; | ||
global={}; | ||
_POST=req.body; | ||
cookies=parse_cookies(req); | ||
parse_url(req.url); | ||
} | ||
|
||
app.all('*',function(req,res){ | ||
fresh_query(req); | ||
|
||
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({'path_array':path_array,'content':content,'_GET':_GET,'_POST':_POST,'replace':replace,'res':res,'cookies':cookies,'global':global}); | ||
module.exec(); | ||
content=module.result().content; | ||
replace=module.result().replace; | ||
global=module.result().global; | ||
res=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({'path_array':path_array,'content':content,'_GET':_GET,'_POST':_POST,'replace':replace,'res':res,'cookies':cookies,'global':global}); | ||
module.exec(); | ||
content=module.result().content; | ||
replace=module.result().replace; | ||
global=module.result().global; | ||
res=module.result().response; | ||
} | ||
|
||
if(!global.redirect){ | ||
replace.content=content; | ||
Object.keys(replace).forEach(function(key){ | ||
t.assign(key,replace[key]); | ||
}); | ||
if(''!=replace.content){ | ||
result=t.get(); | ||
} | ||
else{ | ||
res.status(404).end('Not Found'); | ||
} | ||
|
||
var buf = new Buffer(result, 'utf-8'); | ||
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8', 'Content-Encoding': 'gzip'}); | ||
zlib.gzip(buf, function (_, result) { | ||
res.end(result); | ||
}); | ||
} | ||
}); | ||
|
||
app.listen(app_port,function(){ | ||
console.log(`Starting Hidden Engine on ${app_port} port...`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
module.exports=class he_module{ | ||
constructor(obj){ | ||
this.global=obj.global; | ||
this.content=obj.content; | ||
this.replace=obj.replace; | ||
this.path_array=obj.path_array; | ||
this._GET=obj._GET; | ||
this._POST=obj._POST; | ||
this.cookies=obj.cookies; | ||
this.res=obj.res; | ||
} | ||
exec(){//http://expressjs.com/ru/4x/api.html#res | ||
this.content+='<h1>Hidden Engine Index</h1>'; | ||
if(this.global.auth){ | ||
this.content+='<p>Authorized</p>'; | ||
this.content+='<p><a href="/logout/">Logout</a></p>'; | ||
} | ||
else{ | ||
this.content+='<p>Not authorized</p>'; | ||
this.content+='<p><a href="/login/">Login</a></p>'; | ||
} | ||
this.content+='<h2>POST data test</h2>'; | ||
if(typeof this._POST.data !== 'undefined'){ | ||
this.content+=`<p>Your data: ${this._POST.data}</p>`; | ||
} | ||
else{ | ||
this._POST.data=''; | ||
} | ||
this.content+='<form action="" method="POST"><input type="text" name="data" value="'+this._POST.data+'"><input type="submit" name="send" value="Send!"></form>'; | ||
} | ||
result(){ | ||
return {'content':this.content,'replace':this.replace,'response':this.res,'global':this.global} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
var crypto = require('crypto'); | ||
module.exports=class he_module{ | ||
constructor(obj){ | ||
this.global=obj.global; | ||
this.content=obj.content; | ||
this.replace=obj.replace; | ||
this.path_array=obj.path_array; | ||
this._GET=obj._GET; | ||
this._POST=obj._POST; | ||
this.cookies=obj.cookies; | ||
this.res=obj.res; | ||
} | ||
exec(){ | ||
if(this.global.auth){ | ||
this.global.redirect=true; | ||
this.res.redirect(302,'/'); | ||
} | ||
else{ | ||
if(typeof this._POST.login !== 'undefined'){ | ||
this.res.cookie('login',this._POST.login,{}); | ||
this.res.cookie('password',crypto.createHash('md5').update(this._POST.password).digest("hex"),{}); | ||
this.global.redirect=true; | ||
this.res.redirect(302,'/login/'); | ||
} | ||
this.content+='<h1>Authorization</h1>'; | ||
if(''!=this.cookies.login){ | ||
this.content+='<p><strong>Auth error!</strong></p>'; | ||
} | ||
this.content+=`<form action="" method="POST"> | ||
<input type="text" name="login" placeholder="Login"><br> | ||
<input type="password" name="password" placeholder="Password"><br> | ||
<input type="submit" value="Enter"> | ||
</form>`; | ||
} | ||
} | ||
result(){ | ||
return {'content':this.content,'replace':this.replace,'response':this.res,'global':this.global} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
module.exports=class he_module{ | ||
constructor(obj){ | ||
this.global=obj.global; | ||
this.content=obj.content; | ||
this.replace=obj.replace; | ||
this.path_array=obj.path_array; | ||
this._GET=obj._GET; | ||
this._POST=obj._POST; | ||
this.cookies=obj.cookies; | ||
this.res=obj.res; | ||
} | ||
exec(){ | ||
this.res.cookie('login','',{}); | ||
this.res.cookie('password','',{}); | ||
this.global.redirect=true; | ||
this.res.redirect(302,'/'); | ||
} | ||
result(){ | ||
return {'content':this.content,'replace':this.replace,'response':this.res,'global':this.global} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
var crypto = require('crypto'); | ||
module.exports=class he_module{ | ||
constructor(obj){ | ||
this.global=obj.global; | ||
this.content=obj.content; | ||
this.replace=obj.replace; | ||
this.path_array=obj.path_array; | ||
this._GET=obj._GET; | ||
this._POST=obj._POST; | ||
this.cookies=obj.cookies; | ||
this.res=obj.res; | ||
} | ||
exec(){ | ||
var admin_login='admin'; | ||
var admin_password='he_admin_password'; | ||
var admin_password_hash=crypto.createHash('md5').update(admin_password).digest("hex"); | ||
|
||
this.global.redirect=false; | ||
this.replace.title='NodeJS Hidden Engine'; | ||
this.replace.description='Light engine for node.js apps, author Anatoly Piskunov'; | ||
this.global.auth=false; | ||
if(typeof this.cookies.login !== 'undefined'){ | ||
if(this.cookies.login==admin_login){ | ||
if(this.cookies.password==admin_password_hash){ | ||
this.global.auth=true; | ||
} | ||
} | ||
} | ||
} | ||
result(){ | ||
return {'content':this.content,'replace':this.replace,'response':this.res,'global':this.global} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "hidden_engine", | ||
"version": "0.0.1", | ||
"description": "Hidden Engine for light node.js sites", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Anatoly Piskunov aka On1x", | ||
"license": "MIT", | ||
"dependencies": { | ||
"body-parser": "^1.18.0", | ||
"crypto": "^1.0.1", | ||
"express": "^4.15.4", | ||
"zlib": "^1.0.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>{title}</title> | ||
<meta name="description" content="{description}"> | ||
</head> | ||
<body> | ||
{content} | ||
</body> | ||
</html> |