Skip to content

Commit

Permalink
added a little something to the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
heikoschmidt committed Mar 9, 2024
1 parent ee61378 commit ea04f8b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
87 changes: 83 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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";
}
```
13 changes: 11 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
2 changes: 1 addition & 1 deletion src/pypacer/pypacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/pypacer/pypacer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ 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)
self.assertEqual(p.config.default, "PROXY_DEFAULT")

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")
Expand Down

0 comments on commit ea04f8b

Please sign in to comment.