Wireguard

Image preview of Wireguard plugin.

wireguard.sh

Edit
Open on GitHub
#!/bin/bash
# <xbar.title>Wireguard</xbar.title>
# <xbar.version>v1.0</xbar.version>
# <xbar.author>Daniel Barber</xbar.author>
# <xbar.author.github>danbee</xbar.author.github>
# <xbar.desc>Manages a Wireguard VPN connection</xbar.desc>
# <xbar.image>https://f001.backblazeb2.com/file/bitbar/wireguard.png</xbar.image>
# <xbar.dependencies></xbar.dependencies>

# You will need to add the following line to your sudoers file. Remember to edit
# sudoers with `sudo visudo`.
#
# %admin  ALL=(ALL) NOPASSWD: /usr/local/bin/wg-quick
#
# Rename WG_CONFIG if your Wireguard config is not called `wg0-client`

PATH="/usr/local/bin:$PATH"

WG_CONNECTED="iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAY1BMVEVHcEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD////Iv5pwAAAAH3RSTlMAA0yMvd6sez0V7/qzbglj5qR0Ncj1KA7VLlQflkGEXYPQGAAAAAFiS0dEILNrPYAAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAAHdElNRQfiCB0GISSxSlkqAAABpklEQVQ4y2VT7YKEIAik1GyzTNus1nbz/d/y8AO3u+OXIMLMgABkTcu4EJ1kvJN9A3/twYYQghqnHvQ8BMMev+/neB2sZEt8++SrcvJWpbEhmdiy3wL0IfBapOlCMZkDezsL9A7KWMq1aVPVThhzpADPXTZVEpzgL/Q9+VRxrX44WB8xCvJdbNKHu6klYhjvJSrCsE7PrbV2R9iUITCbOqo5Qz7fmPEYSrQHT+99FfV9qyvhXU42jcPja/hsX+pj5fDEp8h+QKIPLCYrCFMQ4E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s3uzg4PrBU+/vG66Gd14ZsuOTRn+u39FHaPMKBxxwHpu+CKaZBOSRAmokSZwLgYEtZFU89/XQp9mKFMALVPVM30ZM+10cRsB5ICJEmDc8KpiZCwWrSRN+zEH+yAKgyhRJp+m6D2L25cBY6Dp3zq9UjtshVhbSyaKkn3VycpdtcrByhVrMFCIkHw/kbPFgn5t4lIeMW5DbhhrSTF4UoZItT2qla3vkNcmevSBW7P1W9HmxF/Ky1zJhEEu2A+ghqHhdgO5SU2HrvUa+aHlMP/kXX5HOl+sIMbKU/IyoCxkZnpxvaYD48zi7JvU5aXN4O2tW+B/DJz1/c85yPI/v339ndyeoPSR2pwAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0wOC0yOVQxMDozMzozMy0wNDowMMparxMAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMDgtMjlUMTA6MzM6MzItMDQ6MDAdcBwbAAAAAElFTkSuQmCC"

WG_EXECUTABLE=/usr/local/bin/wg-quick

WG_CONFIG="wg0-client"

WG_PIDFILE="/var/run/wireguard/$WG_CONFIG.name"

case "$1" in
    connect)
        sudo $WG_EXECUTABLE up $WG_CONFIG

        # Wait for connection so menu item refreshes instantly
        until [ -f $WG_PIDFILE ]; do sleep 1; done
        ;;
    disconnect)
        sudo $WG_EXECUTABLE down $WG_CONFIG

        # Wait for disconnection so menu item refreshes instantly
        until [ ! -f $WG_PIDFILE ]; do sleep 1; done
        ;;
esac

if [ -f $WG_PIDFILE ]; then
    echo "| templateImage=$WG_CONNECTED"
    echo '---'
    echo "Disconnect Wireguard | bash='$0' param1=disconnect terminal=false refresh=true"
    exit
else
    echo "| templateImage=$WG_DISCONNECTED"
    echo '---'
    echo "Connect Wireguard | bash='$0' param1=connect terminal=false refresh=true"
    exit
fi