FelixKratz / SketchyBar

A highly customizable macOS status bar replacement
https://felixkratz.github.io/SketchyBar/
GNU General Public License v3.0
5.45k stars 85 forks source link

`wifi_change` event does not supply the SSID on Sonoma #407

Closed FelixKratz closed 10 months ago

FelixKratz commented 10 months ago

This is a privacy protecting measure by apple, since the SSID could be used by a rouge application to reveal your location. This is why they decided to make the SSID only available if the app requesting it has location permissions. I don't want to ask for location permissions for this feature, so it would probably be best to just remove the $INFO field for this event, or replace it by something else instead.

Although there are other ways to retrieve the SSID for now, this is clearly unwanted and likely to break in future updates.

bustinbung commented 10 months ago

Quick search gave me this answer. I modified wifi.sh in the plugins directory to change $INFO to the output of the command, and that fixed it for me without asking for location permissions. However, I'm not sure if this only works because I have SIP disabled or not, or because I have admin rights on this computer.

INFO="$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I  | awk -F' SSID: '  '/ SSID: / {print $2}')"
FelixKratz commented 10 months ago

I have added a note to the docs that this event is somewhat broken on Sonoma.

bustinbung commented 10 months ago

In case anyone went searching, I fixed it (albeit a bit hacky) on my end with a combo of a couple things.

The steps outlined below create a new LaunchAgent within launchd that monitors a specific file within /Library/Preferences/SystemConfiguration. This file gets changed every time the network changes, so we can use launchd to fire our modified plugin script to update sketchybar when the file changes.

Modifying the plugin

Make the following edits to your wifi.sh or related plugin file:

# this is new
INFO="$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I  | awk -F' SSID: '  '/ SSID: / {print $2}')"

WIFI=${INFO:-"Not Connected"}
# IMPORTANT: make sure this is the full path to sketchybar,
# or it will throw an error. launchd does not load $PATH
/opt/homebrew/bin/sketchybar --set wifi label="${WIFI}"

# you can remove/change this to suit your needs
echo "$(date +%Y-%m-%d\ %H:%M:%S): $WIFI" >> "/tmp/wifichange.out.log" 2>&1

NOTE: The if block has been removed, as the $SENDER variable won't be set by launchd (I guess you could make it but meh).

Creating a LaunchAgent

Create a .plist file with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>[[some reverse domain name]]</string>

    <key>ProgramArguments</key>
    <array>
        <string>[[absolute path to your modified plugin]]</string>
    </array>

    <!-- you can change these paths if you'd like -->
    <key>StandardOutPath</key>
    <string>/tmp/wifichange.out.log</string>

    <key>StandardErrorPath</key>
    <string>/tmp/wifichange.err.log</string>

    <!-- this path works for me, you can experiment
         and add more files if you'd like -->
    <key>WatchPaths</key>
    <array>
        <string>/Library/Preferences/SystemConfiguration/com.apple.wifi.message-tracer.plist</string>
    </array>
</dict>
</plist>

Adding the agent to launchd

Run the following command to add your new agent to launchd:

launchctl load -w /path/to/your.plist

Verify the agent is running by running the following command:

launchctl list | grep "[label of your agent specified in the .plist]"

You should see the following output:

-   0   [label of your agent]

Where - (usually the PID) indicates that the script is not running, and 0 indicates that the agent exited with code 0 on its last run.

Removing the agent

If this issue gets fixed in sketchybar proper, you can remove the agent from launchd using this command:

launchctl unload -w /path/to/your.plist

Known issues

I believe this covers everything, but if you have questions/issues, feel free to get in touch.

System Information

FelixKratz commented 10 months ago

Ok so I think the event should now be fired properly again, just without the SSID: 1433dd2837d317483ea1b84851db37034c0d07d3

You can try this change with:

brew uninstall sketchybar
brew install sketchybar --head
brew services restart sketchybar

and then go back to the stable release:

brew uninstall sketchybar
brew install sketchybar
brew services restart sketchybar
bustinbung commented 10 months ago

Updating to HEAD did fix the event firing issue, modification to the plugin script works really well. Not sure how you could implement those shell script commands into C (if you even want to), but I appreciate the work. Been having fun messing around with this!

tonnylou44853 commented 5 months ago

The command above stopped working for me after upgrading my system to Sonoma 14.4:

$ /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I
WARNING: The airport command line tool is deprecated and will be removed in a future release.
For diagnosing Wi-Fi related issues, use the Wireless Diagnostics app or wdutil command line tool.

and wdutil is not an option for me since it requires sudo.

In case anyone had a similar problem as above, here is a simple fix that I found: https://stackoverflow.com/a/8542420

TL;DR:

Use

INFO="$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | xargs networksetup -getairportnetwork | sed "s/Current Wi-Fi Network: //")"

if /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I no longer works.