diff --git a/index.js b/index.js index 5cb5385..de99553 100644 --- a/index.js +++ b/index.js @@ -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') }) diff --git a/package.json b/package.json index 6e7a9ea..c7c5190 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/public/index.html b/public/index.html index fa087a7..7d34afa 100644 --- a/public/index.html +++ b/public/index.html @@ -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"); + } @@ -42,6 +49,22 @@ + +
+
+
+ +
+ +
+
copy
+
+
+
+ +
+
+
diff --git a/public/sym/dark.css b/public/sym/dark.css index d59d4b5..ab347fa 100644 --- a/public/sym/dark.css +++ b/public/sym/dark.css @@ -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;