forked from soraghallaigh/fh-enyo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
145 lines (125 loc) · 3.62 KB
/
server.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
var http = require("http"),
https = require("https"),
url = require("url"),
path = require("path"),
fs = require("fs"),
argv = process.argv,
args = {
baseDomain: ".feedhenry.com",
//default domain
domain: "apps",
//app guid
app: "",
//default port
port: 8888
},
//small subset of mime types so to prevent warnings in chrome
mimeTypes = {
js: "text/javascript",
css: "text/css",
html: "text/html",
xml: "text/xml"
};
//parse the parameters
for(var i = 0, il = argv.length; i < il; i++) {
var param = argv[i].split("=");
if(param.length === 2) {
args[param[0]] = param[1];
}
}
var domain = args.domain + args.baseDomain,
injection = ['<script src="https://' + domain + '/static/pec/script/studio/155-scripts.js"></script>',
'<script>',
' $fhclient = $fh.init({',
' "appMode":"debug",',
' "checkDeliveryScheme":true,',
' "debugCloudType":"fh",',
' "debugCloudUrl":',
' "https://' + domain + '",',
' "deliveryScheme":"https://",',
' "destination":{',
' "inline":false,',
' "name":"studio"',
' },',
' "domain": "' + args.domain + '",',
' "host": "' + domain + '",',
' "nameserver":"https://ainm.feedhenry.com",',
' "releaseCloudType":"fh",',
' "releaseCloudUrl":"https://' + domain + '",',
' "swagger_view":"Sju8tJFwM7kox_S1rr1wZ2PS",',
' "urltag":"",',
' "useSecureConnection":true,',
' "user":{',
' "id":"YqxcBngHv4nt3j1VstTjQj0X",',
' "role":"sub"',
' },',
' "widget":{',
' "guid": "' + args.app + '",',
' "inline":false,',
' "instance": "' + args.app +'",',
' "version":328',
' }',
' });',
'</script>'].join('');
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri),
post = new Buffer(parseInt(request.headers["content-length"], 10)),
postLen = 0;
request.on("data", function(chunk) {
chunk.copy(post, postLen);
postLen += chunk.length;
});
request.on("end", function() {
path.exists(filename, function(exists) {
if(!exists && uri.indexOf("/box/srv/") !== -1) {
var proxyReq = https.request({
path: request.url,
method: request.method,
host: args.domain + args.baseDomain,
headers: request.headers
}, function(proxyRes) {
response.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.on("data", function(chunk) {
response.write(chunk);
});
proxyRes.on("end", function() {
response.end();
});
});
proxyReq.write(post);
proxyReq.end();
return;
}
else if(!exists) {
response.writeHead(404,{
"Content-Type": mimeTypes.html
});
response.write("404 Not found");
response.end();
return;
}
if (fs.statSync(filename).isDirectory())
filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {
"Content-Type": "text/plain"
});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200,{
//set the response type or default to text/plain
"content-type": mimeTypes[filename.split(".").pop()] || "text/plain"
});
if(filename.indexOf("index.html") > 0) {
response.write(injection);
}
response.write(file, "binary");
response.end();
});
});
});
}).listen(parseInt(args.port, 10));