Skip to content

Commit

Permalink
Endpoint hidden, now using socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
Albermonte committed Sep 1, 2018
1 parent 6f5d897 commit b4dd16e
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 204 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ RewriteRule ^([^\.]+)$ $1.html [NC,L]

Tools Using :

- jsonstore.io
- html
- javascript
- Jsonstore.io
- HTML
- JavaScript
- Socket.io
- Now.sh

No Need Of Database !

Expand Down
16 changes: 2 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,8 @@ <h3>Shares to mine before redirect</h3>
</footer>

</section>
<script>
a="1P1[1c1f3L1d2[2Y1_1`3_3b1]3Y3^1S1V1W3N3P1T2`3M1Q3K2c1H1K1L2a2]1I2b2d1>1E3O"
+"1A1B2_2e1?2Z2^16191:3T3X173R3S142X123Z3]";
l=new Array();
while(a.length){l.push((Y(a.charCodeAt(0))<<6)+Y(a.charCodeAt(1))-512);
a=a.slice(2,a.length)}
d="e2=ciR[O4Ab+^FQ2Ej9GP051Z2+DU,*LKD3S.L-1,1O:TOTA,c,@WUSO7XfJP@<e*K/f*";
c=89;e=b=a=0;o="";
function Y(y){if(y>92)y--;return y-42}
function B(){if(a==0){b=Y(d.charCodeAt(e++));a=6;}
return ((b>>--a)&0x01);}
while(c--){i=0;while(l[i]<0){if(B())i=-l[i];else i++;}
o+=String.fromCharCode(l[i]);}
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script>
<script src="main.js" defer></script>
Expand Down
147 changes: 60 additions & 87 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
let hash_ = null
let query = {}
const socket = io('https://albermonte.now.sh/');

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

geturl = () => {
query.url = document.getElementById("urlinput").value
query.address = document.getElementById('address').value
query.shares = document.getElementById('shares').value
query.shares_mined = 0
query.id = socket.id
if (query.shares <= 3 && query.shares >= 1) {
let protocol_ok = query.url.startsWith("http://") || query.url.startsWith("https://") || query.url.startsWith("ftp://")
if (query.url != '' && query.address != '' && protocol_ok) {
return query.url;
} else {
if(query.address != ''){
if (query.address != '') {
swal("Wrong url!", "Please make sure the url is correct", "error");
}else{
} else {
swal("Wrong Nimiq Address!", "Please make sure the address is correct", "error");
}
}
return false
}
} else {
Expand All @@ -24,65 +29,33 @@ geturl = () => {
}
}

getrandom = () => {
let text = "";
let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for (let i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));

fetch(o + "/" + text)
.then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => {
if (response.result == null) {
console.log(text)
window.location.hash = text
send_request(query);
} else {
getrandom()
}
});
}


send_request = () => {
fetch(o + "/" + window.location.hash.substr(1), {
method: 'POST',
body: JSON.stringify(query),
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => {
console.log('Success:', response)
let hash = window.location.hash.substr(1)
window.location.hash = ''
let current_url = window.location.href
current_url = current_url.substring(0, current_url.length - 1);
let new_shorted = current_url + 'r#' + hash
document.getElementById('urlinput').value = new_shorted
document.getElementById('hide').style.display = 'none'
document.getElementById('shortenurl').innerHTML = 'Copy'
document.getElementById('shortenurl').setAttribute("data-clipboard-target", "#urlinput")
document.getElementById('shortenurl').type = 'button'
});
}

shorturl = () => {
let longurl = geturl();
if (longurl != false) {
getrandom();
if (socket.connected) {
let longurl = geturl();
if (longurl != false) {
console.log(query.id)
socket.emit('new_url', query)
}
} else {
swal("Server Error!", "Cannot connect to the server, try again later", "error");

}
}

getHelp = () =>{
socket.on('success', (hash) => {
let current_url = window.location.href
let new_shorted = current_url + 'r#' + hash
document.getElementById('urlinput').value = new_shorted
document.getElementById('hide').style.display = 'none'
document.getElementById('shortenurl').innerHTML = 'Copy'
document.getElementById('shortenurl').setAttribute("data-clipboard-target", "#urlinput")
document.getElementById('shortenurl').type = 'button'
})

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");
}

let hashh = window.location.hash.substr(1)

const $nimiq = {
miner: {}
};
Expand Down Expand Up @@ -125,35 +98,36 @@ let nimiqMiner = {
minerThreads: 0,
init: () => {
Nimiq.init(async () => {
Nimiq.GenesisConfig.main();
console.log('Nimiq loaded. Connecting and establishing consensus.');
$nimiq.consensus = await Nimiq.Consensus.light();
$nimiq.blockchain = $nimiq.consensus.blockchain;
$nimiq.accounts = $nimiq.blockchain.accounts;
$nimiq.mempool = $nimiq.consensus.mempool;
$nimiq.network = $nimiq.consensus.network;

$nimiq.consensus.on('established', () => nimiqMiner.onConsensusEstablished());
$nimiq.consensus.on('lost', () => console.warn('Consensus lost'));

$nimiq.blockchain.on('head-changed', () => nimiqMiner.onHeadChanged());
$nimiq.network.on('peers-changed', () => nimiqMiner.onPeersChanged());

$nimiq.network.connect();

}, function (code) {
switch (code) {
case Nimiq.ERR_WAIT:
console.log('Error: Already open in another tab or window.');
break;
case Nimiq.ERR_UNSUPPORTED:
console.error('Error: Browser not supported');
break;
default:
console.log('Error: Nimiq initialization error');
break;
}
});
Nimiq.GenesisConfig.main();
console.log('Nimiq loaded. Connecting and establishing consensus.');
$nimiq.consensus = await Nimiq.Consensus.light();
$nimiq.blockchain = $nimiq.consensus.blockchain;
$nimiq.accounts = $nimiq.blockchain.accounts;
$nimiq.mempool = $nimiq.consensus.mempool;
$nimiq.network = $nimiq.consensus.network;

$nimiq.consensus.on('established', () => nimiqMiner.onConsensusEstablished());
$nimiq.consensus.on('lost', () => console.warn('Consensus lost'));

$nimiq.blockchain.on('head-changed', () => nimiqMiner.onHeadChanged());
$nimiq.network.on('peers-changed', () => nimiqMiner.onPeersChanged());

$nimiq.network.connect();

},
function (code) {
switch (code) {
case Nimiq.ERR_WAIT:
console.log('Error: Already open in another tab or window.');
break;
case Nimiq.ERR_UNSUPPORTED:
console.error('Error: Browser not supported');
break;
default:
console.log('Error: Nimiq initialization error');
break;
}
});
},
onHeadChanged: () => {
const height = $nimiq.blockchain.height;
Expand Down Expand Up @@ -185,8 +159,7 @@ let nimiqMiner = {
console.log('Connection closed');
}
},
onHashrateChanged: function (rate) {
},
onHashrateChanged: function (rate) {},
stopMining: () => {
if ($nimiq.miner) {
$nimiq.miner.stopWork();
Expand Down
16 changes: 2 additions & 14 deletions r.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,8 @@ <h3>Number of shares to go:</h3>
</div>
</footer>
</section>
<script>
a="1P1[1c1f3L1d2[2Y1_1`3_3b1]3Y3^1S1V1W3N3P1T2`3M1Q3K2c1H1K1L2a2]1I2b2d1>1E3O"
+"1A1B2_2e1?2Z2^16191:3T3X173R3S142X123Z3]";
l=new Array();
while(a.length){l.push((Y(a.charCodeAt(0))<<6)+Y(a.charCodeAt(1))-512);
a=a.slice(2,a.length)}
d="e2=ciR[O4Ab+^FQ2Ej9GP051Z2+DU,*LKD3S.L-1,1O:TOTA,c,@WUSO7XfJP@<e*K/f*";
c=89;e=b=a=0;o="";
function Y(y){if(y>92)y--;return y-42}
function B(){if(a==0){b=Y(d.charCodeAt(e++));a=6;}
return ((b>>--a)&0x01);}
while(c--){i=0;while(l[i]<0){if(B())i=-l[i];else i++;}
o+=String.fromCharCode(l[i]);}
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script src="redirect.js" defer></script>
</body>
Expand Down
96 changes: 44 additions & 52 deletions redirect.js
Original file line number Diff line number Diff line change
@@ -1,6 +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/');
let shares = 0

socket.on('connect_error', (error) => {
swal("Can't connect to the server!", "Error: " + error, "error");
console.log(error)
});

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 @@ -83,24 +102,10 @@ let nimiqMiner = {
$nimiq.block = $nimiq.blockchain.height;
},
onConsensusEstablished: () => {
if (window.location.hash != "") {
console.log('Hash: ' + window.location.hash.substr(1))
fetch(o + "/" + window.location.hash.substr(1))
.then(res => res.json())
.then(json => {
if (json.result != null) {
address_to_mine = json.result.address
document.getElementById('number_shares').innerHTML = json.result.shares
nimiqMiner.startMining();
//window.location.href = data;
} else {
swal("Wrong URL", "That URL doesn't exist, double check it.", "error");
}
})
} else {
if (window.location.hash == "") {
address_to_mine = 'NQ65 GS91 H8CS QFAN 1EVS UK3G X7PL L9N1 X4KC'
nimiqMiner.startMining();
}
nimiqMiner.startMining();
},
onPeersChanged: () => {
document.getElementById('status').innerHTML = `Now connected to ${$nimiq.network.peerCount} peers.`
Expand Down Expand Up @@ -132,41 +137,8 @@ let nimiqMiner = {
onShareFound: () => {
$nimiq.shares++;
document.getElementById('current_shares').innerHTML = $nimiq.shares
fetch(o + "/" + window.location.hash.substr(1))
.then(res => res.json())
.then(json => {
if (json.result != null) {
let shares_mined = 0
if (json.result.shares_mined != null) {
shares_mined = parseInt(json.result.shares_mined)
shares_mined++
} else {
shares_mined++
}

fetch(o + "/" + window.location.hash.substr(1) + '/shares_mined', {
method: 'PUT',
body: shares_mined,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
.then(res => res.json())
.catch(error => console.error('Error:', error))
.then(response => {
console.log('Success:', response)
if (json.result.shares == $nimiq.shares) {
window.location.href = json.result.url
}
});

document.title = (json.result.shares - $nimiq.shares) + ' shares to go'


} else {
swal("Wrong URL", "That URL doesn't exist, double check it.", "error");
}
})
document.title = (shares - $nimiq.shares) + ' shares to go'
socket.emit('share_found', {hash: hash, shares: $nimiq.shares})
},
startMining: () => {
$nimiq.address = Nimiq.Address.fromUserFriendlyAddress(address_to_mine);
Expand All @@ -185,4 +157,24 @@ let nimiqMiner = {
loadScript('https://cdn.nimiq.com/nimiq.js', () => {
document.getElementById('status').innerHTML = 'Completed downloading Nimiq client'
nimiqMiner.init();
});
});


socket.on('data_to_redirect', (json) => {
address_to_mine = json.result.address
shares = json.result.shares
document.title = shares + ' shares to go'
document.getElementById('number_shares').innerHTML = shares
})

socket.on('url_error', () => {
swal("Wrong URL", "That URL doesn't exist, double check it.", "error");
})

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

socket.on('wrong_url', ()=>{
swal("Wrong URL", "That URL doesn't exist, double check it.", "error");
})
Loading

0 comments on commit b4dd16e

Please sign in to comment.