Merge branch 'master' into call-audio-video-jitsi
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
// Mapa de seeds para os 32 avatares
|
||||
const avatarSeeds: Record<string, string> = {
|
||||
// Masculinos (16)
|
||||
"avatar-m-1": "John",
|
||||
"avatar-m-2": "Peter",
|
||||
"avatar-m-3": "Michael",
|
||||
"avatar-m-4": "David",
|
||||
"avatar-m-5": "James",
|
||||
"avatar-m-6": "Robert",
|
||||
"avatar-m-7": "William",
|
||||
"avatar-m-8": "Joseph",
|
||||
"avatar-m-9": "Thomas",
|
||||
"avatar-m-10": "Charles",
|
||||
"avatar-m-11": "Daniel",
|
||||
"avatar-m-12": "Matthew",
|
||||
"avatar-m-13": "Anthony",
|
||||
"avatar-m-14": "Mark",
|
||||
"avatar-m-15": "Donald",
|
||||
"avatar-m-16": "Steven",
|
||||
// Femininos (16)
|
||||
"avatar-f-1": "Maria",
|
||||
"avatar-f-2": "Ana",
|
||||
"avatar-f-3": "Patricia",
|
||||
"avatar-f-4": "Jennifer",
|
||||
"avatar-f-5": "Linda",
|
||||
"avatar-f-6": "Barbara",
|
||||
"avatar-f-7": "Elizabeth",
|
||||
"avatar-f-8": "Jessica",
|
||||
"avatar-f-9": "Sarah",
|
||||
"avatar-f-10": "Karen",
|
||||
"avatar-f-11": "Nancy",
|
||||
"avatar-f-12": "Betty",
|
||||
"avatar-f-13": "Helen",
|
||||
"avatar-f-14": "Sandra",
|
||||
"avatar-f-15": "Ashley",
|
||||
"avatar-f-16": "Kimberly",
|
||||
};
|
||||
|
||||
/**
|
||||
* Gera URL do avatar usando API DiceBear com parâmetros simples
|
||||
*/
|
||||
export function getAvatarUrl(avatarId: string): string {
|
||||
const seed = avatarSeeds[avatarId] || avatarId || "default";
|
||||
|
||||
// Usar avataarstyle do DiceBear com parâmetros mínimos
|
||||
// API v7 suporta apenas parâmetros específicos
|
||||
return `https://api.dicebear.com/7.x/avataaars/svg?seed=${encodeURIComponent(seed)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lista todos os IDs de avatares disponíveis
|
||||
*/
|
||||
export function getAllAvatarIds(): string[] {
|
||||
return Object.keys(avatarSeeds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica se um avatarId é válido
|
||||
*/
|
||||
export function isValidAvatarId(avatarId: string): boolean {
|
||||
return avatarId in avatarSeeds;
|
||||
}
|
||||
|
||||
@@ -1,283 +0,0 @@
|
||||
// Galeria de avatares inspirados em artistas do cinema
|
||||
// Usando DiceBear API com estilos variados para aparência cinematográfica
|
||||
|
||||
export interface Avatar {
|
||||
id: string;
|
||||
name: string;
|
||||
url: string;
|
||||
seed: string;
|
||||
style: string;
|
||||
}
|
||||
|
||||
// Avatares inspirados em artistas do cinema (30 avatares estilizados)
|
||||
const cinemaArtistsAvatars = [
|
||||
// 15 Masculinos - Inspirados em grandes atores
|
||||
{
|
||||
id: 'avatar-male-1',
|
||||
name: 'Leonardo DiCaprio',
|
||||
seed: 'Leonardo',
|
||||
style: 'adventurer',
|
||||
bgColor: 'C5CAE9',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-2',
|
||||
name: 'Brad Pitt',
|
||||
seed: 'Bradley',
|
||||
style: 'adventurer',
|
||||
bgColor: 'B2DFDB',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-3',
|
||||
name: 'Tom Hanks',
|
||||
seed: 'Thomas',
|
||||
style: 'adventurer-neutral',
|
||||
bgColor: 'DCEDC8',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-4',
|
||||
name: 'Morgan Freeman',
|
||||
seed: 'Morgan',
|
||||
style: 'adventurer',
|
||||
bgColor: 'F0F4C3',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-5',
|
||||
name: 'Robert De Niro',
|
||||
seed: 'Robert',
|
||||
style: 'adventurer-neutral',
|
||||
bgColor: 'E0E0E0',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-6',
|
||||
name: 'Al Pacino',
|
||||
seed: 'Alfredo',
|
||||
style: 'adventurer',
|
||||
bgColor: 'FFCCBC',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-7',
|
||||
name: 'Johnny Depp',
|
||||
seed: 'John',
|
||||
style: 'adventurer',
|
||||
bgColor: 'D1C4E9',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-8',
|
||||
name: 'Denzel Washington',
|
||||
seed: 'Denzel',
|
||||
style: 'adventurer-neutral',
|
||||
bgColor: 'B3E5FC',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-9',
|
||||
name: 'Will Smith',
|
||||
seed: 'Willard',
|
||||
style: 'adventurer',
|
||||
bgColor: 'FFF9C4',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-10',
|
||||
name: 'Tom Cruise',
|
||||
seed: 'TomC',
|
||||
style: 'adventurer-neutral',
|
||||
bgColor: 'CFD8DC',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-11',
|
||||
name: 'Samuel L Jackson',
|
||||
seed: 'Samuel',
|
||||
style: 'adventurer',
|
||||
bgColor: 'F8BBD0',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-12',
|
||||
name: 'Harrison Ford',
|
||||
seed: 'Harrison',
|
||||
style: 'adventurer-neutral',
|
||||
bgColor: 'C8E6C9',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-13',
|
||||
name: 'Keanu Reeves',
|
||||
seed: 'Keanu',
|
||||
style: 'adventurer',
|
||||
bgColor: 'BBDEFB',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-14',
|
||||
name: 'Matt Damon',
|
||||
seed: 'Matthew',
|
||||
style: 'adventurer-neutral',
|
||||
bgColor: 'FFE0B2',
|
||||
},
|
||||
{
|
||||
id: 'avatar-male-15',
|
||||
name: 'Christian Bale',
|
||||
seed: 'Christian',
|
||||
style: 'adventurer',
|
||||
bgColor: 'E1BEE7',
|
||||
},
|
||||
// 15 Femininos - Inspiradas em grandes atrizes
|
||||
{
|
||||
id: 'avatar-female-1',
|
||||
name: 'Meryl Streep',
|
||||
seed: 'Meryl',
|
||||
style: 'lorelei',
|
||||
bgColor: 'F8BBD0',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-2',
|
||||
name: 'Scarlett Johansson',
|
||||
seed: 'Scarlett',
|
||||
style: 'lorelei',
|
||||
bgColor: 'FFCCBC',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-3',
|
||||
name: 'Jennifer Lawrence',
|
||||
seed: 'Jennifer',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'E1BEE7',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-4',
|
||||
name: 'Angelina Jolie',
|
||||
seed: 'Angelina',
|
||||
style: 'lorelei',
|
||||
bgColor: 'C5CAE9',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-5',
|
||||
name: 'Cate Blanchett',
|
||||
seed: 'Catherine',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'B2DFDB',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-6',
|
||||
name: 'Nicole Kidman',
|
||||
seed: 'Nicole',
|
||||
style: 'lorelei',
|
||||
bgColor: 'DCEDC8',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-7',
|
||||
name: 'Julia Roberts',
|
||||
seed: 'Julia',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'FFF9C4',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-8',
|
||||
name: 'Emma Stone',
|
||||
seed: 'Emma',
|
||||
style: 'lorelei',
|
||||
bgColor: 'CFD8DC',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-9',
|
||||
name: 'Natalie Portman',
|
||||
seed: 'Natalie',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'F0F4C3',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-10',
|
||||
name: 'Charlize Theron',
|
||||
seed: 'Charlize',
|
||||
style: 'lorelei',
|
||||
bgColor: 'E0E0E0',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-11',
|
||||
name: 'Kate Winslet',
|
||||
seed: 'Kate',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'D1C4E9',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-12',
|
||||
name: 'Sandra Bullock',
|
||||
seed: 'Sandra',
|
||||
style: 'lorelei',
|
||||
bgColor: 'B3E5FC',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-13',
|
||||
name: 'Halle Berry',
|
||||
seed: 'Halle',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'C8E6C9',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-14',
|
||||
name: 'Anne Hathaway',
|
||||
seed: 'Anne',
|
||||
style: 'lorelei',
|
||||
bgColor: 'BBDEFB',
|
||||
},
|
||||
{
|
||||
id: 'avatar-female-15',
|
||||
name: 'Amy Adams',
|
||||
seed: 'Amy',
|
||||
style: 'lorelei-neutral',
|
||||
bgColor: 'FFE0B2',
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* Gera uma galeria de avatares inspirados em artistas do cinema
|
||||
* Usa DiceBear API com estilos cinematográficos
|
||||
* @param count Número de avatares a gerar (padrão: 30)
|
||||
* @returns Array de objetos com id, name, url, seed e style
|
||||
*/
|
||||
export function generateAvatarGallery(count: number = 30): Avatar[] {
|
||||
const avatars: Avatar[] = [];
|
||||
|
||||
for (let i = 0; i < Math.min(count, cinemaArtistsAvatars.length); i++) {
|
||||
const avatar = cinemaArtistsAvatars[i];
|
||||
|
||||
// URL do DiceBear com estilo cinematográfico
|
||||
const url = `https://api.dicebear.com/7.x/${avatar.style}/svg?seed=${encodeURIComponent(avatar.seed)}&backgroundColor=${avatar.bgColor}&radius=50&size=200`;
|
||||
|
||||
avatars.push({
|
||||
id: avatar.id,
|
||||
name: avatar.name,
|
||||
url,
|
||||
seed: avatar.seed,
|
||||
style: avatar.style,
|
||||
});
|
||||
}
|
||||
|
||||
return avatars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obter URL do avatar por ID
|
||||
* @param avatarId ID do avatar (ex: "avatar-male-1")
|
||||
* @returns URL do avatar ou string vazia se não encontrado
|
||||
*/
|
||||
export function getAvatarUrl(avatarId: string): string {
|
||||
const gallery = generateAvatarGallery();
|
||||
const avatar = gallery.find(a => a.id === avatarId);
|
||||
return avatar?.url || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gerar avatar aleatório da galeria
|
||||
* @returns Avatar aleatório
|
||||
*/
|
||||
export function getRandomAvatar(): Avatar {
|
||||
const gallery = generateAvatarGallery();
|
||||
const randomIndex = Math.floor(Math.random() * gallery.length);
|
||||
return gallery[randomIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Salvar avatar selecionado (retorna o ID para salvar no backend)
|
||||
* @param avatarId ID do avatar selecionado
|
||||
* @returns ID do avatar
|
||||
*/
|
||||
export function saveAvatarSelection(avatarId: string): string {
|
||||
return avatarId;
|
||||
}
|
||||
@@ -15,6 +15,13 @@ export interface InformacoesDispositivo {
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
precisao?: number;
|
||||
altitude?: number | null;
|
||||
altitudeAccuracy?: number | null;
|
||||
heading?: number | null;
|
||||
speed?: number | null;
|
||||
confiabilidadeGPS?: number; // 0-1
|
||||
suspeitaSpoofing?: boolean;
|
||||
motivoSuspeita?: string;
|
||||
endereco?: string;
|
||||
cidade?: string;
|
||||
estado?: string;
|
||||
@@ -230,12 +237,289 @@ function obterInformacoesMemoria(): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtém localização via GPS com múltiplas tentativas
|
||||
* Calcula distância entre duas coordenadas (fórmula de Haversine)
|
||||
* Retorna distância em metros
|
||||
*/
|
||||
function calcularDistancia(
|
||||
lat1: number,
|
||||
lon1: number,
|
||||
lat2: number,
|
||||
lon2: number
|
||||
): number {
|
||||
const R = 6371000; // Raio da Terra em metros
|
||||
const dLat = ((lat2 - lat1) * Math.PI) / 180;
|
||||
const dLon = ((lon2 - lon1) * Math.PI) / 180;
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos((lat1 * Math.PI) / 180) *
|
||||
Math.cos((lat2 * Math.PI) / 180) *
|
||||
Math.sin(dLon / 2) *
|
||||
Math.sin(dLon / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
return R * c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtém timezone aproximado por coordenadas
|
||||
*/
|
||||
function obterTimezonePorCoordenadas(latitude: number, longitude: number): string {
|
||||
// Pernambuco está em UTC-3 (America/Recife)
|
||||
if (longitude >= -45 && longitude <= -30 && latitude >= -10 && latitude <= 5) {
|
||||
return 'America/Recife'; // UTC-3
|
||||
}
|
||||
|
||||
// Fallback: usar timezone do sistema
|
||||
try {
|
||||
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
} catch {
|
||||
return 'America/Recife'; // Default
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Captura uma única leitura de localização com todas as propriedades disponíveis
|
||||
*/
|
||||
async function capturarLocalizacaoUnica(
|
||||
enableHighAccuracy: boolean = true,
|
||||
timeout: number = 10000
|
||||
): Promise<{
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
precisao?: number;
|
||||
altitude?: number | null;
|
||||
altitudeAccuracy?: number | null;
|
||||
heading?: number | null;
|
||||
speed?: number | null;
|
||||
timestamp?: number;
|
||||
confiabilidade: number; // 0-1
|
||||
}> {
|
||||
return new Promise((resolve) => {
|
||||
if (typeof navigator === 'undefined' || !navigator.geolocation) {
|
||||
resolve({ confiabilidade: 0 });
|
||||
return;
|
||||
}
|
||||
|
||||
const timeoutId = setTimeout(() => {
|
||||
resolve({ confiabilidade: 0 });
|
||||
}, timeout + 1000);
|
||||
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(position) => {
|
||||
clearTimeout(timeoutId);
|
||||
const coords = position.coords;
|
||||
const { latitude, longitude, accuracy } = coords;
|
||||
|
||||
// Validar coordenadas básicas
|
||||
if (
|
||||
isNaN(latitude) ||
|
||||
isNaN(longitude) ||
|
||||
latitude === 0 ||
|
||||
longitude === 0 ||
|
||||
latitude < -90 ||
|
||||
latitude > 90 ||
|
||||
longitude < -180 ||
|
||||
longitude > 180
|
||||
) {
|
||||
resolve({ confiabilidade: 0 });
|
||||
return;
|
||||
}
|
||||
|
||||
// Calcular score de confiabilidade baseado em propriedades do GPS real
|
||||
const sinaisGPSReal = {
|
||||
temAltitude: coords.altitude !== null && coords.altitude !== 0,
|
||||
temAltitudeAccuracy: coords.altitudeAccuracy !== null && coords.altitudeAccuracy > 0,
|
||||
temHeading: coords.heading !== null && !isNaN(coords.heading),
|
||||
temSpeed: coords.speed !== null && !isNaN(coords.speed),
|
||||
precisaoBoa: accuracy < 20, // GPS real geralmente < 20m
|
||||
precisaoMedia: accuracy >= 20 && accuracy < 100,
|
||||
timestampPreciso: position.timestamp > 0
|
||||
};
|
||||
|
||||
// Calcular confiabilidade: cada sinal adiciona pontos
|
||||
let pontos = 0;
|
||||
const maxPontos = 7;
|
||||
|
||||
if (sinaisGPSReal.temAltitude) pontos += 1;
|
||||
if (sinaisGPSReal.temAltitudeAccuracy) pontos += 1;
|
||||
if (sinaisGPSReal.temHeading) pontos += 0.5;
|
||||
if (sinaisGPSReal.temSpeed) pontos += 0.5;
|
||||
if (sinaisGPSReal.precisaoBoa) pontos += 2;
|
||||
if (sinaisGPSReal.precisaoMedia) pontos += 1;
|
||||
if (sinaisGPSReal.timestampPreciso) pontos += 1;
|
||||
|
||||
const confiabilidade = Math.min(pontos / maxPontos, 1);
|
||||
|
||||
resolve({
|
||||
latitude,
|
||||
longitude,
|
||||
precisao: accuracy,
|
||||
altitude: coords.altitude ?? null,
|
||||
altitudeAccuracy: coords.altitudeAccuracy ?? null,
|
||||
heading: coords.heading ?? null,
|
||||
speed: coords.speed ?? null,
|
||||
timestamp: position.timestamp,
|
||||
confiabilidade
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
clearTimeout(timeoutId);
|
||||
console.warn('Erro ao obter localização:', error.code, error.message);
|
||||
resolve({ confiabilidade: 0 });
|
||||
},
|
||||
{
|
||||
enableHighAccuracy,
|
||||
timeout,
|
||||
maximumAge: 0 // Sempre obter nova leitura
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtém localização via GPS com múltiplas leituras para detectar spoofing
|
||||
* Apps de spoofing geralmente retornam valores idênticos em todas as leituras
|
||||
*/
|
||||
async function obterLocalizacaoMultipla(): Promise<{
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
precisao?: number;
|
||||
altitude?: number | null;
|
||||
altitudeAccuracy?: number | null;
|
||||
heading?: number | null;
|
||||
speed?: number | null;
|
||||
confiabilidade: number; // 0-1
|
||||
suspeitaSpoofing: boolean;
|
||||
motivoSuspeita?: string;
|
||||
}> {
|
||||
if (typeof navigator === 'undefined' || !navigator.geolocation) {
|
||||
return { confiabilidade: 0, suspeitaSpoofing: true, motivoSuspeita: 'Geolocalização não suportada' };
|
||||
}
|
||||
|
||||
// Capturar 3 leituras com intervalo de 2 segundos entre elas
|
||||
const leituras: Array<{
|
||||
lat: number;
|
||||
lon: number;
|
||||
precisao: number;
|
||||
altitude: number | null;
|
||||
confiabilidade: number;
|
||||
}> = [];
|
||||
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const leitura = await capturarLocalizacaoUnica(true, 8000);
|
||||
|
||||
if (leitura.latitude && leitura.longitude && leitura.confiabilidade > 0) {
|
||||
leituras.push({
|
||||
lat: leitura.latitude,
|
||||
lon: leitura.longitude,
|
||||
precisao: leitura.precisao || 999,
|
||||
altitude: leitura.altitude ?? null,
|
||||
confiabilidade: leitura.confiabilidade
|
||||
});
|
||||
}
|
||||
|
||||
// Aguardar 2 segundos entre leituras (exceto na última)
|
||||
if (i < 2) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
}
|
||||
}
|
||||
|
||||
if (leituras.length === 0) {
|
||||
return { confiabilidade: 0, suspeitaSpoofing: true, motivoSuspeita: 'Não foi possível obter localização' };
|
||||
}
|
||||
|
||||
// Se tivermos menos de 2 leituras, usar única leitura com baixa confiança
|
||||
if (leituras.length < 2) {
|
||||
const unica = leituras[0];
|
||||
return {
|
||||
latitude: unica.lat,
|
||||
longitude: unica.lon,
|
||||
precisao: unica.precisao,
|
||||
altitude: unica.altitude,
|
||||
altitudeAccuracy: null,
|
||||
heading: null,
|
||||
speed: null,
|
||||
confiabilidade: unica.confiabilidade * 0.5, // Reduzir confiança por ter apenas 1 leitura
|
||||
suspeitaSpoofing: true,
|
||||
motivoSuspeita: 'Apenas uma leitura obtida'
|
||||
};
|
||||
}
|
||||
|
||||
// Verificar se todas as leituras são idênticas (suspeito de spoofing)
|
||||
const primeiraLeitura = leituras[0];
|
||||
const todasIguais = leituras.every(
|
||||
(l) =>
|
||||
Math.abs(l.lat - primeiraLeitura.lat) < 0.00001 && // ~1 metro
|
||||
Math.abs(l.lon - primeiraLeitura.lon) < 0.00001
|
||||
);
|
||||
|
||||
if (todasIguais && leituras.length === 3) {
|
||||
// GPS real varia alguns metros, se todas são idênticas pode ser spoofing
|
||||
return {
|
||||
latitude: primeiraLeitura.lat,
|
||||
longitude: primeiraLeitura.lon,
|
||||
precisao: primeiraLeitura.precisao,
|
||||
altitude: primeiraLeitura.altitude,
|
||||
altitudeAccuracy: null,
|
||||
heading: null,
|
||||
speed: null,
|
||||
confiabilidade: primeiraLeitura.confiabilidade * 0.4, // Reduzir drasticamente confiança
|
||||
suspeitaSpoofing: true,
|
||||
motivoSuspeita: 'Todas as leituras são idênticas (GPS real varia alguns metros)'
|
||||
};
|
||||
}
|
||||
|
||||
// Calcular média das leituras e variância
|
||||
const mediaLat = leituras.reduce((sum, l) => sum + l.lat, 0) / leituras.length;
|
||||
const mediaLon = leituras.reduce((sum, l) => sum + l.lon, 0) / leituras.length;
|
||||
const mediaConfianca = leituras.reduce((sum, l) => sum + l.confiabilidade, 0) / leituras.length;
|
||||
|
||||
// Calcular distância máxima entre leituras
|
||||
let distanciaMaxima = 0;
|
||||
for (let i = 0; i < leituras.length; i++) {
|
||||
for (let j = i + 1; j < leituras.length; j++) {
|
||||
const dist = calcularDistancia(
|
||||
leituras[i].lat,
|
||||
leituras[i].lon,
|
||||
leituras[j].lat,
|
||||
leituras[j].lon
|
||||
);
|
||||
distanciaMaxima = Math.max(distanciaMaxima, dist);
|
||||
}
|
||||
}
|
||||
|
||||
// Se distância máxima for muito grande (> 100m), pode indicar problemas
|
||||
const suspeitoPorDistancia = distanciaMaxima > 100;
|
||||
|
||||
return {
|
||||
latitude: mediaLat,
|
||||
longitude: mediaLon,
|
||||
precisao: primeiraLeitura.precisao,
|
||||
altitude: primeiraLeitura.altitude,
|
||||
altitudeAccuracy: null,
|
||||
heading: null,
|
||||
speed: null,
|
||||
confiabilidade: suspeitoPorDistancia ? mediaConfianca * 0.6 : mediaConfianca,
|
||||
suspeitaSpoofing: suspeitoPorDistancia,
|
||||
motivoSuspeita: suspeitoPorDistancia
|
||||
? `Variação muito grande entre leituras (${Math.round(distanciaMaxima)}m)`
|
||||
: undefined
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtém localização via GPS com múltiplas tentativas e validações anti-spoofing
|
||||
*/
|
||||
async function obterLocalizacao(): Promise<{
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
precisao?: number;
|
||||
altitude?: number | null;
|
||||
altitudeAccuracy?: number | null;
|
||||
heading?: number | null;
|
||||
speed?: number | null;
|
||||
confiabilidadeGPS?: number;
|
||||
suspeitaSpoofing?: boolean;
|
||||
motivoSuspeita?: string;
|
||||
endereco?: string;
|
||||
cidade?: string;
|
||||
estado?: string;
|
||||
@@ -246,127 +530,95 @@ async function obterLocalizacao(): Promise<{
|
||||
return {};
|
||||
}
|
||||
|
||||
// Tentar múltiplas estratégias
|
||||
const estrategias = [
|
||||
// Estratégia 1: Alta precisão (mais lento, mas mais preciso)
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0
|
||||
},
|
||||
// Estratégia 2: Precisão média (balanceado)
|
||||
{
|
||||
enableHighAccuracy: false,
|
||||
timeout: 8000,
|
||||
maximumAge: 30000
|
||||
},
|
||||
// Estratégia 3: Rápido (usa cache)
|
||||
{
|
||||
enableHighAccuracy: false,
|
||||
timeout: 5000,
|
||||
maximumAge: 60000
|
||||
}
|
||||
];
|
||||
// Usar múltiplas leituras para detectar spoofing
|
||||
const localizacaoMultipla = await obterLocalizacaoMultipla();
|
||||
|
||||
for (const options of estrategias) {
|
||||
try {
|
||||
const resultado = await new Promise<{
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
precisao?: number;
|
||||
endereco?: string;
|
||||
cidade?: string;
|
||||
estado?: string;
|
||||
pais?: string;
|
||||
}>((resolve) => {
|
||||
const timeout = setTimeout(() => {
|
||||
resolve({});
|
||||
}, options.timeout + 1000);
|
||||
if (!localizacaoMultipla.latitude || !localizacaoMultipla.longitude) {
|
||||
console.warn('Não foi possível obter localização');
|
||||
return {
|
||||
confiabilidadeGPS: 0,
|
||||
suspeitaSpoofing: true,
|
||||
motivoSuspeita: 'Não foi possível obter localização'
|
||||
};
|
||||
}
|
||||
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
async (position) => {
|
||||
clearTimeout(timeout);
|
||||
const { latitude, longitude, accuracy } = position.coords;
|
||||
const { latitude, longitude, precisao, altitude, altitudeAccuracy, heading, speed, confiabilidade, suspeitaSpoofing, motivoSuspeita } = localizacaoMultipla;
|
||||
|
||||
// Validar coordenadas
|
||||
if (isNaN(latitude) || isNaN(longitude) || latitude === 0 || longitude === 0) {
|
||||
resolve({});
|
||||
return;
|
||||
}
|
||||
// Tentar obter endereço via reverse geocoding
|
||||
let endereco = '';
|
||||
let cidade = '';
|
||||
let estado = '';
|
||||
let pais = '';
|
||||
|
||||
// Tentar obter endereço via reverse geocoding
|
||||
let endereco = '';
|
||||
let cidade = '';
|
||||
let estado = '';
|
||||
let pais = '';
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}&zoom=18&addressdetails=1`,
|
||||
{
|
||||
headers: {
|
||||
'User-Agent': 'SGSE-App/1.0'
|
||||
}
|
||||
}
|
||||
);
|
||||
if (response.ok) {
|
||||
const data = (await response.json()) as {
|
||||
address?: {
|
||||
road?: string;
|
||||
house_number?: string;
|
||||
city?: string;
|
||||
town?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
};
|
||||
};
|
||||
if (data.address) {
|
||||
const addr = data.address;
|
||||
if (addr.road) {
|
||||
endereco = `${addr.road}${addr.house_number ? `, ${addr.house_number}` : ''}`;
|
||||
}
|
||||
cidade = addr.city || addr.town || '';
|
||||
estado = addr.state || '';
|
||||
pais = addr.country || '';
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Erro ao obter endereço:', error);
|
||||
}
|
||||
|
||||
resolve({
|
||||
latitude,
|
||||
longitude,
|
||||
precisao: accuracy,
|
||||
endereco,
|
||||
cidade,
|
||||
estado,
|
||||
pais,
|
||||
});
|
||||
},
|
||||
(error) => {
|
||||
clearTimeout(timeout);
|
||||
console.warn('Erro ao obter localização:', error.code, error.message);
|
||||
resolve({});
|
||||
},
|
||||
options
|
||||
);
|
||||
});
|
||||
|
||||
// Se obteve localização, retornar
|
||||
if (resultado.latitude && resultado.longitude) {
|
||||
console.log('Localização obtida com sucesso:', resultado);
|
||||
return resultado;
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}&zoom=18&addressdetails=1`,
|
||||
{
|
||||
headers: {
|
||||
'User-Agent': 'SGSE-App/1.0'
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Erro na estratégia de geolocalização:', error);
|
||||
continue;
|
||||
);
|
||||
if (response.ok) {
|
||||
const data = (await response.json()) as {
|
||||
address?: {
|
||||
road?: string;
|
||||
house_number?: string;
|
||||
city?: string;
|
||||
town?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
};
|
||||
};
|
||||
if (data.address) {
|
||||
const addr = data.address;
|
||||
if (addr.road) {
|
||||
endereco = `${addr.road}${addr.house_number ? `, ${addr.house_number}` : ''}`;
|
||||
}
|
||||
cidade = addr.city || addr.town || '';
|
||||
estado = addr.state || '';
|
||||
pais = addr.country || '';
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Erro ao obter endereço:', error);
|
||||
}
|
||||
|
||||
// Validar timezone vs localização
|
||||
if (typeof navigator !== 'undefined') {
|
||||
const timezoneAtual = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
const timezoneEsperado = obterTimezonePorCoordenadas(latitude, longitude);
|
||||
|
||||
// Se timezone é muito diferente, pode ser suspeito
|
||||
if (timezoneAtual !== timezoneEsperado && timezoneAtual !== 'America/Recife' && timezoneEsperado !== 'America/Recife') {
|
||||
console.warn(`Timezone inconsistente: esperado ${timezoneEsperado}, atual ${timezoneAtual}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Se todas as estratégias falharam, retornar vazio
|
||||
console.warn('Não foi possível obter localização após todas as tentativas');
|
||||
return {};
|
||||
console.log('Localização obtida com validações:', {
|
||||
latitude,
|
||||
longitude,
|
||||
confiabilidade: confiabilidade.toFixed(2),
|
||||
suspeitaSpoofing,
|
||||
motivoSuspeita
|
||||
});
|
||||
|
||||
return {
|
||||
latitude,
|
||||
longitude,
|
||||
precisao,
|
||||
altitude,
|
||||
altitudeAccuracy,
|
||||
heading,
|
||||
speed,
|
||||
confiabilidadeGPS: confiabilidade,
|
||||
suspeitaSpoofing: suspeitaSpoofing || false,
|
||||
motivoSuspeita,
|
||||
endereco,
|
||||
cidade,
|
||||
estado,
|
||||
pais
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -439,6 +691,13 @@ export async function obterInformacoesDispositivo(): Promise<InformacoesDisposit
|
||||
informacoes.latitude = localizacao.latitude;
|
||||
informacoes.longitude = localizacao.longitude;
|
||||
informacoes.precisao = localizacao.precisao;
|
||||
informacoes.altitude = localizacao.altitude ?? null;
|
||||
informacoes.altitudeAccuracy = localizacao.altitudeAccuracy ?? null;
|
||||
informacoes.heading = localizacao.heading ?? null;
|
||||
informacoes.speed = localizacao.speed ?? null;
|
||||
informacoes.confiabilidadeGPS = localizacao.confiabilidadeGPS;
|
||||
informacoes.suspeitaSpoofing = localizacao.suspeitaSpoofing;
|
||||
informacoes.motivoSuspeita = localizacao.motivoSuspeita;
|
||||
informacoes.endereco = localizacao.endereco;
|
||||
informacoes.cidade = localizacao.cidade;
|
||||
informacoes.estado = localizacao.estado;
|
||||
|
||||
Reference in New Issue
Block a user