From 917c159d5719c03d7021f91a5e697fff652f127c Mon Sep 17 00:00:00 2001 From: daijro Date: Sun, 22 Sep 2024 08:28:29 -0500 Subject: [PATCH] Add geolocation spoofing Adds the following: - geolocation:latitude - geolocation:longitude - geolocation:accuracy If accuracy is not provided, it will be automatically calculated based on the decimal precision of the given latitude and longitude. --- patches/geolocation-spoofing.patch | 152 +++++++++++++++++++++++++++++ settings/properties.json | 5 +- 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 patches/geolocation-spoofing.patch diff --git a/patches/geolocation-spoofing.patch b/patches/geolocation-spoofing.patch new file mode 100644 index 0000000..9606c64 --- /dev/null +++ b/patches/geolocation-spoofing.patch @@ -0,0 +1,152 @@ +diff --git a/dom/geolocation/Geolocation.cpp b/dom/geolocation/Geolocation.cpp +index 274cdebc2e..b0183ecc6a 100644 +--- a/dom/geolocation/Geolocation.cpp ++++ b/dom/geolocation/Geolocation.cpp +@@ -34,6 +34,7 @@ + #include "nsServiceManagerUtils.h" + #include "nsThreadUtils.h" + #include "nsXULAppAPI.h" ++#include "MaskConfig.hpp" + + class nsIPrincipal; + +@@ -1267,6 +1268,12 @@ void Geolocation::NotifyAllowedRequest(nsGeolocationRequest* aRequest) { + + bool Geolocation::RegisterRequestWithPrompt(nsGeolocationRequest* request) { + nsIEventTarget* target = GetMainThreadSerialEventTarget(); ++ if (MaskConfig::GetDouble("geolocation:latitude") && ++ MaskConfig::GetDouble("geolocation:longitude")) { ++ request->RequestDelayedTask(target, ++ nsGeolocationRequest::DelayedTaskType::Allow); ++ return true; ++ } + ContentPermissionRequestBase::PromptResult pr = request->CheckPromptPrefs(); + if (pr == ContentPermissionRequestBase::PromptResult::Granted) { + request->RequestDelayedTask(target, +diff --git a/dom/geolocation/GeolocationPosition.cpp b/dom/geolocation/GeolocationPosition.cpp +index 19ca685251..a286a4a784 100644 +--- a/dom/geolocation/GeolocationPosition.cpp ++++ b/dom/geolocation/GeolocationPosition.cpp +@@ -9,6 +9,7 @@ + + #include "mozilla/FloatingPoint.h" + #include "mozilla/dom/GeolocationPositionBinding.h" ++#include "MaskConfig.hpp" + + using mozilla::EqualOrBothNaN; + +@@ -62,13 +63,19 @@ NS_IMPL_RELEASE(nsGeoPositionCoords) + + NS_IMETHODIMP + nsGeoPositionCoords::GetLatitude(double* aLatitude) { +- *aLatitude = mLat; ++ if (auto value = MaskConfig::GetDouble("geolocation:latitude")) ++ *aLatitude = value.value(); ++ else ++ *aLatitude = mLat; + return NS_OK; + } + + NS_IMETHODIMP + nsGeoPositionCoords::GetLongitude(double* aLongitude) { +- *aLongitude = mLong; ++ if (auto value = MaskConfig::GetDouble("geolocation:longitude")) ++ *aLongitude = value.value(); ++ else ++ *aLongitude = mLong; + return NS_OK; + } + +@@ -80,7 +87,10 @@ nsGeoPositionCoords::GetAltitude(double* aAltitude) { + + NS_IMETHODIMP + nsGeoPositionCoords::GetAccuracy(double* aAccuracy) { +- *aAccuracy = mHError; ++ if (auto value = MaskConfig::GetDouble("geolocation:accuracy")) ++ *aAccuracy = value.value(); ++ else ++ *aAccuracy = mHError; + return NS_OK; + } + +diff --git a/dom/geolocation/moz.build b/dom/geolocation/moz.build +index 2d6b6b5fab..0c7ed74c6d 100644 +--- a/dom/geolocation/moz.build ++++ b/dom/geolocation/moz.build +@@ -58,3 +58,6 @@ elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "gtk": + if CONFIG["MOZ_ENABLE_DBUS"]: + LOCAL_INCLUDES += ["/dom/system/linux"] + CXXFLAGS += CONFIG["MOZ_GTK3_CFLAGS"] ++ ++# DOM Mask ++LOCAL_INCLUDES += ["/camoucfg"] +\ No newline at end of file +diff --git a/dom/system/NetworkGeolocationProvider.sys.mjs b/dom/system/NetworkGeolocationProvider.sys.mjs +index 7edc62b4a6..19b77aa6b6 100644 +--- a/dom/system/NetworkGeolocationProvider.sys.mjs ++++ b/dom/system/NetworkGeolocationProvider.sys.mjs +@@ -442,17 +442,55 @@ NetworkGeolocationProvider.prototype = { + let url = Services.urlFormatter.formatURLPref("geo.provider.network.url"); + LOG("Sending request"); + ++ // Collect the latitude, longitude, and accuracy from the camouflage prefs. ++ // If the values are not set, use the default value of -180. ++ let DEFAULT_DEG = -180; ++ ++ let latitude = ChromeUtils.camouGetDouble("geolocation:latitude", DEFAULT_DEG); ++ let longitude = ChromeUtils.camouGetDouble("geolocation:longitude", DEFAULT_DEG); ++ let accuracy = ChromeUtils.camouGetDouble("geolocation:accuracy", 0); ++ ++ let countDecimalPlaces = (num) => { ++ if (Math.floor(num) === num) return 0; ++ return num.toString().split(".")[1].length || 0; ++ } ++ + let result; + try { +- result = await this.makeRequest(url, wifiData); +- LOG( +- `geo provider reported: ${result.location.lng}:${result.location.lat}` +- ); +- let newLocation = new NetworkGeoPositionObject( +- result.location.lat, +- result.location.lng, +- result.accuracy +- ); ++ let newLocation; ++ if (latitude != DEFAULT_DEG && longitude != DEFAULT_DEG) { ++ // Use the latitude and longitude from the camouflage prefs. ++ ChromeUtils.camouDebug( ++ `Use Camoufox geo: ${latitude}:${longitude} accuracy:${accuracy}` ++ ); ++ // If accuracy is not set, calculate it manually using the decimal precision ++ if (!accuracy) { ++ let latPrecision = countDecimalPlaces(latitude); ++ let lonPrecision = countDecimalPlaces(longitude); ++ let precision = Math.min(latPrecision, lonPrecision); ++ ++ // Estimate accuracy in meters ++ accuracy = (111320 * Math.cos(latitude * Math.PI / 180)) / Math.pow(10, precision); ++ } ++ // Create a new NetworkGeoPositionObject with the calculated accuracy ++ newLocation = new NetworkGeoPositionObject( ++ latitude, ++ longitude, ++ accuracy ++ ); ++ } else { ++ // Continue with the original geolocation request. ++ result = await this.makeRequest(url, wifiData); ++ ++ LOG( ++ `geo provider reported: ${result.location.lng}:${result.location.lat}` ++ ); ++ newLocation = new NetworkGeoPositionObject( ++ result.location.lat, ++ result.location.lng, ++ accuracy || result.accuracy ++ ); ++ } + + if (this.listener) { + this.listener.update(newLocation); diff --git a/settings/properties.json b/settings/properties.json index 705658c..520c646 100644 --- a/settings/properties.json +++ b/settings/properties.json @@ -54,5 +54,8 @@ { "property": "battery:chargingTime", "type": "double" }, { "property": "battery:dischargingTime", "type": "double" }, { "property": "battery:level", "type": "double" }, - { "property": "fonts", "type": "array" } + { "property": "fonts", "type": "array" }, + { "property": "geolocation:latitude", "type": "double" }, + { "property": "geolocation:longitude", "type": "double" }, + { "property": "geolocation:accuracy", "type": "double" } ]