fix remove code check fake location
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m12s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m12s
This commit is contained in:
parent
02f1fd417d
commit
7edfaa4e4f
4 changed files with 157 additions and 579 deletions
|
|
@ -1,255 +0,0 @@
|
|||
import { ref } from 'vue'
|
||||
import { useQuasar } from 'quasar'
|
||||
import { useCounterMixin } from '@/stores/mixin'
|
||||
|
||||
export interface LocationValidationResult {
|
||||
isValid: boolean
|
||||
isMockDetected: boolean
|
||||
confidence: 'low' | 'medium' | 'high'
|
||||
warnings: string[]
|
||||
errors: string[]
|
||||
}
|
||||
|
||||
export interface PositionSnapshot {
|
||||
latitude: number
|
||||
longitude: number
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
// Configuration constants - exported for documentation and testing purposes
|
||||
export const VALIDATION_CONFIG = {
|
||||
MAX_TIMESTAMP_AGE_MS: 60_000, // 60 seconds - maximum acceptable age of location data
|
||||
MAX_ACCURACY_METERS: 100, // 100 meters - maximum acceptable GPS accuracy
|
||||
MAX_SPEED_MS: 100, // 100 m/s (~360 km/h) - maximum plausible movement speed
|
||||
POSITION_HISTORY_SIZE: 5, // number of positions to keep for pattern detection
|
||||
MOCK_INDICATOR_THRESHOLD: 3, // threshold for mock detection (indicators >= 3 = mock)
|
||||
SUSPICIOUS_ACCURACY_MAX: 5, // accuracy ≤ 5m AND integer = suspicious (real GPS never rounds to whole number)
|
||||
// Geographic service area (Thailand) to reduce spoofing risk from remote fake coordinates.
|
||||
SERVICE_AREA: {
|
||||
MIN_LAT: 5.0,
|
||||
MAX_LAT: 21.0,
|
||||
MIN_LON: 97.0,
|
||||
MAX_LON: 106.0,
|
||||
},
|
||||
} as const
|
||||
|
||||
export function useLocationValidation() {
|
||||
const $q = useQuasar()
|
||||
const { messageError } = useCounterMixin()
|
||||
|
||||
// Thai error messages - exported for i18n consistency
|
||||
const errorMessages = {
|
||||
MOCK_DETECTED:
|
||||
'ตรวจพบว่าตำแหน่ง GPS อาจไม่ถูกต้อง กรุณาปิดแอปจำลองตำแหน่งและลองใหม่',
|
||||
INVALID_COORDINATES: 'พิกัดตำแหน่งไม่ถูกต้อง กรุณาลองใหม่',
|
||||
STALE_TIMESTAMP: 'ข้อมูลตำแหน่งเก่าเกินไป กรุณารับสัญญาณ GPS ใหม่',
|
||||
POOR_ACCURACY: 'ความแม่นยำตำแหน่งต่ำเกินไป กรุณาตรวจสอบการรับสัญญาณ GPS',
|
||||
IMPOSSIBLE_SPEED:
|
||||
'ตรวจพบการเคลื่อนที่ด้วยความเร็วผิดปกติ อาจเป็นการจำลองตำแหน่ง',
|
||||
SUSPICIOUS_ACCURACY: 'ตรวจพบค่าความแม่นยำที่ผิดปกติ อาจเป็นการจำลองตำแหน่ง',
|
||||
DUPLICATE_POSITION: 'ตรวจพบพิกัดตำแหน่งซ้ำกัน อาจเป็นการจำลองตำแหน่ง',
|
||||
OUT_OF_SERVICE_AREA:
|
||||
'ตรวจพบตำแหน่งนอกพื้นที่ใช้งานของระบบ กรุณาปิดแอปจำลองตำแหน่งและลองใหม่',
|
||||
}
|
||||
|
||||
const previousPositions = ref<PositionSnapshot[]>([])
|
||||
|
||||
// คำนวณระยะห่างระหว่าง 2 จุด (Haversine formula)
|
||||
const haversineDistance = (
|
||||
lat1: number,
|
||||
lon1: number,
|
||||
lat2: number,
|
||||
lon2: number
|
||||
): number => {
|
||||
const R = 6371e3 // Earth's radius in meters
|
||||
const φ1 = (lat1 * Math.PI) / 180
|
||||
const φ2 = (lat2 * Math.PI) / 180
|
||||
const Δφ = ((lat2 - lat1) * Math.PI) / 180
|
||||
const Δλ = ((lon2 - lon1) * Math.PI) / 180
|
||||
|
||||
const a =
|
||||
Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
|
||||
Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2)
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
|
||||
|
||||
return R * c
|
||||
}
|
||||
|
||||
// ตรวจสอบพิกัดถูกต้อง
|
||||
const validateCoordinates = (lat: number, lon: number): boolean => {
|
||||
return (
|
||||
lat >= -90 &&
|
||||
lat <= 90 &&
|
||||
lon >= -180 &&
|
||||
lon <= 180 &&
|
||||
!isNaN(lat) &&
|
||||
!isNaN(lon) &&
|
||||
!(lat === 0 && lon === 0) // Mock มักใช้ 0,0
|
||||
)
|
||||
}
|
||||
|
||||
// ตรวจสอบความแม่นยำ
|
||||
const validateAccuracy = (accuracy: number | null): boolean => {
|
||||
if (accuracy === null) return true
|
||||
return accuracy <= VALIDATION_CONFIG.MAX_ACCURACY_METERS
|
||||
}
|
||||
|
||||
// ตรวจสอบ Timestamp
|
||||
const validateTimestamp = (timestamp: number): boolean => {
|
||||
const now = Date.now()
|
||||
const age = Math.abs(now - timestamp)
|
||||
return age <= VALIDATION_CONFIG.MAX_TIMESTAMP_AGE_MS
|
||||
}
|
||||
|
||||
// คำนวณความเร็ว
|
||||
const calculateSpeed = (
|
||||
pos1: PositionSnapshot,
|
||||
pos2: PositionSnapshot
|
||||
): number => {
|
||||
const distance = haversineDistance(
|
||||
pos1.latitude,
|
||||
pos1.longitude,
|
||||
pos2.latitude,
|
||||
pos2.longitude
|
||||
)
|
||||
const timeDiff = Math.abs(pos2.timestamp - pos1.timestamp) / 1000 // seconds
|
||||
return timeDiff > 0 ? distance / timeDiff : 0
|
||||
}
|
||||
|
||||
// ตรวจสอบความเร็วปกติ
|
||||
const validateSpeed = (
|
||||
current: PositionSnapshot,
|
||||
previous: PositionSnapshot
|
||||
): boolean => {
|
||||
const speed = calculateSpeed(previous, current)
|
||||
return speed <= VALIDATION_CONFIG.MAX_SPEED_MS
|
||||
}
|
||||
|
||||
// ตรวจสอบค่าความแม่นยำที่ผิดปกติ (mock apps มักรายงานค่าที่เป็นเลขจำนวนเต็มสวยงาม เช่น 5.0, 3.0)
|
||||
const hasSuspiciousPerfectAccuracy = (accuracy: number | null): boolean => {
|
||||
if (accuracy === null) return false
|
||||
return (
|
||||
Number.isInteger(accuracy) &&
|
||||
accuracy <= VALIDATION_CONFIG.SUSPICIOUS_ACCURACY_MAX
|
||||
)
|
||||
}
|
||||
|
||||
// ตรวจสอบพิกัดซ้ำกันทุกค่า (real GPS มีการสั่นเล็กน้อย mock app มักคืนพิกัดเดิมซ้ำ)
|
||||
const hasDuplicateCoordinates = (lat: number, lon: number): boolean => {
|
||||
return previousPositions.value.some(
|
||||
(p) => p.latitude === lat && p.longitude === lon
|
||||
)
|
||||
}
|
||||
|
||||
const isWithinServiceArea = (lat: number, lon: number): boolean => {
|
||||
const area = VALIDATION_CONFIG.SERVICE_AREA
|
||||
return (
|
||||
lat >= area.MIN_LAT &&
|
||||
lat <= area.MAX_LAT &&
|
||||
lon >= area.MIN_LON &&
|
||||
lon <= area.MAX_LON
|
||||
)
|
||||
}
|
||||
|
||||
// Main validation function
|
||||
const validateLocation = (
|
||||
position: GeolocationPosition
|
||||
): LocationValidationResult => {
|
||||
const warnings: string[] = []
|
||||
const errors: string[] = []
|
||||
let mockIndicators = 0
|
||||
|
||||
const { latitude, longitude, accuracy } = position.coords
|
||||
const { timestamp } = position
|
||||
|
||||
// 1. Coordinate validation
|
||||
if (!validateCoordinates(latitude, longitude)) {
|
||||
errors.push(errorMessages.INVALID_COORDINATES)
|
||||
mockIndicators += 3
|
||||
}
|
||||
|
||||
// 1.1 Service-area validation (critical for remote spoofed coordinates)
|
||||
if (!isWithinServiceArea(latitude, longitude)) {
|
||||
errors.push(errorMessages.OUT_OF_SERVICE_AREA)
|
||||
mockIndicators += 3
|
||||
}
|
||||
|
||||
// 2. Timestamp validation
|
||||
if (!validateTimestamp(timestamp)) {
|
||||
errors.push(errorMessages.STALE_TIMESTAMP)
|
||||
mockIndicators += 2
|
||||
}
|
||||
|
||||
// 3. Accuracy validation
|
||||
if (!validateAccuracy(accuracy)) {
|
||||
warnings.push(errorMessages.POOR_ACCURACY)
|
||||
mockIndicators += 1
|
||||
}
|
||||
|
||||
// 4. Compare with previous positions
|
||||
if (previousPositions.value.length > 0) {
|
||||
const previous =
|
||||
previousPositions.value[previousPositions.value.length - 1]
|
||||
|
||||
if (!validateSpeed({ latitude, longitude, timestamp }, previous)) {
|
||||
errors.push(errorMessages.IMPOSSIBLE_SPEED)
|
||||
mockIndicators += 3
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Suspiciously perfect accuracy — real GPS never rounds to a whole-number ≤ 5m
|
||||
if (hasSuspiciousPerfectAccuracy(accuracy)) {
|
||||
warnings.push(errorMessages.SUSPICIOUS_ACCURACY)
|
||||
mockIndicators += 2
|
||||
}
|
||||
|
||||
// 6. Exact duplicate coordinates — real GPS always drifts slightly between readings
|
||||
if (
|
||||
previousPositions.value.length > 0 &&
|
||||
hasDuplicateCoordinates(latitude, longitude)
|
||||
) {
|
||||
warnings.push(errorMessages.DUPLICATE_POSITION)
|
||||
mockIndicators += 2
|
||||
}
|
||||
|
||||
// Store current position
|
||||
previousPositions.value.push({ latitude, longitude, timestamp })
|
||||
if (
|
||||
previousPositions.value.length > VALIDATION_CONFIG.POSITION_HISTORY_SIZE
|
||||
) {
|
||||
previousPositions.value.shift()
|
||||
}
|
||||
|
||||
// Determine result
|
||||
const isMockDetected =
|
||||
mockIndicators >= VALIDATION_CONFIG.MOCK_INDICATOR_THRESHOLD
|
||||
const isValid = errors.length === 0
|
||||
|
||||
let confidence: 'low' | 'medium' | 'high' = 'low'
|
||||
if (mockIndicators >= 5) confidence = 'high'
|
||||
else if (mockIndicators >= 3) confidence = 'medium'
|
||||
|
||||
return {
|
||||
isValid,
|
||||
isMockDetected,
|
||||
confidence,
|
||||
warnings,
|
||||
errors,
|
||||
}
|
||||
}
|
||||
|
||||
const showMockWarning = (result: LocationValidationResult) => {
|
||||
if (!result.isMockDetected) return
|
||||
messageError($q, '', errorMessages.MOCK_DETECTED)
|
||||
}
|
||||
|
||||
const resetValidation = () => {
|
||||
previousPositions.value = []
|
||||
}
|
||||
|
||||
return {
|
||||
validateLocation,
|
||||
showMockWarning,
|
||||
resetValidation,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue