-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (125 loc) · 3.3 KB
/
server.js
File metadata and controls
150 lines (125 loc) · 3.3 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
require('./config.js');
Function.prototype.bind = function(scope)
{
var _this = this;
return function()
{
return _this.apply(scope, arguments);
}
}
var frontController = function()
{
this.packages = new (require('./model/packageCollection').collection);
}
frontController.prototype =
{
process: function(req, res)
{
req.on('end', function() { });
var env = {response: res, request: req}
req.parsed = require('url').parse(req.url, true);
this.getHandshake(env);
var routeParts = req.parsed.pathname.match(/([A-Z0-9a-z]+)/g) || ['index'];
var controller = routeParts[0] || 'index';
var action = routeParts[1] || 'index';
try
{
var response = require('./controller/' + controller)[action](env);
}
catch (e)
{
console.log(e);
var response = { status: 404, msg: 'Invalid request' }
//throw(e);
}
if (response)
{
this.processResponse(env, response);
}
},
statusResponse: function(env, status, message)
{
this.processResponse(env, { status: status, msg: message || '' });
},
processResponse: function(env, response)
{
env.response.writeHead(response.status, {
'Content-Type': 'text/plain'
});
env.response.write(response.msg);
env.response.end();
},
getQuery: function(env)
{
return function(varName)
{
return this.getQueryVar(env, varName);
}.bind(this);
},
getQueryVar: function(env, varName)
{
return env.request.parsed.query[varName];
},
getPackages: function()
{
return this.packages;
},
createHandshake: function(packages, domain)
{
var pc = this.getPackages();
var allowedPackages = pc.getFreePackages();
allowedPackages.push('livecart');
if (packages)
{
allowedPackages = allowedPackages.concat(packages);
}
var packageDetails = {}
allowedPackages.forEach(function(pkg)
{
var packageInfo = {};
pc.getPackageChannels(pkg).forEach(function(channel)
{
packageInfo[channel] = pc.getPackageByName(pkg, channel);
});
// check if object is not empty
if ((function() {for(var v in packageInfo){return true;} return false;})())
{
packageDetails[pkg] = packageInfo;
}
}.bind(this));
var cipher = require('crypto').createCipher('aes-256-cbc', HANDSHAKE_KEY + domain);
var crypted = cipher.update(JSON.stringify(allowedPackages), 'utf8', 'hex');
crypted += cipher.final('hex')
return { packages: packageDetails, handshake: crypted, status: 'ok' }
},
getHandshake: function(env)
{
var handshake = this.getQueryVar(env, 'handshake');
if (handshake)
{
var decipher = require('crypto').createDecipher('aes-256-cbc', HANDSHAKE_KEY + this.getQueryVar(env, 'domain'));
var dec = decipher.update(handshake, 'hex', 'utf8');
dec += decipher.final('utf8');
env.allowedPackages = JSON.parse(dec);
}
},
getRequestRelease: function(env)
{
var name = this.getQueryVar(env, 'package');
if (this.isPackageAllowed(env, name))
{
return this.packages.getRelease(name, this.getQueryVar(env, 'version'));
}
else
{
}
},
isPackageAllowed: function(env, name)
{
return this.packages.isFreePackage(name) || (env.allowedPackages && env.allowedPackages.indexOf(name) > -1);
}
}
fc = new frontController();
require('http').createServer(fc.process.bind(fc)).listen(PORT, HOST);
require('sys').puts("Server at http://" + HOST + ':' + PORT.toString() + '/');
exports.fc = frontController;