-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
538 lines (516 loc) · 21.9 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/**
* Module dependencies.
*/
var tinyurl_version = "0.0.14"; //DO NOT EDIT
var config = require('./config');
var colors = require('./node_modules/colors');
var https = require('https');
var express = require('express')
var redis = require("redis"),
client = redis.createClient();
//var fs = require('fs');
var app = module.exports = express.createServer();
function versionCheck(isstart){
var options = {
host: 'raw.githubusercontent.com',
path: '/Dasoren/tinyurl/master/version'
}
var request = https.request(options, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
var tinyurl_version_new = data;
if(res.statusCode != 200){
console.log(' Error getting version ');
}else if(tinyurl_version_new != tinyurl_version){
console.log("-- This version (".red+tinyurl_version.yellow+") is OUT of date.\n-- The new version is (".red+tinyurl_version_new.cyan+")\n-- Please update by running git clone git://github.com/dasoren/tinyurl.git from the tinyurl folder.\n-- This will not edit your configuration files.".red);
}else if(tinyurl_version_new == tinyurl_version && isstart == "yes"){
console.log("This version (".green+tinyurl_version.cyan+") is up to date.".green);
}else if(tinyurl_version_new == tinyurl_version){
}else{
console.log("This version (".yellow+tinyurl_version.cyan+") might be out of date.".yellow);
}
});
});
request.on('error', function (e) {
console.log(e.message);
});
request.end();
}
if(config.vCheck == 'true'){
// Disable us, if you want to remove version checking for updates
setInterval(versionCheck, 259200000); // Checks version every 3 days (259200000ms)
versionCheck('yes'); // Checks version on start
//
}
/*
var app_port = '8092';
// TTL times for random and customs
var ttl = 2592000; // Random link time out
var cttl = 7776000; // Custom link time out
var log_ttl = 605800; // Log time out
// Your Tiny URL
var config.host_url = 'http://127.0.0.1:8092/';
// Your Default URL
var default_link = 'http://127.0.0.1';
// Redis var naming
var redis_custom = 'tiny:custom:';
var redis_random = 'tiny:random:';
var redis_v1 = 'tiny:v1:';
var redis_v1_log = 'tiny:v1:log:';
// Master delete passowrd
var master_pass = '';
client.select('10');
*/
if(config.master_pass == ''){
master_pass = randomString(20);
console.log('-- Master Pass is '+master_pass+' ');
}
client.select(config.redisDB);
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//Return a random String
function randomString(stringLength) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 5;
if(typeof stringLength == 'number'){
string_length = stringLength;
}
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
//API Version 1.0
app.del('/v1/:id', function(req, res){
var query = require('url').parse(req.url,true).query;
var id = req.params.id;
if(query.pass == null){
var pass = '';
}else{
var pass = query.pass;
}
client.hgetall(config.redis_custom+id, function(err, hash) {
if(hash != null){
if(pass == master_pass){
client.del(redis_custom+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
client.hmset(config.redis_v1_log+id, 'type', 'delete', 'ip', req.connection.remoteAddress, 'timedate', Math.round(Date.now() / 1000), 'note', 'deleted with master password');
client.ttl(config.redis_v1_log+id, config.log_ttl);
}else if(hash.remove != null){
if(hash.remove == pass){
client.del(config.redis_custom+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
client.hmset(config.redis_v1_log+id, 'type', 'delete', 'ip', req.connection.remoteAddress, 'timedate', Math.round(Date.now() / 1000), 'note', 'deleted with password');
client.ttl(config.redis_v1_log+id, config.log_ttl);
}else{
res.writeHead(401, { 'message': 'Password is incorect'});
res.end('{error: 401, Password is incorect}');
}
}else{
res.writeHead(401, { 'message': id+' No password found, can not be deleted.'});
res.end('{error: 201, message: '+id+' No password found, can not be deleted.}');
}
}else{
res.writeHead(404, { 'message': id+' not found'});
res.end('{error: 404, message: '+id+' not found}');
}
});
});
app.get('/v1/del/:id', function(req, res){
var query = require('url').parse(req.url,true).query;
var id = req.params.id;
if(query.pass == null){
var pass = '';
}else{
var pass = query.pass;
}
client.hgetall(config.redis_custom+id, function(err, hash) {
if(hash != null){
if(pass == master_pass){
client.del(config.redis_custom+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
client.hmset(config.redis_v1_log+id, 'type', 'delete', 'ip', req.connection.remoteAddress, 'timedate', Math.round(Date.now() / 1000), 'note', 'deleted with master password');
client.ttl(config.redis_v1_log+id, config.log.ttl);
}else if(hash.remove != null){
if(hash.remove == pass){
client.del(config.redis_custom+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
client.hmset(config.redis_v1_log+id, 'type', 'delete', 'ip', req.connection.remoteAddress, 'timedate', Math.round(Date.now() / 1000), 'note', 'deleted with password');
client.ttl(config.redis_v1_log+id, config.log_ttl);
}else{
res.writeHead(401, { 'message': 'Password is incorect'});
res.end('{error: 401, Password is incorect}');
}
}else{
res.writeHead(401, { 'message': id+' No password found, can not be deleted.'});
res.end('{error: 201, message: '+id+' No password found, can not be deleted.}');
}
}else{
res.writeHead(404, { 'message': id+' not found'});
res.end('{error: 404, message: '+id+' not found}');
}
});
});
app.post('/v1/new', function(req, res){
url = require('url');
var query = require('url').parse(req.url,true).query;
var urlc = query.url;
urlc = url.parse(urlc);
if(urlc.protocol == null){
var protocol = 'http:';
}else{
var protocol = urlc.protocol;
}
if(urlc.path == null){
var path = '';
}else{
var path = urlc.path;
}
if(query.pass == null){
var pass = randomString(4);
}else{
var pass = query.pass;
}
if(query.name == undefined){
var name = randomString(5);
}else if(query.name != undefined){
var name = query.name;
console.log('part 2');
}
if(query.url == undefined){
res.send({error: 'No URL was entered.'});
}else{
client.hgetall(config.redis_v1+name, function(err, hash){
if(hash == null){
client.hmset(config.redis_v1+name, 'url', urlc.host, 'protocol', protocol, 'path', path, 'remove', pass, 'ip', req.connection.remoteAddress, 'timedate', Math.round(Date.now() / 1000));
client.hincrby(config.redis_v1+name, 'views', 1);
client.expire(config.redis_v1+name, config.ttl);
res.writeHead(201, { 'message': name+' Created'});
res.end('{error: 201, message: '+name+' Created, protocol: '+protocol+', url: '+urlc.host+', path: '+path+', pass: '+pass+', link: '+config.host_url+name+'}');
}else{
client.ttl(config.redis_v1+query.name, function(err, ttl){
res.writeHead(409, { 'message': query.name+' in use'});
res.end('{error: 409, message: '+query.name+' in use, name: '+query.name+', link: '+hash.protocol+'//'+hash.url+hash.path+', ttl: '+ttl+'}');
});
}
});
}
});
app.get('/v1/new', function(req, res){
url = require('url');
var query = require('url').parse(req.url,true).query;
var urlc = query.url;
urlc = url.parse(urlc);
if(urlc.protocol == null){
var protocol = 'http:';
}else{
var protocol = urlc.protocol;
}
if(urlc.path == null){
var path = '';
}else{
var path = urlc.path;
}
if(query.pass == null){
var pass = randomString(4);
}else{
var pass = query.pass;
}
if(query.name == undefined){
var name = randomString(5);
}else if(query.name != undefined){
var name = query.name;
}
if(query.url == undefined){
res.send({error: 'No URL was entered.'});
}else{
client.hgetall(config.redis_v1+name, function(err, hash){
if(hash == null){
client.hmset(config.redis_v1+name, 'url', urlc.host, 'protocol', protocol, 'path', path, 'remove', pass);
client.hincrby(config.redis_v1+name, 'views', 1);
client.expire(config.redis_v1+name, config.ttl);
res.writeHead(201, { 'message': name+' Created'});
res.end('{error: 201, message: '+name+' Created, protocol: '+protocol+', url: '+urlc.host+', path: '+path+', pass: '+pass+', link: '+config.host_url+name+'}');
}else{
client.ttl(config.redis_v1+query.name, function(err, ttl){
res.writeHead(409, { 'message': query.name+' in use'});
res.end('{error: 409, message: '+query.name+' in use, name: '+query.name+', link: '+hash.protocol+'//'+hash.url+hash.path+', ttl: '+ttl+'}');
});
}
});
}
});
app.get('/v1/stats/:id', function(req, res){
if(req.params.id == undefined){
res.send({error: 'No ID was entered.'});
}else{
var id = req.params.id;
client.hgetall(config.redis_v1+id, function(err, hash) {
if(hash != null){
res.writeHead(200, { 'message': id+' Stats'});
res.end('{error: 200, message: '+id+' Stats, views: '+hash.views+' protocol: '+hash.protocol+', url: '+hash.url+', path: '+hash.path+', link: '+config.host_url+id+'}');
}else{
res.writeHead(404, {error: 'Invalid ID was entered.'});
res.end('{error: 404, message: Invalid ID was entered}');
}
});
}
});
app.get('/v1/:id', function(req, res){
if(req.params.id == undefined){
res.send({error: 'No ID was entered.'});
}else{
var id = req.params.id;
client.hgetall(config.redis_v1+id, function(err, hash) {
if(hash != null){
if(hash.protocol != null){
client.hincrby(config.redis_v1+id, 'views', 1);
client.expire(config.redis_v1+id, config.ttl);
res.writeHead(302, { 'Location': hash.protocol+'//'+hash.url+hash.path});
res.end('{error: 302, message: moved location: '+hash.protocol+'//'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}else{
client.hincrby(config.redis_v1+id, 'views', 1);
client.expire(config.redis_v1+id, config.ttl);
res.writeHead(302, { 'Location': 'http://'+hash.url+hash.path});
res.end('{error: 302, message: moved location: http://'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}
}else{
res.writeHead(404, {error: 'Invalid ID was entered.'});
res.end('{error: 404, message: Invalid ID was entered}');
}
});
}
});
app.get('/v1/', function(req,res){
res.writeHead(302, { 'Location': config.default_link});
res.end();
});
//API Old
app.del('/:id?', function(req, res){
var query = require('url').parse(req.url,true).query;
var id = req.params.id;
if(query.pass == null){
var pass = '';
}else{
var pass = query.pass;
}
client.hgetall(config.redis_custom+id, function(err, hash) {
if(hash != null){
if(hash.remove != null){
if(hash.remove == pass){
client.del(config.redis_custom+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
}else{
res.writeHead(401, { 'message': 'Password is incorect'});
res.end('{error: 401, Password is incorect}');
}
}else{
client.del(config.redis_custom+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
}
}else{
client.hgetall(config.redis_random+id, function(err, hash) {
if(hash.remove != null){
if(hash.remove == pass){
client.del(config.redis_random+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
}else{
res.writeHead(401, { 'message': 'Password is incorect'});
res.end('{error: 401, Password is incorect}');
}
}else{
client.del(config.redis_random+id);
res.writeHead(202, { 'message': id+' being deleted'});
res.end('{error: 202, message: '+id+' being deleted}');
}
});
}
});
});
app.post('/new', function(req, res){
url = require('url');
var query = require('url').parse(req.url,true).query;
var urlc = query.url;
urlc = url.parse(urlc);
if(urlc.protocol == null){
var protocol = 'http:';
}else{
var protocol = urlc.protocol;
}
if(urlc.path == null){
var path = '';
}else{
var path = urlc.path;
}
if(query.pass == null){
var pass = '';
}else{
var pass = query.pass;
}
if(query.url == undefined){
res.send({error: 'No URL was entered.'});
} else if(query.name == undefined){
query.name = randomString();
}else{
client.hgetall(config.redis_custom+query.name, function(err, hash){
if(hash == null){
client.hmset("tiny:custom:"+query.name, 'url', urlc.host, 'protocol', protocol, 'path', path, 'remove', pass);
client.hincrby(config.redis_custom+query.name, 'views', 1);
client.expire(config.redis_custom+query.name, config.cttl);
res.writeHead(201, { 'message': query.name+' Created'});
res.end('{error: 201, message: '+query.name+' Created, protocol: '+protocol+', url: '+urlc.host+', path: '+path+', pass: '+pass+', link: '+config.host_url+query.name+'}');
}else{
client.ttl(config.redis_custom+query.name, function(err, ttl){
res.writeHead(409, { 'message': query.name+' in use'});
res.end('{error: 409, message: '+query.name+' in use, name: '+query.name+', link: '+hash.protocol+'//'+hash.url+hash.path+', ttl: '+ttl+'}');
});
}
});
}
});
app.get('/new', function(req, res){
url = require('url');
var query = require('url').parse(req.url,true).query;
var urlc = query.url;
urlc = url.parse(urlc);
if(urlc.protocol == null){
var protocol = 'http:';
}else{
var protocol = urlc.protocol;
}
if(urlc.path == null){
var path = '';
}else{
var path = urlc.path;
}
if(query.pass == null){
var pass = '';
}else{
var pass = query.pass;
}
if(query.url == undefined){
res.send({error: 'No URL was entered.'});
} else if(query.name == undefined){
query.name = randomString();
}else{
client.hgetall(config.redis_custom+query.name, function(err, hash){
if(hash == null){
client.hmset("tiny:custom:"+query.name, 'url', urlc.host, 'protocol', protocol, 'path', path, 'remove', pass);
client.hincrby(config.redis_custom+query.name, 'views', 1);
client.expire(config.redis_custom+query.name, config.cttl);
res.writeHead(201, { 'message': query.name+' Created'});
res.end('{error: 201, message: '+query.name+' Created, protocol: '+protocol+', url: '+urlc.host+', path: '+path+', pass: '+pass+', link: '+config.host_url+query.name+'}');
}else{
client.ttl(config.redis_custom+query.name, function(err, ttl){
res.writeHead(409, { 'message': query.name+' in use'});
res.end('{error: 409, message: '+query.name+' in use, name: '+query.name+', link: '+hash.protocol+'//'+hash.url+hash.path+', ttl: '+ttl+'}');
});
}
});
}
});
// OLD API id code
app.get('/:id', function(req, res){
if(req.params.id == undefined){
res.send({error: 'No ID was entered.'});
}else{
var id = req.params.id;
client.hgetall(config.redis_custom+id, function(err, hash) {
if(hash != null){
if(hash.protocol != null){
client.hincrby(config.redis_custom+id, 'views', 1);
client.expire(config.redis_custom+id, config.cttl);
res.writeHead(302, { 'Location': hash.protocol+'//'+hash.url+hash.path});
res.end('{error: 302, message: moved location: '+hash.protocol+'//'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}else{
client.hincrby(config.redis_custom+id, 'views', 1);
client.expire(config.redis_custom+id, config.cttl);
res.writeHead(302, { 'Location': 'http://'+hash.url+hash.path});
res.end('{error: 302, message: moved location: http://'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}
}else{
client.hgetall('url:tiny:'+id, function(err, hash) {
if(hash != null){
if(hash.protocol != null){
client.hincrby(config.redis_random+id, 'views', 1);
client.expire(config.redis_random+id, config.ttl);
res.writeHead(302, { 'Location': hash.protocol+'//'+hash.url+hash.path});
res.end('{error: 302, message: moved location: '+hash.protocol+'//'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}else{
client.hincrby(config.redis_random+id, 'views', 1);
client.expire(config.redis_random+id, config.ttl);
res.writeHead(302, { 'Location': 'http://'+hash.url+hash.path});
res.end('{error: 302, message: moved location: http://'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}
}else{
res.writeHead(404, {error: 'Invalid ID was entered.'});
res.end('{error: 404, message: Invalid ID was entered}');
process.stdout.write(config.host_url+id+' was tried, no value found\n');
}
});
}
});
}
});
// API v1 id code
/*
app.get('/:id', function(req, res){
if(req.params.id == undefined){
res.send({error: 'No ID was entered.'});
}else{
var id = req.params.id;
client.hgetall(redis_v1+id, function(err, hash) {
if(hash != null){
if(hash.protocol != null){
client.hincrby(redis_v1+id, 'views', 1);
client.expire(redis_v1+id, ttl);
res.writeHead(302, { 'Location': hash.protocol+'//'+hash.url+hash.path});
res.end('{error: 302, message: moved location: '+hash.protocol+'//'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}else{
client.hincrby(redis_v1+id, 'views', 1);
client.expire(redis_v1+id, ttl);
res.writeHead(302, { 'Location': 'http://'+hash.url+hash.path});
res.end('{error: 302, message: moved location: http://'+hash.url+'}');
process.stdout.write(config.host_url+id+' used\n');
}
}else{
res.writeHead(404, {error: 'Invalid ID was entered.'});
res.end('{error: 404, message: Invalid ID was entered}');
}
});
}
});
*/
app.get('/', function(req,res){
res.writeHead(302, { 'Location': config.default_link});
res.end();
});
// Redis Error:
client.on("error", function (err) {
console.log("Error " + err);
});
app.listen(config.app_port);
console.log("TinyURL server listening on port "+config.app_port);