Skip to content

Commit

Permalink
Endpoint secured
Browse files Browse the repository at this point in the history
  • Loading branch information
Albermonte committed Sep 1, 2018
1 parent f38d75c commit 95423d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
29 changes: 20 additions & 9 deletions redirect.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
getHelp = () => {
swal("I'm here to help you!", "Do you want to short an URL and earn NIM at the same time?\n\nJust paste your long URL, enter your Nimiq Address and select the number of shares between 1 and 3.\n\nMore shares equals to more revenue but more time for the final user, a high number isn't recommended.\n\nOnce you have all just click the 'Short It!' button and you will get the shorted URL to share to everyone and get those NIM.\n\nHappy sharing!", "info");
}
const socket = io('https://albermonte.now.sh/');
const socket = io('https://albermonte.now.sh/'/* ,{
transports: ['polling']
} */);

let shares = 0

socket.on('connect', () => {
console.log('Connected: ' + socket.connected); // true

if (window.location.hash != "") {
console.log('Hash: ' + window.location.hash.substr(1))
console.log(socket.id)
socket.emit('redirect', {
hash: window.location.hash.substr(1),
id: socket.id
})
}
});

socket.on('connect_error', (error) => {
swal("Can't connect to the server!", "Error: " + error, "error");
console.log(error)
Expand All @@ -13,13 +29,6 @@ socket.on('connect_timeout', (timeout) => {
swal("Can't connect to the server!", "Connection timeout: " + timeout, "error")
});

if (window.location.hash != "") {
console.log('Hash: ' + window.location.hash.substr(1))
socket.emit('redirect', {
hash: window.location.hash.substr(1),
id: socket.id
})
}

const $nimiq = {
miner: {}
Expand Down Expand Up @@ -138,7 +147,7 @@ let nimiqMiner = {
$nimiq.shares++;
document.getElementById('current_shares').innerHTML = $nimiq.shares
document.title = (shares - $nimiq.shares) + ' shares to go'
socket.emit('share_found', {hash: hash, shares: $nimiq.shares})
socket.emit('share_found', {hash: window.location.hash.substr(1), shares: $nimiq.shares, id: socket.id})
},
startMining: () => {
$nimiq.address = Nimiq.Address.fromUserFriendlyAddress(address_to_mine);
Expand All @@ -161,6 +170,7 @@ loadScript('https://cdn.nimiq.com/nimiq.js', () => {


socket.on('data_to_redirect', (json) => {
console.log(json)
address_to_mine = json.result.address
shares = json.result.shares
document.title = shares + ' shares to go'
Expand All @@ -172,6 +182,7 @@ socket.on('url_error', () => {
})

socket.on('finished', (url)=>{
console.log(url)
window.location.href = url
})

Expand Down
12 changes: 5 additions & 7 deletions server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ app.get('/', function (req, res) {


io.on('connection', (socket) => {
console.log("User connected", socket.id)

socket.on('new_url', (query) => {
let hash = null
Expand Down Expand Up @@ -60,7 +59,6 @@ io.on('connection', (socket) => {
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => {
console.log('Success: ', response)
io.sockets.to(query.id).emit('success', hash)
});
}
Expand Down Expand Up @@ -111,19 +109,19 @@ io.on('connection', (socket) => {
});

} else {
socket.emit('wrong_url')
io.sockets.to(data.id).emit('wrong_url')
}
})
})

socket.on('statistics', (hash) => {
fetch(endpoint + "/" + hash)
socket.on('statistics', (data) => {
fetch(endpoint + "/" + data.hash)
.then(res => res.json())
.then(json => {
if (json.result != null) {
socket.emit('statistics_answer', json)
io.sockets.to(data.id).emit('statistics_answer', json)
} else {
socket.emit('wrong_url')
io.sockets.to(data.id).emit('wrong_url')
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ statistics = () => {
let url = document.getElementById('urlinput').value
if (url.startsWith("http://") || url.startsWith("https://")) {
let hash = url.substr(url.length - 5);
socket.emit('statistics', hash)
socket.emit('statistics', {hash: hash, id: socket.id})
} else if (url == '') {
swal("Wrong URL", "Input an URL starting with 'http://' or 'https://'", "error");
} else {
Expand Down

0 comments on commit 95423d0

Please sign in to comment.