first fully working version

master
adb-sh 4 years ago
parent 40c668b6c5
commit 5fd846b174

@ -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);
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){
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)
//redis_client.set("key", "value", redis.print)
//write to redis
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')
})

@ -3,6 +3,7 @@
"version": "1.0.0",
"dependencies": {
"redis": "^3.0.2",
"ws": "^7.3.1"
"ws": "^7.3.1",
"xmlhttprequest": "^1.8.0"
}
}

@ -9,7 +9,7 @@
const socket = new WebSocket(wsurl)
socket.onopen = () => {
socket.send('new_session ')
socket.send('new session')
}
socket.onerror = (error) => {
console.log(`WebSocket error: ${error}`)
@ -20,11 +20,18 @@
if (msg[0] === 'error') {
document.getElementById('error-message').innerText = msg[1]
document.getElementById('error-box').style.display = 'block'
} else if (msg[0] === 'surl') {
document.getElementById('surl-input').value = msg[1]
document.getElementById('new-user-box').style.display = 'block'
}
}
function short_url(){
socket.send('long_url '+document.getElementById('name-input').value)
}
function copy_url() {
document.getElementById('surl-input').select();
document.execCommand("copy");
}
</script>
</head>
<body>
@ -42,6 +49,22 @@
</a>
</form>
</div>
<div class="box-dark" id="new-user-box">
<div class="login">
<div class="inputbox" id="surl">
<input class="input" id="surl-input" type="text" onblur="deselected('surl');" onfocus="selected('surl');" size="30" value="" maxlength="20" placeholder="surl">
</div>
<input type="hidden" value="search" name="login">
<div class="btn-green" id="copy-btn" onclick="copy_url()">
<div class="btn-text">copy</div>
</div>
</div>
<div onclick="visible('new-user-box','off')" style="position: absolute; top:5px; right: 5px;" class="sym_btn-invisible">
<img class="icon" src="sym/ic_close_white_24px.svg" />
</div>
</div>
<div id="error-box" class="error">
<div onclick="document.getElementById('error-box').style.display = 'none'" style="position: absolute; top:5px; right: 5px;" class="sym_btn-invisible">
<img class="icon" src="./sym/ic_close_white_24px.svg">

@ -158,6 +158,12 @@ input[id="name-input"]::-webkit-input-placeholder {
margin-left: calc(50% - 75px);
margin-top: 20px;
}
#copy-btn {
position: absolute;
width: 150px;
margin-left: calc(50% - 75px);
margin-top: 20px;
}
#bottom-link-btn {
position: fixed;
width: 150px;

Loading…
Cancel
Save