forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlag.ts
141 lines (126 loc) · 4.43 KB
/
Flag.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { GatewayNode } from './GatewayNode'
import { Log } from './Log'
import { lookup as IpfsGeoIpLookup } from 'ipfs-geoip'
import { UiComponent } from './UiComponent'
import { TokenBucketLimiter } from '@dutu/rate-limiter'
import { DEFAULT_IPFS_GATEWAY } from './constants'
const log = new Log('Flag')
class Flag extends UiComponent {
/**
*/
public static readonly googleLimiter = new TokenBucketLimiter({ bucketSize: 1, tokensPerInterval: 1, interval: 1000 * 2, stopped: true })
public static readonly cloudFlareLimiter = new TokenBucketLimiter({ bucketSize: 1, tokensPerInterval: 1, interval: 1000 * 2, stopped: true })
constructor (protected parent: GatewayNode, private readonly hostname: string) {
super(parent, 'div', 'Flag')
}
async check (): Promise<void> {
let ask = true
try {
const savedSTR = localStorage.getItem(this.hostname)
if (savedSTR != null) {
const saved = JSON.parse(savedSTR)
const now = Date.now()
const savedTime = saved.time
const elapsed = now - savedTime
const expiration = 7 * 24 * 60 * 60 * 1000 // 7 days
if (elapsed < expiration) {
ask = false
this.onResponse(saved)
}
}
} catch (e) {
log.error(`error while getting savedSTR for ${this.hostname}`, e)
this.onError()
throw e
}
if (ask) {
this.startLimiters()
const url = await this.waitForAvailableEndpoint()
await this.dnsRequest(url)
}
}
private startLimiters (): void {
if (Flag.googleLimiter.isStopped === true) {
Flag.googleLimiter.start()
}
if (Flag.cloudFlareLimiter.isStopped === true) {
Flag.cloudFlareLimiter.start()
}
}
async waitForAvailableEndpoint (): Promise<string> {
const url: string | null = await Promise.race([
Flag.googleLimiter.awaitTokens(1).then(() => Flag.googleLimiter.tryRemoveTokens(1)).then((tokenAvailable: boolean) => {
if (tokenAvailable) {
return `https://dns.google/resolve?name=${this.hostname}&type=A`
}
}),
Flag.cloudFlareLimiter.awaitTokens(1).then(() => Flag.cloudFlareLimiter.tryRemoveTokens(1)).then((tokenAvailable: boolean) => {
if (tokenAvailable) {
return `https://cloudflare-dns.com/dns-query?name=${this.hostname}&type=A`
}
})
])
if (url == null) {
// No available tokens...
log.info('we awaited tokens, but could not retrieve any.. restarting dnsRequest')
return await this.waitForAvailableEndpoint()
} else {
return url
}
}
private async dnsRequest (url: string): Promise<void> {
try {
const response = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/dns-json'
}
})
const responseJson = await response.json()
await this.handleDnsQueryResponse(responseJson)
} catch (err) {
log.error('problem submitting DNS request', url, err)
this.onError()
}
}
async handleDnsQueryResponse (response: DnsQueryResponse): Promise<void> {
if (response.Answer == null) {
log.error('Response does not contain the "Answer" property:', response)
return this.onError()
}
let ip = null
for (let i = 0; i < response.Answer.length && ip == null; i++) {
const answer = response.Answer[i]
if (answer.type === 1) {
ip = answer.data
}
}
if (ip != null) {
try {
const geoipResponse = await IpfsGeoIpLookup(DEFAULT_IPFS_GATEWAY, ip)
if (geoipResponse?.country_code != null) {
this.onResponse(geoipResponse)
geoipResponse.time = Date.now()
const responseSTR = JSON.stringify(geoipResponse)
localStorage.setItem(this.hostname, responseSTR)
} else {
log.error('geoipResponse.country_code is null')
}
} catch (e) {
log.error(`error while getting DNS A record for ${this.hostname}`, e)
this.onError()
}
} else {
log.error('IP is still null', response)
}
}
private onError (): void {
this.tag.empty()
}
onResponse (response: IpfsGeoip.LookupResponse): void {
this.tag.style.setProperty('background-image', `url('https://ipfs.io/ipfs/QmaYjj5BHGAWfopTdE8ESzypbuthsZqTeqz9rEuh3EJZi6/${response.country_code.toLowerCase()}.svg')`)
this.tag.title = response.country_name
this.tag.empty() // remove textContent icon since we're using a background image
}
}
export { Flag }