|
|
|
@ -1,8 +1,10 @@
|
|
|
|
|
const http = require('http');
|
|
|
|
|
const url = require('url');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const ws = require('ws');
|
|
|
|
|
const http = require('http')
|
|
|
|
|
const url = require('url')
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const ws = require('ws')
|
|
|
|
|
const redis = require('redis')
|
|
|
|
|
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
|
|
|
|
|
const host = 'http://127.0.0.1:8080/'
|
|
|
|
|
|
|
|
|
|
//redis client
|
|
|
|
|
const redis_cli = redis.createClient()
|
|
|
|
@ -32,10 +34,16 @@ http.createServer(function (req, res) {
|
|
|
|
|
return res.end();
|
|
|
|
|
})
|
|
|
|
|
}else{
|
|
|
|
|
let url = redis_cli.hget("surl;"+q.pathname.split("/", 2)[1], "url")
|
|
|
|
|
res.writeHead(302, {'Location': 'http://adb.sh'});
|
|
|
|
|
//res.write(data);
|
|
|
|
|
return res.end();
|
|
|
|
|
redis_cli.hget("surl;"+q.pathname.split("/", 2)[1], "url", function(err, obj) {
|
|
|
|
|
if(err) console.log(err);
|
|
|
|
|
if(obj){
|
|
|
|
|
res.writeHead(302, {'Location': obj});
|
|
|
|
|
return res.end();
|
|
|
|
|
}else{
|
|
|
|
|
res.writeHead(404, {'Content-Type': 'text/html'});
|
|
|
|
|
return res.end("404 this short-url does not exist :/");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}).listen(8080);
|
|
|
|
|
|
|
|
|
@ -69,19 +77,38 @@ wss.on('connection', ws => {
|
|
|
|
|
console.log(`Received message => ${message}`)
|
|
|
|
|
let msg = `${message}`.split(";", 2)
|
|
|
|
|
if (msg[0] === 'long_url') {
|
|
|
|
|
if (function valid_url(){
|
|
|
|
|
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'); return false}
|
|
|
|
|
else return true
|
|
|
|
|
}() === true){
|
|
|
|
|
let ran_key = get_key(8)
|
|
|
|
|
//redis_client.set("key", "value", redis.print)
|
|
|
|
|
redis_cli.hmset("surl;"+ran_key, "url", msg[1], "time", Date.now())
|
|
|
|
|
ws.send('short_url;http://127.0.0.1/'+ran_key)
|
|
|
|
|
if (msg[1] === '') ws.send('error;url is empty')
|
|
|
|
|
else if (msg[1].length > 2000) ws.send('error;your url is too long')
|
|
|
|
|
else{
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
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)
|
|
|
|
|
//write to redis
|
|
|
|
|
redis_cli.hmset("surl;"+ran_key, "url", msg[1], "time", Date.now())
|
|
|
|
|
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')
|
|
|
|
|
})
|
|
|
|
|