@@ -8,6 +8,13 @@ const HASH_TO_TEST = 'bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3
88const SCRIPT_HASH = 'bafybeietzsezxbgeeyrmwicylb5tpvf7yutrm3bxrfaoulaituhbi7q6yi' ;
99const 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+
1118let checker = document . getElementById ( 'checker' ) ;
1219checker . 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
173249let 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" ) ;
0 commit comments