|
|
@ -5,25 +5,38 @@ const ws = require('ws');
|
|
|
|
const redis = require('redis')
|
|
|
|
const redis = require('redis')
|
|
|
|
|
|
|
|
|
|
|
|
//redis client
|
|
|
|
//redis client
|
|
|
|
const redis_client = redis.createClient()
|
|
|
|
const redis_cli = redis.createClient()
|
|
|
|
redis_client.on("error", function (error) {
|
|
|
|
redis_cli.on("error", function (error) {
|
|
|
|
console.error(error)
|
|
|
|
console.error(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const outpath = ['sym', '']
|
|
|
|
|
|
|
|
|
|
|
|
//HTTP server
|
|
|
|
//HTTP server
|
|
|
|
http.createServer(function (req, res) {
|
|
|
|
http.createServer(function (req, res) {
|
|
|
|
const q = url.parse(req.url, true);
|
|
|
|
const q = url.parse(req.url, true);
|
|
|
|
let filename = "./public" + q.pathname;
|
|
|
|
let filename = "./public" + q.pathname;
|
|
|
|
if (filename === "./public/") filename = "./public/index.html";
|
|
|
|
if (filename === "./public/") filename = "./public/index.html";
|
|
|
|
fs.readFile(filename, function(err, data) {
|
|
|
|
//q.pathname.split("/", 2)[1] === "sym" || )
|
|
|
|
if (err) {
|
|
|
|
if (function valid_path(){
|
|
|
|
res.writeHead(404, {'Content-Type': 'text/html'});
|
|
|
|
for (let i = 0; i < outpath.length; i++) if (q.pathname.split("/", 2)[1] === outpath[i]) return true
|
|
|
|
return res.end("404 Not Found");
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}() === true){
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/html'});
|
|
|
|
fs.readFile(filename, function(err, data) {
|
|
|
|
res.write(data);
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
res.writeHead(404, {'Content-Type': 'text/html'});
|
|
|
|
|
|
|
|
return res.end("404 Not Found");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/html'});
|
|
|
|
|
|
|
|
res.write(data);
|
|
|
|
|
|
|
|
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();
|
|
|
|
return res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).listen(8080);
|
|
|
|
}).listen(8080);
|
|
|
|
|
|
|
|
|
|
|
|
//WS server
|
|
|
|
//WS server
|
|
|
@ -56,12 +69,15 @@ 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 (msg[1] === '') ws.send('error;url is empty')
|
|
|
|
if (function valid_url(){
|
|
|
|
if (msg[1].length > 2000) ws.send('error;your url is too big')
|
|
|
|
if (msg[1] === ''){ws.send('error;url is empty'); return false}
|
|
|
|
else{
|
|
|
|
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)
|
|
|
|
let ran_key = get_key(8)
|
|
|
|
//redis_client.set("key", "value", redis.print)
|
|
|
|
//redis_client.set("key", "value", redis.print)
|
|
|
|
ws.send('short_url;http://127.0.0.1'+ran_key)
|
|
|
|
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('got your request: '+msg)
|
|
|
|
//ws.send('got your request: '+msg)
|
|
|
|