Skip to content

Commit 22bedbb

Browse files
Use XDG Base Directories
1 parent ee00164 commit 22bedbb

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed

protonvpn_cli/cli.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
)
6464
# Constants
6565
from .constants import (
66-
CONFIG_DIR, CONFIG_FILE, PASSFILE, USER, VERSION, SPLIT_TUNNEL_FILE
66+
CONFIG_DIR, DATA_DIR, CACHE_DIR, CONFIG_FILE, PASSFILE, USER, VERSION, SPLIT_TUNNEL_FILE
6767
)
6868

6969

@@ -80,13 +80,15 @@ def cli():
8080
"""Run user's input command."""
8181

8282
# Initial log values
83-
change_file_owner(os.path.join(CONFIG_DIR, "pvpn-cli.log"))
83+
change_file_owner(os.path.join(CACHE_DIR, "pvpn-cli.log"))
8484
logger.debug("###########################")
8585
logger.debug("### NEW PROCESS STARTED ###")
8686
logger.debug("###########################")
8787
logger.debug(sys.argv)
8888
logger.debug("USER: {0}".format(USER))
8989
logger.debug("CONFIG_DIR: {0}".format(CONFIG_DIR))
90+
logger.debug("DATA_DIR: {0}".format(DATA_DIR))
91+
logger.debug("CACHE_DIR: {0}".format(CACHE_DIR))
9092

9193
args = docopt(__doc__, version="ProtonVPN-CLI v{0}".format(VERSION))
9294
logger.debug("Arguments\n{0}".format(str(args).replace("\n", "")))
@@ -180,6 +182,16 @@ def init_config_file():
180182
logger.debug("Config Directory created")
181183
change_file_owner(CONFIG_DIR)
182184

185+
if not os.path.isdir(CACHE_DIR):
186+
os.mkdir(CACHE_DIR)
187+
logger.debug("Cache Directory created")
188+
change_file_owner(CACHE_DIR)
189+
190+
if not os.path.isdir(DATA_DIR):
191+
os.mkdir(DATA_DIR)
192+
logger.debug("Data Directory created")
193+
change_file_owner(DATA_DIR)
194+
183195
# Warn user about reinitialization
184196
try:
185197
if int(get_config_value("USER", "initialized")):

protonvpn_cli/connection.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
)
2323
# Constants
2424
from .constants import (
25-
CONFIG_DIR, TEMPLATE_FILE, OVPN_FILE, PASSFILE, CONFIG_FILE
25+
CACHE_DIR, DATA_DIR, TEMPLATE_FILE, OVPN_FILE, PASSFILE, CONFIG_FILE
2626
)
2727

2828

@@ -361,7 +361,7 @@ def status():
361361
if not is_connected():
362362
logger.debug("Disconnected")
363363
print("Status: Disconnected")
364-
if os.path.isfile(os.path.join(CONFIG_DIR, "iptables.backup")):
364+
if os.path.isfile(os.path.join(DATA_DIR, "iptables.backup")):
365365
print("[!] Kill Switch is currently active.")
366366
logger.debug("Kill Switch active while VPN disconnected")
367367
else:
@@ -416,7 +416,7 @@ def status():
416416
last_connection = get_config_value("metadata", "connected_time")
417417
connection_time = time.time() - int(last_connection)
418418

419-
if os.path.isfile(os.path.join(CONFIG_DIR, "iptables.backup")):
419+
if os.path.isfile(os.path.join(DATA_DIR, "iptables.backup")):
420420
killswitch_on = True
421421
else:
422422
killswitch_on = False
@@ -471,7 +471,7 @@ def openvpn_connect(servername, protocol):
471471

472472
print("Connecting to {0} via {1}...".format(servername, protocol.upper()))
473473

474-
with open(os.path.join(CONFIG_DIR, "ovpn.log"), "w+") as f:
474+
with open(os.path.join(CACHE_DIR, "ovpn.log"), "w+") as f:
475475
subprocess.Popen(
476476
[
477477
"openvpn",
@@ -484,7 +484,7 @@ def openvpn_connect(servername, protocol):
484484
logger.debug("OpenVPN process started")
485485
time_start = time.time()
486486

487-
with open(os.path.join(CONFIG_DIR, "ovpn.log"), "r") as f:
487+
with open(os.path.join(CACHE_DIR, "ovpn.log"), "r") as f:
488488
while True:
489489
content = f.read()
490490
f.seek(0)
@@ -557,7 +557,7 @@ def manage_dns(mode, dns_server=False):
557557
restore: Revert changes and restore original configuration
558558
"""
559559

560-
backupfile = os.path.join(CONFIG_DIR, "resolv.conf.backup")
560+
backupfile = os.path.join(DATA_DIR, "resolv.conf.backup")
561561
resolvconf_path = os.path.realpath("/etc/resolv.conf")
562562

563563
if mode == "leak_protection":
@@ -642,8 +642,8 @@ def manage_ipv6(mode):
642642
restore: Revert changes and restore original configuration.
643643
"""
644644

645-
ipv6_backupfile = os.path.join(CONFIG_DIR, "ipv6.backup")
646-
ip6tables_backupfile = os.path.join(CONFIG_DIR, "ip6tables.backup")
645+
ipv6_backupfile = os.path.join(DATA_DIR, "ipv6.backup")
646+
ip6tables_backupfile = os.path.join(DATA_DIR, "ip6tables.backup")
647647

648648
if mode == "disable":
649649

@@ -773,7 +773,7 @@ def manage_killswitch(mode, proto=None, port=None):
773773
reason this will completely block access to the internet.
774774
"""
775775

776-
backupfile = os.path.join(CONFIG_DIR, "iptables.backup")
776+
backupfile = os.path.join(DATA_DIR, "iptables.backup")
777777

778778
if mode == "restore":
779779
logger.debug("Restoring iptables")
@@ -797,7 +797,7 @@ def manage_killswitch(mode, proto=None, port=None):
797797
logger.debug("Kill Switch backup exists")
798798
manage_killswitch("restore")
799799

800-
with open(os.path.join(CONFIG_DIR, "ovpn.log"), "r") as f:
800+
with open(os.path.join(CACHE_DIR, "ovpn.log"), "r") as f:
801801
content = f.read()
802802
device = re.search(r"(TUN\/TAP device) (.+) opened", content)
803803
if not device:

protonvpn_cli/constants.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,44 @@
66
except KeyError:
77
USER = getpass.getuser()
88

9-
CONFIG_DIR = os.path.join(os.path.expanduser("~{0}".format(USER)), ".pvpn-cli")
9+
# Ensure backwards compatibility
10+
home_path = os.path.expanduser("~{0}".format(USER))
11+
home_config = os.path.join(home_path, ".pvpn-cli")
12+
if os.path.exists(home_config):
13+
CONFIG_DIR = home_config
14+
DATA_DIR = home_config
15+
CACHE_DIR = home_config
16+
17+
else:
18+
# Get the config directory
19+
xdg_config_str = os.getenv("XDG_CONFIG_HOME")
20+
if not xdg_config_str:
21+
xdg_config = os.path.join(home_path, ".config")
22+
else:
23+
xdg_config = os.path.realpath(xdg_config_str)
24+
CONFIG_DIR = os.path.join(xdg_config, "pvpn-cli")
25+
26+
# Get the data directory
27+
xdg_data_str = os.getenv("XDG_DATA_HOME")
28+
if not xdg_data_str:
29+
xdg_data = os.path.join(home_path, ".local/share")
30+
else:
31+
xdg_data = os.path.realpath(xdg_data_str)
32+
DATA_DIR = os.path.join(xdg_data, "pvpn-cli")
33+
34+
# Get the cache directory
35+
xdg_cache_str = os.getenv("XDG_CACHE_HOME")
36+
if not xdg_cache_str:
37+
xdg_cache = os.path.join(home_path, ".cache")
38+
else:
39+
xdg_cache = os.path.realpath(xdg_cache_str)
40+
CACHE_DIR = os.path.join(xdg_cache, "pvpn-cli")
41+
42+
1043
CONFIG_FILE = os.path.join(CONFIG_DIR, "pvpn-cli.cfg")
11-
TEMPLATE_FILE = os.path.join(CONFIG_DIR, "template.ovpn")
12-
SERVER_INFO_FILE = os.path.join(CONFIG_DIR, "serverinfo.json")
13-
SPLIT_TUNNEL_FILE = os.path.join(CONFIG_DIR, "split_tunnel.txt")
14-
OVPN_FILE = os.path.join(CONFIG_DIR, "connect.ovpn")
15-
PASSFILE = os.path.join(CONFIG_DIR, "pvpnpass")
44+
TEMPLATE_FILE = os.path.join(CACHE_DIR, "template.ovpn")
45+
SERVER_INFO_FILE = os.path.join(CACHE_DIR, "serverinfo.json")
46+
SPLIT_TUNNEL_FILE = os.path.join(DATA_DIR, "split_tunnel.txt")
47+
OVPN_FILE = os.path.join(DATA_DIR, "connect.ovpn")
48+
PASSFILE = os.path.join(DATA_DIR, "pvpnpass")
1649
VERSION = "2.2.0"

protonvpn_cli/logger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from logging.handlers import RotatingFileHandler
44

5-
from .constants import CONFIG_DIR
5+
from .constants import CACHE_DIR
66

77

88
def get_logger():
@@ -13,11 +13,11 @@ def get_logger():
1313
FORMATTER = logging.Formatter(
1414
"%(asctime)s — %(name)s — %(levelname)s — %(funcName)s:%(lineno)d — %(message)s" # noqa
1515
)
16-
LOGFILE = os.path.join(CONFIG_DIR, "pvpn-cli.log")
16+
LOGFILE = os.path.join(CACHE_DIR, "pvpn-cli.log")
1717

1818
# TBD, maybe /var/log is the better option
19-
if not os.path.isdir(CONFIG_DIR):
20-
os.mkdir(CONFIG_DIR)
19+
if not os.path.isdir(CACHE_DIR):
20+
os.mkdir(CACHE_DIR)
2121

2222
logger = logging.getLogger("protonvpn-cli")
2323
logger.setLevel(logging.DEBUG)

0 commit comments

Comments
 (0)