Skip to content

Commit

Permalink
Initial public release
Browse files Browse the repository at this point in the history
  • Loading branch information
ElsaWeb authored and the-glu committed Oct 8, 2021
0 parents commit 247306d
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Byte-compiles files
**/__pycache__/

# Distribution / packaging
build/
dist/
*.egg-info/

# Environments
env/
25 changes: 25 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
image: python:3.7-buster

cache:
key: ${CI_PROJECT_NAME}
paths:
- .cache/pip
- venv

stages:
- test

before_script:
- apt update
- apt install -y python3-dev
- pip install -U pip
- pip install virtualenv
- virtualenv venv
- source venv/bin/activate
- pip install -U black isort

test:formatting:
stage: test
script:
- black --check .
- isort --check-only --diff .
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
- repo: local
hooks:
- id: black
name: black
entry: black
language: system
types: [python]
- repo: local
hooks:
- id: isort
name: isort
entry: isort
language: system
types: [python]
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Arcanite Solutions LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# wg-tray

wg-tray enables to quickly bring up/down wireguard interfaces from the system tray, using `wg` and `wg-quick`.

## Installation

`$ pip install wg-tray`

## Usage
```bash
wg-tray [-h] [-v] [-c CONFIG]

optional arguments:
-h, --help show this help message and exit
-v, --version show program version info and exit
-c CONFIG, --config CONFIG path to the config file listing all wireguard interfaces
(default: none; use root privileges to look up in /etc/wireguard/)
```
The config file should simply list all wireguard interfaces, separated either by newlines or spaces (e.g. `wg0 wg1` or
```
wg0
wg1
```
). The purpose of this config file is to avoid using root access on `ls` to read in `/etc`; however, its correctness is not checked and it is your responsability to keep it up to date.
If no config is provided, the interfaces are dynamically looked up in `/etc/wireguard`.

If you want to avoid being prompted for your root password each time you run `wg-tray`, use a config file and add the following lines to your sudoers file (`sudo visudo` to edit), replacing `<username>` with your user name:
```
<username> ALL=(ALL) NOPASSWD: /usr/bin/wg
<username> ALL=(ALL) NOPASSWD: /usr/bin/wg-quick
```
3 changes: 3 additions & 0 deletions bin/wg-tray
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /usr/bin/env python

from wgtray import wgtray
40 changes: 40 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[build-system]
requires = ["setuptools>=40.8.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.black]
line-length = 150

[tool.isort]
# When imports are broken into multi-line, use the "Vertical Hanging Indent" layout.
multi_line_output = 3

# Always add a trailing comma to import lists (default: False).
include_trailing_comma = true

# Always put imports lists into vertical mode (0 = none allowed on first line)
force_grid_wrap = 0

# When multi-lining imports, use parentheses for line-continuation instead of default \.
use_parentheses = true

# Max import line length.
line_length = 150

# Regardless of what follows the imports, force 2 blank lines after the import list
lines_after_imports = 2

# Insert 2 blank lines between each section
lines_between_sections = 2

# Alphabetical sort in sections (inside a line or in ())
force_alphabetical_sort_within_sections = true

# Sort by lexicographical
lexicographical = true

# Put all from before import
from_first = true

ensure_newline_before_comments = true

31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pathlib


from setuptools import find_packages, setup


# Get the long description from the README file
here = pathlib.Path(__file__).parent.resolve()
long_description = (here / "README.md").read_text()

# Get the global variables of the package
gv = {}
exec((here / "wgtray/__init__.py").read_text(), gv)

setup(
name="wg-tray",
version=gv["__version__"],
description="A simple graphical tool to manage wireguard interfaces from the system tray",
long_description=long_description,
long_description_content_type="text/markdown",
author="Elsa Weber",
author_email="[email protected]",
url="https://git.arcanite.ch/arcanite/wg-tray/",
license="LICENSE.txt",
packages=find_packages(),
install_requires=[
"pyqt5",
],
package_data={"wgtray": ["res/*"]},
scripts=["bin/wg-tray"],
)
2 changes: 2 additions & 0 deletions wgtray/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__version__ = "0.1"
__description__ = "A simple UI tool to handle WireGuard interfaces"
Empty file added wgtray/actions/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions wgtray/actions/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pathlib
import subprocess
import threading


from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtGui import QIcon, QMovie
from PyQt5.QtWidgets import QAction, QSystemTrayIcon


RES_PATH = pathlib.Path(__file__).parent.parent.resolve() / "res"


class WGInterface(QAction):
done = pyqtSignal(bool, str, name="done") # necessary to put outside of __init__

def __init__(self, name, parent, is_up):
super().__init__(name, parent)

self.name = name
self.parent = parent
self.is_up = is_up

self.triggered.connect(self.toggle)
self.done.connect(self.check_status)

def setUp(self, up):
self.is_up = up

def updateIcon(self):
if self.is_up:
icon_path = f"{RES_PATH}/green_arrow_up.png"
else:
icon_path = f"{RES_PATH}/grey_arrow_down.png"
self.setIcon(QIcon(icon_path))

def toggle(self):
# Loading animation
self.loadingSpinner = QMovie(f"{RES_PATH}/loader.gif")
self.loadingSpinner.frameChanged.connect(lambda: self.setIcon(QIcon(self.loadingSpinner.currentPixmap())))
self.loadingSpinner.start()
# Launch command
t = threading.Thread(target=self.bring_up_down, args=(self.is_up,))
t.start()

@pyqtSlot(bool, str)
def check_status(self, is_up, err_message):
self.is_up = is_up

if err_message:
self.parent.tray.showMessage("Error", err_message, QSystemTrayIcon.NoIcon)
self.loadingSpinner.stop()
self.updateIcon()

def bring_up_down(self, is_up):
kw = "down" if is_up else "up"
subp = subprocess.Popen(f"sudo wg-quick {kw} {self.name}", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

_, std_err = subp.communicate() # blocking
stat, err_msg = is_up, ""
if subp.returncode == 0:
stat = not is_up # toggle was successful
else:
err_msg = std_err.decode()

self.done.emit(stat, err_msg)
Binary file added wgtray/res/green_arrow_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wgtray/res/grey_arrow_down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wgtray/res/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wgtray/res/loader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 247306d

Please sign in to comment.