Skip to content

Commit 84a31fe

Browse files
lazyweirdopulsartroniclidel
authored
feat: country flags (#96)
Co-authored-by: pulsartronic <[email protected]> Co-authored-by: Marcin Rataj <[email protected]>
1 parent c921c4e commit 84a31fe

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
You can view this website on GitHub Pages: https://ipfs.github.io/public-gateway-checker/
66

7-
[![screenshot_2020-01-05.png](https://ipfs.io/ipfs/QmPw3s2zijn3zmCDAnWMEaHx9JTSevfG7uZaiCKc5A21U1?filename=screenshot_2020-01-05.png)](https://ipfs.github.io/public-gateway-checker/)
7+
[![screenshot_2020-01-05.png](https://ipfs.io/ipfs/QmRjQyxLwvd6D4fGSDx3uPM1nRmBnX4HwEaK5p8fuZFtpp?filename=screenshot.jpg)](https://ipfs.github.io/public-gateway-checker/)
88

99
**NOTE:** All of these (except `ipfs.io` and `dweb.link`) are hosted by third-parties and should be treated as such.
1010

app.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ const HASH_TO_TEST = 'bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3
88
const SCRIPT_HASH = 'bafybeietzsezxbgeeyrmwicylb5tpvf7yutrm3bxrfaoulaituhbi7q6yi';
99
const HASH_STRING = 'Hello from IPFS Gateway Checker';
1010

11+
const ipfs_http_client = window.IpfsHttpClient({
12+
host: 'ipfs.io',
13+
port: 443,
14+
protocol: 'https'
15+
});
16+
17+
1118
let checker = document.getElementById('checker');
1219
checker.nodes = [];
1320

@@ -169,6 +176,75 @@ Origin.prototype.onerror = function() {
169176
this.tag.textContent = '❌';
170177
};
171178

179+
let Flag = function(parent, hostname) {
180+
this.parent = parent;
181+
this.tag = document.createElement("div");
182+
this.tag.className = "Flag";
183+
this.tag.textContent = '';
184+
185+
let ask = true;
186+
187+
try{
188+
let savedSTR = localStorage.getItem(hostname);
189+
if (savedSTR) {
190+
let saved = JSON.parse(savedSTR);
191+
let now = Date.now();
192+
let savedTime = saved.time;
193+
let elapsed = now - savedTime;
194+
let expiration = 7 * 24 * 60 * 60 * 1000; // 7 days
195+
if (elapsed < expiration) {
196+
ask = false;
197+
this.onResponse(saved);
198+
}
199+
}
200+
} catch(e) {
201+
console.error(`error while getting savedSTR for ${hostname}`, e)
202+
}
203+
204+
if (ask) {
205+
setTimeout(() => {
206+
let request = new XMLHttpRequest();
207+
request.open('GET', `https://cloudflare-dns.com/dns-query?name=${hostname}&type=A`);
208+
request.setRequestHeader("accept", "application/dns-json");
209+
request.onreadystatechange = async () => {
210+
if (4 == request.readyState) {
211+
if (200 == request.status) {
212+
try {
213+
let response = JSON.parse(request.responseText);
214+
let ip = null;
215+
for (let i = 0; i < response.Answer.length && !ip; i++) {
216+
let answer = response.Answer[i];
217+
if (1 == answer.type) {
218+
ip = answer.data;
219+
}
220+
}
221+
if (ip) {
222+
let geoipResponse = await window.IpfsGeoip.lookup(ipfs_http_client, ip);
223+
if (geoipResponse && geoipResponse.country_code) {
224+
this.onResponse(geoipResponse);
225+
geoipResponse.time = Date.now();
226+
let resposeSTR = JSON.stringify(geoipResponse);
227+
localStorage.setItem(hostname, resposeSTR);
228+
}
229+
}
230+
} catch(e) {
231+
console.error(`error while getting DNS A record for ${hostname}`, e)
232+
}
233+
}
234+
}
235+
};
236+
request.onerror = (e) => {};
237+
request.send();
238+
}, 500 * Flag.requests++); // 2 / second, request limit
239+
}
240+
};
241+
242+
Flag.prototype.onResponse = function(response) {
243+
this.tag.style["background-image"] = `url('https://ipfs.io/ipfs/QmaYjj5BHGAWfopTdE8ESzypbuthsZqTeqz9rEuh3EJZi6/${response.country_code.toLowerCase()}.svg')`;
244+
this.tag.title = response.country_name;
245+
};
246+
247+
Flag.requests = 0;
172248

173249
let Node = function(parent, gateway, index) {
174250
this.parent = parent;
@@ -185,11 +261,15 @@ let Node = function(parent, gateway, index) {
185261
this.origin = new Origin(this);
186262
this.tag.append(this.origin.tag);
187263

264+
188265
this.link = document.createElement("div");
189266
let gatewayAndHash = gateway.replace(':hash', HASH_TO_TEST);
190267
this.link.url = new URL(gatewayAndHash);
191268
this.link.textContent = gatewayHostname(this.link.url);
192269
this.link.className = "Link";
270+
271+
this.flag = new Flag(this, this.link.textContent);
272+
this.tag.append(this.flag.tag);
193273
this.tag.append(this.link);
194274

195275
this.took = document.createElement("div");

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<meta name="viewport" content="width=device-width, initial-scale=1.0">
99
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/tachyons.min.css" integrity="sha256-XiJ+PedljEmPP2VaQzSzekfCZdPr0fpqmh9dY6kpsuQ=" crossorigin="anonymous">
1010
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/ipfs.css" integrity="sha256-crEOQ/1aKoWgku50e0aqNzt0Tt/ev2C97PVr5hGpeEY=" crossorigin="anonymous">
11+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js" integrity="sha256-TMFHdG0nkNPPHBpd2UNil3TMjSPxWLcRgvwANAmjuRg=" crossorigin="anonymous"></script>
12+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.min.js" integrity="sha256-xVFCr/DOPgR7kCrn6mb3xCnj2C1BOVxTCP4/h4UzGiQ=" crossorigin="anonymous"></script>
1113
<link href="styles.css" type="text/css" rel="stylesheet"/>
1214
</head>
1315
<body id="checker" class="sans-serif charcoal bg-snow-muted">
@@ -27,5 +29,5 @@
2729
</div>
2830
</div>
2931
</body>
30-
<script src="./app.js?v=0.2"></script>
32+
<script src="./app.js?v=0.3"></script>
3133
</html>

styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ div.Node div.Origin {
100100
user-select: none;
101101
}
102102

103+
div.Node div.Flag {
104+
width: 2em;
105+
height: 1em;
106+
background-repeat: no-repeat;
107+
background-size: contain;
108+
background-position: center;
109+
}
110+
103111
div.Node div.Took {
104112
min-width: 5em;
105113
text-align: right;

0 commit comments

Comments
 (0)