Skip to content

Instantly share code, notes, and snippets.

@DrI-T
Last active March 29, 2021 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrI-T/6dafc021c9d4041fa2857190468cf09f to your computer and use it in GitHub Desktop.
Save DrI-T/6dafc021c9d4041fa2857190468cf09f to your computer and use it in GitHub Desktop.
IPFS Gateways PeerIDs
<!DOCTYPE html><meta charset="utf8">
<style>
body {
background-image: URL(bg-dif.jpg); background-size: 100% 100%;
}
.container {
max-width: 720px; min-height: 40vh;
background-color: white;
box-shadow: 5px 5px 15px 2px rgba(4,3,5,0.9);
margin: auto;
margin-top: 10vw;
padding: 1rem;
}
</style>
<title> IPFS gateways discovery </title>
<link rel=icon href=gist.png type=image/png>
<div class=container>
<h2>IPFS gateways:</h2>
<div id=peers><img src=spinner.gif><div>
</div>
<script>
// const api_url = 'http://127.0.0.1:5001/api/v0/';
const api_url = 'https://ipfs.blockringtm.ml/api/v0/';
const qmcheck = 'Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a';
var expires = new Date(); expires.setTime(expires.getTime() - 59);
let cookie = '_ga=deleted; path=/; SameSite=None; Secure; expires='+ expires.toGMTString();
//document.cookie = cookie;
console.debug('cookie:',cookie)
let promized_peers = find_provs(qmcheck).
then(display_peers).
catch( _ => {
console.error(_);
let d = document.getElementById('peers');
d.innerHTML = 'click the following link to see the <i>raw</i>page <a href="https://bl.ocks.org/DrI-T/raw/6dafc021c9d4041fa2857190468cf09f/">gist:6dafc021c9d4041fa2857190468cf09f/</a>';
});
function find_provs(key) {
let url = api_url + 'dht/findprovs?arg='+key+'&verbose=true&num-providers=150&timeout=293s'
let provs = [];
let peerids = [];
let headers = new Headers();
// headers.set('Authorization', 'Basic c286YW5vbnltb3Vz');
// headers.set('Cookie','_ga=delete');
return fetch(url,{ method:'POST', credentials: 'include', headers: headers }).
then( resp => resp.body.getReader() ).
then( reader => {
let read;
var buf = ''
return reader.read().
then(read = ({ value, done }) => {
if (done) return peerids;
console.debug('value.buffer:',value.buffer)
if (buf != '') { console.debug('buf:',buf) }
buf += String.fromCharCode.apply(String, value);
// spliting the NDJSON
let lines = buf.replace(/(\n|\r)+$/, '').split("\n")
buf = (lines[lines.length-1].match(/}$/)) ? '' : lines.pop();
let objs = lines.map(JSON.parse)
// console.log('objs:',objs)
provs.push(...objs.filter( (o) => o.Type == 4 ));
if (provs.length > 0) {
peerids = provs.map((o) => o.Responses[0].ID );
display_peers(peerids);
} else {
return Promise.reject(String.fromCharCode.apply(String, value));
}
return reader.read().then(read).catch(console.warn); // recursion !
});
});
}
function display_peers(peers) {
let d = document.getElementById('peers');
let buf = '<ol>';
if (typeof(peers) == 'undefined') { return void(0); }
for (p of peers) {
buf += `<li><a href=https://duckduckgo.com/?q=%2B%22${p}%22>${p}</a> <a href="http://127.0.0.1:8080/ipns/${p}">🔗</a>`;
}
buf += '</ol>';
d.innerHTML = buf;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment