Skip to content

Commit

Permalink
implement include and exclude filter at output
Browse files Browse the repository at this point in the history
  • Loading branch information
heikoschmidt committed Mar 14, 2024
1 parent e372c34 commit 4171615
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ subtleties. If you have unusual requirements, it is better to write the proxy sc
## I would still like to implement this

- [x] ~~automatic sorting with overlapping hostnames~~
- [x] ~~implement include and exclude filter at output~~
- [ ] automatic sorting with overlapping network masks
- [ ] publish on pypi
- [ ] implement filter at output
- [ ] add tag to automatically add local networks to the proxy


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pypacer"
version = "0.1.3"
version = "0.1.4"
dynamic = ["dependencies"]
authors = [
{ name = "Heiko Schmidt", email = "[email protected]" },
Expand Down
2 changes: 2 additions & 0 deletions src/examples/unittests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ proxies:
- "foo.example.net"
- route: "PROXY netmask.example.com"
description: a proxy for netmask
tags:
- foo
targets:
- "93.184.0.0/16"
- route: "PROXY ip.example.com"
Expand Down
38 changes: 37 additions & 1 deletion src/pypacer/pypacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,46 @@ def load_from_yaml(self, stream: str):
y = yaml.safe_load(stream)
self.load_from_dict(y)

def output(self) -> str:
def output(self, excludes: list[str] = None, includes: list[str] = None) -> str:
"""Returns the finished ProxyScript as a string
Parameters
----------
excludes : list[str], optional
A list of tags. Proxies that have this tag are excluded.
includes : list[str], optional
A list of tags. Proxies that have this tag are included. Other proxies are not included.
"""
if self.config is None:
raise Exception("No config loaded, use load_from_yaml or load_from_dict first.")
config = copy.deepcopy(self.config)

# handle includes
if includes:
proxies = []
for proxy in config.proxies:
include = False
for tag in proxy.tags:
if tag in includes:
include = True
if include:
proxies.append(proxy)
config.proxies = proxies

# handle excludes
if excludes:
proxies = []
for proxy in config.proxies:
exclude = False
for tag in proxy.tags:
if tag in excludes:
exclude = True
if not exclude:
proxies.append(proxy)
config.proxies = proxies

default = config.get_default_proxy()
config.reorganize_proxies()
config.recognize_overlaps()
Expand Down
6 changes: 5 additions & 1 deletion src/pypacer/pypacerconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ def recognize_overlaps(self):

def get_default_proxy(self) -> Proxy:
defaults = [p for p in self.proxies if "default" in p.tags]
return defaults[0] if len(defaults) > 0 else [p for p in self.proxies][0]
if len(defaults) == 0:
if len(self.proxies) == 0:
return Proxy(route="DIRECT")
return self.proxies[0]
return defaults[0]
26 changes: 26 additions & 0 deletions src/tests/pypacer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ def test_load_config_from_yaml(self):
output = p.output()
open(os.path.join(location, "..", "examples", "unittests.pac"), "w").write(output)

def test_exclude_by_tag(self):
p = PyPacer()
p.load_from_yaml(self.pac_file)
output = p.output(excludes=["foo"])
self.assertTrue("PROXY netmask.example.com" not in output)

def test_exclude_by_tags(self):
p = PyPacer()
p.load_from_yaml(self.pac_file)
output = p.output(excludes=["foo", "default"])
self.assertTrue("PROXY netmask.example.com" not in output)
self.assertTrue("PROXY default.example.com" not in output)

def test_include_by_tag(self):
p = PyPacer()
p.load_from_yaml(self.pac_file)
output = p.output(includes=["foo"])
self.assertTrue("PROXY netmask.example.com" in output)

def test_include_by_tags(self):
p = PyPacer()
p.load_from_yaml(self.pac_file)
output = p.output(includes=["foo", "default"])
self.assertTrue("PROXY netmask.example.com" in output)
self.assertTrue("PROXY default.example.com" in output)

def test_output(self):
p = PyPacer()
p.load_from_yaml(self.pac_file)
Expand Down

0 comments on commit 4171615

Please sign in to comment.