add cloudflare

This commit is contained in:
2025-03-18 12:55:32 +01:00
parent 940b8bbec4
commit a540f1eb0a
3 changed files with 63 additions and 43 deletions
+55 -40
View File
@@ -42,7 +42,14 @@ async function handleTabUpdate(url: string) {
const apiResponse = await fetch(`https://ip.albert.lol/${ip}`) const apiResponse = await fetch(`https://ip.albert.lol/${ip}`)
const apiData = await apiResponse.json() const apiData = await apiResponse.json()
await updateIcon(apiData.country || null) const country = apiData.country || null
const asn = apiData.org?.split(' ')[0]
let iconCode = country
if (!iconCode && asn === 'AS13335') {
iconCode = 'cloudflare'
}
await updateIcon(iconCode)
} catch (error) { } catch (error) {
console.error('Failed to handle tab update:', error) console.error('Failed to handle tab update:', error)
await updateIcon(null) await updateIcon(null)
@@ -60,48 +67,56 @@ browser.tabs.onUpdated.addListener(async (_tabId, changeInfo) => {
export default defineBackground({ export default defineBackground({
main() { main() {
browser.runtime.onMessage.addListener((request: any, _sender, sendResponse) => { browser.runtime.onMessage.addListener(
if (request.type === 'FETCH_SERVER_INFO') { (request: any, _sender, sendResponse) => {
;(async () => { if (request.type === 'FETCH_SERVER_INFO') {
try { ;(async () => {
const ip = await resolveARecord(request.hostname) try {
if (!ip) { const ip = await resolveARecord(request.hostname)
sendResponse({ error: 'DNS resolution failed', data: null }) if (!ip) {
return sendResponse({ error: 'DNS resolution failed', data: null })
return
}
const apiResponse = await fetch(`https://ip.albert.lol/${ip}`)
const apiData = await apiResponse.json()
const parsed = psl.parse(request.hostname)
const origin = 'domain' in parsed ? parsed.domain : null
const country = apiData.country || null
const asn = apiData.org?.split(' ')[0]
let iconCode = country
if (!iconCode && asn === 'AS13335') {
iconCode = 'cloudflare'
}
await updateIcon(iconCode)
sendResponse({
error: null,
data: {
origin,
ip: apiData.ip,
hostname: apiData.hostname || 'N/A',
country: apiData.country || null,
city: apiData.city || null,
org: apiData.org,
},
})
} catch (error) {
await updateIcon(null)
sendResponse({
error: error instanceof Error ? error.message : 'Unknown error',
data: null,
})
} }
})()
return true
}
const apiResponse = await fetch(`https://ip.albert.lol/${ip}`) sendResponse({ error: 'Unknown request type', data: null })
const apiData = await apiResponse.json()
const parsed = psl.parse(request.hostname)
const origin = 'domain' in parsed ? parsed.domain : null
await updateIcon(apiData.country)
sendResponse({
error: null,
data: {
origin,
ip: apiData.ip,
hostname: apiData.hostname || 'N/A',
country: apiData.country || null,
city: apiData.city || null,
org: apiData.org,
},
})
} catch (error) {
await updateIcon(null)
sendResponse({
error: error instanceof Error ? error.message : 'Unknown error',
data: null,
})
}
})()
return true return true
} }
)
sendResponse({ error: 'Unknown request type', data: null })
return true
})
}, },
}) })
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

+8 -3
View File
@@ -1,9 +1,14 @@
export async function updateIcon(countryCode: string | null) { export async function updateIcon(countryCode: string | null) {
const validCode = let validCode
countryCode?.match(/^[A-Z]{2}$/i)?.[0]?.toLowerCase() || 'unknown' if (countryCode === 'cloudflare') {
validCode = 'cloudflare'
} else {
validCode =
countryCode?.match(/^[A-Z]{2}$/i)?.[0]?.toLowerCase() || 'unknown'
}
const loadImageBitmap = async (code: string): Promise<ImageBitmap> => { const loadImageBitmap = async (code: string): Promise<ImageBitmap> => {
const url = browser.runtime.getURL("/") const url = browser.runtime.getURL('/')
try { try {
const response = await fetch(url + `${code}.webp`) const response = await fetch(url + `${code}.webp`)
if (!response.ok) throw new Error('Flag not found') if (!response.ok) throw new Error('Flag not found')