-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (86 loc) · 3.77 KB
/
app.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
var express = require('express');
var geoip = require('geoip-lite');
var useragent = require('express-useragent');
var parse = require('url-parse');
var app = express();
var mysql = require('mysql');
require('dotenv').config()
var con = mysql.createConnection({
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE
});
app.use(useragent.express());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use('/', express.static('serve'))
app.get('/funnyCatPhoto.gif', function(req, res){
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var url = "";
if (req.query.domain){
url = parse("https://"+ req.query.domain + parse(req.header('Referer'), true).pathname, true);
} else {
url = parse(req.header('Referer'), true);
}
if (url.hostname == "" && url.pathname == ""){
var buf = new Buffer(43);
buf.write("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==", "base64");
res.send(buf, { 'Content-Type': 'image/gif' }, 200);
return;
}
var data = [(Math.floor((new Date() / 1000)/60/60)), url.hostname, url.pathname, ip, req.useragent.browser, geoip.lookup(ip).country, req.useragent.os];
let command = 'INSERT INTO `requests` (id, time, domain, path, ip, browser, country, os) VALUES (0, ?, ?, ?, ?, ?, ?, ?)';
con.query(command, data, function(err, result) {
});
var buf = new Buffer(43);
buf.write("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==", "base64");
res.send(buf, { 'Content-Type': 'image/gif' }, 200);
});
app.get('/api/v1/:domain/browsers', function(req, res){
let command = 'SELECT `browser`, COUNT(browser) FROM `requests` WHERE `domain` = ? GROUP BY `browser` ORDER BY COUNT(*) DESC LIMIT 10'
con.query(command, req.params.domain, function(err, result){
res.json(result);
})
});
app.get('/api/v1/:domain/countries', function(req, res){
let command = 'SELECT `country`, COUNT(country) FROM `requests` WHERE `domain` = ? GROUP BY `country` ORDER BY COUNT(*) DESC LIMIT 10'
con.query(command, req.params.domain, function(err, result){
res.json(result);
})
});
app.get('/api/v1/:domain/os', function(req, res){
let command = 'SELECT `os`, COUNT(os) FROM `requests` WHERE `domain` = ? GROUP BY `os` ORDER BY COUNT(*) DESC LIMIT 10'
con.query(command, req.params.domain, function(err, result){
res.json(result);
})
});
app.get('/api/v1/:domain/paths', function(req, res){
let command = 'SELECT `path`, COUNT(path) FROM `requests` WHERE `domain` = ? GROUP BY `path` ORDER BY COUNT(*) DESC LIMIT 10'
con.query(command, req.params.domain, function(err, result){
res.json(result);
})
});
app.get('/api/v1/:domain/requests', function(req, res){
let command = 'SELECT COUNT(*) FROM `requests` WHERE domain = ?'
con.query(command, req.params.domain, function(err, result){
res.json(result);
})
});
app.get('/api/v1/:domain/ips', function(req, res){
let command = 'SELECT COUNT(DISTINCT ip) FROM requests WHERE domain = ?'
con.query(command, req.params.domain, function(err, result){
res.json(result);
})
});
app.get('/api/v1/:domain/requests/:hour', function(req, res){
var time = (Math.floor((new Date() / 1000)/60/60)) - Number(req.params.hour);
let command = 'SELECT `time`, COUNT(time) FROM `requests` WHERE `time` > ? AND `domain` = ? GROUP BY `time`'
con.query(command, [time, req.params.domain], function(err, result){
res.json(result);
})
});
app.listen(3011);