Skip to content

Commit bfb0e14

Browse files
committed
initial config parsing
1 parent 000816c commit bfb0e14

10 files changed

+277
-0
lines changed

MANIFEST.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include rete/VERSION
2+
include rete/config/*.yml
3+
global-exclude *.py[cod] __pycache__ *.so

rete/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

rete/__init__.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python3
2+
# -*- coding:utf-8 -*-
3+
4+
import coloredlogs
5+
import logging
6+
import logging.config
7+
import elevate
8+
import os
9+
10+
REPO_NAME = "retenet"
11+
12+
with open(os.path.join(os.path.dirname(__file__), "VERSION")) as fr:
13+
VERSION = fr.read().strip()
14+
15+
try:
16+
USER_CONFIG_PATH = f"{os.environ['XDG_CONFIG_HOME']}/rete"
17+
except KeyError:
18+
USER_CONFIG_PATH = f"{os.environ['HOME']}/.config/rete"
19+
if not os.path.exists(USER_CONFIG_PATH):
20+
os.makedirs(USER_CONFIG_PATH)
21+
22+
try:
23+
LOG_PATH = f"{os.environ['XDG_DATA_HOME']}/rete"
24+
except KeyError:
25+
LOG_PATH = f"{os.environ['HOME']}/.local/share/rete"
26+
if not os.path.exists(LOG_PATH):
27+
os.makedirs(LOG_PATH)
28+
29+
BROWSERS = ["brave", "chromium", "firefox", "opera", "tbb"]
30+
31+
logging.config.dictConfig(
32+
{
33+
"version": 1,
34+
"disable_existing_loggers": False,
35+
"formatters": {"standard": {"format": "%(message)s"},},
36+
"handlers": {
37+
"screen": {"level": "INFO", "class": "logging.StreamHandler",},
38+
"file": {
39+
"level": "DEBUG",
40+
"class": "logging.FileHandler",
41+
"filename": f"{LOG_PATH}/rete.log",
42+
},
43+
},
44+
"loggers": {
45+
"": {"handlers": ["screen", "file"], "level": "DEBUG", "propagate": True}
46+
},
47+
}
48+
)
49+
coloredlogs.install()

rete/__main__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
4+
from rete.cli import main
5+
6+
if __name__ == "__main__":
7+
main()

rete/cli.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
4+
import argparse
5+
import logging
6+
import elevate
7+
import docker
8+
import yaml
9+
import grp
10+
import os
11+
12+
from rete import BROWSERS, USER_CONFIG_PATH, VERSION
13+
from rete.utils import parse_config, run_container, pull_image
14+
15+
logger = logging.getLogger(__name__)
16+
17+
18+
def get_args():
19+
cfg = parse_config()
20+
21+
parser = argparse.ArgumentParser(f"rete version {VERSION}")
22+
parser.add_argument(
23+
"browser",
24+
nargs="?",
25+
choices=BROWSERS,
26+
default=cfg["browser"]["name"],
27+
help="Supported Browsers",
28+
)
29+
30+
parser.add_argument(
31+
"-p", "--profile", default="", required=False, help="Profile Name"
32+
)
33+
parser.add_argument("-t", action="store_true", help="Temporary Profile")
34+
parser.add_argument("--vpn", required=False, help="Use a VPN")
35+
parser.add_argument("--proxy", required=False, help="PROTO://IP:PORT")
36+
37+
group = parser.add_mutually_exclusive_group()
38+
group.add_argument("--config", required=False, help="Open Config for Editing")
39+
group.add_argument("--rm", required=False, help="Stop and Remove ALL Browsers")
40+
group.add_argument("--update", required=False, help="Check for Upates")
41+
42+
args = parser.parse_args()
43+
44+
if not args.browser:
45+
logger.error("No Browser Specified")
46+
exit(1)
47+
48+
return args
49+
50+
51+
def main():
52+
53+
args = get_args()
54+
55+
user_grps = [g.gr_name for g in grp.getgrall() if os.environ["USER"] in g.gr_mem]
56+
if "docker" not in user_grps:
57+
elevate.elevate(graphical=False)
58+
59+
client = docker.from_env()
60+
# pull_image(client, args.browser)
61+
62+
63+
if __name__ == "__main__":
64+
main()

rete/config/rete.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
browser:
2+
name: firefox
3+
proxy:
4+
dns:
5+
host:
6+
doh: false
7+
8+
profile:
9+
default: personal
10+
list: [htb, lan, media, personal, shopping, work]
11+
12+
vpn:
13+
ovpn:

rete/config/rete_schema.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
type: object
2+
properties:
3+
browser:
4+
type: object
5+
required:
6+
- name
7+
properties:
8+
name:
9+
type: string
10+
enum:
11+
- brave
12+
- chromium
13+
- firefox
14+
- opeara
15+
- tbb
16+
proxy:
17+
type:
18+
- string
19+
- 'null'
20+
pattern: '^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$'
21+
dns:
22+
type: object
23+
properties:
24+
host:
25+
type:
26+
- string
27+
- 'null'
28+
doh:
29+
type: boolean
30+
profile:
31+
type: object
32+
properties:
33+
default:
34+
type: string
35+
list:
36+
type: array
37+
items:
38+
type: string
39+
vpn:
40+
type: object
41+
properties:
42+
ovpn:
43+
type:
44+
- string
45+
- 'null'
46+

rete/utils.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
4+
import jsonschema
5+
import logging
6+
import docker
7+
import shutil
8+
import yaml
9+
import os
10+
11+
from rete import REPO_NAME, USER_CONFIG_PATH
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
def parse_config():
17+
config_path = f"{USER_CONFIG_PATH}/rete.yml"
18+
if not os.path.exists(config_path):
19+
shutil.copy(f"{os.path.dirname(__file__)}/config/rete.yml", config_path)
20+
21+
with open(f"{USER_CONFIG_PATH}/rete.yml") as fr, open(
22+
f"{os.path.dirname(__file__)}/config/rete_schema.yml"
23+
) as fr2:
24+
try:
25+
cfg = yaml.safe_load(fr)
26+
jsonschema.validate(cfg, yaml.safe_load(fr2))
27+
except jsonschema.exceptions.ValidationError as e:
28+
if "Failed validating 'pattern'" in str(e):
29+
logger.error(f"Invalid Proxy in Config File: {USER_CONFIG_PATH}/rete.yml")
30+
else:
31+
logger.error(f"Invalid Option in Config File: {USER_CONFIG_PATH}/rete.yml")
32+
logger.error(e)
33+
exit(1)
34+
35+
return cfg
36+
37+
38+
def run_container(client, browser, detach=False):
39+
cntr = client.containers.run(f"{REPO_NAME}/{browser}", detach=detach)
40+
logger.info(cntr)
41+
42+
43+
def pull_image(client, browser):
44+
cntr = client.images.pull(f"{REPO_NAME}/{browser}")
45+
logger.info(cntr)

setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[metadata]
2+
description-file = README.md
3+
long_description_content_type = text/markdown

setup.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from setuptools import setup, find_packages
5+
import os
6+
7+
here = os.path.abspath(os.path.dirname(__file__))
8+
9+
with open(os.path.join("rete", "VERSION"), "r", encoding="utf-8") as f:
10+
version = f.read()
11+
12+
with open("README.md", "r", encoding="utf-8") as f:
13+
long_description = f.read()
14+
15+
setup(
16+
name="rete",
17+
version=version,
18+
description="",
19+
license="GPLv3",
20+
long_description=long_description,
21+
author="retenet",
22+
author_email="[email protected]",
23+
classifiers=[
24+
"Development Status :: 3 - Alpha",
25+
"Intended Audience :: End Users/Desktop",
26+
"License :: OSI Approved :: MIT License",
27+
"Programming Language :: Python :: 3",
28+
"Operating System :: POSIX :: Linux",
29+
"Environment :: Console",
30+
],
31+
keywords="browser",
32+
packages=find_packages(exclude=["browsers", "contrib", "docs", "tests"]),
33+
install_requires=[
34+
"colorama==0.4.3",
35+
"coloredlogs==14.0",
36+
"docker==4.2.2",
37+
"elevate==0.1.3",
38+
"jsonschema==3.2.0",
39+
"PyYAML==5.3.1",
40+
],
41+
use_scm_version=True,
42+
setup_required=["setuptools_scm"],
43+
extras_require={"dev": ["black==19.10b0", "ipython==7.13.0", "pytest==4.6.7"],},
44+
entry_points={"console_scripts": ["rete = rete.cli:main",],},
45+
include_package_data=True,
46+
)

0 commit comments

Comments
 (0)