forked from raja-jamwal/google-sites-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (140 loc) · 5.25 KB
/
index.js
File metadata and controls
166 lines (140 loc) · 5.25 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Created by Raja Jamwal on 27/2/17.
* (c) DataGrid Softwares LLP
*/
var express = require('express'),
app = express(),
https = require('https'),
config = require('./config.js'),
mysql = require('mysql'),
cache = require('memory-cache'),
_ = require('lodash'),
bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
var connection = null;
app.set('port', process.env.port || config.port);
var handleRequest = function(request, response, domain) {
var domainInfo = cache.get(domain);
if (domainInfo == null){
response.end("");
return;
}
var userSite = domainInfo.site;
mappedDomain = config.googleSitesRoot + userSite,
url = request.url;
console.log(url);
var mappedUrl = url == '/' ? mappedDomain : config.googleSites+url;
https.get(mappedUrl, function(sitesResponse) {
var data = "";
sitesResponse.on('data', function (chunk) {
data += chunk;
});
sitesResponse.on('end', function () {
//Hide Google Footer.
data = data.replace("<footer", "<footer style='display:none;' ");
//Change fav_icon
//<link rel="icon" href="//ssl.gstatic.com/atari/images/favicon_2.ico" />
regex = /<link rel="icon"[^>]*>/g
new_html = "<link rel=\"icon\" href=\"" + domainInfo.fav_icon + "\" />";
data = data.replace(regex, new_html)
//Change og_url
//<meta itemprop="url" property="og:url" content="https://sites.google.com/magik.vn/skyx" />
regex = /<meta itemprop="url" property="og:url" [^>]*>/
new_html = "";
//"<meta itemprop=\"url\" property=\"og:url\" content=\"" + request.url + "\" />";
data = data.replace(regex, new_html)
//Change og_image
//<meta itemprop="image" property="og:image" content="http://english.magik.vn/htdocs/magik.vn_cdn/magik_heroes.jpg" />
regex = /<meta itemprop="image" property="og:image"[^>]*>/
new_html = "<meta itemprop=\"image\" property=\"og:image\" content=\"" + domainInfo.og_image + "\" />"
+ "<meta property=\"og:description\" content=\"" + domainInfo.og_desc + "\"/>";
data = data.replace(regex, new_html)
response.end(data);
});
});
};
app.post("/add/user", function(request, response) {
var fields = {
name: request.body.name,
email: request.body.email,
site: request.body.site,
domain: request.body.domain
};
var insertQuery = "INSERT into users (name, email, site, domain) VALUES (':name', ':email', ':site', ':domain');";
for (var key in fields) {
insertQuery = insertQuery.replace(":"+key, fields[key]);
}
console.log(insertQuery);
connection.query(insertQuery, function(err, rows, fields) {
if (!err)
{
response.send("SUCCESS");
}
else {
response.status(500);
response.send("Oh well Database issue, that's all I know, Contact :supportEmail".replace(':supportEmail', config.support.email));
console.log('Error while performing Query.');
}
});
});
app.all('*', function(request, response) {
var domain = (request.headers.host.split(':')[0]).replace('www.','');
var cacheValid = false;
if (cache.get(domain)){
dataCached = cache.get(domain);
current_time = new Date().getTime();
if (current_time - dataCached['cache_time'] < config.cache_duration) //cache chua qua 10 giay.
{
cacheValid = true;
console.log(">>LOAD CACHE " + domain + " " + (current_time - dataCached['cache_time']));
} else {
console.log(">>EXPIRE CACHE. RELOAD" + domain);
}
} else {
console.log(">>LOAD DB TO CACHE " + domain);
}
if (false === cacheValid) {
console.log("***Making DB Request **");
var query = 'SELECT * from users where domain=":domain" or domain="www.:domain"'.replace(/:domain/g, domain);
console.log(query);
connection.query(query, function(err, rows, fields) {
if (!err)
{
if (!_.isEmpty(rows)) {
var dbResult = _.head(rows);
dbResult['cache_time'] = new Date().getTime();
console.log(JSON.stringify(dbResult));
cache.put(dbResult.domain, dbResult);
handleRequest(request, response, domain);
} else {
response.send('Your site is not registered with us. Contact :supportEmail'.replace(':supportEmail', config.support.email));
}
}
else{
response.send("Oh well Database issue, that's all I know, Contact :supportEmail".replace(':supportEmail', config.support.email));
console.log('Error while performing Query.');
}
});
} else {
console.log("** Not Making DB Request **");
handleRequest(request, response, domain);
}
});
app.listen(app.get('port'), function() {
console.log("-- Server started --");
connection = mysql.createConnection({
host : config.db.host,
user : config.db.user,
password : config.db.password,
database : config.db.database
});
connection.connect(function(err) {
if (err) {
console.log('Error connecting to database');
process.exit(1);
}
console.log('Database is connected');
});
});