diff --git a/README.md b/README.md index 85ac0b9..3c1db66 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,89 @@ -# pypacer - -Generate auto proxy config files (PAC) from a declaration. +# Generate auto proxy config files (PAC) from a declaration. [![Tests](https://github.com/73h/pypacer/actions/workflows/tests.yml/badge.svg)](https://github.com/73h/pypacer/actions/workflows/tests.yml) -*-- Still at work --* +--- This package aims to make it possible to create simple proxy scripts declaratively. It will never cover all the subtleties. If you have unusual requirements, it is better to write the proxy script directly in JavaScript + +## Usage + +ToDo: Build package and publish on pypi + +### Example + +example.yaml: + +```yaml +description: A normal proxy script +version: 1.0 +default: PROXY_DEFAULT +proxies: + DIRECT: + description: take the direct route + route: "DIRECT" + targets: + - ".example.com" + PROXY_NET_MASKS: + description: take the proxy A route + route: "PROXY netmask.example.com" + targets: + - "93.184.0.0/16" + - "example.net" + - "192.0.0.170" + - "127.0.0.1" + PROXY_COMPANY: + description: take the proxy B route + route: "PROXY company.example.com" + targets: + - "example.com" + - "foo.example.com" + - "bar.example.com" + PROXY_DEFAULT: + description: take the default proxy route + route: "PROXY default.example.com" + targets: [] + +``` + +run this in python: + +```python +import os +from pypacer import PyPacer + +p = PyPacer() +with open(os.path.join("examples.yaml"), "r") as f: + p.load_from_yaml(f.read()) + print(p.output()) +``` + +and you get this: + +```javascript +function FindProxyForURL(url, host) { + host = host.toLowerCase(); + if ( + localHostOrDomainIs(host, "example.com") + || localHostOrDomainIs(host, "foo.example.com") + || localHostOrDomainIs(host, "bar.example.com") + ) { + return "PROXY company.example.com"; + } + if ( + dnsDomainIs(host, ".example.com") + ) { + return "DIRECT"; + } + if ( + isInNet(host, "93.184.0.0", "255.255.0.0") + || localHostOrDomainIs(host, "example.net") + || isInNet(host, "192.0.0.170", "255.255.255.255") + || isInNet(host, "127.0.0.1", "255.255.255.255") + ) { + return "PROXY netmask.example.com"; + } + return "PROXY default.example.com"; +} +``` diff --git a/src/main.py b/src/main.py index 927c1ef..6232f22 100644 --- a/src/main.py +++ b/src/main.py @@ -4,7 +4,16 @@ from pypacer.helpers import location if __name__ == "__main__": - pac_file = open(os.path.join(location, "..", "examples", "unittests.yaml"), "r").read() p = PyPacer() - p.load_config_from_yaml(pac_file) + with open(os.path.join(location, "..", "examples", "unittests.yaml"), "r") as f: + p.load_from_yaml(f.read()) + print(p.output()) + + simple_proxy = """ + proxies: + DIRECT: + route: DIRECT + default: DIRECT + """ + p.load_from_yaml(str(simple_proxy)) print(p.output()) diff --git a/src/pypacer/pypacer.py b/src/pypacer/pypacer.py index 8177960..4ee4d1c 100644 --- a/src/pypacer/pypacer.py +++ b/src/pypacer/pypacer.py @@ -11,7 +11,7 @@ class PyPacer: def __init__(self): self.config: Optional[PyPacerConfig] = None - def load_config_from_yaml(self, stream: str): + def load_from_yaml(self, stream: str): y = yaml.safe_load(stream) self.config = PyPacerConfig(**y) self.config.validate() diff --git a/src/pypacer/pypacer_test.py b/src/pypacer/pypacer_test.py index 762cc8a..50ada94 100644 --- a/src/pypacer/pypacer_test.py +++ b/src/pypacer/pypacer_test.py @@ -15,7 +15,7 @@ def setUp(self): def test_load_config_from_yaml(self): p = PyPacer() - p.load_config_from_yaml(self.pac_file) + p.load_from_yaml(self.pac_file) output = p.output() open(os.path.join(location, "..", "examples", "unittests.pac"), "w").write(output) self.assertIsInstance(p.config, PyPacerConfig) @@ -23,12 +23,12 @@ def test_load_config_from_yaml(self): def test_get_default_proxy_route(self): p = PyPacer() - p.load_config_from_yaml(self.pac_file) + p.load_from_yaml(self.pac_file) self.assertEqual(p._get_default_proxy_route(), "PROXY default.example.com") def test_output(self): p = PyPacer() - p.load_config_from_yaml(self.pac_file) + p.load_from_yaml(self.pac_file) pac_file = PACFile(p.output()) self.assertEqual(pac_file.find_proxy_for_url("", "foo.bar"), "PROXY default.example.com") self.assertEqual(pac_file.find_proxy_for_url("", "foo.example.com"), "PROXY company.example.com")