Skip to content

Commit

Permalink
airport: allow less accurate non-sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 27, 2023
1 parent 21020c6 commit cee0ea4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ dump raw signals, without using API:
python -m mozloc.signal
```

### macOS

On macOS, much more accurate results come by running as root by using sudo.
This is because "airport" only emits BSSID if running with sudo.

Possible future implementation could use
[CoreWLAN](https://developer.apple.com/documentation/corewlan/).

### Windows

On Windows, NetSH is used.
Expand Down
21 changes: 10 additions & 11 deletions src/mozloc/airport.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def cli_config_check() -> bool:
def get_signal() -> str:

try:
ret = subprocess.check_output([get_airport(), "-s"], text=True, timeout=30)
ret = subprocess.check_output([get_airport(), "--scan"], text=True, timeout=30)
except subprocess.CalledProcessError as err:
logging.error(f"consider slowing scan cadence. {err}")

Expand All @@ -42,30 +42,29 @@ def get_signal() -> str:
def parse_signal(raw: str) -> list[dict[str, T.Any]]:

isroot = running_as_root()
if not isroot:
raise RuntimeError("airport requires running as sudo to get BSSID")

psudo = r"\s*([0-9a-zA-Z\s\-\.]+)\s+([0-9a-f]{2}(?::[0-9a-f]{2}){5})\s+(-\d{2,3})"
# BSSID only present if sudo
# puser = r"\s*([0-9a-zA-Z\s\-\.]+)\s+(-\d{2,3})"
puser = r"\s*([0-9a-zA-Z\s\-\.]+)\s+(-\d{2,3})"
# non-sudo has no BSSID

p = psudo
i = 2

p = psudo if isroot else puser
isig = 3 if isroot else 2
ibssid = 2
pat = re.compile(p)
dat: list[dict[str, str]] = []

for line in raw.split("\n"):
mat = pat.match(line)
if mat:
# Hidden SSID optout implicitly excluded by regex
ssid = mat.group(1)
ssid = mat.group(1).strip()
# optout
if ssid.endswith("_nomap"):
continue
dat.append(
{"ssid": ssid, "macAddress": mat.group(i), "signalStrength": mat.group(i + 1)}
)
d = {"ssid": ssid, "signalStrength": mat.group(isig)}
if isroot:
d["macAddress"] = mat.group(ibssid)
dat.append(d)

return dat
1 change: 0 additions & 1 deletion src/mozloc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def log_wifi_loc(cadence_sec: float, mozilla_url: str, logfile: Path | None = No
logging.warning(f"cannot locate since at least 2 BSSIDs required\n{dat}")
sleep(cadence_sec)
continue
print(dat)

loc = get_loc_mozilla(dat, mozilla_url)
if loc is None:
Expand Down

0 comments on commit cee0ea4

Please sign in to comment.