mirror of
https://github.com/skidoodle/hostinfo
synced 2026-04-28 09:37:37 +02:00
fix lookup
This commit is contained in:
+42
-12
@@ -74,15 +74,30 @@ async function initTab(tabId: number, url: string, resolveDns = false) {
|
|||||||
|
|
||||||
applyIconForState(tabId, newState);
|
applyIconForState(tabId, newState);
|
||||||
|
|
||||||
if (!isSystem && !hasExistingData && resolveDns) {
|
if (!isSystem && !hasExistingData) {
|
||||||
const ip = await DnsService.resolve(domain);
|
const performDnsFallback = async () => {
|
||||||
if (ip) {
|
const state = tabStates.get(tabId);
|
||||||
await processIp(tabId, url, ip);
|
if (state?.status !== 'loading' || state.url !== url) return;
|
||||||
|
|
||||||
|
const ip = await DnsService.resolve(domain);
|
||||||
|
|
||||||
|
const stateAfterDns = tabStates.get(tabId);
|
||||||
|
if (stateAfterDns?.status !== 'loading' || stateAfterDns.url !== url) return;
|
||||||
|
|
||||||
|
if (ip) {
|
||||||
|
await processIp(tabId, url, ip);
|
||||||
|
} else {
|
||||||
|
await updateState(tabId, {
|
||||||
|
status: 'error',
|
||||||
|
errorMessage: 'Could not resolve host'
|
||||||
|
}, url);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (resolveDns) {
|
||||||
|
await performDnsFallback();
|
||||||
} else {
|
} else {
|
||||||
await updateState(tabId, {
|
setTimeout(performDnsFallback, 1500);
|
||||||
status: 'error',
|
|
||||||
errorMessage: 'Could not resolve host'
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,13 +147,14 @@ async function processIp(tabId: number, url: string, ip: string) {
|
|||||||
applyIconForState(tabId, newState);
|
applyIconForState(tabId, newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateState(tabId: number, updates: Partial<TabState>) {
|
async function updateState(tabId: number, updates: Partial<TabState>, expectedUrl?: string) {
|
||||||
let current = tabStates.get(tabId);
|
let current = tabStates.get(tabId);
|
||||||
if (!current) {
|
if (!current) {
|
||||||
current = await StorageService.getTabState(tabId) || undefined;
|
current = await StorageService.getTabState(tabId) || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current) {
|
if (current) {
|
||||||
|
if (expectedUrl && current.url !== expectedUrl) return;
|
||||||
const newState = { ...current, ...updates };
|
const newState = { ...current, ...updates };
|
||||||
tabStates.set(tabId, newState);
|
tabStates.set(tabId, newState);
|
||||||
await StorageService.setTabState(tabId, newState);
|
await StorageService.setTabState(tabId, newState);
|
||||||
@@ -194,13 +210,16 @@ export default defineBackground({
|
|||||||
}
|
}
|
||||||
const ip = await DnsService.resolve(hostname);
|
const ip = await DnsService.resolve(hostname);
|
||||||
|
|
||||||
|
const currentState = tabStates.get(details.tabId);
|
||||||
|
if (currentState?.status !== 'loading' || currentState.url !== details.url) return;
|
||||||
|
|
||||||
if (ip) {
|
if (ip) {
|
||||||
await processIp(details.tabId, details.url, ip);
|
await processIp(details.tabId, details.url, ip);
|
||||||
} else {
|
} else {
|
||||||
await updateState(details.tabId, {
|
await updateState(details.tabId, {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
errorMessage: 'Could not resolve host'
|
errorMessage: 'Could not resolve host'
|
||||||
});
|
}, details.url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -208,12 +227,23 @@ export default defineBackground({
|
|||||||
browser.webRequest.onErrorOccurred.addListener(
|
browser.webRequest.onErrorOccurred.addListener(
|
||||||
async (details) => {
|
async (details) => {
|
||||||
if (details.type !== 'main_frame') return;
|
if (details.type !== 'main_frame') return;
|
||||||
if (details.error === 'net::ERR_ABORTED') return;
|
if (details.error === 'net::ERR_ABORTED') {
|
||||||
|
try {
|
||||||
|
const tab = await browser.tabs.get(details.tabId);
|
||||||
|
if (tab.url) {
|
||||||
|
const currentState = tabStates.get(details.tabId);
|
||||||
|
if (currentState && currentState.url !== tab.url) {
|
||||||
|
initTab(tab.id!, tab.url, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch { }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await updateState(details.tabId, {
|
await updateState(details.tabId, {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
errorMessage: details.error
|
errorMessage: details.error
|
||||||
});
|
}, details.url);
|
||||||
},
|
},
|
||||||
{ urls: ['<all_urls>'] }
|
{ urls: ['<all_urls>'] }
|
||||||
);
|
);
|
||||||
|
|||||||
+5
-1
@@ -1,13 +1,17 @@
|
|||||||
export const DnsService = {
|
export const DnsService = {
|
||||||
async resolve(hostname: string): Promise<string | null> {
|
async resolve(hostname: string): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`https://cloudflare-dns.com/dns-query?name=${hostname}&type=A`,
|
`https://cloudflare-dns.com/dns-query?name=${hostname}&type=A`,
|
||||||
{
|
{
|
||||||
headers: { accept: 'application/dns-json' },
|
headers: { accept: 'application/dns-json' },
|
||||||
credentials: 'omit'
|
credentials: 'omit',
|
||||||
|
signal: controller.signal,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
clearTimeout(timeout);
|
||||||
|
|
||||||
if (!response.ok) return null;
|
if (!response.ok) return null;
|
||||||
|
|
||||||
|
|||||||
+5
-1
@@ -13,11 +13,15 @@ export const GeoService = {
|
|||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), 5000);
|
||||||
const res = await fetch(`https://ip.albert.lol/${ip}`, {
|
const res = await fetch(`https://ip.albert.lol/${ip}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
cache: 'force-cache',
|
cache: 'force-cache',
|
||||||
credentials: 'omit'
|
credentials: 'omit',
|
||||||
|
signal: controller.signal,
|
||||||
});
|
});
|
||||||
|
clearTimeout(timeout);
|
||||||
|
|
||||||
if (!res.ok) throw new Error(`API Error ${res.status}`);
|
if (!res.ok) throw new Error(`API Error ${res.status}`);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user