|
|
@ -1,8 +1,10 @@
|
|
|
|
const http = require('http');
|
|
|
|
const http = require('http')
|
|
|
|
const url = require('url');
|
|
|
|
const url = require('url')
|
|
|
|
const fs = require('fs');
|
|
|
|
const fs = require('fs')
|
|
|
|
const ws = require('ws');
|
|
|
|
const ws = require('ws')
|
|
|
|
const redis = require('redis')
|
|
|
|
const redis = require('redis')
|
|
|
|
|
|
|
|
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
|
|
|
|
|
|
|
|
const host = 'http://127.0.0.1:8080/'
|
|
|
|
|
|
|
|
|
|
|
|
//redis client
|
|
|
|
//redis client
|
|
|
|
const redis_cli = redis.createClient()
|
|
|
|
const redis_cli = redis.createClient()
|
|
|
@ -32,10 +34,16 @@ http.createServer(function (req, res) {
|
|
|
|
return res.end();
|
|
|
|
return res.end();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
let url = redis_cli.hget("surl;"+q.pathname.split("/", 2)[1], "url")
|
|
|
|
redis_cli.hget("surl;"+q.pathname.split("/", 2)[1], "url", function(err, obj) {
|
|
|
|
res.writeHead(302, {'Location': 'http://adb.sh'});
|
|
|
|
if(err) console.log(err);
|
|
|
|
//res.write(data);
|
|
|
|
if(obj){
|
|
|
|
|
|
|
|
res.writeHead(302, {'Location': obj});
|
|
|
|
return res.end();
|
|
|
|
return res.end();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
res.writeHead(404, {'Content-Type': 'text/html'});
|
|
|
|
|
|
|
|
return res.end("404 this short-url does not exist :/");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).listen(8080);
|
|
|
|
}).listen(8080);
|
|
|
|
|
|
|
|
|
|
|
@ -69,19 +77,38 @@ wss.on('connection', ws => {
|
|
|
|
console.log(`Received message => ${message}`)
|
|
|
|
console.log(`Received message => ${message}`)
|
|
|
|
let msg = `${message}`.split(";", 2)
|
|
|
|
let msg = `${message}`.split(";", 2)
|
|
|
|
if (msg[0] === 'long_url') {
|
|
|
|
if (msg[0] === 'long_url') {
|
|
|
|
if (function valid_url(){
|
|
|
|
if (msg[1] === '') ws.send('error;url is empty')
|
|
|
|
if (msg[1] === ''){ws.send('error;url is empty'); return false}
|
|
|
|
else if (msg[1].length > 2000) ws.send('error;your url is too long')
|
|
|
|
else if (msg[1].length > 2000){ws.send('error;your url is too long'); return false}
|
|
|
|
else{
|
|
|
|
else return true
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
}() === true){
|
|
|
|
console.log(msg[1])
|
|
|
|
|
|
|
|
xhr.open('GET', msg[1], true);
|
|
|
|
|
|
|
|
xhr.timeout = 2000;
|
|
|
|
|
|
|
|
xhr.ontimeout = function(e) {ws.send('error;url timed out')}
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
|
|
|
|
switch(xhr.status) {
|
|
|
|
|
|
|
|
case 200:
|
|
|
|
let ran_key = get_key(8)
|
|
|
|
let ran_key = get_key(8)
|
|
|
|
//redis_client.set("key", "value", redis.print)
|
|
|
|
//write to redis
|
|
|
|
redis_cli.hmset("surl;"+ran_key, "url", msg[1], "time", Date.now())
|
|
|
|
redis_cli.hmset("surl;"+ran_key, "url", msg[1], "time", Date.now())
|
|
|
|
ws.send('short_url;http://127.0.0.1/'+ran_key)
|
|
|
|
ws.send('surl;'+host+ran_key)
|
|
|
|
|
|
|
|
console.log('key;'+ran_key)
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 301: ws.send('error;the url is redirecting (301)');break;
|
|
|
|
|
|
|
|
case 302: ws.send('error;the url is redirecting (302)');break;
|
|
|
|
|
|
|
|
case 303: ws.send('error;the url is redirecting (303)');break;
|
|
|
|
|
|
|
|
case 404: ws.send('error;site does not exist (404)');break;
|
|
|
|
|
|
|
|
case 502: ws.send('error;remote server error (502)');break;
|
|
|
|
|
|
|
|
case 500: ws.send('error;remote server error (500)');break;
|
|
|
|
|
|
|
|
case 503: ws.send('error;remote server error (503)');break;
|
|
|
|
|
|
|
|
default: ws.send('error;no valid url');break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//ws.send('got your request: '+msg)
|
|
|
|
|
|
|
|
console.log(msg[0])
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
ws.send('websocket connected')
|
|
|
|
ws.send('websocket connected')
|
|
|
|
})
|
|
|
|
})
|
|
|
|