Skip to content

Commit

Permalink
Update readme and add generate-config command
Browse files Browse the repository at this point in the history
  • Loading branch information
annervisser committed Jun 25, 2023
1 parent 8f75882 commit 57963af
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 47 deletions.
98 changes: 61 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,95 @@
# Gnome appindicator dns switcher
https://pypi.org/project/gnome-dns-switcher/

Gnome appindicator (toolbar icon) tool to quickly switch between different DNS servers
- Easily available in your toolbox
- Config file to specify DNS servers
Supports multiple connections and switching them separately
- Utility button to restart NetworkManager

![Screenshot of menu with DNS servers](./screenshots/menu.png)

<details>
<summary>💻 More screenshots</summary>

#### Easily available in your toolbar
![](./screenshots/appindicator.png)

#### Shows you what it's done, no surprises
![](./screenshots/notification.png)

#### Supports multiple connections and switching them separately
![](./screenshots/menu_multiple_connections.png)

</details>

## Setup
Install dependencies
```shell
# In case you don't have pip3:
# If you don't yet have pip3:
sudo apt install python3-pip

# Install dependencies to run this application
sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0 gir1.2-appindicator3-0.1
```

Install gnome-dns-switcher
```shell
# sudo so the script is added to $PATH, you can also install normally and fix $PATH :)
sudo pip3 install gnome-dns-switcher

# See config explanation
edit ~/.config/gnome-dns-switcher.yml
# Generate a default configuration
gnome-dns-switcher generate-config

# Start the app in the background
gnome-dns-switcher --config ~/.config/gnome-dns-switcher.yml &
# Modify the config to your needs. See config documentation below
edit ~/.config/gnome-dns-switcher.yml
```


Add to startup applications:
## Usage
### Start the app in the background
Simply add ` &` after your command
```shell
# Sleep 10 to prevent starting up before network connections have been made
bash -c 'sleep 10 && gnome-dns-switcher --config ~/.config/gnome-dns-switcher.yml'```
- Create a config.yml, see [Config](#config)
## Running
gnome-dns-switcher &
```

### Specify a different configuration file (default is `~/.config/gnome-dns-switcher.yml`)
```shell
gnome-dns-switcher --config /path/to/config.yml
```

## Config
### Add to startup applications
```shell
# Sleep 10 seconds before starting to prevent starting up
# before network connections have been made
bash -c 'sleep 10 && gnome-dns-switcher --config ~/.config/gnome-dns-switcher.yml'
```

### Sample:
## Config
The config file allows you to specify which DNS servers to show and what devices are visible.
By default, gnome-dns-switcher looks for a config file at `~/.config/gnome-dns-switcher.yml`

To generate and example configuration, you can run `gnome-dns-switcher generate-config`
### The generated configuration will look like this:
```yaml
servers:
CloudFlare: 1.1.1.1
servers: # DNS Server options to show
# DHCP is always shown as the first option
Quad9: 9.9.9.9
CloudFlare: [1.1.1.1, 1.0.0.1]
Google DNS:
- 8.8.8.8
- 8.8.4.4
localhost: 127.0.0.1, 127.0.1.1
devices:
- wlp2s0
devices: [] # Specify device names here if you want to hide certain devices (ip link show)
```
### Explanation
#### `servers:`
#### `servers` A list of servers that can be switched between
- The name is only used for displaying
- You can define one or more ips, as a yaml array or as a comma-seperated string

- A list of servers that can be switched between
- Name is just used for displaying
- You can define one or more ips
- On launch, we'll try to detect if the current settings match any of the servers
#### `devices:`
- optional, will display all non-bridge connections otherwise
#### `devices` Which devices to show (optional)
- optional, will display all non-bridge connections if not present or empty
- one or more devices to show in the switcher
- List all your devices by running `ip link show` in a terminal

### You possibly need to install these dependencies
```shell
sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0 gir1.2-appindicator3-0.1
```
## Development

### Setup
Expand Down
66 changes: 56 additions & 10 deletions gnome_dns_switcher/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import os
from os.path import isfile
from typing import List

Expand All @@ -7,23 +8,61 @@
from .gnome_helpers import get_connections
from .switcher import Server, DnsSwitcher

CONFIG_PATH = os.path.expanduser("~/.config/gnome-dns-switcher.yml")

APPINDICATOR_ID = 'gnome_dns_switcher'

# language=yaml
DEFAULT_CONFIG = '''
servers:
# DHCP is always shown as the first option
Quad9: 9.9.9.9
CloudFlare: [1.1.1.1, 1.0.0.1]
Google DNS:
- 8.8.8.8
- 8.8.4.4
devices: [] # Specify device names here if you want to hide certain devices (ip link show)
'''


def main():
parser = argparse.ArgumentParser(description='Gnome Dns Switcher')
parser.add_argument("--config", dest="config", required=False, default="./config.yml",
help="Path to config yaml", metavar="FILE",
type=lambda x:
x if isfile(x)
else parser.error("Specified config file ({}) is not a file. "
"Create it or specify a different path with --config".format(x)))
subcommands = parser.add_subparsers()
parser.set_defaults(subcommand=None)

parser.add_argument(
"--config",
dest="config",
required=False,
default=CONFIG_PATH,
help="Path to config yaml",
metavar="FILE",
)

generate_config_command = subcommands.add_parser(
'generate-config',
help=f'Generate an example configuration file in {CONFIG_PATH}'
)
generate_config_command.set_defaults(subcommand='generate-config')

args = parser.parse_args()

config_path = args.config

result = 42 # 42 === reload
config_path = os.path.expanduser(args.config)

if args.subcommand == 'generate-config':
if isfile(config_path):
print(f'A file already exists at {config_path}')
exit(1)
print(f'Generating config at {config_path}')
os.umask(0o077)
os.makedirs(os.path.dirname(config_path), 0o700, exist_ok=True)
with open(config_path, 'x') as config_file:
config_file.write(DEFAULT_CONFIG)
print(f'Config file written!')
if (config_path != CONFIG_PATH):
print(f'To use this config file, run this program with --config {config_path}')
exit(0)

result = 42 # 42 === reload
while result == 42:
connections, servers = load_config(config_path)
menu = DnsSwitcher(APPINDICATOR_ID, servers, connections)
Expand All @@ -33,6 +72,13 @@ def main():


def load_config(config_path):
if not isfile(config_path):
print(
f"There is no file at the specified config path ({config_path}).\n"
f"Create it by running `gnome-dns-switcher generate-config` or specify a different path with --config"
)
exit(1)

with open(config_path, 'r') as config_file:
config = yaml.safe_load(config_file)
servers: List[Server] = [Server('DHCP', None, True)]
Expand Down
Binary file added screenshots/appindicator.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 screenshots/menu.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 screenshots/menu_multiple_connections.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 screenshots/notification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 57963af

Please sign in to comment.