├── .github
└── workflows
│ ├── deploy.yml
│ └── main.yml
├── .gitignore
├── .project
├── .pydevproject
├── .settings
└── org.eclipse.core.resources.prefs
├── .vscode
└── launch.json
├── CHANGELOG.md
├── LICENSE
├── MANIFEST.in
├── README.md
├── README.rst
├── doc
├── advanced.md
├── compat.md
├── configuration.md
├── examples.md
└── leak.md
├── examples
├── basic
│ ├── future.py
│ ├── future_neo.py
│ ├── future_old.py
│ ├── loop.py
│ ├── loop_asyncio.py
│ ├── loop_neo.py
│ ├── loop_old.py
│ ├── sum.py
│ ├── sum_gen.py
│ ├── sum_mix.py
│ └── sum_neo.py
├── echo
│ ├── echoc_udp.py
│ ├── echos_tcp.py
│ ├── echos_udp.py
│ └── hello_http.py
└── http
│ ├── http_aiohttp.py
│ ├── http_aiohttp_neo.py
│ ├── http_asyncio.py
│ └── http_players.py
├── pylintrc
├── pytest.ini
├── requirements.txt
├── res
├── favicon.ico
├── favicon.png
├── icon.png
├── logo-2x.png
└── logo.png
├── scripts
└── build
│ ├── all
│ └── build.py
│ └── build.json
├── setup.cfg
├── setup.py
└── src
└── netius
├── __init__.py
├── adapters
├── __init__.py
├── base.py
├── base.pyi
├── fs.py
├── fs.pyi
├── memory.py
├── memory.pyi
├── mongo.py
├── mongo.pyi
├── null.py
└── null.pyi
├── auth
├── __init__.py
├── address.py
├── address.pyi
├── allow.py
├── allow.pyi
├── base.py
├── base.pyi
├── deny.py
├── deny.pyi
├── dummy.py
├── dummy.pyi
├── memory.py
├── memory.pyi
├── passwd.py
├── passwd.pyi
├── simple.py
└── simple.pyi
├── base
├── __init__.py
├── agent.py
├── async_neo.py
├── async_old.py
├── asynchronous.py
├── client.py
├── common.py
├── compat.py
├── config.py
├── conn.py
├── container.py
├── diag.py
├── errors.py
├── extras
│ ├── dh.pem
│ ├── net.cer
│ ├── net.key
│ └── net.pub
├── legacy.py
├── log.py
├── observer.py
├── poll.py
├── protocol.py
├── request.py
├── server.py
├── service.py
├── stream.py
├── tls.py
├── transport.py
└── util.py
├── clients
├── __init__.py
├── apn.py
├── dht.py
├── dns.py
├── http.py
├── mjpg.py
├── raw.py
├── smtp.py
├── ssdp.py
├── torrent.py
└── ws.py
├── common
├── __init__.py
├── asn.py
├── calc.py
├── dhcp.py
├── dkim.py
├── ftp.py
├── geo.py
├── http.py
├── http2.py
├── mime.py
├── parser.py
├── pop.py
├── rsa.py
├── setup.py
├── smtp.py
├── socks.py
├── stream.py
├── structures.py
├── style.py
├── tftp.py
├── tls.py
├── torrent.py
├── util.py
└── ws.py
├── examples
├── __init__.py
├── http.py
└── upnp.py
├── extra
├── __init__.py
├── desktop.py
├── dhcp_s.py
├── extras
│ └── htpasswd
├── file.py
├── filea.py
├── hello.py
├── hello_w.py
├── proxy_d.py
├── proxy_f.py
├── proxy_r.py
└── smtp_r.py
├── middleware
├── __init__.py
├── annoyer.py
├── base.py
├── blacklist.py
├── dummy.py
├── flood.py
└── proxy.py
├── mock
├── __init__.py
└── appier.py
├── pool
├── __init__.py
├── common.py
├── file.py
├── notify.py
└── task.py
├── servers
├── __init__.py
├── dhcp.py
├── echo.py
├── echo_ws.py
├── extras
│ ├── boy_0.jpg
│ └── boy_1.jpg
├── ftp.py
├── http.py
├── http2.py
├── mjpg.py
├── pop.py
├── proxy.py
├── runners
│ ├── __init__.py
│ └── echo.py
├── smtp.py
├── socks.py
├── tftp.py
├── torrent.py
├── ws.py
└── wsgi.py
├── sh
├── __init__.py
├── auth.py
├── base.py
├── dkim.py
├── rsa.py
└── smtp.py
└── test
├── __init__.py
├── auth
├── __init__.py
├── allow.py
├── deny.py
└── simple.py
├── base
├── __init__.py
├── asynchronous.py
├── common.py
├── config.py
├── tls.py
└── transport.py
├── clients
├── __init__.py
└── http.py
├── common
├── __init__.py
├── calc.py
├── dkim.py
├── http.py
├── mime.py
├── rsa.py
├── setup.py
└── util.py
├── extra
├── __init__.py
├── proxy_r.py
└── smtp_r.py
├── middleware
├── __init__.py
└── proxy.py
├── pool
├── __init__.py
└── common.py
└── servers
├── __init__.py
└── http.py
/.github/workflows/deploy.yml:
--------------------------------------------------------------------------------
1 | name: Deploy Workflow
2 | on:
3 | push:
4 | tags:
5 | - "*"
6 | jobs:
7 | build:
8 | name: Build
9 | timeout-minutes: 10
10 | strategy:
11 | matrix:
12 | python-version: [2.7]
13 | runs-on: ubuntu-latest
14 | container: python:${{ matrix.python-version }}
15 | steps:
16 | - uses: actions/checkout@v4
17 | - run: python --version
18 | - run: pip install -r requirements.txt
19 | - run: |
20 | pip install black
21 | black . --check
22 | if: matrix.python-version == '3.12'
23 | - run: HTTPBIN=httpbin.bemisc.com python setup.py test
24 | - run: pip install twine wheel
25 | - run: python setup.py sdist bdist_wheel
26 | - run: python -m twine upload -u ${PYPI_USERNAME} -p ${PYPI_PASSWORD} dist/*
27 | env:
28 | PYPI_USERNAME: ${{ secrets.PYPI_USERNAME }}
29 | PYPI_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
30 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Main Workflow
2 | on: [push]
3 | jobs:
4 | build:
5 | name: Build
6 | timeout-minutes: 10
7 | strategy:
8 | matrix:
9 | python-version: [
10 | 2.7,
11 | 3.5,
12 | 3.6,
13 | 3.7,
14 | 3.8,
15 | 3.9,
16 | "3.10",
17 | "3.11",
18 | "3.12",
19 | latest,
20 | rc
21 | ]
22 | runs-on: ubuntu-latest
23 | container: python:${{ matrix.python-version }}
24 | steps:
25 | - uses: actions/checkout@v4
26 | - run: python --version
27 | - run: pip install -r requirements.txt
28 | - run: |
29 | pip install black
30 | black . --check
31 | if: matrix.python-version == '3.12'
32 | - run: |
33 | pip install pytest
34 | HTTPBIN=httpbin.bemisc.com pytest
35 | - run: HTTPBIN=httpbin.bemisc.com python setup.py test
36 | if: matrix.python-version != '3.12' && matrix.python-version != 'latest'
37 | build-pypy:
38 | name: Build PyPy
39 | timeout-minutes: 10
40 | strategy:
41 | matrix:
42 | python-version: [2.7, 3.6, 3.9, "3.10"]
43 | runs-on: ubuntu-latest
44 | container: pypy:${{ matrix.python-version }}
45 | steps:
46 | - uses: actions/checkout@v4
47 | - run: pypy --version
48 | - run: pip install -r requirements.txt
49 | - run: |
50 | pip install black
51 | black . --check
52 | if: matrix.python-version == '3.12'
53 | - run: |
54 | pip install pytest
55 | HTTPBIN=httpbin.bemisc.com pytest
56 | - run: HTTPBIN=httpbin.bemisc.com pypy setup.py test
57 | if: matrix.python-version != '3.12' && matrix.python-version != 'latest'
58 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | *.mmdb
3 |
4 | session.shelve*
5 |
6 | .DS_Store
7 |
8 | /.vscode/settings.json
9 |
10 | net.ca
11 | fs.data
12 |
13 | /.env
14 | /.venv
15 |
16 | /dist
17 | /build
18 | /src/netius.egg-info
19 | /scripts/build/builds
20 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | netius
4 |
5 |
6 |
7 |
8 |
9 | org.python.pydev.PyDevBuilder
10 |
11 |
12 |
13 |
14 |
15 | org.python.pydev.pythonNature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.pydevproject:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | /${PROJECT_DIR_NAME}/src
5 | /${PROJECT_DIR_NAME}/examples/basic
6 | /${PROJECT_DIR_NAME}/examples/echo
7 | /${PROJECT_DIR_NAME}/examples/http
8 |
9 | Default
10 | python 3.5
11 |
12 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding/=utf-8
3 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": "0.2.0",
3 | "configurations": [{
4 | "name": "Python",
5 | "type": "python",
6 | "request": "launch",
7 | "program": "${file}",
8 | "env": {
9 | "PYTHONPATH": "${workspaceRoot}/src:{env.PYTHONPATH}"
10 | }
11 | }]
12 | }
13 |
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include README.rst
2 | recursive-include src/netius/base/extras *
3 | recursive-include src/netius/servers/extras *
4 |
--------------------------------------------------------------------------------
/README.rst:
--------------------------------------------------------------------------------
1 | `Netius `__
2 | ==================================
3 |
4 | Fast and readable async non-blocking network apps
5 |
6 | Netius is a Python network library that can be used for the rapid creation of asynchronous non-blocking
7 | servers and clients. It has no dependencies, it's cross-platform, and brings some sample netius-powered
8 | servers out of the box, namely a production-ready WSGI server.
9 |
10 | Simplicity and performance are the main drivers of this project. The codebase adheres to very strict
11 | code standards, and is extensively commented; and as far as performance is concerned, it aims to
12 | be up to par with equivalent native implementations, where `PyPy `__ can be used to
13 | provide the extra boost to raise performance up to these standards.
14 |
15 | Installation
16 | ------------
17 |
18 | pip install netius
19 |
20 | Usage
21 | -----
22 |
23 | WSGI Server
24 | ~~~~~~~~~~~
25 |
26 | .. code:: python
27 |
28 | import netius.servers
29 |
30 | def app(environ, start_response):
31 | status = "200 OK"
32 | contents = "Hello World"
33 | content_l = len(contents)
34 | headers = (
35 | ("Content-Length", content_l),
36 | ("Content-Type", "text/plain"),
37 | ("Connection", "keep-alive")
38 | )
39 | start_response(status, headers)
40 | yield contents
41 |
42 | server = netius.servers.WSGIServer(app = app)
43 | server.serve(port = 8080)
44 |
45 | More
46 | ----
47 |
48 | For more information consult the `website `__.
49 |
--------------------------------------------------------------------------------
/doc/compat.md:
--------------------------------------------------------------------------------
1 | # Compatibility with asyncio
2 |
3 | As part of the effort to make Netius more compatible with asyncio, the following
4 | changes have been made:
5 |
6 | - The `netius` module provides a `COMPAT` mode that allows it to be used to allows Netius protocols
7 | to be used with asyncio. This mode is enabled by setting the `COMPAT` environment variable to `True`.
8 |
9 | ## Testing
10 |
11 | To run the echo server Protocol implementation using netius run:
12 |
13 | ```bash
14 | PYTHONPATH=. python3 netius/servers/echo.py
15 | ```
16 |
17 | To use the compat version meaning that an asyncio-like interface will be used underneath the hoods use:
18 |
19 | ```bash
20 | COMPAT=1 PYTHONPATH=. python3 netius/servers/echo.py
21 | ```
22 |
23 | To use the compat version and make use of the native asyncio event loop use the following:
24 |
25 | ```bash
26 | COMPAT=1 ASYNCIO=1 PYTHONPATH=. python3 netius/servers/echo.py
27 | ```
28 |
--------------------------------------------------------------------------------
/doc/examples.md:
--------------------------------------------------------------------------------
1 | # Examples
2 |
3 | The following are some example usages of netius:
4 |
5 | ## SMTP Client
6 |
7 | ### Gmail
8 |
9 | ```python
10 | smtp_client = SMTPClient(auto_close = True)
11 | smtp_client.message(
12 | [sender],
13 | [receiver],
14 | contents,
15 | host = "smtp.gmail.com",
16 | port = 587,
17 | username = "username@gmail.com",
18 | password = "password",
19 | stls = True
20 | )
21 | ```
22 |
23 | ### Localhost
24 |
25 | ```python
26 | smtp_client = SMTPClient(auto_close = True)
27 | smtp_client.message(
28 | [sender],
29 | [receiver],
30 | contents,
31 | host = "localhost",
32 | port = 25,
33 | stls = True
34 | )
35 | ```
36 |
--------------------------------------------------------------------------------
/doc/leak.md:
--------------------------------------------------------------------------------
1 | # Memory Leaking
2 |
3 | Memory leaking is one of the major issues when creating a service infra-structure. A correct detection of these
4 | type of problems is important to provide a stable production environment.
5 |
6 | ## Status
7 |
8 | The current Python 2.7 implementation leaks memory under normal usage of the netius HTTP client so using a
9 | Python 3.4+ version is recommended for a deployment/production environment to avoid memory leaking.
10 | The leaking of memory under such environments occurs on the native (Python C) codebase so its leaking is
11 | not traceable by tools like guppy.
12 |
13 | > In 3.x range doesn't create a list, so the test above won't create 10 million int objects. Even if it did, the int type in 3.x is basically a 2.x long, which doesn't implement a freelist.
14 |
15 | ## Notes
16 |
17 | > Long running Python jobs that consume a lot of memory while running may not
18 | > return that memory to the operating system until the process actually
19 | > terminates, even if everything is garbage collected properly. That was news
20 | > to me, but it's true. What this means is that processes that do need to use
21 | > a lot of memory will exhibit a "high water" behavior, where they remain
22 | > forever at the level of memory usage that they required at their peak.
23 |
24 | > Note: this behavior may be Linux specific; there are anecdotal reports that
25 | > Python on Windows does not have this problem.
26 |
27 | > This problem arises from the fact that the Python VM does its own internal
28 | > memory management. It's commonly know as memory fragmentation.
29 | > Unfortunately, there doesn't seem to be any fool-proof method of avoiding
30 | > it.
31 |
32 | ## Utilities
33 |
34 | ### Heapy
35 |
36 | A simple yet powerful utility that provides a mechanism to detect "pending" object between two pre-defined
37 | snapshot positions (time values) and that allows a powerful memory leak detection mechanism.
38 |
39 | #### Example
40 |
41 | ```python
42 | import guppy
43 | heap = guppy.hpy()
44 | heap.setrelheap()
45 |
46 | ...
47 |
48 | state = heap.heap()
49 | print(state)
50 | ```
51 |
52 | ### References
53 |
54 | * [Heapy Tutorial](http://smira.ru/wp-content/uploads/2011/08/heapy.html)
55 | * [Muppy Website](http://pythonhosted.org/Pympler/muppy.html)
56 | * [Diagnosing Memory "Leaks" in Python](http://python.dzone.com/articles/diagnosing-memory-leaks-python)
57 | * [Circular References in Python](http://engineering.hearsaysocial.com/2013/06/16/circular-references-in-python)
58 | * [Memory Usage Presentation (PDF)](https://dmalcolm.fedorapeople.org/presentations/MemoryUsage.pdf)
59 | * [StackOverflow: Which Python memory profiler is recommended?](http://stackoverflow.com/questions/110259/which-python-memory-profiler-is-recommended)
60 | * [StackOverflow: Releasing memory in Python](https://stackoverflow.com/questions/15455048/releasing-memory-in-python)
61 |
--------------------------------------------------------------------------------
/examples/basic/future.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import threading
32 |
33 | import netius
34 |
35 |
36 | def set(future, raise_e=True):
37 | if raise_e:
38 | future.set_exception(Exception("Awaiting error"))
39 | else:
40 | future.set_result(42)
41 |
42 |
43 | @netius.coroutine
44 | def await_forever():
45 | print("Awaiting forever")
46 | future = netius.build_future()
47 | thread = threading.Thread(target=set, args=(future,), kwargs=dict(raise_e=True))
48 | thread.start()
49 | result = yield from future
50 | return result
51 |
52 |
53 | loop = netius.get_loop(_compat=True)
54 | result = loop.run_until_complete(await_forever())
55 | loop.close()
56 |
57 | print(result)
58 |
--------------------------------------------------------------------------------
/examples/basic/future_neo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import threading
32 |
33 | import netius
34 |
35 |
36 | def set(future, raise_e=True):
37 | if raise_e:
38 | future.set_exception(Exception("Awaiting error"))
39 | else:
40 | future.set_result(42)
41 |
42 |
43 | async def await_forever():
44 | print("Awaiting forever")
45 | future = netius.build_future()
46 | thread = threading.Thread(target=set, args=(future,), kwargs=dict(raise_e=True))
47 | thread.start()
48 | return await future
49 |
50 |
51 | loop = netius.get_loop(_compat=True)
52 | result = loop.run_until_complete(await_forever())
53 | loop.close()
54 |
55 | print(result)
56 |
--------------------------------------------------------------------------------
/examples/basic/future_old.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import threading
32 |
33 | import netius
34 |
35 |
36 | def set(future, raise_e=True):
37 | if raise_e:
38 | future.set_exception(Exception("Awaiting error"))
39 | else:
40 | future.set_result(42)
41 |
42 |
43 | @netius.coroutine
44 | def await_forever():
45 | print("Awaiting forever")
46 | future = netius.build_future()
47 | thread = threading.Thread(target=set, args=(future,), kwargs=dict(raise_e=True))
48 | thread.start()
49 | for value in future:
50 | yield value
51 |
52 |
53 | loop = netius.get_loop(_compat=True)
54 | result = loop.run_until_complete(await_forever())
55 | loop.close()
56 |
57 | print(result)
58 |
--------------------------------------------------------------------------------
/examples/basic/loop.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | @netius.coroutine
35 | def compute(x, y):
36 | print("Compute %s + %s ..." % (x, y))
37 | yield from netius.sleep(1.0)
38 | return x + y
39 |
40 |
41 | @netius.coroutine
42 | def print_sum(x, y):
43 | result = yield from compute(x, y)
44 | print("%s + %s = %s" % (x, y, result))
45 |
46 |
47 | loop = netius.get_loop(_compat=True)
48 | loop.run_until_complete(print_sum(1, 2))
49 | loop.close()
50 |
--------------------------------------------------------------------------------
/examples/basic/loop_asyncio.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import asyncio
32 |
33 | import netius
34 |
35 |
36 | @asyncio.coroutine
37 | def compute(x, y):
38 | print("Compute %s + %s ..." % (x, y))
39 | yield from asyncio.sleep(1.0)
40 | return x + y
41 |
42 |
43 | @asyncio.coroutine
44 | def print_sum(x, y):
45 | result = yield from compute(x, y)
46 | print("%s + %s = %s" % (x, y, result))
47 |
48 |
49 | loop = netius.get_loop(_compat=True)
50 | loop.run_until_complete(print_sum(1, 2))
51 | loop.close()
52 |
--------------------------------------------------------------------------------
/examples/basic/loop_neo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | async def compute(x, y):
35 | print("Compute %s + %s ..." % (x, y))
36 | await netius.sleep(1.0)
37 | return x + y
38 |
39 |
40 | async def print_sum(x, y):
41 | result = await compute(x, y)
42 | print("%s + %s = %s" % (x, y, result))
43 |
44 |
45 | loop = netius.get_loop(_compat=True)
46 | loop.run_until_complete(print_sum(1, 2))
47 | loop.close()
48 |
--------------------------------------------------------------------------------
/examples/basic/loop_old.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | @netius.coroutine
35 | def compute(future, x, y):
36 | print("Compute %s + %s ..." % (x, y))
37 | for value in netius.sleep(1.0):
38 | yield value
39 | future.set_result(x + y)
40 |
41 |
42 | @netius.coroutine
43 | def print_sum(x, y):
44 | future = netius.build_future()
45 | for value in compute(future, x, y):
46 | yield value
47 | result = future.result()
48 | print("%s + %s = %s" % (x, y, result))
49 |
50 |
51 | loop = netius.get_loop(_compat=True)
52 | loop.run_until_complete(print_sum(1, 2))
53 | loop.close()
54 |
--------------------------------------------------------------------------------
/examples/basic/sum.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | @netius.coroutine
35 | def compute(x, y):
36 | print("Compute %s + %s ..." % (x, y))
37 | yield from netius.sleep(1.0)
38 | return x + y
39 |
40 |
41 | loop = netius.get_loop(_compat=True)
42 | result = loop.run_until_complete(compute(1, 2))
43 | loop.close()
44 |
45 | print(result)
46 |
--------------------------------------------------------------------------------
/examples/basic/sum_gen.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | async def compute(x, y):
35 | result = None
36 | async for value in _compute(x, y):
37 | result = value
38 | return result
39 |
40 |
41 | async def _compute(x, y):
42 | print("Compute %s + %s ..." % (x, y))
43 | await netius.sleep(1.0)
44 | yield x + y
45 |
46 |
47 | loop = netius.get_loop(_compat=True)
48 | result = loop.run_until_complete(compute(1, 2))
49 | loop.close()
50 |
51 | print(result)
52 |
--------------------------------------------------------------------------------
/examples/basic/sum_mix.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | async def compute(x, y):
35 | return await _compute(x, y)
36 |
37 |
38 | @netius.coroutine
39 | def _compute(x, y):
40 | print("Compute %s + %s ..." % (x, y))
41 | yield from netius.sleep(1.0)
42 | return x + y
43 |
44 |
45 | loop = netius.get_loop(_compat=True)
46 | result = loop.run_until_complete(compute(1, 2))
47 | loop.close()
48 |
49 | print(result)
50 |
--------------------------------------------------------------------------------
/examples/basic/sum_neo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | async def compute(x, y):
35 | print("Compute %s + %s ..." % (x, y))
36 | await netius.sleep(1.0)
37 | return x + y
38 |
39 |
40 | loop = netius.get_loop(_compat=True)
41 | result = loop.run_until_complete(compute(1, 2))
42 | loop.close()
43 |
44 | print(result)
45 |
--------------------------------------------------------------------------------
/examples/echo/echoc_udp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import asyncio
32 |
33 | import netius
34 |
35 |
36 | class EchoClientProtocol(object):
37 |
38 | def __init__(self, message, loop):
39 | self.message = message
40 | self.loop = loop
41 | self.transport = None
42 |
43 | def connection_made(self, transport):
44 | self.transport = transport
45 | print("Send: %s" % self.message)
46 | self.transport.sendto(self.message.encode())
47 |
48 | def datagram_received(self, data, addr):
49 | print("Received: %s" % data.decode())
50 | print("Close the socket")
51 | self.transport.close()
52 |
53 | def error_received(self, exc):
54 | print("Error received: %s" % exc)
55 |
56 | def connection_lost(self, exc):
57 | print("Socket closed, stop the event loop")
58 | loop = asyncio.get_event_loop()
59 | loop.stop()
60 |
61 |
62 | message = "Hello World!"
63 |
64 | loop = netius.get_loop(_compat=True)
65 | connect = loop.create_datagram_endpoint(
66 | lambda: EchoClientProtocol(message, loop), remote_addr=("127.0.0.1", 9999)
67 | )
68 | transport, protocol = loop.run_until_complete(connect)
69 | loop.run_forever()
70 | transport.close()
71 | loop.close()
72 |
--------------------------------------------------------------------------------
/examples/echo/echos_tcp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2018 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2018 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 | import asyncio
34 |
35 |
36 | class EchoServerClientProtocol(asyncio.Protocol):
37 | """
38 | Simple protocol implementation for an echo protocol
39 | that writes back the received message through the
40 | response pipeline. This implementation is inspired by
41 | the Python asyncio documentation example.
42 |
43 | :see: https://docs.python.org/3.6/library/asyncio-protocol.html#protocol-examples
44 | """
45 |
46 | def connection_made(self, transport):
47 | peername = transport.get_extra_info("peername")
48 | print("Connection from %s" % str(peername))
49 | self.transport = transport
50 |
51 | def data_received(self, data):
52 | message = data.decode()
53 | print("Data received: %s" % message)
54 |
55 | print("Sending: %s" % message)
56 | self.transport.write(data)
57 |
58 | print("Closing the client socket")
59 | self.transport.close()
60 |
61 |
62 | loop = netius.get_loop(_compat=True)
63 |
64 | coro = loop.create_server(lambda: EchoServerClientProtocol(), "127.0.0.1", 8888)
65 | server = loop.run_until_complete(coro)
66 |
67 | print("Serving on %s" % (server.sockets[0].getsockname(),))
68 |
69 | try:
70 | loop.run_forever()
71 | except KeyboardInterrupt:
72 | pass
73 |
74 | server.close()
75 | loop.run_until_complete(server.wait_closed())
76 | loop.close()
77 |
--------------------------------------------------------------------------------
/examples/echo/echos_udp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | class EchoServerProtocol(object):
35 |
36 | def connection_made(self, transport):
37 | print("Bind the server connection")
38 | self.transport = transport
39 |
40 | def datagram_received(self, data, addr):
41 | message = data.decode()
42 | print("Received %r from %s" % (message, addr))
43 | print("Send %r to %s" % (message, addr))
44 | self.transport.sendto(data, addr)
45 |
46 |
47 | print("Starting UDP server")
48 |
49 | loop = netius.get_loop(_compat=True)
50 | listen = loop.create_datagram_endpoint(
51 | lambda: EchoServerProtocol(), local_addr=("127.0.0.1", 9999)
52 | )
53 | transport, protocol = loop.run_until_complete(listen)
54 |
55 | try:
56 | loop.run_forever()
57 | except KeyboardInterrupt:
58 | pass
59 |
60 | transport.close()
61 | loop.close()
62 |
--------------------------------------------------------------------------------
/examples/echo/hello_http.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2018 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2018 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 | import asyncio
34 |
35 |
36 | class HelloHTTPServerProtocol(asyncio.Protocol):
37 |
38 | def __init__(self, keep_alive=True):
39 | super().__init__()
40 | self.keep_alive = keep_alive
41 |
42 | def connection_made(self, transport):
43 | self.transport = transport
44 | self.peername = transport.get_extra_info("peername")
45 | print("Connection from %s" % str(self.peername))
46 |
47 | def connection_lost(self, exc):
48 | print("Connection from %s lost (%s)" % (str(self.peername), str(exc)))
49 |
50 | def data_received(self, data):
51 | keep_alive_s = b"keep-alive" if self.keep_alive else b"close"
52 | self.transport.write(
53 | b"HTTP/1.1 200 OK\r\nConnection: %s\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nHello, world!"
54 | % keep_alive_s
55 | )
56 | if not self.keep_alive:
57 | self.transport.close()
58 |
59 |
60 | loop = netius.get_loop(_compat=True)
61 |
62 | coro = loop.create_server(lambda: HelloHTTPServerProtocol(), "127.0.0.1", 8888)
63 | server = loop.run_until_complete(coro)
64 |
65 | print("Serving on %s" % (server.sockets[0].getsockname(),))
66 |
67 | try:
68 | loop.run_forever()
69 | except KeyboardInterrupt:
70 | pass
71 |
72 | server.close()
73 | loop.run_until_complete(server.wait_closed())
74 | loop.close()
75 |
--------------------------------------------------------------------------------
/examples/http/http_aiohttp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import asyncio
32 | import aiohttp
33 |
34 | import netius
35 |
36 | netius.verify(
37 | int(aiohttp.__version__[0]) < 3,
38 | message="Requires legacy (2.x.x or older) version of aiohttp",
39 | )
40 |
41 |
42 | @asyncio.coroutine
43 | def print_http(url):
44 | response = yield from aiohttp.request("GET", url)
45 | print(response.status)
46 | data = yield from response.read()
47 | print(data)
48 |
49 |
50 | loop = netius.get_loop(_compat=True)
51 | loop.run_until_complete(print_http("https://www.flickr.com/"))
52 | loop.close()
53 |
--------------------------------------------------------------------------------
/examples/http/http_aiohttp_neo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import aiohttp
32 |
33 | import netius
34 |
35 |
36 | async def print_http(session, url):
37 | async with session.get(url) as response:
38 | print(response.status)
39 | data = await response.read()
40 | print(data)
41 |
42 |
43 | async def go(loop, url):
44 | async with aiohttp.ClientSession(loop=loop) as session:
45 | await print_http(session, url)
46 |
47 |
48 | loop = netius.get_loop(_compat=True)
49 | loop.run_until_complete(go(loop, "https://www.flickr.com/"))
50 | loop.close()
51 |
--------------------------------------------------------------------------------
/examples/http/http_asyncio.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import asyncio
32 |
33 | import urllib.parse
34 |
35 | import netius
36 |
37 |
38 | @asyncio.coroutine
39 | def print_http_headers(url, encoding="utf-8"):
40 | url = urllib.parse.urlsplit(url)
41 |
42 | if url.scheme == "https":
43 | connect = asyncio.open_connection(url.hostname, url.port or 443, ssl=True)
44 | else:
45 | connect = asyncio.open_connection(url.hostname, url.port or 80)
46 |
47 | reader, writer = yield from connect
48 | query = "HEAD {path} HTTP/1.1\r\nConnection: keep-alive\r\nHost: {hostname}\r\n\r\n"
49 | query = query.format(path=url.path or "/", hostname=url.hostname)
50 | writer.write(query.encode(encoding))
51 |
52 | while True:
53 | line = yield from reader.readline()
54 | if not line:
55 | break
56 | line = line.decode(encoding).rstrip()
57 | if line:
58 | print(line)
59 | else:
60 | break
61 |
62 | writer.close()
63 |
64 |
65 | loop = netius.get_loop(_compat=True)
66 | task = asyncio.ensure_future(print_http_headers("https://www.flickr.com/"))
67 | loop.run_until_complete(task)
68 | loop.close()
69 |
--------------------------------------------------------------------------------
/examples/http/http_players.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 |
33 | import asyncio
34 | import aiofiles
35 | import aiohttp
36 |
37 | import netius
38 |
39 | BASE_URL = "http://stats.nba.com/stats"
40 | HEADERS = {
41 | "user-agent": (
42 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) "
43 | "AppleWebKit/537.36 (KHTML, like Gecko) "
44 | "Chrome/45.0.2454.101 Safari/537.36"
45 | ),
46 | "connection": ("keep-alive"),
47 | }
48 |
49 |
50 | async def get_players(player_args, season="2016-17"):
51 | endpoint = "/commonallplayers"
52 |
53 | params = dict(season=season, leagueid="00", isonlycurrentseason="1")
54 | url = BASE_URL + endpoint
55 |
56 | print("Getting all players for season %s ..." % season)
57 |
58 | async with aiohttp.ClientSession() as session:
59 | async with session.get(url, headers=HEADERS, params=params) as resp:
60 | data = await resp.json()
61 |
62 | player_args.extend([(item[0], item[2]) for item in data["resultSets"][0]["rowSet"]])
63 |
64 |
65 | async def get_player(player_id, player_name):
66 | endpoint = "/commonplayerinfo"
67 | params = dict(playerid=player_id)
68 | url = BASE_URL + endpoint
69 |
70 | print("Getting player %s" % player_name)
71 |
72 | async with aiohttp.ClientSession() as session:
73 | async with session.get(url, headers=HEADERS, params=params) as resp:
74 | data = await resp.text()
75 | print(data)
76 |
77 | async with aiofiles.open(
78 | "players/%s.json" % player_name.replace(" ", "_"), "w"
79 | ) as file:
80 | await file.write(data)
81 |
82 |
83 | loop = netius.get_loop(_compat=True)
84 |
85 | os.makedirs("players", exist_ok=True)
86 |
87 | player_args = []
88 | loop.run_until_complete(get_players(player_args))
89 | loop.run_until_complete(asyncio.gather(*(get_player(*args) for args in player_args)))
90 | loop.close()
91 |
--------------------------------------------------------------------------------
/pylintrc:
--------------------------------------------------------------------------------
1 | [messages control]
2 | disable=C0103,C0111,C0121,C0123,C0301,C0302,C0321,C0325,C0326,C0330,C0412,C0413,E1128,W0102,W0106,W0108,W0150,W0201,W0212,W0221,W0401,W0603,W0613,W0621,W0622,W0702,W1113
3 |
--------------------------------------------------------------------------------
/pytest.ini:
--------------------------------------------------------------------------------
1 | [pytest]
2 | python_files = *.py
3 | testpaths = src/netius/test
4 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/requirements.txt
--------------------------------------------------------------------------------
/res/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/res/favicon.ico
--------------------------------------------------------------------------------
/res/favicon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/res/favicon.png
--------------------------------------------------------------------------------
/res/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/res/icon.png
--------------------------------------------------------------------------------
/res/logo-2x.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/res/logo-2x.png
--------------------------------------------------------------------------------
/res/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/res/logo.png
--------------------------------------------------------------------------------
/scripts/build/all/build.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | import os
5 |
6 | import atm
7 |
8 | def build(file = None):
9 | # runs the initial assertion for the various commands
10 | # that are mandatory for execution, this should avoid
11 | # errors in the middle of the build
12 | atm.assert_c(("git", "python --version"))
13 |
14 | # starts the build process with the configuration file
15 | # that was provided to the configuration script
16 | atm.build(file, arch = "all")
17 |
18 | # retrieves the various values from the global configuration
19 | # that are going to be used around the configuration
20 | name_ver = atm.conf("name_ver")
21 | name_src = atm.conf("name_src")
22 |
23 | # creates the various paths to the folders to be used
24 | # for the build operation, from the ones already loaded
25 | repo_f = atm.path("repo")
26 | result_f = atm.path("result")
27 | tmp_f = atm.path("tmp")
28 | dist_f = atm.path("dist")
29 |
30 | # clones the current repository using the git command and then
31 | # copies the resulting directory to the result and temporary
32 | # directories, to be used latter in the build
33 | atm.git(clean = True)
34 | atm.copy(repo_f, result_f)
35 | atm.copy(repo_f, os.path.join(tmp_f, name_src))
36 |
37 | # changes the current directory to the repository one and runs
38 | # the python tests and source build on it, then copies the
39 | # resulting source build file to the distribution directory
40 | os.chdir(repo_f)
41 | atm.pytest()
42 | atm.pysdist()
43 | atm.copy(os.path.join("dist", name_ver + ".zip"), dist_f)
44 |
45 | # creates the various hash files for the complete set of files in
46 | # the distribution directory
47 | os.chdir(dist_f)
48 | atm.hash_d()
49 |
50 | def run():
51 | # parses the various arguments provided by the
52 | # command line and retrieves it defaulting to
53 | # pre-defined values in case they do not exist
54 | arguments = atm.parse_args(names = ())
55 | file = arguments.get("file", None)
56 |
57 | # starts the build process with the parameters
58 | # retrieved from the current environment
59 | build(
60 | file = file
61 | )
62 |
63 | def cleanup():
64 | atm.cleanup()
65 |
66 | if __name__ == "__main__":
67 | try: run()
68 | finally: cleanup()
69 | else:
70 | __path__ = []
71 |
--------------------------------------------------------------------------------
/scripts/build/build.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "netius",
3 | "version": "0.1.19",
4 | "description": "Netius System",
5 | "author": "Hive Solutions ",
6 | "repo": "git@bitbucket.org:hivesolutions/netius.git",
7 | "scripts": {
8 | "*": "all/build.py"
9 | },
10 | "recursion": [0, 0, 1, 0, 0]
11 | }
12 |
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [bdist_wheel]
2 | universal = 1
3 |
--------------------------------------------------------------------------------
/src/netius/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import adapters
29 | from . import auth
30 | from . import base
31 |
32 | from .adapters import *
33 | from .auth import *
34 | from .base import *
35 |
--------------------------------------------------------------------------------
/src/netius/adapters/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import base
29 | from . import fs
30 | from . import memory
31 | from . import mongo
32 | from . import null
33 |
34 | from .base import BaseAdapter
35 | from .fs import FsAdapter
36 | from .memory import MemoryAdapter
37 | from .mongo import MongoAdapter
38 | from .null import NullAdapter
39 |
--------------------------------------------------------------------------------
/src/netius/adapters/base.pyi:
--------------------------------------------------------------------------------
1 | from typing import Any, BinaryIO, Sequence
2 |
3 | class BaseAdapter:
4 | def set(self, value: Any, owner: str = ...): ...
5 | def get(self, key: str) -> bytes | None: ...
6 | def get_file(self, key: str, mode: str = ...) -> BinaryIO: ...
7 | def delete(self, key: str, owner: str = ...): ...
8 | def append(self, key: str, value: Any): ...
9 | def truncate(self, key: str, count: int): ...
10 | def size(self, key: str) -> int: ...
11 | def sizes(self, owner: str | None = ...) -> list[int]: ...
12 | def total(self, owner: str | None = ...) -> int: ...
13 | def reserve(self, owner: str = ...): ...
14 | def count(self, owner: str | None = ...) -> int: ...
15 | def list(self, owner: str | None = ...) -> Sequence[str]: ...
16 | def generate(self) -> str: ...
17 |
--------------------------------------------------------------------------------
/src/netius/adapters/fs.pyi:
--------------------------------------------------------------------------------
1 | from os import PathLike
2 |
3 | from netius import BaseAdapter
4 |
5 | class FsAdapter(BaseAdapter):
6 | base_path: str
7 |
8 | def __init__(self, base_path: str | None = ...): ...
9 | def _path(self, owner: str | None = ...) -> str: ...
10 | def _ensure(self, owner: str) -> str: ...
11 | def _symlink(self, source: PathLike[str], target: PathLike[str]): ...
12 |
--------------------------------------------------------------------------------
/src/netius/adapters/memory.pyi:
--------------------------------------------------------------------------------
1 | from typing import Any, BinaryIO, Callable, Mapping
2 |
3 | from netius import BaseAdapter
4 |
5 | class MemoryAdapter(BaseAdapter):
6 | def __init__(self): ...
7 | def _ensure(self, owner: str) -> Mapping[str, Any]: ...
8 | def _build_close(self, file: BinaryIO, key: str) -> Callable[[], None]: ...
9 |
--------------------------------------------------------------------------------
/src/netius/adapters/mongo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 |
34 | class MongoAdapter(base.BaseAdapter):
35 |
36 | def set(self, value, owner="nobody"):
37 | pass
38 |
39 | def get(self, key):
40 | pass
41 |
--------------------------------------------------------------------------------
/src/netius/adapters/mongo.pyi:
--------------------------------------------------------------------------------
1 | from netius import BaseAdapter
2 |
3 | class MongoAdapter(BaseAdapter):
4 | pass
5 |
--------------------------------------------------------------------------------
/src/netius/adapters/null.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 |
34 | class NullAdapter(base.BaseAdapter):
35 | pass
36 |
--------------------------------------------------------------------------------
/src/netius/adapters/null.pyi:
--------------------------------------------------------------------------------
1 | from netius import BaseAdapter
2 |
3 | class NullAdapter(BaseAdapter):
4 | pass
5 |
--------------------------------------------------------------------------------
/src/netius/auth/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import address
29 | from . import allow
30 | from . import base
31 | from . import deny
32 | from . import dummy
33 | from . import memory
34 | from . import passwd
35 | from . import simple
36 |
37 | from .address import AddressAuth
38 | from .allow import AllowAuth
39 | from .base import Auth
40 | from .deny import DenyAuth
41 | from .dummy import DummyAuth
42 | from .memory import MemoryAuth
43 | from .passwd import PasswdAuth
44 | from .simple import SimpleAuth
45 |
--------------------------------------------------------------------------------
/src/netius/auth/address.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 |
34 | class AddressAuth(base.Auth):
35 |
36 | def __init__(self, allowed=[], *args, **kwargs):
37 | base.Auth.__init__(self, *args, **kwargs)
38 | self.allowed = allowed
39 |
40 | @classmethod
41 | def auth(cls, allowed=[], *args, **kwargs):
42 | import netius.common
43 |
44 | host = kwargs.get("host", None)
45 | headers = kwargs.get("headers", {})
46 | if not host and not headers:
47 | return False
48 | address = headers.get("X-Forwarded-For", host)
49 | address = headers.get("X-Client-IP", address)
50 | address = headers.get("X-Real-IP", address)
51 | address = address.split(",", 1)[0].strip()
52 | return netius.common.assert_ip4(address, allowed, default=False)
53 |
54 | @classmethod
55 | def is_simple(cls):
56 | return True
57 |
58 | def auth_i(self, *args, **kwargs):
59 | return self.__class__.auth(allowed=self.allowed, *args, **kwargs)
60 |
--------------------------------------------------------------------------------
/src/netius/auth/address.pyi:
--------------------------------------------------------------------------------
1 | from typing import Sequence
2 |
3 | from netius import Auth
4 |
5 | class AddressAuth(Auth):
6 | def __init__(self, allowed: Sequence[str] = ..., *args, **kwargs): ...
7 | @classmethod
8 | def auth(cls, allowed: Sequence[str] = ..., *args, **kwargs) -> bool: ...
9 |
--------------------------------------------------------------------------------
/src/netius/auth/allow.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 |
34 | class AllowAuth(base.Auth):
35 |
36 | @classmethod
37 | def auth(cls, *args, **kwargs):
38 | return True
39 |
40 | @classmethod
41 | def is_simple(cls):
42 | return True
43 |
--------------------------------------------------------------------------------
/src/netius/auth/allow.pyi:
--------------------------------------------------------------------------------
1 | from netius import Auth
2 |
3 | class AllowAuth(Auth):
4 | pass
5 |
--------------------------------------------------------------------------------
/src/netius/auth/base.pyi:
--------------------------------------------------------------------------------
1 | from os import PathLike
2 | from typing import Any, Literal, Mapping, NoReturn
3 |
4 | HashType = Literal["plain", "md5", "sha1", "sha256", "sha512"]
5 |
6 | class Auth:
7 | def __init__(self, *args, **kwargs): ...
8 | @classmethod
9 | def auth(cls, *args, **kwargs) -> bool: ...
10 | @classmethod
11 | def meta(cls, *args, **kwargs) -> Mapping[str, Any]: ...
12 | @classmethod
13 | def auth_assert(cls, *args, **kwargs) -> NoReturn: ...
14 | @classmethod
15 | def verify(cls, encoded: str, decoded: str) -> bool: ...
16 | @classmethod
17 | def generate(cls, password: str, type: HashType = ..., salt: str = ...) -> str: ...
18 | @classmethod
19 | def unpack(cls, password) -> tuple[HashType, str | None, str, str | None]: ...
20 | @classmethod
21 | def get_file(
22 | cls, path: PathLike[str], cache: bool = ..., encoding: str | None = ...
23 | ) -> str | bytes: ...
24 | @classmethod
25 | def is_simple(cls) -> bool: ...
26 | def auth_i(self, *args, **kwargs) -> bool: ...
27 | def auth_assert_i(self, *args, **kwargs) -> NoReturn: ...
28 | def is_simple_i(self) -> bool: ...
29 |
--------------------------------------------------------------------------------
/src/netius/auth/deny.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 |
34 | class DenyAuth(base.Auth):
35 |
36 | @classmethod
37 | def auth(cls, *args, **kwargs):
38 | return False
39 |
40 | @classmethod
41 | def is_simple(cls):
42 | return True
43 |
--------------------------------------------------------------------------------
/src/netius/auth/deny.pyi:
--------------------------------------------------------------------------------
1 | from netius import Auth
2 |
3 | class DenyAuth(Auth):
4 | pass
5 |
--------------------------------------------------------------------------------
/src/netius/auth/dummy.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 |
34 | class DummyAuth(base.Auth):
35 |
36 | def __init__(self, value=True, *args, **kwargs):
37 | base.Auth.__init__(self, *args, **kwargs)
38 | self.value = value
39 |
40 | @classmethod
41 | def auth(cls, value=True, *args, **kwargs):
42 | return value
43 |
44 | @classmethod
45 | def is_simple(cls):
46 | return True
47 |
48 | def auth_i(self, *args, **kwargs):
49 | return self.__class__.auth(value=self.value, *args, **kwargs)
50 |
--------------------------------------------------------------------------------
/src/netius/auth/dummy.pyi:
--------------------------------------------------------------------------------
1 | from netius import Auth
2 |
3 | class DummyAuth(Auth):
4 | def __init__(self, value: bool = ..., *args, **kwargs): ...
5 | @classmethod
6 | def auth(cls, value: bool = ..., *args, **kwargs) -> bool: ...
7 |
--------------------------------------------------------------------------------
/src/netius/auth/memory.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | import netius
29 |
30 | from . import base
31 |
32 |
33 | class MemoryAuth(base.Auth):
34 |
35 | def __init__(self, registry=None, *args, **kwargs):
36 | base.Auth.__init__(self, *args, **kwargs)
37 | self.registry = registry
38 |
39 | @classmethod
40 | def auth(cls, username, password, registry=None, *args, **kwargs):
41 | registry = registry or cls.get_registry()
42 | if not registry:
43 | return False
44 | register = registry.get(username, None)
45 | if not register:
46 | return False
47 | _password = register.get("password")
48 | return cls.verify(_password, password)
49 |
50 | @classmethod
51 | def meta(cls, username, registry=None, *args, **kwargs):
52 | registry = registry or cls.get_registry()
53 | if not registry:
54 | return {}
55 | register = registry.get(username, {})
56 | return register
57 |
58 | @classmethod
59 | def get_registry(cls):
60 | if hasattr(cls, "registry"):
61 | return cls.registry
62 | cls.registry = cls.load_registry()
63 | return cls.registry
64 |
65 | @classmethod
66 | def load_registry(cls):
67 | return netius.conf("REGISTRY", {})
68 |
69 | def auth_i(self, username, password, *args, **kwargs):
70 | return self.__class__.auth(
71 | username, password, registry=self.registry, *args, **kwargs
72 | )
73 |
--------------------------------------------------------------------------------
/src/netius/auth/memory.pyi:
--------------------------------------------------------------------------------
1 | from typing import Any, Mapping
2 |
3 | from netius import Auth
4 |
5 | class MemoryAuth(Auth):
6 | def __init__(self, registry: Mapping[str, Any] | None = ..., *args, **kwargs): ...
7 | @classmethod
8 | def auth(
9 | cls,
10 | username: str,
11 | password: str,
12 | registry: Mapping[str, Any] | None = ...,
13 | *args,
14 | **kwargs
15 | ) -> bool: ...
16 | @classmethod
17 | def meta(
18 | cls, username: str, registry: Mapping[str, Any] | None = ..., *args, **kwargs
19 | ) -> Mapping[str, Any]: ...
20 | @classmethod
21 | def get_registry(cls) -> Mapping[str, Any]: ...
22 | @classmethod
23 | def load_registry(cls) -> Mapping[str, Any]: ...
24 |
--------------------------------------------------------------------------------
/src/netius/auth/passwd.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 |
33 | from . import base
34 |
35 |
36 | class PasswdAuth(base.Auth):
37 |
38 | def __init__(self, path=None, *args, **kwargs):
39 | base.Auth.__init__(self, *args, **kwargs)
40 | self.path = path
41 |
42 | @classmethod
43 | def auth(cls, username, password, path="passwd", *args, **kwargs):
44 | passwd = cls.get_passwd(path)
45 | _password = passwd.get(username, None)
46 | if not _password:
47 | return False
48 | return cls.verify(_password, password)
49 |
50 | @classmethod
51 | def get_passwd(cls, path, cache=True):
52 | path = os.path.expanduser(path)
53 | path = os.path.abspath(path)
54 | path = os.path.normpath(path)
55 |
56 | if not hasattr(cls, "_pwcache"):
57 | cls._pwcache = dict()
58 |
59 | result = cls._pwcache.get(path, None) if hasattr(cls, "_pwcache") else None
60 | if cache and not result == None:
61 | return result
62 |
63 | htpasswd = dict()
64 | contents = cls.get_file(path, cache=cache, encoding="utf-8")
65 | for line in contents.split("\n"):
66 | line = line.strip()
67 | if not line:
68 | continue
69 | username, password = line.split(":", 1)
70 | htpasswd[username] = password
71 |
72 | if cache:
73 | cls._pwcache[path] = htpasswd
74 | return htpasswd
75 |
76 | def auth_i(self, username, password, *args, **kwargs):
77 | return self.__class__.auth(username, password, path=self.path, *args, **kwargs)
78 |
--------------------------------------------------------------------------------
/src/netius/auth/passwd.pyi:
--------------------------------------------------------------------------------
1 | from os import PathLike
2 | from typing import Mapping
3 |
4 | from netius import Auth
5 |
6 | class PasswdAuth(Auth):
7 | def __init__(self, path: PathLike[str] | None = ..., *args, **kwargs): ...
8 | @classmethod
9 | def auth(
10 | cls, username: str, password: str, path: str = ..., *args, **kwargs
11 | ) -> bool: ...
12 | @classmethod
13 | def get_passwd(
14 | cls, path: PathLike[str], cache: bool = ...
15 | ) -> Mapping[str, str]: ...
16 | def auth_i(self, username: str, password: str, *args, **kwargs) -> bool: ...
17 |
--------------------------------------------------------------------------------
/src/netius/auth/simple.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import base
29 |
30 |
31 | class SimpleAuth(base.Auth):
32 |
33 | def __init__(self, username=None, password=None, *args, **kwargs):
34 | base.Auth.__init__(self, *args, **kwargs)
35 | self.username = username
36 | self.password = password
37 |
38 | @classmethod
39 | def auth(cls, username, password, target=None, *args, **kwargs):
40 | if not target:
41 | return False
42 | _username, _password = target
43 | if _username and not username == _username:
44 | return False
45 | if not password == _password:
46 | return False
47 | return True
48 |
49 | def auth_i(self, username, password, *args, **kwargs):
50 | return self.__class__.auth(
51 | username, password, target=(self.username, self.password), *args, **kwargs
52 | )
53 |
--------------------------------------------------------------------------------
/src/netius/auth/simple.pyi:
--------------------------------------------------------------------------------
1 | from typing import Sequence
2 |
3 | from netius import Auth
4 |
5 | class SimpleAuth(Auth):
6 | def __init__(
7 | self, username: str | None = ..., password: str | None = ..., *args, **kwargs
8 | ): ...
9 | @classmethod
10 | def auth(
11 | cls,
12 | username: str,
13 | password: str,
14 | target: Sequence[str, str] | None = ...,
15 | *args,
16 | **kwargs
17 | ) -> bool: ...
18 | def auth_i(self, username: str, password: str, *args, **kwargs) -> bool: ...
19 |
--------------------------------------------------------------------------------
/src/netius/base/agent.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import threading
32 |
33 | from . import legacy
34 | from . import observer
35 |
36 |
37 | class Agent(observer.Observable):
38 | """
39 | Top level class for the entry point classes of the multiple
40 | client and server protocol implementations.
41 |
42 | These classes should contain a series of utilities that facilitate
43 | the interaction with the Protocol, Event Loop and Transport
44 | objects (with the end developer in mind).
45 |
46 | Most of the interaction for a simple protocol should be implemented
47 | using static or class methods, avoiding internal object state and
48 | instantiation of the concrete Agent class.
49 |
50 | For complex protocols instantiation may be useful to provided extra
51 | flexibility and context for abstract operations.
52 | """
53 |
54 | @classmethod
55 | def cleanup_s(cls):
56 | pass
57 |
58 | def cleanup(self, destroy=True):
59 | if destroy:
60 | self.destroy()
61 |
62 | def destroy(self):
63 | observer.Observable.destroy(self)
64 |
65 |
66 | class ClientAgent(Agent):
67 |
68 | _clients = dict()
69 | """ The global static clients map meant to be reused by the
70 | various static clients that may be created, this client
71 | may leak creating blocking threads that will prevent the
72 | system from exiting correctly, in order to prevent that
73 | the cleanup method should be called """
74 |
75 | @classmethod
76 | def cleanup_s(cls):
77 | super(ClientAgent, cls).cleanup_s()
78 | for client in legacy.itervalues(cls._clients):
79 | client.close()
80 | cls._clients.clear()
81 |
82 | @classmethod
83 | def get_client_s(cls, *args, **kwargs):
84 | tid = threading.current_thread().ident
85 | client = cls._clients.get(tid, None)
86 | if client:
87 | return client
88 | client = cls(*args, **kwargs)
89 | cls._clients[tid] = client
90 | return client
91 |
92 |
93 | class ServerAgent(Agent):
94 | pass
95 |
--------------------------------------------------------------------------------
/src/netius/base/asynchronous.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | # imports the base (old) version of the async implementation
32 | # that should be compatible with all the available python
33 | # interpreters, base collection of async library
34 | from .async_old import * # @UnusedWildImport pylint: disable=W0614
35 |
36 | # verifies if the current python interpreter version supports
37 | # the new version of the async implementation and if that's the
38 | # case runs the additional import of symbols, this should override
39 | # most of the symbols that have just been created
40 | if is_neo():
41 | from .async_neo import * # @UnusedWildImport pylint: disable=W0614
42 |
--------------------------------------------------------------------------------
/src/netius/base/diag.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 | import logging
33 |
34 | try:
35 | import appier
36 |
37 | loaded = True
38 | except ImportError:
39 | import netius.mock
40 |
41 | appier = netius.mock.appier
42 | loaded = False
43 |
44 |
45 | class DiagApp(appier.APIApp):
46 |
47 | def __init__(self, system, *args, **kwargs):
48 | appier.APIApp.__init__(self, name="diag", *args, **kwargs)
49 | self.system = system
50 |
51 | @appier.route("/logger", "GET")
52 | def show_logger(self):
53 | level = self.system.logger.level
54 | level = logging.getLevelName(level)
55 | return dict(level=level)
56 |
57 | @appier.route("/logger/set", ("GET", "POST"))
58 | def set_logger(self):
59 | level = self.field("level", "DEBUG")
60 | self.system.level_logging(level)
61 | return self.show_logger()
62 |
63 | @appier.route("/environ", "GET")
64 | def show_environ(self):
65 | return self.json(dict(os.environ), sort_keys=True)
66 |
67 | @appier.route("/info", "GET")
68 | def system_info(self):
69 | full = self.field("full", True, cast=bool)
70 | info = self.system.info_dict(full=full)
71 | return self.json(info, sort_keys=True)
72 |
73 | @appier.route("/connections", "GET")
74 | def list_connections(self):
75 | full = self.field("full", True, cast=bool)
76 | info = self.system.connections_dict(full=full)
77 | return self.json(info, sort_keys=True)
78 |
79 | @appier.route("/connections/", "GET")
80 | def show_connection(self, id):
81 | full = self.field("full", True, cast=bool)
82 | info = self.system.connection_dict(id, full=full)
83 | return self.json(info, sort_keys=True)
84 |
--------------------------------------------------------------------------------
/src/netius/base/extras/dh.pem:
--------------------------------------------------------------------------------
1 | -----BEGIN DH PARAMETERS-----
2 | MIIBCAKCAQEAhOcLnuUOpheBzs34EQ2HB1XsgZ182PsK5KEKd61tn4H1l/8b0Jm/
3 | v3qoAkff1S3/8ASywTaoHRcpoWYuytdEjQFdRnS6U/4du2uEG7YIShZgasXCRp/F
4 | 2aKOKsNUEEpHWn154/3t5TB7+Ny6BM9DG+FIxIEsdJnHB2zDAPmuKjnY4dhSx/il
5 | eIIBDWG6uROQ344a2sgYeTJt/70fa+oSbTvV2Mev7clhFGyclHMjG17U35p5bZcG
6 | J+1uREotV5mRjz8+XHq9SP8GN4G3dMcpF0WtBG02slcybx5aXL3jWZeg2qWwk9uO
7 | OVR11hlb0RoQQmnG86trdRHO+/i3ORfVGwIBAg==
8 | -----END DH PARAMETERS-----
9 |
--------------------------------------------------------------------------------
/src/netius/base/extras/net.cer:
--------------------------------------------------------------------------------
1 | -----BEGIN CERTIFICATE-----
2 | MIIDBzCCAe+gAwIBAgIJAPYCBHOVjTxIMA0GCSqGSIb3DQEBCwUAMBkxFzAVBgNV
3 | BAMMDm5ldGl1cy5oaXZlLnB0MCAXDTE5MDczMTE4MzUzMloYDzIxMTkwNzA3MTgz
4 | NTMyWjAZMRcwFQYDVQQDDA5uZXRpdXMuaGl2ZS5wdDCCASIwDQYJKoZIhvcNAQEB
5 | BQADggEPADCCAQoCggEBAMm1c2PYQ9QL6QdD1JRnm39+KD4kcLjXw5oqRJrQc7+L
6 | DaF3MQQuz8IE35buYw54HZexWjHf8mX6D+vtvmAw176EWeeHkaMIz9mZNYG8job6
7 | UK+L5S5sf2zNjjaVO/fTWS1W7Hg2abIwn/KdpKVDqRyZsCU5UIyCEcPFtPq5bnbN
8 | KJndnzveJucoPrgREUdvi0AOxMd0IFymsIX2ai50LJn5MHF+Z7S7Wy6y/WeRBHaU
9 | 330QAXX00LR4vGpnxA33cliMz6Vs5I+TV4UVaNs9AH43ofO84VMS+ZxpYHwjEVCW
10 | YYegVkJlQ+4BhkOdAkKQ1ILxlSfgenYTMXcmmqbBh2cCAwEAAaNQME4wHQYDVR0O
11 | BBYEFFmH119MJQryHVraLFk9db8K4PgzMB8GA1UdIwQYMBaAFFmH119MJQryHVra
12 | LFk9db8K4PgzMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAH/HOHbV
13 | B7vV0dcygSIhjY4ooppAI2D3Qsf5b03z7Gpol2rhyVeXv5Dpz1fVl++ZX7qPSP+9
14 | HSMklVkcY2bRIrqIq41RIim99gXMuBQpZT9J9V56dIFZIsBKVnpnVdR0+R+YNWGh
15 | BHSwocrZWYP9wQ4cJR5xRka8Hei9KTp+pws+qL2cY/2FDKN4mkKMUsNS9y2sd5P1
16 | 95vuAIPGR0SiO66KJpTUt7pE4/AwTeBqKnx/6KdI/v7bLEBgojMcUsTlj+yK/OJ1
17 | LIeYwOL1IPkec3kSTvQwK0NsR6eCWCyp+DhsZFDHG1cGDOSqSAQgfcXTdUXRbOor
18 | 49RFDkSayAHsPv0=
19 | -----END CERTIFICATE-----
20 |
--------------------------------------------------------------------------------
/src/netius/base/extras/net.key:
--------------------------------------------------------------------------------
1 | -----BEGIN RSA PRIVATE KEY-----
2 | MIIEpAIBAAKCAQEAybVzY9hD1AvpB0PUlGebf34oPiRwuNfDmipEmtBzv4sNoXcx
3 | BC7PwgTflu5jDngdl7FaMd/yZfoP6+2+YDDXvoRZ54eRowjP2Zk1gbyOhvpQr4vl
4 | Lmx/bM2ONpU799NZLVbseDZpsjCf8p2kpUOpHJmwJTlQjIIRw8W0+rluds0omd2f
5 | O94m5yg+uBERR2+LQA7Ex3QgXKawhfZqLnQsmfkwcX5ntLtbLrL9Z5EEdpTffRAB
6 | dfTQtHi8amfEDfdyWIzPpWzkj5NXhRVo2z0Afjeh87zhUxL5nGlgfCMRUJZhh6BW
7 | QmVD7gGGQ50CQpDUgvGVJ+B6dhMxdyaapsGHZwIDAQABAoIBAQDAhYfVTJ2wzo1k
8 | ecF0xE8OqQMQDQfp5Ua05pMEkOFpePdRncoTC/sQXEZscKvrK4pTNtu3ruBVpQ99
9 | SDXk6bmCBiEpc6P10HtSXYFMiwQeoNxSMVxVqwkUeD28q0PJjtgXBlg9hPHtOavw
10 | jbaQZrwKqEYPwVy4P41lJldO154vlH3UwuRULqK/cZNpynMVimfgUTb8sYDafAFg
11 | WHR25VKJ3Vnx+pjkYeGKpBV2SKa87IvmKRXTol0rqfUhYbSrk7ZGrv5SoH2nfYbw
12 | coErU0Db3uXt7AbkozTpqKsBfe1GnLdMY2TxU+VerYL7SrW5+8G2tvLGmYKKmoY5
13 | fnQHriSBAoGBAPpJio+tj6ILA7v7LyDNbNq7tJCLQuMZGU5IL9w8KvhLZPgyu2Zl
14 | 6/G9fQ9gr+sCYd/l+ndfSIAZ/8aphLwVVwrHnxXzQ7qIIDkKDvYlA1bEMSkeG7iH
15 | 1xh1EwLg89k5YVFBZZp8lvB/bpkchmhLEB38UtqA/h0PQzlUkesZImRpAoGBAM5Q
16 | D0AMejt2h8TcQcjLetNwtBSM9je/Qn1l+UhwwZYS/cKkoeR3qMlJ5hMcymO4SVti
17 | WeLDk4bc6DW3GiRNllh2fDm0p2lRKzoOe9Etg7SZpUpB5SzJGN+P69dDE0N3QrE5
18 | MAWVccXqdAMmtHDCfUr2XI3OnS3P/Np8/dv1EtNPAoGANEw1lbwQbS2cBCWCPXpv
19 | Km1aV3Gh8k4GSaMvzPcNi4BFgs8m9UiKuqJ7hUEQwSxoQx6M7XCNd6b3GQ3G3H4h
20 | B/GaPj3sgwd6pDCD2870Gac5FnxiEnbf7Q5ZgJp8mYEx4oalslgiOfKGq5uYQiad
21 | IYbd0SZu8qeDxnDzZIqF8zECgYB7QUcrG4k21yRbjr7aZ/4ULePX1zMNsPzXD8Hy
22 | 7jIpJ2VlEJgLdxVmU/jY+D1Nf/6LUuGiYPdU/crYtHAwug2VAgxLKUtO1wg9IgQE
23 | vu1NSvF9wIpazDVI8qGSM+dhSfGVETZIT7Tt6ZivfUMVjovt6er3aTVr9jL4kO3U
24 | p8A8bwKBgQDuKwlxiMnokdFtonxotdQT5tECr3rGdBd7rnu17sXN/I/E1MTAf2hI
25 | AeIGx/id9Wd3xBn0HlpBOa475q0Le42lBqGa4JWycrGuFFz9EbjsJ/uXSeRuVdLB
26 | Vvko3Dauqo/O/Kn4mLN2ZqIEWpSKUr3COTCh07qmUXEaZOpFUx3sTQ==
27 | -----END RSA PRIVATE KEY-----
28 |
--------------------------------------------------------------------------------
/src/netius/base/extras/net.pub:
--------------------------------------------------------------------------------
1 | -----BEGIN PUBLIC KEY-----
2 | MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8C8x6ai1tgiPsWM8RhWeB/T1M
3 | 6Nh+EdBc1WGssEi6NvJwjCB1f9/F14rq0JweHA7n8CNmBJFdcx8/GYf5TJBEX9R7
4 | YDGy0EswjCm4C+IDuSMBmi5caXYJoTouJ7OA7ghifSF22T288ldEgKyIlSfzFSRA
5 | d2b0NSlskCNqSpAjNQIDAQAB
6 | -----END PUBLIC KEY-----
7 |
--------------------------------------------------------------------------------
/src/netius/base/observer.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 |
32 | class Observable(object):
33 | """
34 | The base class that implements the observable
35 | patter allowing the object to handle a series of
36 | event in a dynamic fashion.
37 |
38 | This class should be friendly to multiple inheritance
39 | and should avoid variable naming collision.
40 | """
41 |
42 | def __init__(self, *args, **kwargs):
43 | self.events = {}
44 |
45 | def build(self):
46 | pass
47 |
48 | def destroy(self):
49 | self.unbind_all()
50 |
51 | def bind(self, name, method, oneshot=False):
52 | if oneshot:
53 | method.oneshot = oneshot
54 | methods = self.events.get(name, [])
55 | methods.append(method)
56 | self.events[name] = methods
57 |
58 | def unbind(self, name, method=None):
59 | methods = self.events.get(name, None)
60 | if not methods:
61 | return
62 | if method:
63 | methods.remove(method)
64 | else:
65 | del methods[:]
66 |
67 | def unbind_all(self):
68 | if not hasattr(self, "events"):
69 | return
70 | for methods in self.events.values():
71 | del methods[:]
72 | self.events.clear()
73 |
74 | def trigger(self, name, *args, **kwargs):
75 | methods = self.events.get(name, None)
76 | if not methods:
77 | return
78 | oneshots = None
79 | for method in methods:
80 | method(*args, **kwargs)
81 | if not hasattr(method, "oneshot"):
82 | continue
83 | if not method.oneshot:
84 | continue
85 | oneshots = [] if oneshots == None else oneshots
86 | oneshots.append(method)
87 | if not oneshots:
88 | return
89 | for oneshot in oneshots:
90 | self.unbind(name, oneshot)
91 |
--------------------------------------------------------------------------------
/src/netius/base/request.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import time
32 |
33 | from . import errors
34 |
35 | REQUEST_TIMEOUT = 10.0
36 | """ The timeout until a request is considered to be
37 | expired and is discarded from the request related
38 | structures, this is crucial to avoid memory leaks """
39 |
40 |
41 | class Request(object):
42 | """
43 | Abstract request structure used to represent
44 | a request in a server/client model, this allows
45 | for easy identification and response (callback).
46 | """
47 |
48 | IDENTIFIER = 0x0000
49 | """ The global class identifier value that is going to
50 | be used when assigning new values to the request """
51 |
52 | def __init__(self, timeout=REQUEST_TIMEOUT, callback=None):
53 | self.id = self.__class__._generate_id()
54 | self.timeout = time.time() + timeout
55 | self.callback = callback
56 |
57 | @classmethod
58 | def _generate_id(cls):
59 | cls.IDENTIFIER = (cls.IDENTIFIER + 1) & 0xFFFF
60 | return cls.IDENTIFIER
61 |
62 |
63 | class Response(object):
64 | """
65 | Top level abstract representation of a response to
66 | be sent based on a previously created request, the
67 | input of this object should be raw data and a relation
68 | between the request and the response is required.
69 |
70 | The association/relation between the response and the
71 | request should be done using the original request
72 | generated identifier.
73 | """
74 |
75 | def __init__(self, data, request=None):
76 | self.data = data
77 | self.request = request
78 |
79 | def parse(self):
80 | pass
81 |
82 | def get_request(self):
83 | return self.request
84 |
85 | def get_id(self):
86 | raise errors.NetiusError("Not implemented")
87 |
--------------------------------------------------------------------------------
/src/netius/base/service.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2017 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2017 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import uuid
32 |
33 | from . import observer
34 | from . import transport
35 |
36 | BUFFER_SIZE_S = None
37 | """ The size of both the send and receive buffers for
38 | the socket representing the server, this socket is
39 | responsible for the handling of the new connections """
40 |
41 | BUFFER_SIZE_C = None
42 | """ The size of the buffers (send and receive) that
43 | is going to be set on the on the sockets created by
44 | the server (client sockets), this is critical for a
45 | good performance of the server (large value) """
46 |
47 |
48 | class Service(observer.Observable):
49 | """
50 | Top level class responsible for the single representation
51 | of the meta-data associated with a service.
52 |
53 | This is considered to be the equivalent to a connection object
54 | for the servers (as opposed to clients).
55 |
56 | This implementation takes inspiration from the asyncio stream
57 | and should be very compatible in terms of API.
58 | """
59 |
60 | def __init__(
61 | self,
62 | owner=None,
63 | transport=None,
64 | socket=None,
65 | host=None,
66 | port=None,
67 | ssl=False,
68 | receive_buffer_s=BUFFER_SIZE_S,
69 | send_buffer_s=BUFFER_SIZE_S,
70 | receive_buffer_c=BUFFER_SIZE_C,
71 | send_buffer_c=BUFFER_SIZE_C,
72 | ):
73 | observer.Observable.__init__(self)
74 | self.id = str(uuid.uuid4())
75 | self.owner = owner
76 | self.transport = transport
77 | self.socket = socket
78 | self.host = host
79 | self.port = port
80 | self.ssl = ssl
81 | self.receive_buffer_s = receive_buffer_s
82 | self.send_buffer_s = send_buffer_s
83 | self.receive_buffer_c = receive_buffer_c
84 | self.send_buffer_c = send_buffer_c
85 |
86 | def on_socket_c(self, socket_c, address):
87 | connection = self.owner.build_connection_client(
88 | socket_c,
89 | address,
90 | ssl=self.ssl,
91 | receive_buffer_c=self.receive_buffer_c,
92 | send_buffer_c=self.send_buffer_c,
93 | )
94 | self.transport = transport.TransportStream(self, connection)
95 | self.trigger("connection", connection)
96 |
--------------------------------------------------------------------------------
/src/netius/base/stream.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import observer
32 |
33 | OPEN = 1
34 | """ The open status value, meant to be used in situations
35 | where the status of the entity is open (opposite of closed) """
36 |
37 | CLOSED = 2
38 | """ Closed status value to be used in entities which have
39 | no pending structured opened and operations are limited """
40 |
41 | PENDING = 3
42 | """ The pending status used for transient states (eg: created)
43 | connections under this state must be used carefully """
44 |
45 |
46 | class Stream(observer.Observable):
47 | """
48 | Abstract stream class responsible for the representation of
49 | a "virtual" connection state for situation where multiplexing
50 | of single connection exists (connections within connections)
51 |
52 | Most of the interface for a stream should be "logically" similar
53 | to the one defined by a connection.
54 |
55 | A good example of the stream usage is the HTTP2 protocol where
56 | multiple parallel streams co-exist within a single TCP connection
57 | allowing huge performance improvements.
58 | """
59 |
60 | def __init__(self, owner=None):
61 | observer.Observable.__init__(self)
62 | self.status = PENDING
63 | self.owner = owner
64 | self.connection = owner.owner
65 |
66 | def reset(self):
67 | pass
68 |
69 | def open(self):
70 | if self.status == OPEN:
71 | return
72 | self.status = OPEN
73 | self.connection.owner.on_stream_c(self)
74 |
75 | def close(self):
76 | if self.status == CLOSED:
77 | return
78 | self.status = CLOSED
79 | self.connection.owner.on_stream_d(self)
80 |
81 | def info_dict(self, full=False):
82 | info = dict(status=self.status)
83 | return info
84 |
85 | def is_open(self):
86 | return self.status == OPEN
87 |
88 | def is_closed(self):
89 | return self.status == CLOSED
90 |
91 | def is_pending(self):
92 | return self.status == PENDING
93 |
--------------------------------------------------------------------------------
/src/netius/clients/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import apn
29 | from . import dht
30 | from . import dns
31 | from . import http
32 | from . import mjpg
33 | from . import raw
34 | from . import smtp
35 | from . import ssdp
36 | from . import torrent
37 | from . import ws
38 |
39 | from .apn import APNProtocol, APNClient
40 | from .dht import DHTRequest, DHTResponse, DHTClient
41 | from .dns import DNSRequest, DNSResponse, DNSProtocol, DNSClient
42 | from .http import HTTPProtocol, HTTPClient
43 | from .mjpg import MJPGProtocol, MJPGClient
44 | from .raw import RawProtocol, RawClient
45 | from .smtp import SMTPConnection, SMTPClient
46 | from .ssdp import SSDPProtocol, SSDPClient
47 | from .torrent import CHOKED, UNCHOKED, TorrentConnection, TorrentClient
48 | from .ws import WSProtocol, WSClient
49 |
--------------------------------------------------------------------------------
/src/netius/clients/raw.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | class RawProtocol(netius.StreamProtocol):
35 |
36 | def send_basic(self):
37 | """
38 | Sends a basic HTTP 1.0 request, that can be used to
39 | run a simple operation on the raw protocol.
40 | """
41 |
42 | self.send("GET / HTTP/1.0\r\n\r\n")
43 |
44 |
45 | class RawClient(netius.ClientAgent):
46 |
47 | protocol = RawProtocol
48 |
49 | @classmethod
50 | def run_s(cls, host, port=8080, loop=None, *args, **kwargs):
51 | protocol = cls.protocol()
52 |
53 | loop = netius.connect_stream(
54 | lambda: protocol, host=host, port=port, loop=loop, *args, **kwargs
55 | )
56 |
57 | return loop, protocol
58 |
59 |
60 | if __name__ == "__main__":
61 |
62 | def on_open(protocol):
63 | protocol.send_basic()
64 |
65 | def on_data(protocol, data):
66 | print(data)
67 |
68 | def on_finsh(protocol):
69 | netius.compat_loop(loop).stop()
70 |
71 | loop, protocol = RawClient.run_s("localhost")
72 |
73 | protocol.bind("open", on_open)
74 | protocol.bind("data", on_data)
75 | protocol.bind("finish", on_finsh)
76 |
77 | loop.run_forever()
78 | loop.close()
79 |
80 | else:
81 | __path__ = []
82 |
--------------------------------------------------------------------------------
/src/netius/common/parser.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | class Parser(netius.Observable):
35 |
36 | FIELDS = ("_pid",)
37 |
38 | def __init__(self, owner):
39 | netius.Observable.__init__(self)
40 | self.owner = owner
41 | self._pid = 0
42 |
43 | @classmethod
44 | def mock(cls, owner, state):
45 | mock = cls(owner)
46 | mock.set_state(state)
47 | return mock
48 |
49 | def build(self):
50 | netius.Observable.build(self)
51 |
52 | def destroy(self):
53 | netius.Observable.destroy(self)
54 | self.owner = None
55 | self._pid = 0
56 |
57 | def get_state(self):
58 | cls = self.__class__
59 | fields = cls.FIELDS
60 | state = dict()
61 | for field in fields:
62 | state[field] = getattr(self, field)
63 | return state
64 |
65 | def set_state(self, state):
66 | cls = self.__class__
67 | fields = cls.FIELDS
68 | for field in fields:
69 | value = state[field]
70 | setattr(self, field, value)
71 |
72 | def info_dict(self):
73 | info = self.get_state()
74 | return info
75 |
76 | def parse(self, data):
77 | self._pid = (self._pid + 1) % 2147483647
78 |
--------------------------------------------------------------------------------
/src/netius/common/setup.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 |
33 | CA_URL = "https://curl.se/ca/cacert.pem"
34 |
35 | COMMON_PATH = os.path.dirname(__file__)
36 | BASE_PATH = os.path.join(COMMON_PATH, "..", "base")
37 | EXTRAS_PATH = os.path.join(BASE_PATH, "extras")
38 | SSL_CA_PATH = os.path.join(EXTRAS_PATH, "net.ca")
39 |
40 |
41 | def ensure_setup():
42 | ensure_ca()
43 |
44 |
45 | def ensure_ca(path=SSL_CA_PATH):
46 | if os.path.exists(path):
47 | return
48 | _download_ca(path=path)
49 |
50 |
51 | def _download_ca(path=SSL_CA_PATH, raise_e=True):
52 | import netius.clients
53 |
54 | ca_url = CA_URL
55 | while True:
56 | result = netius.clients.HTTPClient.method_s("GET", ca_url, asynchronous=False)
57 | if not result["code"] in (301, 302, 303):
58 | break
59 | headers = result.get("headers", {})
60 | location = headers.get("Location", None)
61 | if not location:
62 | break
63 | ca_url = location
64 | if not result["code"] == 200:
65 | if not raise_e:
66 | return
67 | raise Exception("Error while downloading CA file from '%s'" % CA_URL)
68 | response = netius.clients.HTTPClient.to_response(result)
69 | contents = response.read()
70 | _store_contents(contents, path)
71 |
72 |
73 | def _store_contents(contents, path):
74 | file = open(path, "wb")
75 | try:
76 | file.write(contents)
77 | finally:
78 | file.close()
79 | return path
80 |
--------------------------------------------------------------------------------
/src/netius/common/structures.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 | import heapq
33 |
34 |
35 | class PriorityDict(dict):
36 |
37 | def __init__(self, *args, **kwargs):
38 | dict.__init__(self, *args, **kwargs)
39 | self._rebuild_heap()
40 |
41 | def __setitem__(self, key, val):
42 | dict.__setitem__(self, key, val)
43 |
44 | if len(self._heap) < len(self) * 2:
45 | heapq.heappush(self._heap, (val, key))
46 | else:
47 | self._rebuild_heap()
48 |
49 | def smallest(self):
50 | heap = self._heap
51 | v, k = heap[0]
52 | while not k in self or self[k] != v:
53 | heapq.heappop(heap)
54 | v, k = heap[0]
55 | return k
56 |
57 | def pop_smallest(self):
58 | heap = self._heap
59 | v, k = heapq.heappop(heap)
60 | while not k in self or self[k] != v:
61 | v, k = heapq.heappop(heap)
62 | del self[k]
63 | return k
64 |
65 | def setdefault(self, key, val):
66 | if not key in self:
67 | self[key] = val
68 | return val
69 | return self[key]
70 |
71 | def update(self, *args, **kwargs):
72 | dict.update(self, *args, **kwargs)
73 | self._rebuild_heap()
74 |
75 | def sorted_iter(self):
76 | while self:
77 | yield self.pop_smallest()
78 |
79 | def _rebuild_heap(self):
80 | self._heap = [(v, k) for k, v in self.items()]
81 | heapq.heapify(self._heap)
82 |
83 |
84 | def file_iterator(file_object, chunk_size=40960):
85 | file_object.seek(0, os.SEEK_END)
86 | size = file_object.tell()
87 | file_object.seek(0, os.SEEK_SET)
88 | yield size
89 | while True:
90 | data = file_object.read(chunk_size)
91 | if not data:
92 | break
93 | yield data
94 |
--------------------------------------------------------------------------------
/src/netius/common/style.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | BASE_STYLE = """"""
106 |
--------------------------------------------------------------------------------
/src/netius/common/tftp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | RRQ_TFTP = 0x01
32 | WRQ_TFTP = 0x02
33 | DATA_TFTP = 0x03
34 | ACK_TFTP = 0x04
35 | ERROR_TFTP = 0x05
36 |
37 | TYPES_TFTP = {
38 | RRQ_TFTP: "rrq",
39 | WRQ_TFTP: "wrq",
40 | DATA_TFTP: "data",
41 | ACK_TFTP: "ack",
42 | ERROR_TFTP: "error",
43 | }
44 |
--------------------------------------------------------------------------------
/src/netius/common/tls.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 |
33 | import netius
34 |
35 |
36 | class TLSContextDict(dict):
37 |
38 | def __init__(self, owner, domains, *args, **kwargs):
39 | dict.__init__(self, *args, **kwargs)
40 | self.owner = owner
41 | self.load(domains)
42 |
43 | def load(self, domains):
44 | secure = self.owner.get_env("SSL_SECURE", 1, cast=int)
45 | for domain in domains:
46 | if not self.has_definition(domain):
47 | continue
48 | cer_path = self.cer_path(domain)
49 | key_path = self.key_path(domain)
50 | values = dict(cer_file=cer_path, key_file=key_path)
51 | context = self.owner._ssl_ctx(values, secure=secure)
52 | self[domain] = (context, values)
53 |
54 | def has_definition(self, domain):
55 | cer_path = self.cer_path(domain)
56 | key_path = self.key_path(domain)
57 | if not os.path.exists(cer_path):
58 | return False
59 | if not os.path.exists(key_path):
60 | return False
61 | return True
62 |
63 | def cer_path(self, domain):
64 | raise netius.NotImplemented("Missing implementation")
65 |
66 | def key_path(self, domain):
67 | raise netius.NotImplemented("Missing implementation")
68 |
69 |
70 | class LetsEncryptDict(TLSContextDict):
71 |
72 | def __init__(self, owner, domains, *args, **kwargs):
73 | self.letse_path = kwargs.get("letse_path", "/data/letsencrypt/etc/live")
74 | TLSContextDict.__init__(self, owner, domains, *args, **kwargs)
75 |
76 | def cer_path(self, domain):
77 | domain_path = os.path.join(self.letse_path, domain)
78 | return os.path.join(domain_path, "fullchain.pem")
79 |
80 | def key_path(self, domain):
81 | domain_path = os.path.join(self.letse_path, domain)
82 | return os.path.join(domain_path, "privkey.pem")
83 |
--------------------------------------------------------------------------------
/src/netius/examples/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import http
29 | from . import upnp
30 |
31 | from .http import http_static, http_callback
32 | from .upnp import upnp_map
33 |
--------------------------------------------------------------------------------
/src/netius/examples/http.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius.clients
32 |
33 |
34 | def http_static():
35 | request = netius.clients.HTTPClient.get_s(
36 | "https://www.flickr.com/", asynchronous=False
37 | )
38 | print(request["data"])
39 |
40 |
41 | def http_callback():
42 | def callback(protocol, parser, request):
43 | print(request["data"])
44 | protocol.close()
45 |
46 | def on_close(protocol):
47 | netius.compat_loop(loop).stop()
48 |
49 | client = netius.clients.HTTPClient()
50 | loop, _ = client.get(
51 | "https://www.flickr.com/", on_result=callback, on_close=on_close
52 | )
53 | loop.run_forever()
54 | loop.close()
55 |
--------------------------------------------------------------------------------
/src/netius/extra/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import desktop
29 | from . import dhcp_s
30 | from . import file
31 | from . import filea
32 | from . import hello_w
33 | from . import hello
34 | from . import proxy_d
35 | from . import proxy_f
36 | from . import proxy_r
37 | from . import smtp_r
38 |
39 | from .desktop import DesktopServer
40 | from .dhcp_s import DHCPServerS
41 | from .file import FileServer
42 | from .filea import FileAsyncServer
43 | from .hello import HelloServer
44 | from .proxy_d import DockerProxyServer
45 | from .proxy_f import ForwardProxyServer
46 | from .proxy_r import ReverseProxyServer
47 | from .smtp_r import RelaySMTPServer
48 |
--------------------------------------------------------------------------------
/src/netius/extra/desktop.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius.servers
32 |
33 | try:
34 | import PIL.ImageGrab
35 | except ImportError:
36 | PIL = None
37 |
38 |
39 | class DesktopServer(netius.servers.MJPGServer):
40 |
41 | def get_delay(self, connection):
42 | return 1
43 |
44 | def get_image(self, connection):
45 | if not PIL:
46 | return None
47 | image = PIL.ImageGrab.grab()
48 | buffer = netius.legacy.BytesIO()
49 | try:
50 | image.save(buffer, "JPEG")
51 | data = buffer.getvalue()
52 | finally:
53 | buffer.close()
54 | return data
55 |
56 |
57 | if __name__ == "__main__":
58 | server = DesktopServer()
59 | server.serve(env=True)
60 | else:
61 | __path__ = []
62 |
--------------------------------------------------------------------------------
/src/netius/extra/extras/htpasswd:
--------------------------------------------------------------------------------
1 | admin:password123
2 |
--------------------------------------------------------------------------------
/src/netius/extra/hello.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius.servers
32 |
33 |
34 | class HelloServer(netius.servers.HTTP2Server):
35 | """
36 | Simple Hello (World) HTTP server meant to be used for benchmarks
37 | and other operations that require a simple in-memory HTTP server.
38 |
39 | Most of the implementation on the server is done in the upper
40 | layers from which this server class inherits.
41 |
42 | Performance should always be considered critical when changing
43 | or adding new features to this server implementation.
44 | """
45 |
46 | def __init__(self, message="Hello World", *args, **kwargs):
47 | netius.servers.HTTP2Server.__init__(self, *args, **kwargs)
48 | self.message = message
49 |
50 | def on_serve(self):
51 | netius.servers.HTTP2Server.on_serve(self)
52 | if self.env:
53 | self.message = self.get_env("MESSAGE", self.message, cast=str)
54 | if self.env:
55 | self.keep_alive = self.get_env("KEEP_ALIVE", True, cast=bool)
56 | self.info("Serving '%s' as welcome message ..." % self.message)
57 |
58 | def on_data_http(self, connection, parser):
59 | netius.servers.HTTP2Server.on_data_http(self, connection, parser)
60 |
61 | keep_alive = self.keep_alive and parser.keep_alive
62 | callback = self._hello_keep if keep_alive else self._hello_close
63 | connection_s = "keep-alive" if keep_alive else "close"
64 | headers = {"Connection": connection_s, "Content-Type": "text/plain"}
65 |
66 | connection.send_response(
67 | data=self.message,
68 | headers=headers,
69 | code=200,
70 | code_s="OK",
71 | apply=True,
72 | callback=callback,
73 | )
74 |
75 | def _hello_close(self, connection):
76 | self.delay(connection.close)
77 |
78 | def _hello_keep(self, connection):
79 | pass
80 |
81 |
82 | if __name__ == "__main__":
83 | import logging
84 |
85 | server = HelloServer(level=logging.INFO)
86 | server.serve(env=True)
87 | else:
88 | __path__ = []
89 |
--------------------------------------------------------------------------------
/src/netius/extra/hello_w.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius.servers
32 |
33 |
34 | def app(environ, start_response):
35 | status = "200 OK"
36 | contents = "Hello World"
37 | content_l = len(contents)
38 | headers = (
39 | ("Content-Length", content_l),
40 | ("Content-type", "text/plain"),
41 | ("Connection", "keep-alive"),
42 | )
43 | start_response(status, headers)
44 | yield contents
45 |
46 |
47 | if __name__ == "__main__":
48 | server = netius.servers.WSGIServer(app=app)
49 | server.serve(env=True)
50 | else:
51 | __path__ = []
52 |
--------------------------------------------------------------------------------
/src/netius/middleware/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import annoyer
29 | from . import base
30 | from . import blacklist
31 | from . import dummy
32 | from . import flood
33 | from . import proxy
34 |
35 | from .annoyer import AnnoyerMiddleware
36 | from .base import Middleware
37 | from .blacklist import BlacklistMiddleware
38 | from .dummy import DummyMiddleware
39 | from .flood import FloodMiddleware
40 | from .proxy import ProxyMiddleware
41 |
--------------------------------------------------------------------------------
/src/netius/middleware/annoyer.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import sys
32 | import time
33 | import datetime
34 | import threading
35 |
36 | import netius
37 |
38 | from .base import Middleware
39 |
40 |
41 | class AnnoyerMiddleware(Middleware):
42 | """
43 | Simple middleware that prints an "annoying" status message
44 | to the standard output (stdout) from time to time providing
45 | a simple diagnostics strategy.
46 | """
47 |
48 | def __init__(self, owner, period=10.0):
49 | Middleware.__init__(self, owner)
50 | self.period = period
51 | self._initial = None
52 | self._thread = None
53 | self._running = False
54 |
55 | def start(self):
56 | Middleware.start(self)
57 | self.period = netius.conf("ANNOYER_PERIOD", self.period, cast=float)
58 | self._thread = threading.Thread(target=self._run)
59 | self._thread.start()
60 |
61 | def stop(self):
62 | Middleware.stop(self)
63 | if self._thread:
64 | self._running = False
65 | self._thread.join()
66 | self._thread = None
67 |
68 | def _run(self):
69 | self._initial = datetime.datetime.utcnow()
70 | self._running = True
71 | while self._running:
72 | delta = datetime.datetime.utcnow() - self._initial
73 | delta_s = self.owner._format_delta(delta)
74 | message = "Uptime => %s | Connections => %d\n" % (
75 | delta_s,
76 | len(self.owner.connections),
77 | )
78 | sys.stdout.write(message)
79 | sys.stdout.flush()
80 | time.sleep(self.period)
81 |
--------------------------------------------------------------------------------
/src/netius/middleware/base.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 |
32 | class Middleware(object):
33 |
34 | def __init__(self, owner):
35 | self.owner = owner
36 |
37 | def start(self):
38 | pass
39 |
40 | def stop(self):
41 | pass
42 |
--------------------------------------------------------------------------------
/src/netius/middleware/blacklist.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 | from .base import Middleware
34 |
35 |
36 | class BlacklistMiddleware(Middleware):
37 | """
38 | Simple middleware implementation for blacklisting of IP
39 | addresses using a very minimalistic approach.
40 | """
41 |
42 | def __init__(self, owner, blacklist=None, whitelist=None):
43 | Middleware.__init__(self, owner)
44 | self.blacklist = blacklist or []
45 | self.whitelist = whitelist or []
46 |
47 | def start(self):
48 | Middleware.start(self)
49 | self.blacklist = netius.conf("BLACKLIST", self.blacklist, cast=list)
50 | self.whitelist = netius.conf("WHITELIST", self.whitelist, cast=list)
51 | self.owner.bind("connection_c", self.on_connection_c)
52 |
53 | def stop(self):
54 | Middleware.stop(self)
55 | self.owner.unbind("connection_c", self.on_connection_c)
56 |
57 | def on_connection_c(self, owner, connection):
58 | host = connection.address[0]
59 | if not host in self.blacklist and not "*" in self.blacklist:
60 | return
61 | if host in self.whitelist:
62 | return
63 | self.owner.warning("Connection from '%s' dropped (blacklisted)" % host)
64 | connection.close()
65 |
--------------------------------------------------------------------------------
/src/netius/middleware/dummy.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from .base import Middleware
32 |
33 |
34 | class DummyMiddleware(Middleware):
35 | """
36 | Simple middleware implementation for testing/debugging
37 | purposes that may be used as reference for the implementation
38 | of more complex middleware units.
39 | """
40 |
41 | def start(self):
42 | Middleware.start(self)
43 | self.owner.bind("connection_c", self.on_connection_c)
44 |
45 | def stop(self):
46 | Middleware.stop(self)
47 | self.owner.unbind("connection_c", self.on_connection_c)
48 |
49 | def on_connection_c(self, owner, connection):
50 | print("Received connection from %s" % (connection.address,))
51 |
--------------------------------------------------------------------------------
/src/netius/middleware/flood.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import time
32 |
33 | import netius
34 |
35 | from .base import Middleware
36 |
37 |
38 | class FloodMiddleware(Middleware):
39 | """
40 | Simple middleware implementation for avoiding flooding
41 | of connection creation from a certain address.
42 | """
43 |
44 | def __init__(self, owner, conns_per_min=600, whitelist=None):
45 | Middleware.__init__(self, owner)
46 | self.blacklist = conns_per_min
47 | self.whitelist = whitelist or []
48 |
49 | def start(self):
50 | Middleware.start(self)
51 | self.conns_per_min = netius.conf("CONNS_PER_MIN", self.conns_per_min, cast=int)
52 | self.whitelist = netius.conf("WHITELIST", self.whitelist, cast=list)
53 | self.blacklist = []
54 | self.conn_map = dict()
55 | self.minute = int(time.time() // 60)
56 | self.owner.bind("connection_c", self.on_connection_c)
57 |
58 | def stop(self):
59 | Middleware.stop(self)
60 | self.owner.unbind("connection_c", self.on_connection_c)
61 |
62 | def on_connection_c(self, owner, connection):
63 | host = connection.address[0]
64 | self._update_flood(host)
65 | if not host in self.blacklist and not "*" in self.blacklist:
66 | return
67 | if host in self.whitelist:
68 | return
69 | self.owner.warning("Connection from '%s' dropped (flooding avoidance)" % host)
70 | connection.close()
71 |
72 | def _update_flood(self, host):
73 | minute = int(time.time() // 60)
74 | if minute == self.minute:
75 | self.conn_map.clear()
76 | self.minute = minute
77 | count = self.conn_map.get(host, 0)
78 | count += 1
79 | self.conn_map[host] = count
80 | if count > self.conns_per_min:
81 | self.blacklist.append(host)
82 |
--------------------------------------------------------------------------------
/src/netius/mock/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import appier
29 |
--------------------------------------------------------------------------------
/src/netius/mock/appier.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 |
32 | class APIApp(object):
33 | pass
34 |
35 |
36 | def route(*args, **kwargs):
37 |
38 | def decorator(*args, **kwargs):
39 | pass
40 |
41 | return decorator
42 |
--------------------------------------------------------------------------------
/src/netius/pool/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import common
29 | from . import file
30 | from . import notify
31 | from . import task
32 |
33 | from .common import (
34 | Thread,
35 | ThreadPool,
36 | EventPool,
37 | EventFile,
38 | UnixEventFile,
39 | SocketEventFile,
40 | )
41 | from .file import FileThread, FilePool
42 | from .notify import NotifyPool
43 | from .task import TaskThread, TaskPool
44 |
--------------------------------------------------------------------------------
/src/netius/pool/notify.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import common
32 |
33 |
34 | class NotifyPool(common.EventPool):
35 |
36 | def __init__(self):
37 | common.EventPool.__init__(self, count=0)
38 |
--------------------------------------------------------------------------------
/src/netius/pool/task.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 | from . import common
34 |
35 | TASK_WORK = 10
36 |
37 |
38 | class TaskThread(common.Thread):
39 |
40 | def execute(self, work):
41 | type = work[0]
42 | if not type == TASK_WORK:
43 | netius.NotImplemented("Cannot execute type '%d'" % type)
44 |
45 | callable, args, kwargs, callback = work[1:]
46 | result = callable(*args, **kwargs)
47 | if callback:
48 | callback(result)
49 |
50 |
51 | class TaskPool(common.EventPool):
52 |
53 | def __init__(self, base=TaskThread, count=10):
54 | common.EventPool.__init__(self, base=base, count=count)
55 |
56 | def execute(self, callable, args=[], kwargs={}, callback=None):
57 | work = (TASK_WORK, callable, args, kwargs, callback)
58 | self.push(work)
59 |
--------------------------------------------------------------------------------
/src/netius/servers/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import dhcp
29 | from . import echo_ws
30 | from . import echo
31 | from . import ftp
32 | from . import http
33 | from . import http2
34 | from . import mjpg
35 | from . import pop
36 | from . import proxy
37 | from . import smtp
38 | from . import socks
39 | from . import tftp
40 | from . import torrent
41 | from . import ws
42 | from . import wsgi
43 |
44 | from .dhcp import DHCPRequest, DHCPServer
45 | from .echo_ws import EchoWSServer
46 | from .echo import EchoProtocol, EchoServer
47 | from .ftp import FTPConnection, FTPServer
48 | from .http import HTTPConnection, HTTPServer
49 | from .http2 import HTTP2Server
50 | from .mjpg import MJPGServer
51 | from .pop import POPConnection, POPServer
52 | from .proxy import ProxyConnection, ProxyServer
53 | from .smtp import TERMINATION_SIZE, SMTPConnection, SMTPServer
54 | from .socks import SOCKSConnection, SOCKSServer
55 | from .tftp import TFTPRequest, TFTPServer
56 | from .torrent import Pieces, TorrentTask, TorrentServer
57 | from .ws import WSConnection, WSServer
58 | from .wsgi import WSGIServer
59 |
--------------------------------------------------------------------------------
/src/netius/servers/echo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius
32 |
33 |
34 | class EchoProtocol(netius.StreamProtocol):
35 |
36 | def on_data(self, data):
37 | netius.StreamProtocol.on_data(self, data)
38 | self.send(data)
39 |
40 | def serve(self, host="127.0.0.1", port=8888, ssl=False, env=False, loop=None):
41 | loop = netius.serve_stream(
42 | lambda: self, host=host, port=port, ssl=ssl, loop=loop, env=env
43 | )
44 | return loop, self
45 |
46 |
47 | class EchoServer(netius.ServerAgent):
48 |
49 | protocol = EchoProtocol
50 |
51 | @classmethod
52 | def serve_s(cls, **kwargs):
53 | protocol = cls.protocol()
54 | return protocol.serve(**kwargs)
55 |
56 |
57 | if __name__ == "__main__":
58 | loop, _protocol = EchoServer.serve_s()
59 | loop.run_forever()
60 | loop.close()
61 | else:
62 | __path__ = []
63 |
--------------------------------------------------------------------------------
/src/netius/servers/echo_ws.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import ws
32 |
33 |
34 | class EchoWSServer(ws.WSServer):
35 |
36 | def on_data_ws(self, connection, data):
37 | ws.WSServer.on_data_ws(self, connection, data)
38 | connection.send_ws(data)
39 |
40 |
41 | if __name__ == "__main__":
42 | server = EchoWSServer()
43 | server.serve(env=True)
44 | else:
45 | __path__ = []
46 |
--------------------------------------------------------------------------------
/src/netius/servers/extras/boy_0.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/src/netius/servers/extras/boy_0.jpg
--------------------------------------------------------------------------------
/src/netius/servers/extras/boy_1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hivesolutions/netius/fa90889f611d0f6f7825541a1b4307537a373430/src/netius/servers/extras/boy_1.jpg
--------------------------------------------------------------------------------
/src/netius/servers/runners/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/servers/runners/echo.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 |
33 | import netius
34 |
35 | from netius.servers import EchoProtocol, EchoServer
36 |
37 |
38 | async def main_asyncio():
39 | # retrieves a reference to the event loop as we plan to use
40 | # low-level APIs, this should return the default event loop
41 | import asyncio
42 |
43 | loop = asyncio.get_running_loop()
44 | server = await loop.create_server(lambda: EchoProtocol(), "127.0.0.1", 8888)
45 | async with server:
46 | await server.serve_forever()
47 |
48 |
49 | def run_native():
50 | loop, _protocol = EchoServer.serve_s(host="127.0.0.1", port=8888)
51 | loop.run_forever()
52 | loop.close()
53 |
54 |
55 | def run_asyncio():
56 | netius.run(main_asyncio())
57 |
58 |
59 | if __name__ == "__main__":
60 | if os.environ.get("ASYNCIO", "0") == "1" or os.environ.get("COMPAT", "0") == "1":
61 | run_asyncio()
62 | else:
63 | run_native()
64 | else:
65 | __path__ = []
66 |
--------------------------------------------------------------------------------
/src/netius/sh/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | from . import base
29 | from . import dkim
30 | from . import rsa
31 | from . import smtp
32 |
--------------------------------------------------------------------------------
/src/netius/sh/auth.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
28 | import netius
29 |
30 | from . import base
31 |
32 |
33 | def generate(password, type="sha256", salt="netius"):
34 | print(netius.Auth.generate(password, type=type, salt=salt))
35 |
36 |
37 | if __name__ == "__main__":
38 | base.sh_call(globals(), locals())
39 | else:
40 | __path__ = []
41 |
--------------------------------------------------------------------------------
/src/netius/sh/base.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 |
32 | def sh_call(globals={}, locals={}):
33 | import sys
34 |
35 | if not len(sys.argv) > 1:
36 | raise RuntimeError("invalid number of arguments (no method)")
37 |
38 | name = sys.argv[1]
39 |
40 | method = globals[name]
41 | method(*sys.argv[2:])
42 |
--------------------------------------------------------------------------------
/src/netius/sh/dkim.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import netius.common
32 |
33 | from . import base
34 |
35 |
36 | def generate(domain, suffix=None, number_bits=1024):
37 | number_bits = int(number_bits)
38 | result = netius.common.dkim_generate(domain, suffix=suffix, number_bits=number_bits)
39 | print(result["dns_txt"])
40 | print(result["private_pem"])
41 |
42 |
43 | def sign(email_path, key_path, selector, domain):
44 | file = open(email_path, "rb")
45 | try:
46 | contents = file.read()
47 | finally:
48 | file.close()
49 |
50 | contents = contents.lstrip()
51 | private_key = netius.common.open_private_key(key_path)
52 | signature = netius.common.dkim_sign(contents, selector, domain, private_key)
53 |
54 | file = open(email_path, "wb")
55 | try:
56 | file.write(signature)
57 | file.write(contents)
58 | finally:
59 | file.close()
60 |
61 |
62 | if __name__ == "__main__":
63 | base.sh_call(globals(), locals())
64 | else:
65 | __path__ = []
66 |
--------------------------------------------------------------------------------
/src/netius/sh/rsa.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import pprint
32 |
33 | import netius.common
34 |
35 | from . import base
36 |
37 |
38 | def read_private(path):
39 | private_key = netius.common.open_private_key(path)
40 | pprint.pprint(private_key)
41 |
42 |
43 | def read_public(path):
44 | public_key = netius.common.open_public_key(path)
45 | pprint.pprint(public_key)
46 |
47 |
48 | def private_to_public(private_path, public_path):
49 | private_key = netius.common.open_private_key(private_path)
50 | public_key = netius.common.private_to_public(private_key)
51 | netius.common.write_public_key(public_path, public_key)
52 |
53 |
54 | if __name__ == "__main__":
55 | base.sh_call(globals(), locals())
56 | else:
57 | __path__ = []
58 |
--------------------------------------------------------------------------------
/src/netius/sh/smtp.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | from . import base
32 |
33 | import netius.clients
34 |
35 |
36 | def send(
37 | path, sender, receiver, host=None, port=25, username=None, password=None, stls=True
38 | ):
39 | file = open(path, "rb")
40 | try:
41 | contents = file.read()
42 | finally:
43 | file.close()
44 | smtp_client = netius.clients.SMTPClient(auto_close=True)
45 | smtp_client.message(
46 | [sender],
47 | [receiver],
48 | contents,
49 | host=host,
50 | port=port,
51 | username=username,
52 | password=password,
53 | stls=stls,
54 | )
55 |
56 |
57 | if __name__ == "__main__":
58 | base.sh_call(globals(), locals())
59 | else:
60 | __path__ = []
61 |
--------------------------------------------------------------------------------
/src/netius/test/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/auth/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/auth/allow.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.auth
34 |
35 |
36 | class AllowAuthTest(unittest.TestCase):
37 |
38 | def test_simple(self):
39 | result = netius.auth.AllowAuth.auth("root", "root")
40 | self.assertEqual(result, True)
41 |
42 | result = netius.auth.AllowAuth.auth(None, None)
43 | self.assertEqual(result, True)
44 |
45 | auth = netius.auth.AllowAuth()
46 |
47 | result = auth.auth_i("root", "root")
48 | self.assertEqual(result, True)
49 |
50 | result = auth.auth_i(None, None)
51 | self.assertEqual(result, True)
52 |
--------------------------------------------------------------------------------
/src/netius/test/auth/deny.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.auth
34 |
35 |
36 | class DenyAuthTest(unittest.TestCase):
37 |
38 | def test_simple(self):
39 | result = netius.auth.DenyAuth.auth("root", "root")
40 | self.assertEqual(result, False)
41 |
42 | result = netius.auth.DenyAuth.auth(None, None)
43 | self.assertEqual(result, False)
44 |
45 | auth = netius.auth.DenyAuth()
46 |
47 | result = auth.auth_i("root", "root")
48 | self.assertEqual(result, False)
49 |
50 | result = auth.auth_i(None, None)
51 | self.assertEqual(result, False)
52 |
--------------------------------------------------------------------------------
/src/netius/test/auth/simple.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.auth
34 |
35 |
36 | class SimpleAuthTest(unittest.TestCase):
37 |
38 | def test_simple(self):
39 | auth = netius.auth.SimpleAuth("root", "root")
40 |
41 | result = auth.auth_i("root", "root")
42 | self.assertEqual(result, True)
43 |
44 | result = auth.auth_i("root", "root_")
45 | self.assertEqual(result, False)
46 |
47 | result = auth.auth_i("root", "")
48 | self.assertEqual(result, False)
49 |
50 | result = auth.auth_i("root", None)
51 | self.assertEqual(result, False)
52 |
53 | result = auth.auth_i(None, "root")
54 | self.assertEqual(result, False)
55 |
56 | auth = netius.auth.SimpleAuth(None, "root")
57 |
58 | result = auth.auth_i(None, "root")
59 | self.assertEqual(result, True)
60 |
61 | result = auth.auth_i("root", "root")
62 | self.assertEqual(result, True)
63 |
--------------------------------------------------------------------------------
/src/netius/test/base/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/base/common.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius
34 |
35 |
36 | class BaseTest(unittest.TestCase):
37 |
38 | def test_resolve_hostname(self):
39 | loop = netius.get_main()
40 | future = loop.resolve_hostname("gmail.com")
41 | result = loop.run_coroutine(future)
42 | loop.close()
43 |
44 | self.assertNotEqual(result, None)
45 | self.assertEqual(isinstance(result, str), True)
46 |
--------------------------------------------------------------------------------
/src/netius/test/base/tls.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.common
34 |
35 |
36 | class TLSTest(unittest.TestCase):
37 |
38 | def test_fingerprint(self):
39 | key_der = netius.common.open_pem_key(netius.SSL_KEY_PATH)
40 | result = netius.fingerprint(key_der)
41 | self.assertEqual(result, "5b4e55fa5ba652a9cb0c3be2dcfa303b5ae647d6")
42 |
43 | cer_der = netius.common.open_pem_key(netius.SSL_CER_PATH, token="CERTIFICATE")
44 | result = netius.fingerprint(cer_der)
45 | self.assertEqual(result, "55ed3769f523281134d87393ffda7f78c9dff786")
46 |
47 | def test_match_hostname(self):
48 | certificate = dict(
49 | subject=((("commonName", "domain.com"),),),
50 | subjectAltName=(
51 | ("DNS", "api.domain.com"),
52 | ("DNS", "embed.domain.com"),
53 | ("DNS", "instore.domain.com"),
54 | ("DNS", "domain.com"),
55 | ("DNS", "www.domain.com"),
56 | ),
57 | version=3,
58 | )
59 | netius.match_hostname(certificate, "domain.com")
60 | self.assertRaises(
61 | BaseException,
62 | lambda: netius.match_hostname(certificate, "other.domain.com"),
63 | )
64 |
65 | def test_dnsname_match(self):
66 | result = netius.dnsname_match("domain.com", "domain.com")
67 | self.assertEqual(result, True)
68 |
69 | result = netius.dnsname_match("other.domain.com", "domain.com")
70 | self.assertEqual(result, False)
71 |
72 | result = netius.dnsname_match("*.com", "domain.com")
73 | self.assertEqual(result, True)
74 |
75 | result = netius.dnsname_match("*.net", "domain.com")
76 | self.assertEqual(result, False)
77 |
--------------------------------------------------------------------------------
/src/netius/test/base/transport.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius
34 |
35 |
36 | class TransportTest(unittest.TestCase):
37 |
38 | def test_write_closing(self):
39 | connection = netius.Connection()
40 | transport = netius.Transport(None, connection)
41 |
42 | self.assertEqual(transport._loop, None)
43 | self.assertEqual(transport._connection, connection)
44 | self.assertEqual(transport.is_closing(), False)
45 | self.assertEqual(connection.is_closed(), False)
46 |
47 | connection.status = netius.CLOSED
48 |
49 | self.assertEqual(transport._loop, None)
50 | self.assertEqual(transport._connection, connection)
51 | self.assertEqual(transport.is_closing(), True)
52 | self.assertEqual(connection.is_closed(), True)
53 |
54 | transport.write(b"")
55 |
--------------------------------------------------------------------------------
/src/netius/test/clients/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/common/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/common/calc.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.common
34 |
35 |
36 | class CalcTest(unittest.TestCase):
37 |
38 | def test_jacobi_witness(self):
39 | result = netius.common.jacobi_witness(12, 2)
40 | self.assertEqual(result, True)
41 |
42 | result = netius.common.jacobi_witness(3, 2)
43 | self.assertEqual(result, False)
44 |
--------------------------------------------------------------------------------
/src/netius/test/common/mime.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.common
34 |
35 |
36 | class MimeTest(unittest.TestCase):
37 |
38 | def test_headers(self):
39 | headers = netius.common.mime.Headers()
40 | headers.set("Header", "Value")
41 | headers_s = headers.join()
42 | self.assertEqual(headers_s, b"Header: Value")
43 |
44 | headers = netius.common.mime.Headers()
45 | headers.set(b"Header", b"Value")
46 | headers_s = headers.join()
47 | self.assertEqual(headers_s, b"Header: Value")
48 |
49 | headers = netius.common.mime.Headers()
50 | headers.set(b"Header", netius.legacy.u("值").encode("utf-8"))
51 | headers_s = headers.join()
52 | self.assertEqual(headers_s, netius.legacy.u("Header: 值").encode("utf-8"))
53 |
--------------------------------------------------------------------------------
/src/netius/test/common/rsa.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.common
34 |
35 |
36 | class RSATest(unittest.TestCase):
37 |
38 | def test_rsa_crypt(self):
39 | number = 87521618088882533792115812
40 | exponent = 36510217105848231284079274231564906186307560780534247831648648175045225318561460162728717234530889577546652846221026215879731017938647097448942278391102846302059960991452569135619524616218782987723300816771871234796688783233883245022643852052966438968493009984572453713313165894751634193657763224930190644678
41 | modulus = 96932149016243683313202436463884391894442557094585987087679466243837140612162774296465245821614029972754395721149915741918184810668111591608828515674917153485786305039311784635247807314685477198540013198150331209390992944093549127578416635618276838812255204836659792055944277295324948158500484872595001982643
42 | expected = 77582017281983556473055444885999671275845710142539492742164750164915723397427639169767703318619952155855354799282839237590830862415463205074741285319459521280599333234352017115848949043330478158462953508557273618244552244536252761419615268258174029143566887421875425091295665101338067462122465562948190873420
43 | result = netius.common.rsa_crypt(number, exponent, modulus)
44 | self.assertEqual(result, expected)
45 |
--------------------------------------------------------------------------------
/src/netius/test/common/setup.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import os
32 | import unittest
33 |
34 | import netius.common
35 |
36 |
37 | class CommonTest(unittest.TestCase):
38 |
39 | def test__download_ca(self):
40 | netius.common.ensure_ca(path="test.ca")
41 | file = open("test.ca", "rb")
42 | try:
43 | data = file.read()
44 | finally:
45 | file.close()
46 | os.unlink("test.ca")
47 |
48 | self.assertNotEqual(data, None)
49 | self.assertNotEqual(len(data), 0)
50 |
--------------------------------------------------------------------------------
/src/netius/test/extra/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/extra/proxy_r.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 | import collections
33 |
34 | import netius.extra
35 |
36 |
37 | class ReverseProxyServerTest(unittest.TestCase):
38 |
39 | def setUp(self):
40 | unittest.TestCase.setUp(self)
41 | self.server = netius.extra.ReverseProxyServer(
42 | hosts={"host.com": "http://localhost"}, alias={"alias.host.com": "host.com"}
43 | )
44 |
45 | def tearDown(self):
46 | unittest.TestCase.tearDown(self)
47 | self.server.cleanup()
48 |
49 | def test_alias(self):
50 | Parser = collections.namedtuple("Parser", "headers")
51 | parser = Parser(headers=dict(host="alias.host.com"))
52 | result = self.server.rules_host(None, parser)
53 | self.assertEqual(result, ("http://localhost", None))
54 |
--------------------------------------------------------------------------------
/src/netius/test/extra/smtp_r.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.extra
34 |
35 | PRIVATE_KEY = b"MIICVwIAAoGAgRWSX07LB0VzpDy14taaO1b+juQVhQpyKy/fxaLupohy4UDOxHJU\
36 | Iz7jzR6B8l93KXWqxG5UZK2CduL6TKJGQZ+jGkTk0YU3d3r5kwPNOX1o+qhICJF8\
37 | tcWZcw1MUV816sxJ3hi6RTz7faRvJtj9J2SM2cY3eq0xQSM/dvD1fqUCAwEAAQKB\
38 | gDaUp3qTN3fQnxAf94x9z2Mt6p8CxDKn8xRdvtGzjhNueJzUKVmZOghZLDtsHegd\
39 | A6bNMTKzsA2N7C9W1B0ZNHkmc6cbUyM/gXPLzpErFF4c5sTYAaJGKK+3/3BrrliG\
40 | 6vgzTXt3KZRlInfrumZRo4h7yE/IokfmzBwjbyP7N3lhAkDpfTwLidRBTgYVz5yO\
41 | /7j55vl2GN80xDk0IDfO17/O8qyQlt+J6pksE0ojTkAjD2N4rx3dL4kPgmx80r/D\
42 | AdNNAkCNh4LBukRUMT+ulfngrnzQ4QDnCUXpANKpe3HZk4Yfysj1+zrlWFilzO3y\
43 | t/RpGu4GtH1LUNQNjrp94CcBNPy5AkBW6KCTAuiYrjwhnjd+Gr11d33fcX6Tm35X\
44 | Yq6jNTdWBooo/5+RLFt7RmrQHW5OHoo9/6C0Fd+EgF11UNTD90f5AkBBB6/0FgNJ\
45 | cCujq7PaIjKlw40nm2ItEry5NUh1wcxSFVpLdDl2oiZxYH1BFndOSBpwqEQd9DDL\
46 | Xfag2fryGge5AkCFPjggILI8jZZoEW9gJoyqh13fkf+WjtwL1mLztK2gQcrvlyUd\
47 | /ddIy8ZEkmGRiHMcX0SGdsEprW/EpbhSdakC"
48 |
49 | MESSAGE = b"Header: Value\r\n\r\nHello World"
50 |
51 | RESULT = b"DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=netius.hive.pt;\r\n\
52 | i=email@netius.hive.pt; l=13; q=dns/txt; s=20160523113052;\r\n\
53 | t=1464003802; h=Header;\r\n\
54 | bh=sIAi0xXPHrEtJmW97Q5q9AZTwKC+l1Iy+0m8vQIc/DY=; b=Pr7dVjQIX3ovG78v1X45seFwA/+uyIAofJbxn5iXTRBA5Mv+YVdiI9QMm/gU1ljoSGqqC+hvLS4iB2N1kC4fGuDxXOyNaApOLSA2hl/mBpzca6SNyu6CYvUDdhmfD+8TsYMe6Vy8UY9lWpPYNgfb9BhORqPvxiC8A8F9ScTVT/s=\r\nHeader: Value\r\n\r\nHello World"
55 |
56 | REGISTRY = {
57 | "netius.hive.pt": dict(
58 | key_b64=PRIVATE_KEY, selector="20160523113052", domain="netius.hive.pt"
59 | )
60 | }
61 |
62 |
63 | class RelaySMTPServerTest(unittest.TestCase):
64 |
65 | def setUp(self):
66 | unittest.TestCase.setUp(self)
67 | self.server = netius.extra.RelaySMTPServer()
68 |
69 | def tearDown(self):
70 | unittest.TestCase.tearDown(self)
71 | self.server.cleanup()
72 |
73 | def test_dkim(self):
74 | self.server.dkim = REGISTRY
75 | result = self.server.dkim_contents(
76 | MESSAGE, email="email@netius.hive.pt", creation=1464003802
77 | )
78 |
79 | self.assertEqual(result, RESULT)
80 |
--------------------------------------------------------------------------------
/src/netius/test/middleware/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/pool/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/pool/common.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.pool
34 |
35 |
36 | class EventPoolTest(unittest.TestCase):
37 |
38 | def test_event(self):
39 | pool = netius.pool.EventPool()
40 | pool.push_event(("test", 1))
41 |
42 | self.assertNotEqual(pool.events, [])
43 | self.assertEqual(pool.pop_event(), ("test", 1))
44 |
--------------------------------------------------------------------------------
/src/netius/test/servers/__init__.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
23 | """ The copyright for the module """
24 |
25 | __license__ = "Apache License, Version 2.0"
26 | """ The license for the module """
27 |
--------------------------------------------------------------------------------
/src/netius/test/servers/http.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: utf-8 -*-
3 |
4 | # Hive Netius System
5 | # Copyright (c) 2008-2024 Hive Solutions Lda.
6 | #
7 | # This file is part of Hive Netius System.
8 | #
9 | # Hive Netius System is free software: you can redistribute it and/or modify
10 | # it under the terms of the Apache License as published by the Apache
11 | # Foundation, either version 2.0 of the License, or (at your option) any
12 | # later version.
13 | #
14 | # Hive Netius System is distributed in the hope that it will be useful,
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | # Apache License for more details.
18 | #
19 | # You should have received a copy of the Apache License along with
20 | # Hive Netius System. If not, see .
21 |
22 | __author__ = "João Magalhães "
23 | """ The author(s) of the module """
24 |
25 | __copyright__ = "Copyright (c) 2008-2024 Hive Solutions Lda."
26 | """ The copyright for the module """
27 |
28 | __license__ = "Apache License, Version 2.0"
29 | """ The license for the module """
30 |
31 | import unittest
32 |
33 | import netius.servers
34 |
35 |
36 | class HTTPServerTest(unittest.TestCase):
37 |
38 | def test__headers_upper(self):
39 | http_server = netius.servers.HTTPServer()
40 | headers = {"content-type": "plain/text", "content-length": "12"}
41 | http_server._headers_upper(headers)
42 |
43 | self.assertEqual(
44 | headers, {"Content-Type": "plain/text", "Content-Length": "12"}
45 | )
46 |
47 | headers = {"content-Type": "plain/text", "content-LEngtH": "12"}
48 | http_server._headers_upper(headers)
49 |
50 | self.assertEqual(
51 | headers, {"Content-Type": "plain/text", "Content-Length": "12"}
52 | )
53 |
54 | def test__headers_normalize(self):
55 | http_server = netius.servers.HTTPServer()
56 | headers = {"Content-Type": ["plain/text"], "Content-Length": ["12"]}
57 | http_server._headers_normalize(headers)
58 |
59 | self.assertEqual(
60 | headers, {"Content-Type": "plain/text", "Content-Length": "12"}
61 | )
62 |
63 | headers = {
64 | "Content-Type": ["application/json", "charset=utf-8"],
65 | "Content-Length": "12",
66 | }
67 | http_server._headers_normalize(headers)
68 |
69 | self.assertEqual(
70 | headers,
71 | {"Content-Type": "application/json;charset=utf-8", "Content-Length": "12"},
72 | )
73 |
--------------------------------------------------------------------------------