pax_global_header 0000666 0000000 0000000 00000000064 14730517453 0014523 g ustar 00root root 0000000 0000000 52 comment=b72e8b65df4509e4627f34b4cf59477a9c2eac80
NabuCasa-snitun-b72e8b6/ 0000775 0000000 0000000 00000000000 14730517453 0015137 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/.devcontainer.json 0000664 0000000 0000000 00000002370 14730517453 0020573 0 ustar 00root root 0000000 0000000 {
"name": "NabuCasa SniTun Dev",
"image": "mcr.microsoft.com/vscode/devcontainers/python:0-3.10",
"postCreateCommand": "python3 -m pip install -e .[test,lint]",
"postStartCommand": "python3 -m pip install -e .",
"containerUser": "vscode",
"containerEnv": {
"GIT_EDITOR": "code --wait"
},
"customizations": {
"vscode": {
"extensions": [
"charliermarsh.ruff",
"esbenp.prettier-vscode",
"ms-python.python",
"ms-python.vscode-pylance",
"visualstudioexptteam.vscodeintellicode"
],
"settings": {
"python.pythonPath": "/usr/local/bin/python",
"python.formatting.provider": "ruff",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.rulers": [
88
],
"editor.codeActionsOnSave": {
"source.fixAll": "always",
"source.organizeImports": "always"
},
"files.trimTrailingWhitespace": true,
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh"
}
}
}
} NabuCasa-snitun-b72e8b6/.github/ 0000775 0000000 0000000 00000000000 14730517453 0016477 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/.github/dependabot.yml 0000664 0000000 0000000 00000000367 14730517453 0021335 0 ustar 00root root 0000000 0000000 version: 2
updates:
- package-ecosystem: pip
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 10
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: monthly
open-pull-requests-limit: 10
NabuCasa-snitun-b72e8b6/.github/release-drafter.yml 0000664 0000000 0000000 00000000324 14730517453 0022266 0 ustar 00root root 0000000 0000000 change-template: "- #$NUMBER $TITLE @$AUTHOR"
sort-direction: ascending
categories:
- title: "Dependency Updates"
label: "dependencies"
collapse-after: 1
template: |
## What’s Changed
$CHANGES
NabuCasa-snitun-b72e8b6/.github/workflows/ 0000775 0000000 0000000 00000000000 14730517453 0020534 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/.github/workflows/ci.yml 0000664 0000000 0000000 00000002061 14730517453 0021651 0 ustar 00root root 0000000 0000000 name: Run Tests
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.2.2
- name: Set up Python 3.10
uses: actions/setup-python@v5.3.0
with:
python-version: "3.10"
- name: Install dependencies
shell: bash
run: |
python3 -m pip install -e .[lint]
- name: Lint
shell: bash
run: |
scripts/lint
build:
runs-on: ubuntu-latest
needs: lint
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"
steps:
- uses: actions/checkout@v4.2.2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5.3.0
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
shell: bash
run: |
python3 -m pip install -e .[test]
- name: Test
shell: bash
run: |
scripts/test
NabuCasa-snitun-b72e8b6/.github/workflows/release-drafter.yml 0000664 0000000 0000000 00000000474 14730517453 0024331 0 ustar 00root root 0000000 0000000 name: Release Drafter
on:
push:
# branches to consider in the event; optional, defaults to all
branches:
- main
jobs:
update_release_draft:
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v6.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NabuCasa-snitun-b72e8b6/.github/workflows/release.yml 0000664 0000000 0000000 00000002434 14730517453 0022702 0 ustar 00root root 0000000 0000000 name: Upload Python Package
on:
release:
types:
- published
permissions: {}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.2.2
- name: Set up Python
uses: actions/setup-python@v5.3.0
with:
python-version: "3.x"
- name: Verify version
uses: home-assistant/actions/helpers/verify-version@master
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools build
- name: Build
run: |
python -m build
- name: Upload dists
uses: actions/upload-artifact@v4.4.3
with:
name: "dist"
path: "dist/"
if-no-files-found: error
retention-days: 5
publish:
name: Upload release to PyPI
runs-on: ubuntu-latest
needs: "build"
environment:
name: release
url: https://pypi.org/p/snitun
permissions:
id-token: write
steps:
- name: Download dists
uses: actions/download-artifact@v4.1.8
with:
name: "dist"
path: "dist/"
- name: Publish dists to PyPI
# Pinned to a commit for security purposes
uses: pypa/gh-action-pypi-publish@15c56dba361d8335944d31a2ecd17d700fc7bcbc # v1.12.2
NabuCasa-snitun-b72e8b6/.gitignore 0000664 0000000 0000000 00000002307 14730517453 0017131 0 ustar 00root root 0000000 0000000 # Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
# Editors
.vscode/
NabuCasa-snitun-b72e8b6/LICENSE 0000664 0000000 0000000 00000104515 14730517453 0016152 0 ustar 00root root 0000000 0000000 GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
Copyright (C)
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
NabuCasa-snitun-b72e8b6/README.md 0000664 0000000 0000000 00000006101 14730517453 0016414 0 ustar 00root root 0000000 0000000 # SniTun
End-to-End encryption with SNI proxy on top of a TCP multiplexer
## Connection flow
```
[ CLIENT ] --AUTH/CONFIG--> [ SESSION MASTER ] (Trusted connection)
[ CLIENT ] <--FERNET-TOKEN- [ SESSION MASTER ]
[ CLIENT ] --------FERNET-TOKEN---------------------> [ SNITUN ] (Unsecure connection)
[ CLIENT ] <-------CHALLENGE-RESPONSE-(AES/CBC)-----> [ SNITUN ]
<---> <------------------------------>
[ ENDPOINT ] <---> [ CLIENT ] <---------MULTIPLEXER---(AES/CBC)--------> [ SNITUN ] <------EXTERNAL-CONECTIONS-----> [ DEVICE ]
| <---> <------------------------------> |
| |
| <--------------------------------------------------END-TO-END-SSL------------------------------------------------->|
(Trusted connection)
```
## Fernet token
The session master creates a Fernet token from the client's configuration (AES/whitelist) and attaches the hostname and a UTC timestamp until which the token is valid.
```json
{
"valid": 1923841,
"hostname": "myname.ui.nabu.casa",
"aes_key": "hexstring",
"aes_iv": "hexstring"
}
```
The SniTun server must be able to decrypt this token to validate the client's authenticity. SniTun then initiates a challenge-response handling to validate the AES key and ensure that it is the same client that requested the Fernet token from the session master.
Note: SniTun server does not perform any user authentication!
### Challenge/Response
The SniTun server creates a SHA256 hash from a random 40-bit value. This value is encrypted and sent to the client, who then decrypts the value and performs another SHA256 hash with the value and sends it encrypted back to SniTun. If it is valid, the client enters the Multiplexer mode.
## Multiplexer Protocol
The header is encrypted using AES/CBC. The payload should be SSL. The ID changes for every TCP connection and is unique for each connection. The size is for the data payload.
The extra information could include the caller IP address for a new message. Otherwise, it is random bits.
```
|________________________________________________________|
|-----------------HEADER---------------------------------|______________________________________________|
|------ID-----|--FLAG--|--SIZE--|---------EXTRA ---------|--------------------DATA----------------------|
| 16 bytes | 1 byte | 4 bytes| 11 bytes | variable |
|--------------------------------------------------------|----------------------------------------------|
```
Message Flags/Types:
- `0x01`: New | The extra data includes the first byte as an ASCII value of 4 or 6, followed by the caller IP in bytes.
- `0x02`: DATA
- `0x04`: Close
- `0x05`: Ping | The extra data is a `ping` or `pong` response to a ping.
NabuCasa-snitun-b72e8b6/pyproject.toml 0000664 0000000 0000000 00000010613 14730517453 0020054 0 ustar 00root root 0000000 0000000 [build-system]
build-backend = "setuptools.build_meta"
requires = [
"setuptools>=62.3",
]
[project]
authors = [
{name = "Nabu Casa, Inc.", email = "opensource@nabucasa.com"},
]
classifiers = [
"Intended Audience :: End Users/Desktop",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
"Topic :: Internet :: Proxy Servers",
"Topic :: Software Development :: Libraries :: Python Modules",
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"aiohttp>=3.9.3",
"attrs>=18.2.0",
"async_timeout>=3.0.1",
"cryptography>=2.5",
]
description = "SNI proxy with TCP multiplexer"
keywords = ["sni", "proxy", "multiplexer", "tls"]
license = {text = "GPL v3"}
name = "snitun"
readme = "README.md"
requires-python = ">=3.10"
version = "0.40.0"
[project.optional-dependencies]
lint = [
"pylint==3.2.7",
"ruff==0.8.1",
]
test = [
"pytest-aiohttp==1.0.5",
"pytest-timeout==2.3.1",
"pytest==8.3.4",
]
[project.urls]
Homepage = "https://www.nabucasa.com/"
Repository = "https://github.com/NabuCasa/snitun.git"
[tool.isort]
combine_as_imports = true
default_section = "THIRDPARTY"
force_grid_wrap = 0
force_sort_within_sections = true
forced_separate = "tests"
include_trailing_comma = true
indent = " "
line_length = 88
multi_line_output = 3
sections = [
"FUTURE",
"STDLIB",
"THIRDPARTY",
"FIRSTPARTY",
"LOCALFOLDER",
]
use_parentheses = true
[tool.pylint.BASIC]
disable = [
"abstract-method",
"cyclic-import",
"duplicate-code",
"global-statement",
"line-too-long",
"locally-disabled",
"missing-docstring",
"not-context-manager",
"protected-access",
"too-few-public-methods",
"too-few-public-methods",
"too-many-arguments",
"too-many-branches",
"too-many-branches",
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
"too-many-locals",
"too-many-public-methods",
"too-many-return-statements",
"too-many-statements",
"unused-argument",
]
generated-members=[
"botocore.errorfactory",
]
good-names= [
"_",
"ex",
"fp",
"i",
"id",
"iv",
"j",
"k",
"Run",
"T",
]
[tool.pylint.EXCEPTIONS]
overgeneral-exceptions=[
"builtins.Exception",
]
[tool.pylint.MAIN]
ignore=[
"tests_*",
]
reports=false
[tool.pytest.ini_options]
asyncio_mode = "auto"
[tool.ruff]
fix = true
line-length = 88
show-fixes = true
target-version = "py310"
[tool.ruff.lint]
ignore = [
"ANN101", # https://docs.astral.sh/ruff/rules/missing-type-self/
"EM101", # https://docs.astral.sh/ruff/rules/raw-string-in-exception/
"EM102", # https://docs.astral.sh/ruff/rules/f-string-in-exception/
"FBT", # https://docs.astral.sh/ruff/rules/#flake8-boolean-trap-fbt
"N818", # https://docs.astral.sh/ruff/rules/error-suffix-on-exception-name/
"PLR2004", # https://docs.astral.sh/ruff/rules/magic-value-comparison/
"PERF203", # https://docs.astral.sh/ruff/rules/try-except-in-loop/
"S101", # https://docs.astral.sh/ruff/rules/assert/
"S104", # https://docs.astral.sh/ruff/rules/hardcoded-bind-all-interfaces/
"TCH001", # https://docs.astral.sh/ruff/rules/typing-only-first-party-import/
"TCH003", # https://docs.astral.sh/ruff/rules/typing-only-standard-library-import/
"TID252", # https://docs.astral.sh/ruff/rules/relative-imports/
"TRY003", # https://docs.astral.sh/ruff/rules/raise-vanilla-args/
"TRY301", # https://docs.astral.sh/ruff/rules/raise-within-try/
"TRY400", # https://docs.astral.sh/ruff/rules/error-instead-of-exception/
]
select = [
"ALL",
]
[tool.ruff.lint.extend-per-file-ignores]
"py.typed" = [
"D100",
]
[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
mark-parentheses = false
[tool.ruff.lint.isort]
combine-as-imports = true
force-sort-within-sections = true
known-first-party = [
"snitun",
]
[tool.ruff.lint.mccabe]
max-complexity = 22
[tool.ruff.lint.pydocstyle]
# Use Google-style docstrings.
convention = "pep257"
[tool.ruff.lint.pylint]
max-args = 15
max-branches = 30
max-returns = 8
max-statements = 80
[tool.setuptools]
include-package-data = true
platforms = [
"any",
]
zip-safe = false
[tool.setuptools.packages.find]
include = [
"snitun",
"snitun.server",
"snitun.client",
"snitun.multiplexer",
"snitun.utils",
]
NabuCasa-snitun-b72e8b6/scripts/ 0000775 0000000 0000000 00000000000 14730517453 0016626 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/scripts/lint 0000775 0000000 0000000 00000000132 14730517453 0017516 0 ustar 00root root 0000000 0000000 #!/bin/sh
cd "$(dirname "$0")/.."
python3 -m ruff check snitun
python3 -m pylint snitun
NabuCasa-snitun-b72e8b6/scripts/test 0000775 0000000 0000000 00000000066 14730517453 0017535 0 ustar 00root root 0000000 0000000 #!/bin/sh
cd "$(dirname "$0")/.."
python3 -m pytest
NabuCasa-snitun-b72e8b6/snitun/ 0000775 0000000 0000000 00000000000 14730517453 0016457 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/snitun/__init__.py 0000664 0000000 0000000 00000000054 14730517453 0020567 0 ustar 00root root 0000000 0000000 """SniTun - SNI Proxy + TCP multiplexer."""
NabuCasa-snitun-b72e8b6/snitun/client/ 0000775 0000000 0000000 00000000000 14730517453 0017735 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/snitun/client/__init__.py 0000664 0000000 0000000 00000000035 14730517453 0022044 0 ustar 00root root 0000000 0000000 """SniTun Client library."""
NabuCasa-snitun-b72e8b6/snitun/client/client_peer.py 0000664 0000000 0000000 00000011034 14730517453 0022577 0 ustar 00root root 0000000 0000000 """SniTun client for server connection."""
from __future__ import annotations
import asyncio
import hashlib
import logging
import async_timeout
from ..exceptions import (
MultiplexerTransportDecrypt,
MultiplexerTransportError,
SniTunConnectionError,
)
from ..multiplexer.core import Multiplexer
from ..multiplexer.crypto import CryptoTransport
from .connector import Connector
_LOGGER = logging.getLogger(__name__)
CONNECTION_TIMEOUT = 60
class ClientPeer:
"""Client to SniTun Server."""
def __init__(self, snitun_host: str, snitun_port: int | None = None) -> None:
"""Initialize ClientPeer connector."""
self._multiplexer = None
self._loop = asyncio.get_event_loop()
self._snitun_host = snitun_host
self._snitun_port = snitun_port or 8080
@property
def is_connected(self) -> bool:
"""Return true, if a connection exists."""
return self._multiplexer is not None
def wait(self) -> asyncio.Task:
"""Block until connection to peer is closed."""
if not self._multiplexer:
raise RuntimeError("No SniTun connection available")
return self._multiplexer.wait()
async def start(
self,
connector: Connector,
fernet_token: bytes,
aes_key: bytes,
aes_iv: bytes,
throttling: int | None = None,
) -> None:
"""Connect an start ClientPeer."""
if self._multiplexer:
raise RuntimeError("SniTun connection available")
# Connect to SniTun server
_LOGGER.debug(
"Opening connection to %s:%s", self._snitun_host, self._snitun_port,
)
try:
async with async_timeout.timeout(CONNECTION_TIMEOUT):
reader, writer = await asyncio.open_connection(
host=self._snitun_host, port=self._snitun_port,
)
except asyncio.TimeoutError:
raise SniTunConnectionError(
"Connection timeout for SniTun server "
f"{self._snitun_host}:{self._snitun_port}",
) from None
except OSError as err:
raise SniTunConnectionError(
"Can't connect to SniTun server "
f"{self._snitun_host}:{self._snitun_port} with: {err}",
) from err
# Send fernet token
writer.write(fernet_token)
try:
async with async_timeout.timeout(CONNECTION_TIMEOUT):
await writer.drain()
except asyncio.TimeoutError:
raise SniTunConnectionError(
"Timeout for writting connection token",
) from None
# Challenge/Response
crypto = CryptoTransport(aes_key, aes_iv)
try:
async with async_timeout.timeout(CONNECTION_TIMEOUT):
challenge = await reader.readexactly(32)
answer = hashlib.sha256(crypto.decrypt(challenge)).digest()
writer.write(crypto.encrypt(answer))
await writer.drain()
except asyncio.TimeoutError:
raise SniTunConnectionError(
"Challenge/Response timeout error to SniTun server",
) from None
except (
MultiplexerTransportDecrypt,
asyncio.IncompleteReadError,
OSError,
) as err:
raise SniTunConnectionError(
f"Challenge/Response error with SniTun server ({err})",
) from err
# Run multiplexer
self._multiplexer = Multiplexer(
crypto,
reader,
writer,
new_connections=connector.handler,
throttling=throttling,
)
# Task a process for pings/cleanups
self._loop.create_task(self._handler())
async def stop(self) -> None:
"""Stop connection to SniTun server."""
if not self._multiplexer:
raise RuntimeError("No SniTun connection available")
self._multiplexer.shutdown()
await self._multiplexer.wait()
async def _handler(self) -> None:
"""Wait until connection is closed."""
async def _wait_with_timeout() -> None:
try:
async with async_timeout.timeout(50):
await self._multiplexer.wait()
except asyncio.TimeoutError:
await self._multiplexer.ping()
try:
while self._multiplexer.is_connected:
await _wait_with_timeout()
except MultiplexerTransportError:
pass
finally:
self._multiplexer = None
NabuCasa-snitun-b72e8b6/snitun/client/connector.py 0000664 0000000 0000000 00000011002 14730517453 0022273 0 ustar 00root root 0000000 0000000 """Connector to end resource."""
from __future__ import annotations
import asyncio
from collections.abc import Coroutine
from contextlib import suppress
import ipaddress
import logging
from typing import Any
from ..exceptions import MultiplexerTransportClose, MultiplexerTransportError
from ..multiplexer.channel import MultiplexerChannel
from ..multiplexer.core import Multiplexer
_LOGGER = logging.getLogger(__name__)
class Connector:
"""Connector to end resource."""
def __init__(
self,
end_host: str,
end_port: int | None=None,
whitelist: bool=False,
endpoint_connection_error_callback: Coroutine[Any, Any, None] | None = None,
) -> None:
"""Initialize Connector."""
self._loop = asyncio.get_event_loop()
self._end_host = end_host
self._end_port = end_port or 443
self._whitelist = set()
self._whitelist_enabled = whitelist
self._endpoint_connection_error_callback = endpoint_connection_error_callback
@property
def whitelist(self) -> set:
"""Allow to block requests per IP Return None or access to a set."""
return self._whitelist
def _whitelist_policy(self, ip_address: ipaddress.IPv4Address) -> bool:
"""Return True if the ip address can access to endpoint."""
if self._whitelist_enabled:
return ip_address in self._whitelist
return True
async def handler(
self, multiplexer: Multiplexer, channel: MultiplexerChannel,
) -> None:
"""Handle new connection from SNIProxy."""
from_endpoint = None
from_peer = None
_LOGGER.debug(
"Receive from %s a request for %s", channel.ip_address, self._end_host,
)
# Check policy
if not self._whitelist_policy(channel.ip_address):
_LOGGER.warning("Block request from %s per policy", channel.ip_address)
await multiplexer.delete_channel(channel)
return
# Open connection to endpoint
try:
reader, writer = await asyncio.open_connection(
host=self._end_host, port=self._end_port,
)
except OSError:
_LOGGER.error(
"Can't connect to endpoint %s:%s", self._end_host, self._end_port,
)
await multiplexer.delete_channel(channel)
if self._endpoint_connection_error_callback:
await self._endpoint_connection_error_callback()
return
try:
# Process stream from multiplexer
while not writer.transport.is_closing():
if not from_endpoint:
from_endpoint = self._loop.create_task(reader.read(4096))
if not from_peer:
from_peer = self._loop.create_task(channel.read())
# Wait until data need to be processed
await asyncio.wait(
[from_endpoint, from_peer], return_when=asyncio.FIRST_COMPLETED,
)
# From proxy
if from_endpoint.done():
if from_endpoint.exception():
raise from_endpoint.exception()
await channel.write(from_endpoint.result())
from_endpoint = None
# From peer
if from_peer.done():
if from_peer.exception():
raise from_peer.exception()
writer.write(from_peer.result())
from_peer = None
# Flush buffer
await writer.drain()
except (MultiplexerTransportError, OSError, RuntimeError):
_LOGGER.debug("Transport closed by endpoint for %s", channel.id)
with suppress(MultiplexerTransportError):
await multiplexer.delete_channel(channel)
except MultiplexerTransportClose:
_LOGGER.debug("Peer close connection for %s", channel.id)
finally:
# Cleanup peer reader
if from_peer:
if not from_peer.done():
from_peer.cancel()
else:
# Avoid exception was never retrieved
from_peer.exception()
# Cleanup endpoint reader
if from_endpoint and not from_endpoint.done():
from_endpoint.cancel()
# Close Transport
if not writer.transport.is_closing():
with suppress(OSError):
writer.close()
NabuCasa-snitun-b72e8b6/snitun/exceptions.py 0000664 0000000 0000000 00000001511 14730517453 0021210 0 ustar 00root root 0000000 0000000 """SniTun Exceptions."""
class SniTunError(Exception):
"""Base Exception for SniTun exceptions."""
class SniTunChallengeError(SniTunError):
"""Raise if a challenge error is occure."""
class SniTunInvalidPeer(SniTunError):
"""Raise if peer config is invalid."""
class ParseSNIError(SniTunError):
"""Invalid ClientHello data."""
class ParseSNIIncompleteError(ParseSNIError):
"""Incomplete ClientHello data."""
class MultiplexerTransportError(SniTunError):
"""Raise if multiplexer have an problem with peer."""
class MultiplexerTransportClose(SniTunError):
"""Raise if connection to peer is closed."""
class MultiplexerTransportDecrypt(SniTunError):
"""Raise if decryption of message fails."""
class SniTunConnectionError(SniTunError):
"""Raise if SniTun client can't connect to server."""
NabuCasa-snitun-b72e8b6/snitun/multiplexer/ 0000775 0000000 0000000 00000000000 14730517453 0021031 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/snitun/multiplexer/__init__.py 0000664 0000000 0000000 00000000036 14730517453 0023141 0 ustar 00root root 0000000 0000000 """Multiplexer for SniTun."""
NabuCasa-snitun-b72e8b6/snitun/multiplexer/channel.py 0000664 0000000 0000000 00000007247 14730517453 0023025 0 ustar 00root root 0000000 0000000 """Multiplexer channel."""
from __future__ import annotations
import asyncio
from contextlib import suppress
from ipaddress import IPv4Address
import logging
import async_timeout
from ..exceptions import MultiplexerTransportClose, MultiplexerTransportError
from ..utils.ipaddress import ip_address_to_bytes
from .message import (
CHANNEL_FLOW_CLOSE,
CHANNEL_FLOW_DATA,
CHANNEL_FLOW_NEW,
MultiplexerChannelId,
MultiplexerMessage,
)
_LOGGER = logging.getLogger(__name__)
class MultiplexerChannel:
"""Represent a multiplexer channel."""
__slots__ = ["_input", "_output", "_id", "_ip_address", "_throttling", "_closing"]
def __init__(
self,
output: asyncio.Queue,
ip_address: IPv4Address,
channel_id: MultiplexerChannelId | None = None,
throttling: float | None = None,
) -> None:
"""Initialize Multiplexer Channel."""
self._input = asyncio.Queue(8000)
self._output = output
self._id = channel_id or MultiplexerChannelId()
self._ip_address = ip_address
self._throttling = throttling
self._closing = False
@property
def id(self) -> MultiplexerChannelId:
"""Return ID of this channel."""
return self._id
@property
def ip_address(self) -> IPv4Address:
"""Return caller IP4Address."""
return self._ip_address
@property
def healthy(self) -> bool:
"""Return True if a error is occurse."""
return self._input.full()
@property
def closing(self) -> bool:
"""Return True if channel is in closing state."""
return self._closing
def close(self) -> None:
"""Close channel on next run."""
self._closing = True
with suppress(asyncio.QueueFull):
self._input.put_nowait(None)
async def write(self, data: bytes) -> None:
"""Send data to peer."""
if not data:
raise MultiplexerTransportError
if self._closing:
raise MultiplexerTransportClose
# Create message
message = MultiplexerMessage(self._id, CHANNEL_FLOW_DATA, data)
try:
async with async_timeout.timeout(5):
await self._output.put(message)
except asyncio.TimeoutError:
_LOGGER.debug("Can't write to peer transport")
raise MultiplexerTransportError from None
if not self._throttling:
return
await asyncio.sleep(self._throttling)
async def read(self) -> MultiplexerMessage:
"""Read data from peer."""
if self._closing and self._input.empty():
message = None
else:
message = await self._input.get()
# Send data
if message is not None:
return message.data
_LOGGER.debug("Read a close message for channel %s", self._id)
raise MultiplexerTransportClose
def init_close(self) -> MultiplexerMessage:
"""Init close message for transport."""
_LOGGER.debug("Close channel %s", self._id)
return MultiplexerMessage(self._id, CHANNEL_FLOW_CLOSE)
def init_new(self) -> MultiplexerMessage:
"""Init new session for transport."""
_LOGGER.debug("New channel %s", self._id)
extra = b"4" + ip_address_to_bytes(self.ip_address)
return MultiplexerMessage(self._id, CHANNEL_FLOW_NEW, b"", extra)
def message_transport(self, message: MultiplexerMessage) -> None:
"""Only for internal ussage of core transport."""
if self._closing:
return
try:
self._input.put_nowait(message)
except asyncio.QueueFull:
_LOGGER.warning("Channel %s input is full", self._id)
NabuCasa-snitun-b72e8b6/snitun/multiplexer/core.py 0000664 0000000 0000000 00000024760 14730517453 0022344 0 ustar 00root root 0000000 0000000 """Multiplexer for SniTun."""
from __future__ import annotations
import asyncio
from collections.abc import Coroutine
from contextlib import suppress
import ipaddress
import logging
import os
from typing import Any
import async_timeout
from ..exceptions import (
MultiplexerTransportClose,
MultiplexerTransportDecrypt,
MultiplexerTransportError,
)
from ..utils.ipaddress import bytes_to_ip_address
from .channel import MultiplexerChannel
from .crypto import CryptoTransport
from .message import (
CHANNEL_FLOW_CLOSE,
CHANNEL_FLOW_DATA,
CHANNEL_FLOW_NEW,
CHANNEL_FLOW_PING,
MultiplexerChannelId,
MultiplexerMessage,
)
_LOGGER = logging.getLogger(__name__)
PEER_TCP_TIMEOUT = 90
class Multiplexer:
"""Multiplexer Socket wrapper."""
__slots__ = [
"_crypto",
"_reader",
"_writer",
"_loop",
"_queue",
"_healthy",
"_processing_task",
"_channels",
"_new_connections",
"_throttling",
]
def __init__(
self,
crypto: CryptoTransport,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
new_connections: Coroutine[Any, Any, None] | None =None,
throttling: int | None = None,
) -> None:
"""Initialize Multiplexer."""
self._crypto = crypto
self._reader = reader
self._writer = writer
self._loop = asyncio.get_event_loop()
self._queue = asyncio.Queue(12000)
self._healthy = asyncio.Event()
self._processing_task = self._loop.create_task(self._runner())
self._channels = {}
self._new_connections = new_connections
self._throttling = 1 / throttling if throttling else None
@property
def is_connected(self) -> bool:
"""Return True is they is connected."""
return not self._processing_task.done()
def wait(self) -> asyncio.Task:
"""Block until the connection is closed.
Return a awaitable object.
"""
return asyncio.shield(self._processing_task)
def shutdown(self) -> None:
"""Shutdown connection."""
if self._processing_task.done():
return
_LOGGER.debug("Cancel connection")
self._processing_task.cancel()
self._graceful_channel_shutdown()
def _graceful_channel_shutdown(self) -> None:
"""Graceful shutdown of channels."""
for channel in self._channels.values():
channel.close()
self._channels.clear()
async def ping(self) -> None:
"""Send a ping flow message to hold the connection open."""
self._healthy.clear()
try:
self._write_message(
MultiplexerMessage(
MultiplexerChannelId(), CHANNEL_FLOW_PING, b"", b"ping",
),
)
# Wait until pong is received
async with async_timeout.timeout(PEER_TCP_TIMEOUT):
await self._healthy.wait()
except asyncio.TimeoutError:
_LOGGER.error("Timeout error while pinging peer")
self._loop.call_soon(self.shutdown)
raise MultiplexerTransportError from None
except OSError as exception:
_LOGGER.error("Peer ping failed - %s", exception)
self._loop.call_soon(self.shutdown)
raise MultiplexerTransportError from None
async def _runner(self) -> None:
"""Runner task of processing stream."""
transport = self._writer.transport
from_peer = None
to_peer = None
# Process stream
self._healthy.set()
try:
while not transport.is_closing():
if not from_peer:
from_peer = self._loop.create_task(self._reader.readexactly(32))
if not to_peer:
to_peer = self._loop.create_task(self._queue.get())
# Wait until data need to be processed
async with async_timeout.timeout(PEER_TCP_TIMEOUT):
await asyncio.wait(
[from_peer, to_peer], return_when=asyncio.FIRST_COMPLETED,
)
# From peer
if from_peer.done():
if from_peer.exception():
raise from_peer.exception()
await self._read_message(from_peer.result())
from_peer = None
# To peer
if to_peer.done():
if to_peer.exception():
raise to_peer.exception()
self._write_message(to_peer.result())
to_peer = None
# Flush buffer
await self._writer.drain()
# throttling
if not self._throttling:
continue
await asyncio.sleep(self._throttling)
except (asyncio.CancelledError, asyncio.TimeoutError, TimeoutError):
_LOGGER.debug("Receive canceling")
with suppress(OSError):
self._writer.write_eof()
await self._writer.drain()
except (
MultiplexerTransportClose,
asyncio.IncompleteReadError,
ConnectionResetError,
OSError,
):
_LOGGER.debug("Transport was closed")
finally:
# Cleanup peer writer
if to_peer and not to_peer.done():
to_peer.cancel()
# Cleanup peer reader
if from_peer:
if not from_peer.done():
from_peer.cancel()
else:
# Avoid exception was never retrieved
from_peer.exception()
# Cleanup transport
if not transport.is_closing():
with suppress(OSError):
self._writer.close()
self._graceful_channel_shutdown()
_LOGGER.debug("Multiplexer connection is closed")
def _write_message(self, message: MultiplexerMessage) -> None:
"""Write message to peer."""
header = message.id.bytes
header += message.flow_type.to_bytes(1, byteorder="big")
header += len(message.data).to_bytes(4, byteorder="big")
header += message.extra + os.urandom(11 - len(message.extra))
data = self._crypto.encrypt(header) + message.data
try:
self._writer.write(data)
except RuntimeError:
raise MultiplexerTransportClose from None
async def _read_message(self, header: bytes) -> None:
"""Read message from peer."""
if not header:
raise MultiplexerTransportClose
try:
header = self._crypto.decrypt(header)
channel_id = header[:16]
flow_type = header[16]
data_size = int.from_bytes(header[17:21], byteorder="big")
extra = header[21:]
except (IndexError, MultiplexerTransportDecrypt):
_LOGGER.warning("Wrong message header received")
return
# Read message data
if data_size:
data = await self._reader.readexactly(data_size)
else:
data = b""
message = MultiplexerMessage(
MultiplexerChannelId(channel_id), flow_type, data, extra,
)
# Process message to queue
await self._process_message(message)
async def _process_message(self, message: MultiplexerMessage) -> None:
"""Process received message."""
# DATA
if message.flow_type == CHANNEL_FLOW_DATA:
# check if message exists
if message.id not in self._channels:
_LOGGER.debug("Receive data from unknown channel")
return
channel = self._channels[message.id]
if channel.closing:
pass
elif channel.healthy:
_LOGGER.warning("Abort connection, channel is not healthy")
channel.close()
self._loop.create_task(self.delete_channel(channel))
else:
channel.message_transport(message)
# New
elif message.flow_type == CHANNEL_FLOW_NEW:
# Check if we would handle new connection
if not self._new_connections:
_LOGGER.warning("Request new Channel is not allow")
return
ip_address = bytes_to_ip_address(message.extra[1:5])
channel = MultiplexerChannel(
self._queue,
ip_address,
channel_id=message.id,
throttling=self._throttling,
)
self._channels[channel.id] = channel
self._loop.create_task(self._new_connections(self, channel))
# Close
elif message.flow_type == CHANNEL_FLOW_CLOSE:
# check if message exists
if message.id not in self._channels:
_LOGGER.debug("Receive close from unknown channel")
return
channel = self._channels.pop(message.id)
channel.close()
# Ping
elif message.flow_type == CHANNEL_FLOW_PING:
if message.extra.startswith(b"pong"):
_LOGGER.debug("Receive pong from peer / reset healthy")
self._healthy.set()
else:
_LOGGER.debug("Receive ping from peer / send pong")
self._write_message(
MultiplexerMessage(message.id, CHANNEL_FLOW_PING, b"", b"pong"),
)
else:
_LOGGER.warning("Receive unknown message type")
async def create_channel(
self, ip_address: ipaddress.IPv4Address,
) -> MultiplexerChannel:
"""Create a new channel for transport."""
channel = MultiplexerChannel(
self._queue, ip_address, throttling=self._throttling,
)
message = channel.init_new()
try:
async with async_timeout.timeout(5):
await self._queue.put(message)
except asyncio.TimeoutError:
raise MultiplexerTransportError from None
self._channels[channel.id] = channel
return channel
async def delete_channel(self, channel: MultiplexerChannel) -> None:
"""Delete channel from transport."""
message = channel.init_close()
try:
async with async_timeout.timeout(5):
await self._queue.put(message)
except asyncio.TimeoutError:
raise MultiplexerTransportError from None
finally:
self._channels.pop(channel.id, None)
NabuCasa-snitun-b72e8b6/snitun/multiplexer/crypto.py 0000664 0000000 0000000 00000002103 14730517453 0022717 0 ustar 00root root 0000000 0000000 """Encrypt or Decrypt multiplexer transport data."""
from cryptography.exceptions import InvalidTag
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from ..exceptions import MultiplexerTransportDecrypt
class CryptoTransport:
"""Encrypt/Decrypt Transport flow."""
__slots__ = ["_cipher", "_encryptor", "_decryptor"]
def __init__(self, key: bytes, iv: bytes) -> None:
"""Initialize crypto data."""
self._cipher = Cipher(
algorithms.AES(key), modes.CBC(iv), backend=default_backend(),
)
self._encryptor = self._cipher.encryptor()
self._decryptor = self._cipher.decryptor()
def encrypt(self, data: bytes) -> bytes:
"""Encrypt data from transport."""
return self._encryptor.update(data)
def decrypt(self, data: bytes) -> bytes:
"""Decrypt data from transport."""
try:
return self._decryptor.update(data)
except InvalidTag:
raise MultiplexerTransportDecrypt from None
NabuCasa-snitun-b72e8b6/snitun/multiplexer/message.py 0000664 0000000 0000000 00000002135 14730517453 0023030 0 ustar 00root root 0000000 0000000 """Multiplexer message handling."""
import binascii
import os
import attr
CHANNEL_FLOW_NEW = 0x01
CHANNEL_FLOW_DATA = 0x02
CHANNEL_FLOW_CLOSE = 0x04
CHANNEL_FLOW_PING = 0x08
CHANNEL_FLOW_ALL = [
CHANNEL_FLOW_NEW,
CHANNEL_FLOW_CLOSE,
CHANNEL_FLOW_DATA,
CHANNEL_FLOW_PING,
]
@attr.s(frozen=True, slots=True, eq=True, hash=True)
class MultiplexerChannelId:
"""Represent a channel ID aka multiplexer stream."""
bytes: bytes = attr.ib(default=attr.Factory(lambda: os.urandom(16)), eq=True)
hex: str = attr.ib(
default=attr.Factory(
lambda self: binascii.hexlify(self.bytes).decode("utf-8"), takes_self=True,
),
eq=False,
)
def __str__(self) -> str:
"""Return string representation for logger."""
return self.hex
@attr.s(frozen=True, slots=True)
class MultiplexerMessage:
"""Represent a message from multiplexer stream."""
id: MultiplexerChannelId = attr.ib()
flow_type: int = attr.ib(validator=attr.validators.in_(CHANNEL_FLOW_ALL))
data: bytes = attr.ib(default=b"")
extra: bytes = attr.ib(default=b"")
NabuCasa-snitun-b72e8b6/snitun/server/ 0000775 0000000 0000000 00000000000 14730517453 0017765 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/snitun/server/__init__.py 0000664 0000000 0000000 00000000035 14730517453 0022074 0 ustar 00root root 0000000 0000000 """SniTun server library."""
NabuCasa-snitun-b72e8b6/snitun/server/listener_peer.py 0000664 0000000 0000000 00000005323 14730517453 0023202 0 ustar 00root root 0000000 0000000 """Public peer interface."""
from __future__ import annotations
import asyncio
from contextlib import suppress
import logging
import async_timeout
from ..exceptions import SniTunChallengeError, SniTunInvalidPeer
from .peer_manager import PeerManager
_LOGGER = logging.getLogger(__name__)
CHECK_VALID_EXPIRE = 14400
class PeerListener:
"""Peer Listener class."""
def __init__(
self,
peer_manager: PeerManager,
host: str | None = None,
port: int | None = None,
) -> None:
"""Initialize SNI Proxy interface."""
self._peer_manager = peer_manager
self._host = host
self._port = port or 8080
self._server = None
async def start(self) -> None:
"""Start peer server."""
self._server = await asyncio.start_server(
self.handle_connection,
host=self._host,
port=self._port,
)
async def stop(self) -> None:
"""Stop peer server."""
self._server.close()
await self._server.wait_closed()
async def handle_connection(
self,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
data: bytes | None = None,
) -> None:
"""Handle incoming requests."""
if not data:
try:
async with async_timeout.timeout(2):
fernet_data = await reader.read(2048)
except asyncio.TimeoutError:
_LOGGER.warning("Abort peer handshake")
writer.close()
return
except OSError:
return
else:
fernet_data = data
peer = None
try:
# Connection closed before data received
if not fernet_data:
return
peer = self._peer_manager.create_peer(fernet_data)
# Start multiplexer
await peer.init_multiplexer_challenge(reader, writer)
self._peer_manager.add_peer(peer)
while peer.is_connected:
try:
async with async_timeout.timeout(CHECK_VALID_EXPIRE):
await peer.wait_disconnect()
except asyncio.TimeoutError:
if not peer.is_valid:
break
except SniTunInvalidPeer:
_LOGGER.debug("Close because invalid fernet data")
except SniTunChallengeError:
_LOGGER.debug("Close because challenge was wrong")
finally:
if peer:
self._peer_manager.remove_peer(peer)
# Cleanup transport
if not writer.transport.is_closing():
with suppress(OSError):
writer.close()
NabuCasa-snitun-b72e8b6/snitun/server/listener_sni.py 0000664 0000000 0000000 00000013740 14730517453 0023042 0 ustar 00root root 0000000 0000000 """Public proxy interface with SNI."""
from __future__ import annotations
import asyncio
from contextlib import suppress
import ipaddress
import logging
import async_timeout
from ..exceptions import (
MultiplexerTransportClose,
MultiplexerTransportError,
ParseSNIError,
)
from ..multiplexer.core import Multiplexer
from .peer_manager import PeerManager
from .sni import parse_tls_sni, payload_reader
_LOGGER = logging.getLogger(__name__)
TCP_SESSION_TIMEOUT = 60
class SNIProxy:
"""SNI Proxy class."""
def __init__(
self,
peer_manager: PeerManager,
host: str | None = None,
port: int | None = None,
) -> None:
"""Initialize SNI Proxy interface."""
self._peer_manager = peer_manager
self._loop = asyncio.get_event_loop()
self._host = host
self._port = port or 443
self._server = None
async def start(self) -> None:
"""Start Proxy server."""
self._server = await asyncio.start_server(
self.handle_connection,
host=self._host,
port=self._port,
)
async def stop(self) -> None:
"""Stop proxy server."""
self._server.close()
await self._server.wait_closed()
async def handle_connection(
self,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
data: bytes | None = None,
sni: str | None = None,
) -> None:
"""Handle incoming requests."""
if data is None:
try:
async with async_timeout.timeout(2):
client_hello = await payload_reader(reader)
except asyncio.TimeoutError:
_LOGGER.warning("Abort SNI handshake")
writer.close()
return
except OSError:
return
else:
client_hello = data
# Connection closed before data received
if not client_hello:
with suppress(OSError):
writer.close()
return
try:
# Read Hostname
if sni is None:
try:
hostname = parse_tls_sni(client_hello)
except ParseSNIError:
_LOGGER.warning("Receive invalid ClientHello on public Interface")
return
else:
hostname = sni
# Peer available?
if not self._peer_manager.peer_available(hostname):
_LOGGER.debug("Hostname %s not connected", hostname)
return
peer = self._peer_manager.get_peer(hostname)
# Proxy data over mutliplexer to client
_LOGGER.debug("Processing for hostname %s started", hostname)
await self._proxy_peer(peer.multiplexer, client_hello, reader, writer)
finally:
if not writer.transport.is_closing():
with suppress(OSError):
writer.close()
async def _proxy_peer(
self,
multiplexer: Multiplexer,
client_hello: bytes,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
) -> None:
"""Proxy data between end points."""
transport = writer.transport
try:
ip_address = ipaddress.ip_address(writer.get_extra_info("peername")[0])
except (TypeError, AttributeError):
_LOGGER.error("Can't read source IP")
return
# Open multiplexer channel
try:
channel = await multiplexer.create_channel(ip_address)
except MultiplexerTransportError:
_LOGGER.error("New transport channel to peer fails")
return
from_proxy = None
from_peer = None
try:
await channel.write(client_hello)
# Process stream into multiplexer
while not transport.is_closing():
if not from_proxy:
from_proxy = self._loop.create_task(reader.read(4096))
if not from_peer:
from_peer = self._loop.create_task(channel.read())
# Wait until data need to be processed
async with async_timeout.timeout(TCP_SESSION_TIMEOUT):
await asyncio.wait(
[from_proxy, from_peer],
return_when=asyncio.FIRST_COMPLETED,
)
# From proxy
if from_proxy.done():
if from_proxy.exception():
raise from_proxy.exception()
await channel.write(from_proxy.result())
from_proxy = None
# From peer
if from_peer.done():
if from_peer.exception():
raise from_peer.exception()
writer.write(from_peer.result())
from_peer = None
# Flush buffer
await writer.drain()
except (asyncio.TimeoutError, TimeoutError):
_LOGGER.debug("Close TCP session after timeout for %s", channel.id)
with suppress(MultiplexerTransportError):
await multiplexer.delete_channel(channel)
except (MultiplexerTransportError, OSError, RuntimeError, ConnectionResetError):
_LOGGER.debug("Transport closed by Proxy for %s", channel.id)
with suppress(MultiplexerTransportError):
await multiplexer.delete_channel(channel)
except MultiplexerTransportClose:
_LOGGER.debug("Peer close connection for %s", channel.id)
finally:
# Cleanup peer reader
if from_peer:
if not from_peer.done():
from_peer.cancel()
else:
# Avoid exception was never retrieved
from_peer.exception()
# Cleanup proxy reader
if from_proxy and not from_proxy.done():
from_proxy.cancel()
NabuCasa-snitun-b72e8b6/snitun/server/peer.py 0000664 0000000 0000000 00000006463 14730517453 0021303 0 ustar 00root root 0000000 0000000 """Represent a single Peer."""
from __future__ import annotations
import asyncio
from collections.abc import Coroutine
from datetime import datetime, timezone
import hashlib
import logging
import os
import async_timeout
from ..exceptions import MultiplexerTransportDecrypt, SniTunChallengeError
from ..multiplexer.core import Multiplexer
from ..multiplexer.crypto import CryptoTransport
_LOGGER = logging.getLogger(__name__)
class Peer:
"""Representation of a Peer."""
def __init__(
self,
hostname: str,
valid: datetime,
aes_key: bytes,
aes_iv: bytes,
throttling: int | None = None,
alias: list[str] | None = None,
) -> None:
"""Initialize a Peer."""
self._hostname = hostname
self._valid = valid
self._throttling = throttling
self._alias = alias or []
self._multiplexer = None
self._crypto = CryptoTransport(aes_key, aes_iv)
@property
def hostname(self) -> str:
"""Return his hostname."""
return self._hostname
@property
def alias(self) -> list[str]:
"""Return the alias."""
return self._alias
@property
def all_hostnames(self) -> list[str]:
"""Return a list of the base hostname and any alias."""
return [self._hostname, *self._alias]
@property
def is_connected(self) -> bool:
"""Return True if we are connected to peer."""
if not self._multiplexer:
return False
return self._multiplexer.is_connected
@property
def is_valid(self) -> bool:
"""Return True if the peer is valid."""
return self._valid > datetime.now(tz=timezone.utc)
@property
def multiplexer(self) -> Multiplexer | None:
"""Return Multiplexer object."""
return self._multiplexer
@property
def is_ready(self) -> bool:
"""Return true if the Peer is ready to process data."""
if self.multiplexer is None:
return False
if not self.multiplexer.is_connected:
return False
return True
async def init_multiplexer_challenge(
self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter,
) -> None:
"""Initialize multiplexer."""
try:
token = hashlib.sha256(os.urandom(40)).digest()
writer.write(self._crypto.encrypt(token))
async with async_timeout.timeout(60):
await writer.drain()
data = await reader.readexactly(32)
# Check Token
data = self._crypto.decrypt(data)
assert hashlib.sha256(token).digest() == data
except (
asyncio.TimeoutError,
asyncio.IncompleteReadError,
MultiplexerTransportDecrypt,
AssertionError,
OSError,
) as err:
raise SniTunChallengeError("Wrong challenge from peer") from err
# Start Multiplexer
self._multiplexer = Multiplexer(
self._crypto, reader, writer, throttling=self._throttling,
)
def wait_disconnect(self) -> Coroutine:
"""Wait until peer is disconnected.
Return a coroutine.
"""
if not self._multiplexer:
raise RuntimeError("No Transport initialize for peer")
return self._multiplexer.wait()
NabuCasa-snitun-b72e8b6/snitun/server/peer_manager.py 0000664 0000000 0000000 00000010372 14730517453 0022767 0 ustar 00root root 0000000 0000000 """Manage peer connections."""
from __future__ import annotations
import asyncio
from collections.abc import Callable
from datetime import datetime, timezone
from enum import Enum
import json
import logging
import async_timeout
from cryptography.fernet import Fernet, InvalidToken, MultiFernet
from ..exceptions import SniTunInvalidPeer
from .peer import Peer
_LOGGER = logging.getLogger(__name__)
class PeerManagerEvent(str, Enum):
"""Peer Manager event flags."""
CONNECTED = "connected"
DISCONNECTED = "disconnected"
class PeerManager:
"""Manage Peer connections."""
def __init__(
self,
fernet_tokens: list[str],
throttling: int | None = None,
event_callback: Callable[[Peer, PeerManagerEvent], None] | None = None,
) -> None:
"""Initialize Peer Manager."""
self._fernet = MultiFernet([Fernet(key) for key in fernet_tokens])
self._loop = asyncio.get_event_loop()
self._throttling = throttling
self._event_callback = event_callback
self._peers: dict[str, Peer] = {}
@property
def connections(self) -> int:
"""Return count of connected devices."""
return len(self._peers)
def create_peer(self, fernet_data: bytes) -> Peer:
"""Create a new peer from crypt config."""
try:
data = self._fernet.decrypt(fernet_data).decode("utf-8")
config = json.loads(data)
except (InvalidToken, json.JSONDecodeError, UnicodeDecodeError) as err:
raise SniTunInvalidPeer("Invalid fernet token") from err
# Check if token is valid
valid = datetime.fromtimestamp(config["valid"], tz=timezone.utc)
if valid < datetime.now(tz=timezone.utc):
raise SniTunInvalidPeer("Token was expired")
# Extract configuration
hostname = config["hostname"]
aes_key = bytes.fromhex(config["aes_key"])
aes_iv = bytes.fromhex(config["aes_iv"])
return Peer(
hostname,
valid,
aes_key,
aes_iv,
throttling=self._throttling,
alias=config.get("alias", []),
)
def add_peer(self, peer: Peer) -> None:
"""Register peer to internal hostname list."""
if self.peer_available(peer.hostname):
_LOGGER.warning("Found stale peer connection")
self._peers[peer.hostname].multiplexer.shutdown()
_LOGGER.debug("New peer connection: %s", peer.hostname)
self._peers[peer.hostname] = peer
for alias in peer.alias:
_LOGGER.debug("New peer connection alias: %s for %s", alias, peer.hostname)
self._peers[alias] = peer
if self._event_callback:
self._loop.call_soon(self._event_callback, peer, PeerManagerEvent.CONNECTED)
def remove_peer(self, peer: Peer) -> None:
"""Remove peer from list."""
if self._peers.get(peer.hostname) != peer:
return
_LOGGER.debug("Close peer connection: %s", peer.hostname)
for hostname in peer.all_hostnames:
self._peers.pop(hostname, None)
if self._event_callback:
self._loop.call_soon(
self._event_callback,
peer,
PeerManagerEvent.DISCONNECTED,
)
def peer_available(self, hostname: str) -> bool:
"""Check if peer available and return True or False."""
if hostname in self._peers:
return self._peers[hostname].is_ready
return False
def get_peer(self, hostname: str) -> Peer | None:
"""Get peer."""
return self._peers.get(hostname)
async def close_connections(self, timeout: int = 10) -> None:
"""Close all peer connections.
Use this function only if you do not controll the server socket.
"""
peers = list(self._peers.values())
for peer in peers:
if peer.is_connected:
peer.multiplexer.shutdown()
if waiters := [peer.wait_disconnect() for peer in peers]:
try:
async with async_timeout.timeout(timeout):
await asyncio.gather(*waiters, return_exceptions=True)
except asyncio.TimeoutError:
_LOGGER.error("Timeout while waiting for peer disconnect")
NabuCasa-snitun-b72e8b6/snitun/server/run.py 0000664 0000000 0000000 00000026146 14730517453 0021154 0 ustar 00root root 0000000 0000000 """SniTun reference implementation."""
from __future__ import annotations
import asyncio
from collections.abc import Awaitable, Iterable
from contextlib import suppress
from itertools import cycle
import logging
from multiprocessing import cpu_count
import os
import select
import signal
import socket
from threading import Thread
import async_timeout
import attr
from ..exceptions import ParseSNIIncompleteError
from ..utils.server import MAX_BUFFER_SIZE, MAX_READ_SIZE
from .listener_peer import PeerListener
from .listener_sni import SNIProxy
from .peer_manager import PeerManager
from .sni import ParseSNIError, parse_tls_sni
from .worker import ServerWorker
_LOGGER = logging.getLogger(__name__)
WORKER_STALE_MAX = 30
class SniTunServer:
"""SniTunServer helper class for Dual port Asyncio."""
def __init__(
self,
fernet_keys: list[str],
sni_port: int | None = None,
sni_host: str | None = None,
peer_port: int | None = None,
peer_host: str | None = None,
throttling: int | None = None,
) -> None:
"""Initialize SniTun Server."""
self._peers: PeerManager = PeerManager(fernet_keys, throttling=throttling)
self._list_sni: SNIProxy = SNIProxy(self._peers, host=sni_host, port=sni_port)
self._list_peer: PeerListener = PeerListener(
self._peers,
host=peer_host,
port=peer_port,
)
@property
def peers(self) -> PeerManager:
"""Return peer manager."""
return self._peers
def start(self) -> Awaitable[None]:
"""Run server.
Return coroutine.
"""
return asyncio.wait(
[
asyncio.create_task(self._list_peer.start()),
asyncio.create_task(self._list_sni.start()),
],
)
def stop(self) -> Awaitable[None]:
"""Stop server.
Return coroutine.
"""
return asyncio.wait(
[
asyncio.create_task(self._list_peer.stop()),
asyncio.create_task(self._list_sni.stop()),
],
)
class SniTunServerSingle:
"""SniTunServer helper class for Single port Asnycio."""
def __init__(
self,
fernet_keys: list[str],
host: str | None = None,
port: int | None = None,
throttling: int | None = None,
) -> None:
"""Initialize SniTun Server."""
self._loop: asyncio.BaseEventLoop = asyncio.get_event_loop()
self._server: asyncio.AbstractServer | None = None
self._peers: PeerManager = PeerManager(fernet_keys, throttling=throttling)
self._list_sni: SNIProxy = SNIProxy(self._peers)
self._list_peer: PeerListener = PeerListener(self._peers)
self._host: str = host or "0.0.0.0"
self._port: int = port or 443
@property
def peers(self) -> PeerManager:
"""Return peer manager."""
return self._peers
async def start(self) -> None:
"""Run server."""
self._server = await asyncio.start_server(
self._handler,
host=self._host,
port=self._port,
)
async def stop(self) -> None:
"""Stop server."""
self._server.close()
await self._server.wait_closed()
async def _handler(
self,
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
) -> None:
"""Handle incoming connection."""
try:
async with async_timeout.timeout(10):
data = await reader.read(2048)
except asyncio.TimeoutError:
_LOGGER.warning("Abort connection initializing")
writer.close()
return
except OSError:
return
# Connection closed / healty check
if not data:
writer.close()
return
# Select the correct handler for process data
if data[0] == 0x16:
self._loop.create_task(
self._list_sni.handle_connection(reader, writer, data=data),
)
elif data.startswith(b"gA"):
self._loop.create_task(
self._list_peer.handle_connection(reader, writer, data=data),
)
else:
_LOGGER.warning("No valid ClientHello found: %s", data)
writer.close()
return
@attr.s(slots=True)
class Connection:
"""Connection data class."""
sock: socket.socket = attr.ib()
epoll: select.epoll = attr.ib()
buffer: bytes = attr.ib(default=b"")
stale: int = attr.ib(default=0)
close: bool = attr.ib(default=False)
@property
def fileno(self) -> int:
"""Return filehanle ID."""
return self.sock.fileno()
def soft_close(self) -> None:
"""Socket got handled over."""
self.close = True
self.epoll.unregister(self.fileno)
def close_socket(self, shutdown: bool = True) -> None:
"""Gracefull shutdown a socket or free the handle."""
self.soft_close()
with suppress(OSError):
if shutdown:
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
class SniTunServerWorker(Thread):
"""SniTunServer helper class for Worker."""
def __init__(
self,
fernet_keys: list[str],
host: str | None = None,
port: int | None = None,
worker_size: int | None = None,
throttling: int | None = None,
) -> None:
"""Initialize SniTun Server."""
super().__init__()
self._host: str = host or "0.0.0.0"
self._port: int = port or 443
self._fernet_keys: list[str] = fernet_keys
self._throttling: int | None = throttling
self._worker_size: int = worker_size or (cpu_count() * 2)
self._workers: list[ServerWorker] = []
self._running: bool = False
# TCP server
self._server: socket.socket | None = None
self._poller: select.epoll | None = None
@property
def peer_counter(self) -> int:
"""Return number of active peer connections."""
return sum(worker.peer_size for worker in self._workers)
def start(self) -> None:
"""Run server."""
# Init first all worker, we don't want the epoll on the childs
_LOGGER.info("Run SniTun with %d worker", self._worker_size)
for _ in range(self._worker_size):
worker = ServerWorker(self._fernet_keys, throttling=self._throttling)
worker.start()
self._workers.append(worker)
self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._server.bind((self._host, self._port))
self._server.setblocking(False)
self._server.listen(80 * 1000)
self._running = True
self._poller = select.epoll()
self._poller.register(self._server.fileno(), select.EPOLLIN)
super().start()
def stop(self) -> None:
"""Stop server."""
self._running = False
self.join()
# Shutdown all workers
for worker in self._workers:
worker.shutdown()
worker.close()
self._workers.clear()
self._server.close()
self._poller.close()
def run(self) -> None:
"""Handle incoming connection."""
fd_server = self._server.fileno()
connections: dict[int, Connection] = {}
worker_lb = cycle(self._workers)
_LOGGER.info("Server started, fd: %s", fd_server)
while self._running:
events = self._poller.poll(1)
for fileno, event in events:
# New Connection
if fileno == fd_server:
con, _ = self._server.accept()
con.setblocking(False)
self._poller.register(
con.fileno(),
select.EPOLLIN | select.EPOLLHUP | select.EPOLLERR,
)
connections[con.fileno()] = Connection(con, self._poller)
# Read hello & forward to worker
elif event & select.EPOLLIN:
client = connections.get(fileno)
client.stale = 0 # reset stale count
# Process connection
self._process(client, worker_lb)
# Partial read
if not client.close:
continue
connections.pop(fileno)
# Close
else:
client = connections.pop(fileno)
client.close_socket(shutdown=False)
# cleanup stale connection
for client_id in tuple(connections.keys()):
client = connections.get(client_id)
if client.stale >= WORKER_STALE_MAX:
connections.pop(client.fileno)
client.close_socket()
else:
client.stale += 1
# Check if worker are running
for worker in self._workers:
if worker.is_alive():
continue
_LOGGER.critical("Worker '%s' crashed!", worker.name)
os.kill(os.getpid(), signal.SIGINT)
def _process(
self,
client: Connection,
workers_lb: Iterable[ServerWorker],
) -> None:
"""Process connection & helo."""
try:
data = client.sock.recv(MAX_READ_SIZE)
except OSError as err:
_LOGGER.warning("Receive fails: %s", err)
client.close_socket(shutdown=False)
return
# No data received
if not data:
client.close_socket()
return
client.buffer += data
# Peer connection
if client.buffer.startswith(b"gA"):
client.soft_close()
next(workers_lb).handover_connection(client.sock, client.buffer)
_LOGGER.debug("Handover new peer connection: %s", client.buffer)
return
# TLS/SSL connection
if client.buffer[0] != 0x16:
_LOGGER.warning("No valid ClientHello found: %s", client.buffer)
client.close_socket()
return
# Get Hostname
try:
hostname = parse_tls_sni(client.buffer)
except ParseSNIIncompleteError:
# Check Buffer Size
if len(client.buffer) >= MAX_BUFFER_SIZE:
_LOGGER.warning("Connection %d exceed buffer size", client.fileno)
client.close_socket()
return
except ParseSNIError:
_LOGGER.warning("Receive invalid ClientHello on public Interface")
client.close_socket()
return
# Distribute to child worker
for worker in self._workers:
if not worker.is_responsible_peer(hostname):
continue
client.soft_close()
worker.handover_connection(client.sock, client.buffer, sni=hostname)
_LOGGER.debug("Handover %s to %s", hostname, worker.name)
return
_LOGGER.debug("No responsible worker for %s", hostname)
client.close_socket()
return
NabuCasa-snitun-b72e8b6/snitun/server/sni.py 0000664 0000000 0000000 00000010457 14730517453 0021137 0 ustar 00root root 0000000 0000000 """TLS ClientHello parser."""
from __future__ import annotations
import asyncio
import logging
from ..exceptions import ParseSNIError, ParseSNIIncompleteError
from ..utils.server import MAX_BUFFER_SIZE, MAX_READ_SIZE
_LOGGER = logging.getLogger(__name__)
TLS_HEADER_LEN = 5
TLS_HANDSHAKE_CONTENT_TYPE = 0x16
TLS_HANDSHAKE_TYPE_CLIENT_HELLO = 0x01
async def payload_reader(reader: asyncio.StreamReader) -> bytes | None:
"""Read data from reader."""
try:
header = await reader.read(6)
except ConnectionResetError:
raise ParseSNIError from None
if not header:
raise ParseSNIError
if len(header) < 5:
raise ParseSNIError
if (
header[0] != TLS_HANDSHAKE_CONTENT_TYPE
or header[5] != TLS_HANDSHAKE_TYPE_CLIENT_HELLO
):
return None
tls_size = (header[3] << 8) + header[4] + TLS_HEADER_LEN
data = header
while (data_size := len(data)) < tls_size and data_size <= MAX_BUFFER_SIZE:
try:
data += await reader.read(MAX_READ_SIZE)
except ConnectionResetError:
raise ParseSNIError from None
return data
def parse_tls_sni(data: bytes) -> str:
"""Parse TLS SNI extention."""
if (data_size := len(data)) < TLS_HEADER_LEN:
_LOGGER.debug("Invalid TLS header")
raise ParseSNIError
# If TLS handshake
if data[0] != TLS_HANDSHAKE_CONTENT_TYPE:
_LOGGER.debug("Not TLS handshake received")
raise ParseSNIError
# Check compatible ClientHello
if int(data[1]) < 3:
_LOGGER.debug("Received ClientHello without SNI support")
raise ParseSNIError
# Calculate TLS record size
tls_size = (data[3] << 8) + data[4] + TLS_HEADER_LEN
if data_size < tls_size:
_LOGGER.debug("Can't calculate the TLS record size")
raise ParseSNIIncompleteError
# Check if handshake is a ClientHello
pos = TLS_HEADER_LEN
if data[pos] != TLS_HANDSHAKE_TYPE_CLIENT_HELLO:
_LOGGER.debug("Invalid ClientHello type")
raise ParseSNIError
# Seek fixed length header part
pos += 38
# Seek SessionID
try:
pos += 1 + data[pos]
except IndexError:
_LOGGER.debug("Invalid SessionID")
raise ParseSNIError from None
# Seek Cipher Suites
try:
pos += 2 + (data[pos] << 8) + data[pos + 1]
except IndexError:
_LOGGER.debug("Invalid CipherSuites")
raise ParseSNIError from None
# Seek Compression Methods
try:
pos += 1 + data[pos]
except IndexError:
_LOGGER.debug("Invalid CompressionMethods")
raise ParseSNIError from None
# Check data buffer + extension size
if pos + 2 > data_size:
_LOGGER.debug("Mismatch Extension TLS header")
raise ParseSNIError
# Process extension
return _parse_extension(data, pos, data_size)
def _parse_extension(data: bytes, pos: int, data_size: int) -> str:
"""Parse TLS ClientHello Extension."""
# Seek Extension start
try:
tls_extension_size = (data[pos] << 8) + data[pos + 1]
pos += 2
except IndexError:
raise ParseSNIError from None
# Check data buffer + extension size
if pos + tls_extension_size > data_size:
_LOGGER.debug("Mismatch Extension TLS header")
raise ParseSNIError
# Loop over extension until we have our SNI
while pos + 4 <= data_size:
# SNI?
if data[pos] == 0x00 and data[pos + 1] == 0x00:
return _parse_host_name(data, pos + 4, data_size)
pos += 4 + (data[pos + 2] << 8) + data[pos + 3]
_LOGGER.debug("Can't find any ServerName Extension")
raise ParseSNIError
def _parse_host_name(data: bytes, pos: int, data_size: int) -> str:
"""Parse TLS ServerName Extension."""
# Seek list size
pos += 2
while pos + 3 < data_size:
size = (data[pos + 1] << 8) + data[pos + 2]
# Unknown server name type
if data[pos] != 0x00:
_LOGGER.debug("Unknown ServerName type")
pos += 3 + size
continue
try:
return bytes(data[pos + 3 : pos + 3 + size]).decode("utf-8")
except (IndexError, UnicodeDecodeError):
_LOGGER.debug("Wrong host length/format")
raise ParseSNIError from None
_LOGGER.debug("Not found any valid ServerName")
raise ParseSNIError
NabuCasa-snitun-b72e8b6/snitun/server/worker.py 0000664 0000000 0000000 00000011113 14730517453 0021645 0 ustar 00root root 0000000 0000000 """SniTun worker for traffics."""
from __future__ import annotations
import asyncio
import logging
from multiprocessing import Manager, Process, Queue
from socket import socket
from threading import Thread
from typing import TYPE_CHECKING
from .listener_peer import PeerListener
from .listener_sni import SNIProxy
from .peer import Peer
from .peer_manager import PeerManager, PeerManagerEvent
_LOGGER = logging.getLogger(__name__)
if TYPE_CHECKING:
from multiprocessing.managers import SyncManager
class ServerWorker(Process):
"""Worker for multiplexer."""
def __init__(
self,
fernet_keys: list[str],
throttling: int | None = None,
) -> None:
"""Initialize worker & communication."""
super().__init__()
self._fernet_keys: list[str] = fernet_keys
self._throttling: int | None = throttling
# Used on the child
self._peers: PeerManager | None = None
self._list_sni: SNIProxy | None = None
self._list_peer: PeerListener | None = None
self._loop: asyncio.BaseEventLoop | None = None
# Communication between Parent/Child
self._manager: SyncManager = Manager()
self._new: Queue = self._manager.Queue()
self._sync: dict[str, None] = self._manager.dict()
self._peer_count = self._manager.Value("peer_count", 0)
@property
def peer_size(self) -> int:
"""Return amount of managed peers."""
return self._peer_count.value
def is_responsible_peer(self, sni: str) -> bool:
"""Return True if worker is responsible for this peer domain."""
return sni in self._sync
async def _async_init(self) -> None:
"""Initialize child process data."""
self._peers = PeerManager(
self._fernet_keys,
throttling=self._throttling,
event_callback=self._event_stream,
)
self._list_sni = SNIProxy(self._peers)
self._list_peer = PeerListener(self._peers)
def _event_stream(self, peer: Peer, event: PeerManagerEvent) -> None:
"""Event stream peer connection data."""
if event == PeerManagerEvent.CONNECTED:
if peer.hostname not in self._sync:
self._peer_count.set(self._peer_count.value + 1)
for hostname in peer.all_hostnames:
self._sync[hostname] = None
else:
if peer.hostname in self._sync:
self._peer_count.set(self._peer_count.value - 1)
for hostname in peer.all_hostnames:
self._sync.pop(hostname, None)
def shutdown(self) -> None:
"""Shutdown child process."""
self._new.put(None)
self.join(10)
def handover_connection(
self,
con: socket,
data: bytes,
sni: str | None = None,
) -> None:
"""Move new connection to worker."""
self._new.put_nowait((con, data, sni))
def run(self) -> None:
"""Run the worker process."""
_LOGGER.info("Start worker: %s", self.name)
# Init new event loop
self._loop = asyncio.new_event_loop()
asyncio.set_event_loop(self._loop)
# Start eventloop
running_loop = Thread(target=self._loop.run_forever)
running_loop.start()
# Init backend
asyncio.run_coroutine_threadsafe(self._async_init(), loop=self._loop).result()
while True:
new = self._new.get()
if new is None:
break
new[0].setblocking(False)
asyncio.run_coroutine_threadsafe(
self._async_new_connection(*new),
loop=self._loop,
)
# Shutdown worker
_LOGGER.info("Stoping worker: %s", self.name)
asyncio.run_coroutine_threadsafe(
self._peers.close_connections(),
loop=self._loop,
).result()
self._loop.call_soon_threadsafe(self._loop.stop)
running_loop.join(10)
async def _async_new_connection(
self,
con: socket,
data: bytes,
sni: str | None,
) -> None:
"""Handle incoming connection."""
try:
reader, writer = await asyncio.open_connection(sock=con)
except OSError:
con.close()
return
# Select the correct handler for process connection
if sni:
self._loop.create_task(
self._list_sni.handle_connection(reader, writer, data=data, sni=sni),
)
else:
self._loop.create_task(
self._list_peer.handle_connection(reader, writer, data=data),
)
NabuCasa-snitun-b72e8b6/snitun/utils/ 0000775 0000000 0000000 00000000000 14730517453 0017617 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/snitun/utils/__init__.py 0000664 0000000 0000000 00000000054 14730517453 0021727 0 ustar 00root root 0000000 0000000 """Utils & function for implementations."""
NabuCasa-snitun-b72e8b6/snitun/utils/aes.py 0000664 0000000 0000000 00000000314 14730517453 0020737 0 ustar 00root root 0000000 0000000 """AES helper functions."""
from __future__ import annotations
import os
def generate_aes_keyset() -> tuple[bytes]:
"""Generate AES key + IV for CBC."""
return (os.urandom(32), os.urandom(16))
NabuCasa-snitun-b72e8b6/snitun/utils/aiohttp_client.py 0000664 0000000 0000000 00000007477 14730517453 0023216 0 ustar 00root root 0000000 0000000 """Helper for handle aiohttp internal server."""
from __future__ import annotations
import asyncio
from collections.abc import Coroutine
from contextlib import suppress
import logging
import socket
import ssl
from typing import Any
from aiohttp.web import AppRunner, SockSite
import async_timeout
from ..client.client_peer import ClientPeer
from ..client.connector import Connector
_LOGGER = logging.getLogger(__name__)
class SniTunClientAioHttp:
"""Help to handle a internal aiohttp app runner."""
def __init__(
self,
runner: AppRunner,
context: ssl.SSLContext,
snitun_server: str,
snitun_port: int | None=None,
)->None:
"""Initialize SniTunClient with aiohttp."""
self._connector = None
self._client = ClientPeer(snitun_server, snitun_port)
self._socket = socket.socket()
self._server_name = f"{snitun_server}:{snitun_port}"
# Init interface
self._socket.setblocking(False)
self._socket.bind(("127.0.0.1", 0))
self._site = SockSite(runner, self._socket, ssl_context=context)
@property
def is_connected(self) -> bool:
"""Return True if we are connected to snitun."""
return self._client.is_connected
@property
def whitelist(self) -> set:
"""Return whitelist from connector."""
if self._connector:
return self._connector.whitelist
return set()
def wait(self) -> asyncio.Task:
"""Block until connection to snitun is closed."""
return self._client.wait()
async def start(
self,
whitelist: bool = False,
endpoint_connection_error_callback: Coroutine[Any, Any, None] | None = None,
) -> None:
"""Start internal server."""
await self._site.start()
host, port = self._socket.getsockname()[:2]
self._connector = Connector(
host,
port,
whitelist,
endpoint_connection_error_callback=endpoint_connection_error_callback,
)
_LOGGER.info("AioHTTP snitun client started on %s:%s", host, port)
async def stop(self, *, wait: bool = False) -> None:
"""
Stop internal server.
Args:
wait: wait for the socket to close.
"""
await self.disconnect()
with suppress(OSError):
self._socket.close()
with suppress(RuntimeError):
self._site._runner._unreg_site(self._site) # noqa: SLF001
if wait:
# Wait for the socket to close
await _async_waitfor_socket_closed(self._socket)
_LOGGER.info("AioHTTP snitun client closed")
async def connect(
self,
fernet_key: bytes,
aes_key: bytes,
aes_iv: bytes,
throttling: int | None = None,
) -> None:
"""Connect to SniTun server."""
if self._client.is_connected:
return
await self._client.start(
self._connector, fernet_key, aes_key, aes_iv, throttling=throttling,
)
_LOGGER.info("AioHTTP snitun client connected to: %s", self._server_name)
async def disconnect(self) -> None:
"""Disconnect from SniTun server."""
if not self._client.is_connected:
return
await self._client.stop()
_LOGGER.info("AioHTTP snitun client disconnected from: %s", self._server_name)
async def _async_waitfor_socket_closed(sock: socket.socket | None = None) -> None:
"""Wait for the socket to be closed."""
if sock is None:
return
loop = asyncio.get_event_loop()
try:
async with async_timeout.timeout(60):
while (await loop.run_in_executor(None, sock.fileno)) != -1:
await asyncio.sleep(1)
except asyncio.TimeoutError:
_LOGGER.warning("Timeout while waiting for the socket to close.")
NabuCasa-snitun-b72e8b6/snitun/utils/ipaddress.py 0000664 0000000 0000000 00000001075 14730517453 0022152 0 ustar 00root root 0000000 0000000 """Utils for handling IP address."""
import ipaddress
import socket
def bytes_to_ip_address(data: bytes) -> ipaddress.IPv4Address:
"""Convert bytes into a IP address."""
try:
return ipaddress.ip_address(socket.inet_ntop(socket.AF_INET, data))
except (ValueError, OSError):
return ipaddress.ip_address(0)
def ip_address_to_bytes(ip_address: ipaddress.IPv4Address) -> bytes:
"""Convert a IP address object into bytes."""
try:
return socket.inet_pton(socket.AF_INET, str(ip_address))
except OSError:
return bytes(4)
NabuCasa-snitun-b72e8b6/snitun/utils/server.py 0000664 0000000 0000000 00000001447 14730517453 0021505 0 ustar 00root root 0000000 0000000 """Utils for server handling."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
import json
from cryptography.fernet import Fernet, MultiFernet
MAX_READ_SIZE = 4_096
MAX_BUFFER_SIZE = 1_024_000
def generate_client_token(
tokens: list[str],
valid_delta: timedelta,
hostname: str,
aes_key: bytes,
aes_iv: bytes,
) -> bytes:
"""Generate a token for client."""
fernet = MultiFernet([Fernet(key) for key in tokens])
valid = datetime.now(tz=timezone.utc) + valid_delta
return fernet.encrypt(
json.dumps(
{
"valid": valid.timestamp(),
"hostname": hostname,
"aes_key": aes_key.hex(),
"aes_iv": aes_iv.hex(),
},
).encode(),
)
NabuCasa-snitun-b72e8b6/tests/ 0000775 0000000 0000000 00000000000 14730517453 0016301 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/tests/__init__.py 0000664 0000000 0000000 00000000024 14730517453 0020406 0 ustar 00root root 0000000 0000000 """SniTun tests."""
NabuCasa-snitun-b72e8b6/tests/client/ 0000775 0000000 0000000 00000000000 14730517453 0017557 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/tests/client/__init__.py 0000664 0000000 0000000 00000000037 14730517453 0021670 0 ustar 00root root 0000000 0000000 """Tests for SniTun client."""
NabuCasa-snitun-b72e8b6/tests/client/test_client_peer.py 0000664 0000000 0000000 00000017220 14730517453 0023463 0 ustar 00root root 0000000 0000000 """Test Client Peer connections."""
import asyncio
from datetime import datetime, timedelta, timezone
import ipaddress
import os
import pytest
from snitun.client.client_peer import ClientPeer
from snitun.client.connector import Connector
from snitun.exceptions import SniTunConnectionError
from ..server.const_fernet import create_peer_config
IP_ADDR = ipaddress.ip_address("8.8.8.8")
async def test_init_client_peer(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
assert client.is_connected
assert client._multiplexer._throttling is None
await client.stop()
await asyncio.sleep(0.1)
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
async def test_init_client_peer_with_alias(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer with custom tomain."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
assert not peer_manager.peer_available("localhost.custom")
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(
valid.timestamp(), hostname, aes_key, aes_iv, alias=["localhost.custom"]
)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
assert peer_manager.peer_available("localhost.custom")
assert client.is_connected
assert client._multiplexer._throttling is None
await client.stop()
await asyncio.sleep(0.1)
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
assert not peer_manager.peer_available("localhost.custom")
async def test_init_client_peer_invalid_token(
peer_listener, peer_manager, test_endpoint
):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = datetime.now(tz=timezone.utc) + timedelta(days=-1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
with pytest.raises(SniTunConnectionError):
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert not peer_manager.peer_available("localhost")
async def test_flow_client_peer(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer, test flow."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
peer = peer_manager.get_peer("localhost")
channel = await peer.multiplexer.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
assert channel.ip_address == IP_ADDR
test_connection.writer.write(b"Hiro")
await test_connection.writer.drain()
data = await channel.read()
assert data == b"Hiro"
await client.stop()
await asyncio.sleep(0.1)
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
test_connection.close.set()
async def test_close_client_peer(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer, test flow - close it."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
peer = peer_manager.get_peer("localhost")
channel = await peer.multiplexer.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
assert channel.ip_address == IP_ADDR
test_connection.writer.write(b"Hiro")
await test_connection.writer.drain()
data = await channel.read()
assert data == b"Hiro"
await client.stop()
await asyncio.sleep(0.1)
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
data = await test_connection.reader.read(1024)
assert not data
test_connection.close.set()
async def test_init_client_peer_wait(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
assert client.is_connected
assert not client.wait().done()
await client.stop()
await asyncio.sleep(0.1)
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
with pytest.raises(RuntimeError):
assert client.wait().done()
async def test_init_client_peer_throttling(peer_listener, peer_manager, test_endpoint):
"""Test setup of ClientPeer."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
await client.start(connector, fernet_token, aes_key, aes_iv, throttling=500)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
assert client.is_connected
assert client._multiplexer._throttling == 0.002
await client.stop()
await asyncio.sleep(0.1)
assert not client.is_connected
assert not peer_manager.peer_available("localhost")
NabuCasa-snitun-b72e8b6/tests/client/test_connector.py 0000664 0000000 0000000 00000012056 14730517453 0023166 0 ustar 00root root 0000000 0000000 """Test client connector."""
import asyncio
import ipaddress
from unittest.mock import AsyncMock, patch
import pytest
from snitun.client.connector import Connector
from snitun.exceptions import MultiplexerTransportClose
IP_ADDR = ipaddress.ip_address("8.8.8.8")
BAD_ADDR = ipaddress.ip_address("8.8.1.1")
async def test_init_connector(test_endpoint, multiplexer_client, multiplexer_server):
"""Test and init a connector."""
assert not test_endpoint
connector = Connector("127.0.0.1", "8822")
multiplexer_client._new_connections = connector.handler
channel = await multiplexer_server.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
test_connection.close.set()
async def test_flow_connector(test_endpoint, multiplexer_client, multiplexer_server):
"""Test and and perform a connector flow."""
assert not test_endpoint
connector = Connector("127.0.0.1", "8822")
multiplexer_client._new_connections = connector.handler
channel = await multiplexer_server.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
test_connection.writer.write(b"Hiro")
await test_connection.writer.drain()
data = await channel.read()
assert data == b"Hiro"
test_connection.close.set()
async def test_close_connector_remote(
test_endpoint, multiplexer_client, multiplexer_server
):
"""Test and init a connector with remote close."""
assert not test_endpoint
connector = Connector("127.0.0.1", "8822")
multiplexer_client._new_connections = connector.handler
channel = await multiplexer_server.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
test_connection.writer.write(b"Hiro")
await test_connection.writer.drain()
data = await channel.read()
assert data == b"Hiro"
await multiplexer_server.delete_channel(channel)
data = await test_connection.reader.read(1024)
assert not data
test_connection.close.set()
async def test_close_connector_local(
test_endpoint, multiplexer_client, multiplexer_server
):
"""Test and init a connector."""
assert not test_endpoint
connector = Connector("127.0.0.1", "8822")
multiplexer_client._new_connections = connector.handler
channel = await multiplexer_server.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
test_connection.writer.write(b"Hiro")
await test_connection.writer.drain()
data = await channel.read()
assert data == b"Hiro"
test_connection.writer.close()
test_connection.close.set()
await asyncio.sleep(0.1)
with pytest.raises(MultiplexerTransportClose):
await channel.read()
async def test_init_connector_whitelist(
test_endpoint, multiplexer_client, multiplexer_server
):
"""Test and init a connector with whitelist."""
assert not test_endpoint
connector = Connector("127.0.0.1", "8822", True)
multiplexer_client._new_connections = connector.handler
connector.whitelist.add(IP_ADDR)
assert IP_ADDR in connector.whitelist
channel = await multiplexer_server.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert test_endpoint
test_connection = test_endpoint[0]
await channel.write(b"Hallo")
data = await test_connection.reader.read(1024)
assert data == b"Hallo"
test_connection.close.set()
async def test_init_connector_whitelist_bad(
test_endpoint, multiplexer_client, multiplexer_server
):
"""Test and init a connector with whitelist bad requests."""
assert not test_endpoint
connector = Connector("127.0.0.1", "8822", True)
multiplexer_client._new_connections = connector.handler
connector.whitelist.add(IP_ADDR)
assert IP_ADDR in connector.whitelist
assert BAD_ADDR not in connector.whitelist
channel = await multiplexer_server.create_channel(BAD_ADDR)
await asyncio.sleep(0.1)
assert not test_endpoint
with pytest.raises(MultiplexerTransportClose):
await channel.read()
async def test_connector_error_callback(multiplexer_client, multiplexer_server):
"""Test connector endpoint error callback."""
callback = AsyncMock()
connector = Connector("127.0.0.1", "8822", False, callback)
channel = await multiplexer_server.create_channel(IP_ADDR)
callback.assert_not_called()
with patch("asyncio.open_connection", side_effect=OSError("Lorem ipsum...")):
await connector.handler(multiplexer_client, channel)
callback.assert_called_once()
NabuCasa-snitun-b72e8b6/tests/conftest.py 0000664 0000000 0000000 00000013476 14730517453 0020513 0 ustar 00root root 0000000 0000000 """Pytest fixtures for SniTun."""
import asyncio
from datetime import datetime, timedelta, timezone
import logging
import os
from unittest.mock import patch
import select
import socket
from threading import Thread
import attr
import pytest
from snitun.multiplexer.core import Multiplexer
from snitun.multiplexer.crypto import CryptoTransport
from snitun.server.listener_peer import PeerListener
from snitun.server.listener_sni import SNIProxy
from snitun.server.peer import Peer
from snitun.server.peer_manager import PeerManager
from .server.const_fernet import FERNET_TOKENS
logging.basicConfig(level=logging.DEBUG)
# pylint: disable=redefined-outer-name
@attr.s
class Client:
"""Represent a TCP client object."""
reader = attr.ib(type=asyncio.StreamReader)
writer = attr.ib(type=asyncio.StreamWriter)
close = attr.ib(type=asyncio.Event, default=asyncio.Event())
@pytest.fixture
def raise_timeout():
"""Raise timeout on async-timeout."""
with patch("async_timeout.timeout", side_effect=asyncio.TimeoutError()):
yield
@pytest.fixture
async def test_server():
"""Create a TCP test server."""
connections = []
async def process_data(reader, writer):
"""Read data from client."""
client = Client(reader, writer)
connections.append(client)
await client.close.wait()
server = await asyncio.start_server(process_data, host="127.0.0.1", port="8866")
yield connections
server.close()
@pytest.fixture
async def test_endpoint():
"""Create a TCP test endpoint."""
connections = []
async def process_data(reader, writer):
"""Read data from client."""
client = Client(reader, writer)
connections.append(client)
await client.close.wait()
server = await asyncio.start_server(process_data, host="127.0.0.1", port="8822")
yield connections
server.close()
@pytest.fixture
async def test_client(test_server):
"""Create a TCP test client."""
reader, writer = await asyncio.open_connection(host="127.0.0.1", port="8866")
yield Client(reader, writer)
writer.close()
@pytest.fixture
def test_server_sync(event_loop):
"""Create a TCP test server."""
connections = []
shutdown = False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", 8366))
sock.listen(2)
sock.setblocking(False)
def _incoming() -> None:
nonlocal shutdown
poller = select.epoll()
poller.register(sock, select.EPOLLIN)
while not shutdown:
events = poller.poll(0.1)
for _, _ in events:
connection, _ = sock.accept()
connections.append(connection)
poller.close()
runner = Thread(target=_incoming)
runner.start()
yield connections
shutdown = True
runner.join()
sock.close()
@pytest.fixture
def test_client_sync(test_server_sync):
"""Create a TCP test client."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 8366))
yield sock
sock.close()
@pytest.fixture
def test_client_ssl_sync(test_server_sync):
"""Create a TCP test client for SSL."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 8366))
yield sock
sock.close()
@pytest.fixture
async def multiplexer_server(test_server, test_client, crypto_transport):
"""Create a multiplexer client from server."""
client = test_server[0]
async def mock_new_channel(multiplexer, channel):
"""Mock new channel."""
multiplexer = Multiplexer(
crypto_transport, client.reader, client.writer, mock_new_channel
)
yield multiplexer
multiplexer.shutdown()
client.close.set()
@pytest.fixture
async def multiplexer_client(test_client, crypto_transport):
"""Create a multiplexer client from server."""
async def mock_new_channel(multiplexer, channel):
"""Mock new channel."""
multiplexer = Multiplexer(
crypto_transport, test_client.reader, test_client.writer, mock_new_channel
)
yield multiplexer
multiplexer.shutdown()
@pytest.fixture
async def peer_manager(multiplexer_server, peer):
"""Create a localhost peer for tests."""
manager = PeerManager(FERNET_TOKENS)
manager._peers[peer.hostname] = peer
yield manager
@pytest.fixture
async def sni_proxy(peer_manager):
"""Create a SNI Proxy."""
proxy = SNIProxy(peer_manager, "127.0.0.1", "8863")
await proxy.start()
yield proxy
await proxy.stop()
@pytest.fixture
async def test_client_ssl(sni_proxy):
"""Create a TCP test client."""
reader, writer = await asyncio.open_connection(host="127.0.0.1", port="8863")
yield Client(reader, writer)
writer.close()
@pytest.fixture
def crypto_transport():
"""Create a CryptoTransport object."""
key = os.urandom(32)
iv = os.urandom(16)
crypto = CryptoTransport(key, iv)
yield crypto
@pytest.fixture
async def peer(crypto_transport, multiplexer_server):
"""Init a peer with transport."""
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
peer = Peer("localhost", valid, os.urandom(32), os.urandom(16))
peer._crypto = crypto_transport
peer._multiplexer = multiplexer_server
yield peer
@pytest.fixture
async def peer_listener(peer_manager, peer):
"""Create a Peer listener."""
listener = PeerListener(peer_manager, "127.0.0.1", "8893")
await listener.start()
# Cleanup mock peer
peer_manager.remove_peer(peer)
yield listener
await listener.stop()
@pytest.fixture
async def test_client_peer(peer_listener):
"""Create a TCP test client."""
reader, writer = await asyncio.open_connection(host="127.0.0.1", port="8893")
yield Client(reader, writer)
writer.close()
NabuCasa-snitun-b72e8b6/tests/multiplexer/ 0000775 0000000 0000000 00000000000 14730517453 0020653 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/tests/multiplexer/__init__.py 0000664 0000000 0000000 00000000035 14730517453 0022762 0 ustar 00root root 0000000 0000000 """Tests for multiplexer."""
NabuCasa-snitun-b72e8b6/tests/multiplexer/test_channel.py 0000664 0000000 0000000 00000011424 14730517453 0023676 0 ustar 00root root 0000000 0000000 """Test Multiplexer channels."""
import asyncio
import ipaddress
import pytest
from snitun.utils.ipaddress import ip_address_to_bytes
from snitun.exceptions import MultiplexerTransportClose, MultiplexerTransportError
from snitun.multiplexer.channel import MultiplexerChannel
from snitun.multiplexer.message import (
CHANNEL_FLOW_CLOSE,
CHANNEL_FLOW_DATA,
CHANNEL_FLOW_NEW,
MultiplexerMessage,
MultiplexerChannelId,
)
IP_ADDR = ipaddress.ip_address("8.8.8.8")
async def test_initial_channel_msg():
"""Test new MultiplexerChannel with id."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
message = channel.init_new()
assert message.id == channel.id
assert message.flow_type == CHANNEL_FLOW_NEW
assert message.data == b""
assert message.extra == b"4" + ip_address_to_bytes(IP_ADDR)
async def test_close_channel_msg():
"""Test close MultiplexerChannel."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
message = channel.init_close()
assert message.id == channel.id
assert message.flow_type == CHANNEL_FLOW_CLOSE
assert message.data == b""
async def test_write_data():
"""Test send data over MultiplexerChannel."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
await channel.write(b"test")
assert not output.empty()
message = output.get_nowait()
assert message.id == channel.id
assert message.flow_type == CHANNEL_FLOW_DATA
assert message.data == b"test"
async def test_closing():
"""Test send data over MultiplexerChannel."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
assert not channel.closing
channel.close()
assert channel.closing
async def test_write_data_after_close():
"""Test send data over MultiplexerChannel."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
assert not channel.closing
channel.close()
with pytest.raises(MultiplexerTransportClose):
await channel.write(b"test")
assert channel.closing
async def test_write_data_empty():
"""Test send data over MultiplexerChannel."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
with pytest.raises(MultiplexerTransportError):
await channel.write(b"")
async def test_read_data():
"""Test send data over MultiplexerChannel."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
message = MultiplexerMessage(channel.id, CHANNEL_FLOW_DATA, b"test")
channel.message_transport(message)
data = await channel.read()
assert data == b"test"
async def test_read_data_on_close():
"""Test send data over MultiplexerChannel on close."""
output = asyncio.Queue()
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
assert not channel.closing
channel.close()
with pytest.raises(MultiplexerTransportClose):
data = await channel.read()
assert channel.closing
async def test_write_data_peer_error(raise_timeout):
"""Test send data over MultiplexerChannel but peer don't response."""
output = asyncio.Queue(1)
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
# fill peer queue
output.put_nowait(None)
with pytest.raises(MultiplexerTransportError):
await channel.write(b"test")
async def test_message_transport_never_lock():
"""Message transport should never lock down."""
output = asyncio.Queue(1)
channel = MultiplexerChannel(output, IP_ADDR)
assert isinstance(channel.id, MultiplexerChannelId)
for _ in range(1, 10000):
channel.message_transport(channel.init_close())
assert channel.healthy
async def test_write_throttling(event_loop):
"""Message transport should never lock down."""
loop = event_loop
output = asyncio.Queue(500)
channel = MultiplexerChannel(output, IP_ADDR, throttling=0.1)
assert isinstance(channel.id, MultiplexerChannelId)
async def _write_background():
"""Write message in background."""
for _ in range(1, 10000):
await channel.write(b"test")
background_task = loop.create_task(_write_background())
await asyncio.sleep(0.3)
assert not background_task.done()
assert output.qsize() <= 4
background_task.cancel()
NabuCasa-snitun-b72e8b6/tests/multiplexer/test_core.py 0000664 0000000 0000000 00000027311 14730517453 0023220 0 ustar 00root root 0000000 0000000 """Tests for core multiplexer handler."""
import asyncio
import ipaddress
from unittest.mock import patch
import pytest
from snitun.exceptions import MultiplexerTransportClose, MultiplexerTransportError
from snitun.multiplexer.core import Multiplexer
from snitun.multiplexer.message import CHANNEL_FLOW_PING
IP_ADDR = ipaddress.ip_address("8.8.8.8")
async def test_init_multiplexer_server(test_server, test_client, crypto_transport):
"""Test to create a new Multiplexer from server socket."""
client = test_server[0]
multiplexer = Multiplexer(crypto_transport, client.reader, client.writer)
assert multiplexer.is_connected
assert multiplexer._throttling is None
multiplexer.shutdown()
client.close.set()
async def test_init_multiplexer_client(test_client, crypto_transport):
"""Test to create a new Multiplexer from client socket."""
multiplexer = Multiplexer(crypto_transport, test_client.reader, test_client.writer)
assert multiplexer.is_connected
assert multiplexer._throttling is None
multiplexer.shutdown()
async def test_init_multiplexer_server_throttling(
test_server, test_client, crypto_transport
):
"""Test to create a new Multiplexer from server socket."""
client = test_server[0]
multiplexer = Multiplexer(
crypto_transport, client.reader, client.writer, throttling=500
)
assert multiplexer.is_connected
assert multiplexer._throttling == 0.002
multiplexer.shutdown()
client.close.set()
async def test_init_multiplexer_client_throttling(test_client, crypto_transport):
"""Test to create a new Multiplexer from client socket."""
multiplexer = Multiplexer(
crypto_transport, test_client.reader, test_client.writer, throttling=500
)
assert multiplexer.is_connected
assert multiplexer._throttling == 0.002
multiplexer.shutdown()
async def test_multiplexer_server_close(multiplexer_server, multiplexer_client):
"""Test a close from server peers."""
assert multiplexer_server.is_connected
assert multiplexer_client.is_connected
multiplexer_server.shutdown()
await asyncio.sleep(0.1)
assert not multiplexer_server.is_connected
assert not multiplexer_client.is_connected
async def test_multiplexer_client_close(multiplexer_server, multiplexer_client):
"""Test a close from client peers."""
assert multiplexer_server.is_connected
assert multiplexer_client.is_connected
multiplexer_client.shutdown()
await asyncio.sleep(0.1)
assert not multiplexer_server.is_connected
assert not multiplexer_client.is_connected
async def test_multiplexer_ping(event_loop, test_server, multiplexer_client):
"""Test a ping between peers."""
loop = event_loop
client = test_server[0]
ping_task = loop.create_task(multiplexer_client.ping())
await asyncio.sleep(0.1)
data = await client.reader.read(60)
data = multiplexer_client._crypto.decrypt(data)
assert data[16] == CHANNEL_FLOW_PING
assert int.from_bytes(data[17:21], "big") == 0
assert data[21:25] == b"ping"
ping_task.cancel()
async def test_multiplexer_ping_error(event_loop, test_server, multiplexer_client):
"""Test a ping between peers."""
from snitun.multiplexer import core as multi_core
loop = event_loop
multi_core.PEER_TCP_TIMEOUT = 0.2
client = test_server[0]
ping_task = loop.create_task(multiplexer_client.ping())
await asyncio.sleep(0.3)
data = await client.reader.read(60)
data = multiplexer_client._crypto.decrypt(data)
assert data[16] == CHANNEL_FLOW_PING
assert int.from_bytes(data[17:21], "big") == 0
assert data[21:25] == b"ping"
assert ping_task.done()
with pytest.raises(MultiplexerTransportError):
raise ping_task.exception()
multi_core.PEER_TCP_TIMEOUT = 90
async def test_multiplexer_ping_pong(multiplexer_client, multiplexer_server):
"""Test that without new channel callback can't create new channels."""
await multiplexer_client.ping()
assert multiplexer_client._healthy.is_set()
async def test_multiplexer_cant_init_channel(multiplexer_client, multiplexer_server):
"""Test that without new channel callback can't create new channels."""
assert not multiplexer_client._channels
assert not multiplexer_server._channels
# Disable new channels
multiplexer_server._new_connections = None
await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert multiplexer_client._channels
assert not multiplexer_server._channels
async def test_multiplexer_init_channel(multiplexer_client, multiplexer_server):
"""Test that new channels are created."""
assert not multiplexer_client._channels
assert not multiplexer_server._channels
channel = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert multiplexer_client._channels
assert multiplexer_server._channels
assert multiplexer_client._channels[channel.id]
assert multiplexer_server._channels[channel.id]
assert multiplexer_client._channels[channel.id].ip_address == IP_ADDR
assert multiplexer_server._channels[channel.id].ip_address == IP_ADDR
async def test_multiplexer_init_channel_full(multiplexer_client, raise_timeout):
"""Test that new channels are created but peer error is available."""
assert not multiplexer_client._channels
with pytest.raises(MultiplexerTransportError):
channel = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
async def test_multiplexer_close_channel(multiplexer_client, multiplexer_server):
"""Test that channels are nice removed."""
assert not multiplexer_client._channels
assert not multiplexer_server._channels
channel = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert multiplexer_client._channels
assert multiplexer_server._channels
assert multiplexer_client._channels[channel.id]
assert multiplexer_server._channels[channel.id]
assert multiplexer_client._channels[channel.id].ip_address == IP_ADDR
assert multiplexer_server._channels[channel.id].ip_address == IP_ADDR
await multiplexer_client.delete_channel(channel)
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
assert not multiplexer_server._channels
async def test_multiplexer_close_channel_full(multiplexer_client):
"""Test that channels are nice removed but peer error is available."""
assert not multiplexer_client._channels
channel = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
assert multiplexer_client._channels
with patch("async_timeout.timeout", side_effect=asyncio.TimeoutError()):
with pytest.raises(MultiplexerTransportError):
channel = await multiplexer_client.delete_channel(channel)
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
async def test_multiplexer_data_channel(multiplexer_client, multiplexer_server):
"""Test that new channels are created."""
assert not multiplexer_client._channels
assert not multiplexer_server._channels
channel_client = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
channel_server = multiplexer_server._channels.get(channel_client.id)
assert channel_client
assert channel_server
await channel_client.write(b"test 1")
await asyncio.sleep(0.1)
data = await channel_server.read()
assert data == b"test 1"
await channel_server.write(b"test 2")
await asyncio.sleep(0.1)
data = await channel_client.read()
assert data == b"test 2"
async def test_multiplexer_channel_shutdown(
event_loop, multiplexer_client, multiplexer_server
):
"""Test that new channels are created and graceful shutdown."""
loop = event_loop
assert not multiplexer_client._channels
assert not multiplexer_server._channels
channel_client = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
channel_server = multiplexer_server._channels.get(channel_client.id)
client_read = loop.create_task(channel_client.read())
server_read = loop.create_task(channel_server.read())
assert not client_read.done()
assert not server_read.done()
multiplexer_client.shutdown()
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
assert client_read.done()
with pytest.raises(MultiplexerTransportClose):
raise client_read.exception()
assert not multiplexer_server._channels
assert server_read.done()
with pytest.raises(MultiplexerTransportClose):
raise server_read.exception()
async def test_multiplexer_data_channel_abort_full(
multiplexer_client, multiplexer_server
):
"""Test that new channels are created."""
assert not multiplexer_client._channels
assert not multiplexer_server._channels
channel_client = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
channel_server = multiplexer_server._channels.get(channel_client.id)
assert channel_client
assert channel_server
with pytest.raises(MultiplexerTransportClose):
for count in range(1, 50000):
await channel_client.write(b"test xxxx")
with pytest.raises(MultiplexerTransportClose):
for count in range(1, 50000):
data = await channel_server.read()
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
assert not multiplexer_server._channels
async def test_multiplexer_throttling(
event_loop, multiplexer_client, multiplexer_server
):
"""Test that new channels are created and graceful shutdown."""
loop = event_loop
assert not multiplexer_client._channels
assert not multiplexer_server._channels
data_in = []
channel_client = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
channel_server = multiplexer_server._channels.get(channel_client.id)
multiplexer_server._throttling = 0.1
multiplexer_client._throttling = 0.1
async def _sender():
"""Send data much as possible."""
for count in range(1, 100000):
await channel_client.write(b"data")
async def _receiver():
"""Receive data much as possible."""
for count in range(1, 100000):
data = await channel_server.read()
data_in.append(data)
receiver = loop.create_task(_receiver())
sender = loop.create_task(_sender())
await asyncio.sleep(0.8)
assert not sender.done()
assert not receiver.done()
assert len(data_in) <= 8
receiver.cancel()
sender.cancel()
async def test_multiplexer_core_peer_timeout(
event_loop, multiplexer_client, multiplexer_server
):
"""Test that new channels are created and graceful shutdown."""
from snitun.multiplexer import core as multi_core
loop = event_loop
multi_core.PEER_TCP_TIMEOUT = 0.2
assert not multiplexer_client._channels
assert not multiplexer_server._channels
channel_client = await multiplexer_client.create_channel(IP_ADDR)
await asyncio.sleep(0.1)
channel_server = multiplexer_server._channels.get(channel_client.id)
client_read = loop.create_task(channel_client.read())
server_read = loop.create_task(channel_server.read())
assert not client_read.done()
assert not server_read.done()
await multiplexer_client.ping()
await asyncio.sleep(0.3)
assert not multiplexer_client._channels
assert not multiplexer_server._channels
assert server_read.done()
assert client_read.done()
with pytest.raises(MultiplexerTransportClose):
raise server_read.exception()
with pytest.raises(MultiplexerTransportClose):
raise client_read.exception()
multi_core.PEER_TCP_TIMEOUT = 90
NabuCasa-snitun-b72e8b6/tests/multiplexer/test_crypto.py 0000664 0000000 0000000 00000000640 14730517453 0023604 0 ustar 00root root 0000000 0000000 """Test crypto module for transport."""
import os
import pytest
from snitun.multiplexer.crypto import CryptoTransport
def test_setup_crypto_transport():
"""Test crypto transport setup."""
key = os.urandom(32)
iv = os.urandom(16)
crypto = CryptoTransport(key, iv)
for _ in range(1, 10):
test_data = os.urandom(32)
assert crypto.decrypt(crypto.encrypt(test_data)) == test_data
NabuCasa-snitun-b72e8b6/tests/server/ 0000775 0000000 0000000 00000000000 14730517453 0017607 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/tests/server/__init__.py 0000664 0000000 0000000 00000000033 14730517453 0021714 0 ustar 00root root 0000000 0000000 """SniTun server tests."""
NabuCasa-snitun-b72e8b6/tests/server/const_fernet.py 0000664 0000000 0000000 00000001440 14730517453 0022651 0 ustar 00root root 0000000 0000000 """Const value for Fernet tests."""
import json
from typing import List, Optional
from cryptography.fernet import Fernet, MultiFernet
FERNET_TOKENS = [
"XIKL24X0Fu83UmPLmWkXOBvvqsLq41tz2LljwafDyZw=",
"ep1FyYA6epwbFxrtEJ2dii5BGvTx5-xU1oUCrF61qMA=",
]
def create_peer_config(
valid: int,
hostname: str,
aes_key: bytes,
aes_iv: bytes,
alias: Optional[List[str]] = None,
) -> bytes:
"""Create a fernet token."""
fernet = MultiFernet([Fernet(key) for key in FERNET_TOKENS])
return fernet.encrypt(
json.dumps(
{
"valid": valid,
"hostname": hostname,
"alias": alias or [],
"aes_key": aes_key.hex(),
"aes_iv": aes_iv.hex(),
}
).encode()
)
NabuCasa-snitun-b72e8b6/tests/server/const_tls.py 0000664 0000000 0000000 00000031201 14730517453 0022166 0 ustar 00root root 0000000 0000000 """TLS ClientHello packages from Wireshark."""
# yapf: disable
TLS_1_0: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x01, # Version: TLS 1.0
0x00, 0x68, # Length
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0x64, # Length
0x03, 0x01, # Version: TLS 1.0
# Random
0x4e, 0x55, 0xde, 0x32, 0x80, 0x07, 0x92, 0x9f,
0x50, 0x41, 0xe4, 0xf9, 0x58, 0x32, 0xfc, 0x4f,
0x10, 0xb3, 0xde, 0x44, 0x4d, 0xa9, 0x67, 0x78,
0xea, 0xd1, 0x5f, 0x29, 0x09, 0x04, 0xc1, 0x06,
0x00, # Session ID Length
0x00, 0x28, # Cipher Suites Length
0x00, 0x39,
0x00, 0x38,
0x00, 0x35,
0x00, 0x16,
0x00, 0x13,
0x00, 0x0a,
0x00, 0x33,
0x00, 0x32,
0x00, 0x2f,
0x00, 0x05,
0x00, 0x04,
0x00, 0x15,
0x00, 0x12,
0x00, 0x09,
0x00, 0x14,
0x00, 0x11,
0x00, 0x08,
0x00, 0x06,
0x00, 0x03,
0x00, 0xff,
0x02, # Compression Methods
0x01,
0x00,
0x00, 0x12, # Extensions Length
0x00, 0x00, # Extension Type: Server Name
0x00, 0x0e, # Length
0x00, 0x0c, # Server Name Indication Length
0x00, # Server Name Type: host_name
0x00, 0x09, # Length
# "localhost"
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74
])
TLS_1_2: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x01, # Version: TLS 1.0
0x00, 0x48, # Length
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0x42, # Length
0x03, 0x03, # Version: TLS 1.2
# Random
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, # Session ID Length
0x00, 0x04, # Cipher Suites Length
0x00, 0x01, # NULL-MD5
0x00, 0xff, # RENEGOTIATION INFO SCSV
0x01, # Compression Methods
0x00, # NULL
0x00, 0x17, # Extensions Length
# Extension
0x00, 0x00, # Extension Type: Server Name
0x00, 0x0e, # Length
0x00, 0x0c, # Server Name Indication Length
0x00, # Server Name Type: host_name
0x00, 0x09, # Length
# "localhost"
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
# Extension
0x00, 0x0f, # Extension Type: Heart Beat
0x00, 0x01, # Length
0x01 # Mode: Peer allows to send requests
])
TLS_1_2_ORDER: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x01, # Version: TLS 1.0
0x00, 0x48, # Length
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0x42, # Length
0x03, 0x03, # Version: TLS 1.2
# Random
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, # Session ID Length
0x00, 0x04, # Cipher Suites Length
0x00, 0x01, # NULL-MD5
0x00, 0xff, # RENEGOTIATION INFO SCSV
0x01, # Compression Methods
0x00, # NULL
0x00, 0x17, # Extensions Length
# Extension
0x00, 0x0f, # Extension Type: Heart Beat
0x00, 0x01, # Length
0x01, # Mode: Peer allows to send requests
# Extension
0x00, 0x00, # Extension Type: Server Name
0x00, 0x0e, # Length
0x00, 0x0c, # Server Name Indication Length
0x00, # Server Name Type: host_name
0x00, 0x09, # Length
# "localhost"
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74
])
TLS_1_2_MORE: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x01, # Version: TLS 1.0
0x00, 0x47, # Length
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0x41, # Length
0x03, 0x03, # Version: TLS 1.2
# Random
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, # Session ID Length
0x00, 0x04, # Cipher Suites Length
0x00, 0x01, # NULL-MD5
0x00, 0xff, # RENEGOTIATION INFO SCSV
0x01, # Compression Methods
0x00, # NULL
0x00, 0x16, # Extensions Length
# Extension
0x00, 0x00, # Extension Type: Server Name
0x00, 0x0e, # Length
0x00, 0x0c, # Server Name Indication Length
0x00, # Server Name Type: host_name
0x00, 0x09, # Length
# "localhost"
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
# Extension
0x00, 0x23, # Extension Type: Session Ticket TLS
0x00, 0x00, # Length
])
TLS_1_0_OLD: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x01, # Version: TLS 1.0
0x00, 0xec, # Length 104
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0xe8, # Length 100
0x03, 0x01, # Version: TLS 1.0
# Random
0x4e, 0x55, 0xde, 0x32, 0x80, 0x07, 0x92, 0x9f,
0x50, 0x41, 0xe4, 0xf9, 0x58, 0x32, 0xfc, 0x4f,
0x10, 0xb3, 0xde, 0x44, 0x4d, 0xa9, 0x67, 0x78,
0xea, 0xd1, 0x5f, 0x29, 0x09, 0x04, 0xc1, 0x06,
0x00, # Session ID Length
0x00, 0x28, # Cipher Suites Length
0x00, 0x39,
0x00, 0x38,
0x00, 0x35,
0x00, 0x16,
0x00, 0x13,
0x00, 0x0a,
0x00, 0x33,
0x00, 0x32,
0x00, 0x2f,
0x00, 0x05,
0x00, 0x04,
0x00, 0x15,
0x00, 0x12,
0x00, 0x09,
0x00, 0x14,
0x00, 0x11,
0x00, 0x08,
0x00, 0x06,
0x00, 0x03,
0x00, 0xff,
0x02, # Compression Methods
0x01,
0x00,
0x00, 0x96, # Extensions Length 18 + 4 + 132 = 150
0x00, 0x15, # Extension Type: Padding
0x00, 0x80, # Length
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, # Extension Type: Server Name
0x00, 0x0e, # Length
0x00, 0x0c, # Server Name Indication Length
0x00, # Server Name Type: host_name
0x00, 0x09, # Length
# "localhost"
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74
])
SSL_3_0: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x00, # Version: SSL 3.0
0x00, 0x7f, # Length
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0x7b, # Length
0x03, 0x00, # Version: SSL 3.0
# Random
0x53, 0x11, 0x25, 0xc2, 0x92, 0xd6, 0xca, 0xf1,
0x79, 0x90, 0xba, 0x38, 0x8f, 0xad, 0xc8, 0x13,
0xa3, 0x1b, 0x57, 0xd9, 0xf4, 0x3e, 0xd2, 0x8b,
0xb6, 0x5e, 0xe3, 0x12, 0xca, 0x81, 0x2f, 0xc5,
0x00, # Session ID Length
0x00, 0x54, # Cipher Suites Length
0xc0, 0x14,
0xc0, 0x0a,
0xc0, 0x22,
0xc0, 0x21,
0x00, 0x39,
0x00, 0x38,
0xc0, 0x0f,
0xc0, 0x05,
0x00, 0x35,
0xc0, 0x12,
0xc0, 0x08,
0xc0, 0x1c,
0xc0, 0x1b,
0x00, 0x16,
0x00, 0x13,
0xc0, 0x0d,
0xc0, 0x03,
0x00, 0x0a,
0xc0, 0x13,
0xc0, 0x09,
0xc0, 0x1f,
0xc0, 0x1e,
0x00, 0x33,
0x00, 0x32,
0xc0, 0x0e,
0xc0, 0x04,
0x00, 0x2f,
0xc0, 0x11,
0xc0, 0x07,
0xc0, 0x0c,
0xc0, 0x02,
0x00, 0x05,
0x00, 0x04,
0x00, 0x15,
0x00, 0x12,
0x00, 0x09,
0x00, 0x14,
0x00, 0x11,
0x00, 0x08,
0x00, 0x06,
0x00, 0x03,
0x00, 0xff,
0x01, # Compression Methods
0x00
])
SSL_2_0: bytes = bytes([
0x80, 0x67, # Length (leading bit set)
0x01, # Handshake Type: Client Hello
0x03, 0x01, # Version
0x00, 0x4e, # Cipher spec length
0x00, 0x00, # Session ID length
0x00, 0x10, # Challenge length
# Cipher Suites
0x00, 0x00, 0x39,
0x00, 0x00, 0x38,
0x00, 0x00, 0x35,
0x00, 0x00, 0x16,
0x00, 0x00, 0x13,
0x00, 0x00, 0x0a,
0x07, 0x00,
0xc0, 0x00,
0x00, 0x33,
0x00, 0x00, 0x32,
0x00, 0x00, 0x2f,
0x03, 0x00,
0x80, 0x00,
0x00, 0x05,
0x00, 0x00, 0x04,
0x01, 0x00,
0x80, 0x00,
0x00, 0x15,
0x00, 0x00, 0x12,
0x00, 0x00, 0x09,
0x06, 0x00,
0x40, 0x00,
0x00, 0x14, 0x00,
0x00, 0x11, 0x00,
0x00, 0x08, 0x00,
0x00, 0x06, 0x04,
0x00, 0x80, 0x00,
0x00, 0x03, 0x02,
0x00, 0x80, 0x00,
0x00, 0xff,
# Session ID
0x74, 0x15, 0xdc, 0x11, 0x0b, 0xcb, 0x2b, 0x03,
0x5d, 0xb1, 0x5a, 0x2f, 0xac, 0x72, 0x45, 0x2e
])
BAD_DATA1: bytes = bytes([
0x16, 0x03, 0x01, 0x00, 0x68, 0x01, 0x00, 0x00,
0x64, 0x03, 0x01, 0x4e, 0x4e, 0xbe, 0xc2, 0xa1,
0x21, 0xad, 0xbc, 0x28, 0x33, 0xca, 0xa1, 0xd6,
0x6e, 0x57, 0xb9, 0x1f, 0x8c, 0x19, 0x0e, 0x44,
0x16, 0x9e, 0x7d, 0x20, 0x35, 0x4b, 0x65, 0xb2,
0xc0, 0xd5, 0xa8, 0x00, 0x00, 0x28, 0x00, 0x39,
0x00, 0x38, 0x00, 0x35, 0x00, 0x16, 0x00, 0x13,
0x00, 0x0a, 0x00, 0x33, 0x00, 0x32, 0x00, 0x2f,
0x00, 0x05, 0x00, 0x04, 0x00, 0x15, 0x00, 0x12,
0x00, 0x09, 0x00, 0x14, 0x00, 0x11, 0x00, 0x08,
0x00, 0x06, 0x00, 0x03, 0x00, 0xff, 0x02, 0x01,
0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x0e, 0x00
])
BAD_DATA2: bytes = bytes([
0x16, 0x03, 0x01, 0x00
])
TLS_1_2_BAD: bytes = bytes([
# TLS record
0x16, # Content Type: Handshake
0x03, 0x01, # Version: TLS 1.0
0x00, 0x48, # Length
# Handshake
0x01, # Handshake Type: Client Hello
0x00, 0x00, 0x42, # Length
0x03, 0x03, # Version: TLS 1.2
# Random
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, # Session ID Length
0x00, 0x04, # Cipher Suites Length
0x00, 0x01, # NULL-MD5
0x00, 0xff, # RENEGOTIATION INFO SCSV
0x01, # Compression Methods
0x00, # NULL
0x00, 0x17, # Extensions Length
# Extension
0x00, 0x00, # Extension Type: Server Name
0x00, 0x0e, # Length
0x00, 0x0c, # Server Name Indication Length
0x00, # Server Name Type: host_name
0x00, 0x09, # Length
# "localhost"
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74,
# Extension
0x00, 0x0f, # Extension Type: Heart Beat
0x00, 0x01, # Length
0x01 # Mode: Peer allows to send requests
])
NabuCasa-snitun-b72e8b6/tests/server/test_all.py 0000664 0000000 0000000 00000003725 14730517453 0021777 0 ustar 00root root 0000000 0000000 """Tests for peer listener & manager."""
import asyncio
from datetime import datetime, timedelta, timezone
import hashlib
import ipaddress
import os
from snitun.multiplexer.core import Multiplexer
from snitun.multiplexer.crypto import CryptoTransport
from .const_fernet import create_peer_config
from .const_tls import TLS_1_2
IP_ADDR = ipaddress.ip_address("127.0.0.1")
async def test_server_full(
peer_manager, peer_listener, test_client_peer, sni_proxy, test_client_ssl
):
"""Run a full flow of with a peer after that disconnect."""
peer_messages = []
peer_address = []
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
test_client_peer.writer.write(fernet_token)
await test_client_peer.writer.drain()
token = await test_client_peer.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_peer.writer.write(crypto.encrypt(token))
await test_client_peer.writer.drain()
await asyncio.sleep(0.1)
assert peer_manager.peer_available(hostname)
async def mock_new_channel(multiplexer, channel):
"""Mock new channel."""
while True:
message = await channel.read()
peer_messages.append(message)
peer_address.append(channel.ip_address)
multiplexer = Multiplexer(
crypto, test_client_peer.reader, test_client_peer.writer, mock_new_channel
)
test_client_ssl.writer.write(TLS_1_2)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert peer_messages
assert peer_messages[0] == TLS_1_2
assert peer_address
assert peer_address[0] == IP_ADDR
multiplexer.shutdown()
await multiplexer.wait()
await asyncio.sleep(0.1)
assert not peer_manager.peer_available(hostname)
NabuCasa-snitun-b72e8b6/tests/server/test_listener_peer.py 0000664 0000000 0000000 00000011254 14730517453 0024063 0 ustar 00root root 0000000 0000000 """Tests for peer listener & manager."""
import asyncio
from datetime import datetime, timedelta, timezone
import hashlib
import os
import pytest
from snitun.multiplexer.crypto import CryptoTransport
from snitun.server.listener_peer import PeerListener
from .const_fernet import create_peer_config
async def test_init_listener(peer_manager):
"""Create a PeerListener instance and start/stop it."""
listener = PeerListener(peer_manager, "127.0.0.1", "8893")
await listener.start()
await asyncio.sleep(0.1)
await listener.stop()
async def test_peer_listener(peer_manager, peer_listener, test_client_peer):
"""Run a full flow of with a peer."""
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
test_client_peer.writer.write(fernet_token)
await test_client_peer.writer.drain()
token = await test_client_peer.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_peer.writer.write(crypto.encrypt(token))
await test_client_peer.writer.drain()
await asyncio.sleep(0.1)
assert peer_manager.peer_available(hostname)
async def test_peer_listener_invalid(peer_manager, peer_listener, test_client_peer):
"""Run a full flow of with a peer."""
valid = datetime.now(tz=timezone.utc) - timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
test_client_peer.writer.write(fernet_token)
await test_client_peer.writer.drain()
with pytest.raises(asyncio.IncompleteReadError):
token = await test_client_peer.reader.readexactly(32)
async def test_peer_listener_disconnect(peer_manager, peer_listener, test_client_peer):
"""Run a full flow of with a peer after that disconnect."""
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
test_client_peer.writer.write(fernet_token)
await test_client_peer.writer.drain()
token = await test_client_peer.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_peer.writer.write(crypto.encrypt(token))
await test_client_peer.writer.drain()
await asyncio.sleep(0.1)
assert peer_manager.peer_available(hostname)
test_client_peer.writer.close()
await asyncio.sleep(0.1)
assert not peer_manager.peer_available(hostname)
async def test_peer_listener_timeout(
raise_timeout, peer_manager, peer_listener, test_client_peer
):
"""Run a full flow of with a peer."""
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
test_client_peer.writer.write(fernet_token)
await test_client_peer.writer.drain()
with pytest.raises(asyncio.IncompleteReadError):
token = await test_client_peer.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_peer.writer.write(crypto.encrypt(token))
await test_client_peer.writer.drain()
await asyncio.sleep(0.1)
assert not peer_manager.peer_available(hostname)
async def test_peer_listener_expire(peer_manager, peer_listener, test_client_peer):
"""Run a full flow of with a peer."""
from snitun.server import listener_peer
listener_peer.CHECK_VALID_EXPIRE = 0.1
valid = datetime.now() + timedelta(seconds=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
test_client_peer.writer.write(fernet_token)
await test_client_peer.writer.drain()
token = await test_client_peer.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_peer.writer.write(crypto.encrypt(token))
await test_client_peer.writer.drain()
await asyncio.sleep(0.1)
assert peer_manager.peer_available(hostname)
await asyncio.sleep(1)
assert not peer_manager.peer_available(hostname)
listener_peer.CHECK_VALID_EXPIRE = 3600
NabuCasa-snitun-b72e8b6/tests/server/test_listener_sni.py 0000664 0000000 0000000 00000011570 14730517453 0023722 0 ustar 00root root 0000000 0000000 """Test for SSL SNI proxy."""
from __future__ import annotations
import asyncio
import ipaddress
import pytest
from snitun.server.listener_sni import SNIProxy
from .const_tls import TLS_1_2
IP_ADDR = ipaddress.ip_address("127.0.0.1")
async def test_proxy_up_down():
"""Simple start stop of proxy."""
proxy = SNIProxy({}, "127.0.0.1", "8863")
await proxy.start()
await proxy.stop()
@pytest.mark.parametrize(
"payloads",
[
[TLS_1_2],
[TLS_1_2[:6], TLS_1_2[6:]],
[TLS_1_2[:6], TLS_1_2[6:20], TLS_1_2[20:]],
[TLS_1_2[:6], TLS_1_2[6:20], TLS_1_2[20:32], TLS_1_2[32:]],
],
)
async def test_sni_proxy_flow(
multiplexer_client,
test_client_ssl,
payloads: list[bytes],
):
"""Test a normal flow of connection and exchange data."""
for payload in payloads:
test_client_ssl.writer.write(payload)
await asyncio.sleep(0.1)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert multiplexer_client._channels
channel = next(iter(multiplexer_client._channels.values()))
assert channel.ip_address == IP_ADDR
client_hello = await channel.read()
assert client_hello == TLS_1_2
test_client_ssl.writer.write(b"Very secret!")
await test_client_ssl.writer.drain()
data = await channel.read()
assert data == b"Very secret!"
await channel.write(b"my answer")
data = await test_client_ssl.reader.read(1024)
assert data == b"my answer"
async def test_sni_proxy_flow_close_by_client(
multiplexer_client,
test_client_ssl,
event_loop,
):
"""Test a normal flow of connection data and close by client."""
loop = event_loop
test_client_ssl.writer.write(TLS_1_2)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert multiplexer_client._channels
channel = next(iter(multiplexer_client._channels.values()))
assert channel.ip_address == IP_ADDR
client_hello = await channel.read()
assert client_hello == TLS_1_2
test_client_ssl.writer.write(b"Very secret!")
await test_client_ssl.writer.drain()
data = await channel.read()
assert data == b"Very secret!"
ssl_client_read = loop.create_task(test_client_ssl.reader.read(2024))
await asyncio.sleep(0.1)
assert not ssl_client_read.done()
await multiplexer_client.delete_channel(channel)
await asyncio.sleep(0.1)
assert ssl_client_read.done()
async def test_sni_proxy_flow_close_by_server(
multiplexer_client,
test_client_ssl,
event_loop,
):
"""Test a normal flow of connection data and close by server."""
loop = event_loop
test_client_ssl.writer.write(TLS_1_2)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert multiplexer_client._channels
channel = next(iter(multiplexer_client._channels.values()))
assert channel.ip_address == IP_ADDR
client_hello = await channel.read()
assert client_hello == TLS_1_2
test_client_ssl.writer.write(b"Very secret!")
await test_client_ssl.writer.drain()
data = await channel.read()
assert data == b"Very secret!"
client_read = loop.create_task(channel.read())
await asyncio.sleep(0.1)
assert not client_read.done()
test_client_ssl.writer.close()
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
assert client_read.done()
async def test_sni_proxy_flow_peer_not(peer, multiplexer_client, test_client_ssl):
"""Test a normal flow of connection with peer is not ready."""
peer._multiplexer = None # Fake peer state
test_client_ssl.writer.write(TLS_1_2)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
async def test_sni_proxy_timeout(multiplexer_client, test_client_ssl, raise_timeout):
"""Test a normal flow of connection and exchange data."""
test_client_ssl.writer.write(TLS_1_2)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert not multiplexer_client._channels
async def test_sni_proxy_flow_timeout(multiplexer_client, test_client_ssl):
"""Test a normal flow of connection and exchange data."""
from snitun.server import listener_sni
listener_sni.TCP_SESSION_TIMEOUT = 0.2
test_client_ssl.writer.write(TLS_1_2)
await test_client_ssl.writer.drain()
await asyncio.sleep(0.1)
assert multiplexer_client._channels
channel = next(iter(multiplexer_client._channels.values()))
assert channel.ip_address == IP_ADDR
client_hello = await channel.read()
assert client_hello == TLS_1_2
test_client_ssl.writer.write(b"Very secret!")
await test_client_ssl.writer.drain()
data = await channel.read()
assert data == b"Very secret!"
await channel.write(b"my answer")
data = await test_client_ssl.reader.read(1024)
assert data == b"my answer"
await asyncio.sleep(0.3)
assert not multiplexer_client._channels
NabuCasa-snitun-b72e8b6/tests/server/test_peer.py 0000664 0000000 0000000 00000013564 14730517453 0022164 0 ustar 00root root 0000000 0000000 """Test a Peer object."""
from datetime import datetime, timedelta, timezone
import asyncio
import hashlib
import os
import pytest
from snitun.multiplexer.message import CHANNEL_FLOW_PING
from snitun.multiplexer.crypto import CryptoTransport
from snitun.server.peer import Peer
from snitun.exceptions import SniTunChallengeError
def test_init_peer():
"""Test simple init of peer."""
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
peer = Peer(
"localhost",
valid,
os.urandom(32),
os.urandom(16),
alias="localhost.custom",
)
assert peer.is_valid
assert peer.hostname == "localhost"
assert peer.multiplexer is None
assert peer.alias == "localhost.custom"
async def test_init_peer_multiplexer(event_loop, test_client, test_server):
"""Test setup multiplexer."""
loop = event_loop
client = test_server[0]
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
peer = Peer("localhost", valid, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
with pytest.raises(RuntimeError):
await peer.wait_disconnect()
init_task = loop.create_task(
peer.init_multiplexer_challenge(test_client.reader, test_client.writer)
)
await asyncio.sleep(0.1)
assert not init_task.done()
assert not peer.is_ready
assert not peer.is_connected
token = await client.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
client.writer.write(crypto.encrypt(token))
await client.writer.drain()
await asyncio.sleep(0.1)
assert init_task.exception() is None
assert init_task.done()
assert peer.is_ready
assert peer.is_connected
assert peer.multiplexer._throttling is None
client.writer.close()
client.close.set()
await asyncio.sleep(0.1)
assert not peer.multiplexer.is_connected
async def test_init_peer_multiplexer_crypto(event_loop, test_client, test_server):
"""Test setup multiplexer with crypto."""
loop = event_loop
client = test_server[0]
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
peer = Peer("localhost", valid, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
with pytest.raises(RuntimeError):
await peer.wait_disconnect()
init_task = loop.create_task(
peer.init_multiplexer_challenge(test_client.reader, test_client.writer)
)
await asyncio.sleep(0.1)
assert not init_task.done()
assert not peer.is_ready
assert not peer.is_connected
token = await client.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
client.writer.write(crypto.encrypt(token))
await client.writer.drain()
await asyncio.sleep(0.1)
assert init_task.exception() is None
assert init_task.done()
assert peer.is_ready
assert peer.is_connected
ping_task = loop.create_task(peer.multiplexer.ping())
await asyncio.sleep(0.1)
ping_data = await client.reader.read(1024)
ping = crypto.decrypt(ping_data)
assert ping[16] == CHANNEL_FLOW_PING
assert int.from_bytes(ping[17:21], "big") == 0
assert ping[21:25] == b"ping"
ping_task.cancel()
client.writer.close()
client.close.set()
await asyncio.sleep(0.1)
assert peer.multiplexer.wait().done()
async def test_init_peer_wrong_challenge(event_loop, test_client, test_server):
"""Test setup multiplexer wrong challenge."""
loop = event_loop
client = test_server[0]
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
peer = Peer("localhost", valid, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
with pytest.raises(RuntimeError):
await peer.wait_disconnect()
init_task = loop.create_task(
peer.init_multiplexer_challenge(test_client.reader, test_client.writer)
)
await asyncio.sleep(0.1)
assert not init_task.done()
token = await client.reader.readexactly(32)
client.writer.write(crypto.encrypt(token))
await client.writer.drain()
await asyncio.sleep(0.1)
with pytest.raises(SniTunChallengeError):
raise init_task.exception()
assert init_task.done()
client.writer.close()
client.close.set()
def test_init_peer_invalid():
"""Test simple init of peer with invalid date."""
valid = datetime.now(tz=timezone.utc) - timedelta(days=1)
peer = Peer("localhost", valid, os.urandom(32), os.urandom(16))
assert not peer.is_valid
assert peer.hostname == "localhost"
assert peer.multiplexer is None
async def test_init_peer_multiplexer_throttling(event_loop, test_client, test_server):
"""Test setup multiplexer."""
loop = event_loop
client = test_server[0]
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
peer = Peer("localhost", valid, aes_key, aes_iv, throttling=500)
crypto = CryptoTransport(aes_key, aes_iv)
with pytest.raises(RuntimeError):
await peer.wait_disconnect()
init_task = loop.create_task(
peer.init_multiplexer_challenge(test_client.reader, test_client.writer)
)
await asyncio.sleep(0.1)
assert not init_task.done()
assert not peer.is_ready
assert not peer.is_connected
token = await client.reader.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
client.writer.write(crypto.encrypt(token))
await client.writer.drain()
await asyncio.sleep(0.1)
assert init_task.exception() is None
assert init_task.done()
assert peer.is_ready
assert peer.is_connected
assert peer.multiplexer._throttling == 0.002
client.writer.close()
client.close.set()
await asyncio.sleep(0.1)
assert not peer.multiplexer.is_connected
NabuCasa-snitun-b72e8b6/tests/server/test_peer_manager.py 0000664 0000000 0000000 00000021111 14730517453 0023641 0 ustar 00root root 0000000 0000000 """Test peer manager."""
import asyncio
import os
from datetime import datetime, timedelta, timezone
from unittest.mock import patch
import pytest
from snitun.exceptions import SniTunInvalidPeer
from snitun.server.peer import Peer
from snitun.server.peer_manager import PeerManager, PeerManagerEvent
from .const_fernet import FERNET_TOKENS, create_peer_config
async def test_simple_init_peer_manager():
"""Simple init a peer manager."""
manager = PeerManager(FERNET_TOKENS)
assert manager._fernet
assert not manager._peers
assert manager._throttling is None
async def test_init_new_peer():
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
peer = manager.create_peer(fernet_token)
assert peer.hostname == hostname
assert not peer.is_ready
assert not manager.get_peer(hostname)
assert not manager.peer_available(hostname)
assert hostname not in manager._peers
assert manager.connections == 0
manager.add_peer(peer)
assert manager.get_peer(hostname)
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
async def test_init_new_peer_with_alias():
"""Init a new peer with custom domain."""
manager = PeerManager(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
alias = "localhost.custom"
fernet_token = create_peer_config(
valid.timestamp(), hostname, aes_key, aes_iv, alias=[alias]
)
peer = manager.create_peer(fernet_token)
assert peer.hostname == hostname
assert peer.alias == [alias]
assert not peer.is_ready
assert not manager.get_peer(hostname)
assert not manager.get_peer(alias)
assert not manager.peer_available(hostname)
assert not manager.peer_available(alias)
assert hostname not in manager._peers
assert alias not in manager._peers
assert manager.connections == 0
manager.add_peer(peer)
assert manager.get_peer(hostname)
assert manager.get_peer(alias)
assert not manager.peer_available(hostname)
assert not manager.peer_available(alias)
assert hostname in manager._peers
assert alias in manager._peers
assert manager.connections == 2
async def test_init_new_peer_not_valid_time():
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) - timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
with pytest.raises(SniTunInvalidPeer):
manager.create_peer(fernet_token)
async def test_init_new_peer_invalid_fernet():
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS)
with pytest.raises(SniTunInvalidPeer):
manager.create_peer(os.urandom(100))
async def test_init_new_peer_with_removing():
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
peer = manager.create_peer(fernet_token)
assert peer.hostname == hostname
assert not peer.is_ready
manager.add_peer(peer)
assert manager.get_peer(hostname)
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
manager.remove_peer(peer)
assert manager.get_peer(hostname) is None
assert not manager.peer_available(hostname)
assert not hostname in manager._peers
async def test_init_new_peer_with_events():
"""Init a new peer and remove with events."""
events = []
def _events(ev_peer: Peer, type_event: PeerManagerEvent) -> None:
events.append((ev_peer, type_event))
manager = PeerManager(FERNET_TOKENS, event_callback=_events)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
peer = manager.create_peer(fernet_token)
assert peer.hostname == hostname
assert not peer.is_ready
manager.add_peer(peer)
assert manager.get_peer(hostname)
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
await asyncio.sleep(0.1)
assert events[-1][0] == peer
assert events[-1][1] == PeerManagerEvent.CONNECTED
manager.remove_peer(peer)
assert manager.get_peer(hostname) is None
assert not manager.peer_available(hostname)
assert not hostname in manager._peers
await asyncio.sleep(0.1)
assert events[-1][0] == peer
assert events[-1][1] == PeerManagerEvent.DISCONNECTED
async def test_init_new_peer_throttling():
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS, throttling=500)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
peer = manager.create_peer(fernet_token)
assert peer.hostname == hostname
assert not peer.is_ready
assert peer._throttling == 500
manager.add_peer(peer)
assert manager.get_peer(hostname)
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
async def test_init_dual_peer_with_removing():
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
peer1 = manager.create_peer(fernet_token)
peer2 = manager.create_peer(fernet_token)
assert peer1.hostname == hostname
assert peer2.hostname == hostname
assert not peer1.is_ready
assert not peer2.is_ready
manager.add_peer(peer1)
assert manager.get_peer(hostname) == peer1
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
manager.add_peer(peer2)
assert manager.get_peer(hostname) == peer2
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
manager.remove_peer(peer1)
assert manager.get_peer(hostname) == peer2
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
manager.remove_peer(peer2)
assert manager.get_peer(hostname) is None
assert not manager.peer_available(hostname)
assert not hostname in manager._peers
async def test_init_dual_peer_with_multiplexer(multiplexer_client):
"""Init a new peer."""
manager = PeerManager(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
peer1 = manager.create_peer(fernet_token)
peer2 = manager.create_peer(fernet_token)
assert peer1.hostname == hostname
assert peer2.hostname == hostname
assert not peer1.is_ready
assert not peer2.is_ready
peer1._multiplexer = multiplexer_client
assert peer1.is_ready
manager.add_peer(peer1)
assert manager.get_peer(hostname) == peer1
assert manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
manager.add_peer(peer2)
assert manager.get_peer(hostname) == peer2
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
manager.remove_peer(peer1)
assert manager.get_peer(hostname) == peer2
assert not manager.peer_available(hostname)
assert hostname in manager._peers
assert manager.connections == 1
await asyncio.sleep(0.1)
assert not multiplexer_client.is_connected
manager.remove_peer(peer2)
assert manager.get_peer(hostname) is None
assert not manager.peer_available(hostname)
assert not hostname in manager._peers
NabuCasa-snitun-b72e8b6/tests/server/test_run.py 0000664 0000000 0000000 00000026566 14730517453 0022043 0 ustar 00root root 0000000 0000000 """Test runner of SniTun Server."""
from __future__ import annotations
import asyncio
from datetime import datetime, timedelta, timezone
import hashlib
import ipaddress
import os
import socket
import time
from unittest.mock import patch
import pytest
from snitun.multiplexer.core import Multiplexer
from snitun.multiplexer.crypto import CryptoTransport
from snitun.server.run import SniTunServer, SniTunServerSingle, SniTunServerWorker
from .const_fernet import FERNET_TOKENS, create_peer_config
from .const_tls import TLS_1_2
IP_ADDR = ipaddress.ip_address("127.0.0.1")
async def test_snitun_runner_updown():
"""Test SniTun Server runner object."""
server = SniTunServer(
FERNET_TOKENS,
peer_host="127.0.0.1",
sni_host="127.0.0.1",
sni_port=32000,
)
await server.start()
await asyncio.sleep(0.1)
await server.stop()
async def test_snitun_single_runner_updown():
"""Test SniTun Single Server runner object."""
server = SniTunServerSingle(FERNET_TOKENS, host="127.0.0.1", port=32000)
await server.start()
await asyncio.sleep(0.1)
await server.stop()
def test_snitun_worker_runner_updown(event_loop):
"""Test SniTun Worker Server runner object."""
server = SniTunServerWorker(
FERNET_TOKENS,
host="127.0.0.1",
port=32001,
worker_size=2,
)
server.start()
time.sleep(0.1)
server.stop()
async def test_snitun_single_runner():
"""Test SniTunSingle Server runner object."""
peer_messages = []
peer_address = []
server = SniTunServerSingle(FERNET_TOKENS, host="127.0.0.1", port=32000)
await server.start()
reader_peer, writer_peer = await asyncio.open_connection(
host="127.0.0.1",
port="32000",
)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
writer_peer.write(fernet_token)
await writer_peer.drain()
token = await reader_peer.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
writer_peer.write(crypto.encrypt(token))
await writer_peer.drain()
await asyncio.sleep(0.1)
assert server.peers.peer_available(hostname)
async def mock_new_channel(multiplexer, channel):
"""Mock new channel."""
while True:
message = await channel.read()
peer_messages.append(message)
peer_address.append(channel.ip_address)
_, writer_ssl = await asyncio.open_connection(host="127.0.0.1", port="32000")
multiplexer = Multiplexer(crypto, reader_peer, writer_peer, mock_new_channel)
writer_ssl.write(TLS_1_2)
await writer_ssl.drain()
await asyncio.sleep(0.1)
assert peer_messages
assert peer_messages[0] == TLS_1_2
assert peer_address
assert peer_address[0] == IP_ADDR
multiplexer.shutdown()
await multiplexer.wait()
await asyncio.sleep(0.1)
assert not server.peers.peer_available(hostname)
writer_ssl.close()
await server.stop()
async def test_snitun_single_runner_timeout(raise_timeout):
"""Test SniTunSingle Server runner object."""
server = SniTunServerSingle(FERNET_TOKENS, host="127.0.0.1", port="32000")
await server.start()
reader_peer, writer_peer = await asyncio.open_connection(
host="127.0.0.1",
port="32000",
)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
writer_peer.write(fernet_token)
await writer_peer.drain()
with pytest.raises(ConnectionResetError):
token = await reader_peer.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
writer_peer.write(crypto.encrypt(token))
await writer_peer.drain()
await asyncio.sleep(0.1)
assert not server.peers.peer_available(hostname)
await server.stop()
async def test_snitun_single_runner_invalid_payload(raise_timeout):
"""Test SniTunSingle Server runner object with invalid payload."""
server = SniTunServerSingle(FERNET_TOKENS, host="127.0.0.1", port="32000")
await server.start()
reader_peer, writer_peer = await asyncio.open_connection(
host="127.0.0.1",
port="32000",
)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
crypto = CryptoTransport(aes_key, aes_iv)
writer_peer.write(b"INVALID")
await writer_peer.drain()
with pytest.raises(ConnectionResetError):
token = await reader_peer.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
writer_peer.write(crypto.encrypt(token))
await writer_peer.drain()
await asyncio.sleep(0.1)
assert not server.peers.peer_available(hostname)
await server.stop()
async def test_snitun_single_runner_throttling():
"""Test SniTunSingle Server runner object."""
peer_messages = []
peer_address = []
server = SniTunServerSingle(
FERNET_TOKENS,
host="127.0.0.1",
port="32000",
throttling=500,
)
await server.start()
reader_peer, writer_peer = await asyncio.open_connection(
host="127.0.0.1",
port="32000",
)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
writer_peer.write(fernet_token)
await writer_peer.drain()
token = await reader_peer.readexactly(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
writer_peer.write(crypto.encrypt(token))
await writer_peer.drain()
await asyncio.sleep(0.1)
assert server.peers.peer_available(hostname)
async def mock_new_channel(multiplexer, channel):
"""Mock new channel."""
while True:
message = await channel.read()
peer_messages.append(message)
peer_address.append(channel.ip_address)
_, writer_ssl = await asyncio.open_connection(host="127.0.0.1", port="32000")
multiplexer = Multiplexer(crypto, reader_peer, writer_peer, mock_new_channel)
writer_ssl.write(TLS_1_2)
await writer_ssl.drain()
await asyncio.sleep(0.1)
assert peer_messages
assert peer_messages[0] == TLS_1_2
assert peer_address
assert peer_address[0] == IP_ADDR
peer = server.peers.get_peer(hostname)
assert peer._multiplexer._throttling == 0.002
multiplexer.shutdown()
await multiplexer.wait()
await asyncio.sleep(0.1)
assert not server.peers.peer_available(hostname)
writer_ssl.close()
await server.stop()
@pytest.mark.parametrize(
"payloads",
[
[TLS_1_2],
[TLS_1_2[:6], TLS_1_2[6:]],
[TLS_1_2[:6], TLS_1_2[6:20], TLS_1_2[20:]],
[TLS_1_2[:6], TLS_1_2[6:20], TLS_1_2[20:32], TLS_1_2[32:]],
],
)
def test_snitun_worker_runner(event_loop, payloads: list[bytes]):
"""Test SniTunWorker Server runner object."""
loop = event_loop
peer_messages = []
peer_address = []
server = SniTunServerWorker(
FERNET_TOKENS,
host="127.0.0.1",
port=32001,
worker_size=2,
)
server.start()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 32001))
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
sock.sendall(fernet_token)
token = sock.recv(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
sock.sendall(crypto.encrypt(token))
time.sleep(1)
assert any(worker.is_responsible_peer(hostname) for worker in server._workers)
assert server.peer_counter == 1
async def mock_new_channel(multiplexer, channel):
"""Mock new channel."""
while True:
message = await channel.read()
peer_messages.append(message)
peer_address.append(channel.ip_address)
sock_ssl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_ssl.connect(("127.0.0.1", 32001))
async def _create_multiplexer() -> Multiplexer:
"""Create and return the peer multiplexer."""
reader_peer, writer_peer = await asyncio.open_connection(sock=sock)
return Multiplexer(crypto, reader_peer, writer_peer, mock_new_channel)
multiplexer = loop.run_until_complete(_create_multiplexer())
for payload in payloads:
sock_ssl.sendall(payload)
loop.run_until_complete(asyncio.sleep(0.1))
assert peer_messages
assert peer_messages[0] == TLS_1_2
assert peer_address
assert peer_address[0] == IP_ADDR
loop.call_soon_threadsafe(multiplexer.shutdown)
loop.run_until_complete(multiplexer.wait())
time.sleep(1)
assert not any(worker.is_responsible_peer(hostname) for worker in server._workers)
sock_ssl.close()
server.stop()
def test_snitun_worker_timeout(event_loop):
"""Test SniTunWorker Server runner object timeout."""
from snitun.server import run
run.WORKER_STALE_MAX = 1
server = SniTunServerWorker(
FERNET_TOKENS,
host="127.0.0.1",
port=32001,
worker_size=2,
)
server.start()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 32001))
time.sleep(1.5)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
crypto = CryptoTransport(aes_key, aes_iv)
with pytest.raises(OSError):
sock.sendall(fernet_token)
token = sock.recv(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
sock.sendall(crypto.encrypt(token))
server.stop()
def test_snitun_worker_runner_invalid_payload(event_loop):
"""Test SniTunWorker Server runner invalid payload."""
server = SniTunServerWorker(
FERNET_TOKENS,
host="127.0.0.1",
port=32001,
worker_size=2,
)
server.start()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 32001))
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
crypto = CryptoTransport(aes_key, aes_iv)
sock.sendall(b"INVALID")
with pytest.raises(OSError):
for _ in range(3):
token = sock.recv(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
sock.sendall(crypto.encrypt(token))
server.stop()
@patch("snitun.server.run.os.kill")
def test_snitun_worker_crash(kill, event_loop):
"""Test SniTunWorker Server runner object with crashing worker."""
server = SniTunServerWorker(
FERNET_TOKENS,
host="127.0.0.1",
port=32001,
worker_size=2,
)
server.start()
for worker in server._workers:
worker.shutdown()
break
time.sleep(1.5)
assert kill.called
server.stop()
NabuCasa-snitun-b72e8b6/tests/server/test_sni.py 0000664 0000000 0000000 00000001376 14730517453 0022020 0 ustar 00root root 0000000 0000000 """Tests for SNI parser."""
import pytest
from snitun.exceptions import ParseSNIError
from snitun.server import sni
from . import const_tls as raw
@pytest.mark.parametrize(
"test_package",
[
raw.TLS_1_0,
raw.TLS_1_0_OLD,
raw.TLS_1_2,
raw.TLS_1_2_MORE,
raw.TLS_1_2_ORDER,
raw.TLS_1_2_BAD,
],
)
def test_good_client_hello(test_package: bytes):
"""Test good TLS packages."""
assert sni.parse_tls_sni(test_package) == "localhost"
@pytest.mark.parametrize(
"test_package", [raw.BAD_DATA1, raw.BAD_DATA2, raw.SSL_2_0, raw.SSL_3_0]
)
def test_bad_client_hello(test_package: bytes):
"""Test bad client hello."""
with pytest.raises(ParseSNIError):
sni.parse_tls_sni(test_package)
NabuCasa-snitun-b72e8b6/tests/server/test_worker.py 0000664 0000000 0000000 00000007017 14730517453 0022536 0 ustar 00root root 0000000 0000000 """Tests for the server worker."""
from datetime import datetime, timedelta, timezone
import hashlib
import os
import socket
import time
from snitun.multiplexer.crypto import CryptoTransport
from snitun.server.worker import ServerWorker
from .const_fernet import FERNET_TOKENS, create_peer_config
from .const_tls import TLS_1_2
def test_worker_up_down(event_loop):
"""Test if worker start and stop."""
worker = ServerWorker(FERNET_TOKENS)
worker.start()
assert worker.is_alive()
assert worker.peer_size == 0
worker.shutdown()
assert worker.exitcode == 0
assert not worker.is_alive()
def test_peer_connection(test_server_sync, test_client_sync, event_loop):
"""Run a full flow of with a peer."""
worker = ServerWorker(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
worker.start()
crypto = CryptoTransport(aes_key, aes_iv)
worker.handover_connection(test_server_sync[-1], fernet_token, None)
token = test_client_sync.recv(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_sync.sendall(crypto.encrypt(token))
time.sleep(1)
assert worker.is_responsible_peer(hostname)
assert worker.peer_size == 1
worker.shutdown()
assert worker.peer_size == 0
def test_peer_connection_disconnect(test_server_sync, test_client_sync, event_loop):
"""Run a full flow of with a peer & disconnect."""
worker = ServerWorker(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = create_peer_config(valid.timestamp(), hostname, aes_key, aes_iv)
worker.start()
crypto = CryptoTransport(aes_key, aes_iv)
worker.handover_connection(test_server_sync[-1], fernet_token, None)
token = test_client_sync.recv(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_sync.sendall(crypto.encrypt(token))
time.sleep(1)
assert worker.is_responsible_peer(hostname)
assert worker.peer_size == 1
test_client_sync.shutdown(socket.SHUT_RDWR)
time.sleep(1)
assert not worker.is_responsible_peer(hostname)
assert worker.peer_size == 0
worker.shutdown()
def test_sni_connection(
test_server_sync,
test_client_sync,
test_client_ssl_sync,
event_loop,
):
"""Run a full flow of with a peer."""
worker = ServerWorker(FERNET_TOKENS)
valid = datetime.now(tz=timezone.utc) + timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
alias = ["localhost.custom"]
fernet_token = create_peer_config(
valid.timestamp(),
hostname,
aes_key,
aes_iv,
alias=alias,
)
worker.start()
crypto = CryptoTransport(aes_key, aes_iv)
worker.handover_connection(test_server_sync[0], fernet_token, None)
token = test_client_sync.recv(32)
token = hashlib.sha256(crypto.decrypt(token)).digest()
test_client_sync.sendall(crypto.encrypt(token))
time.sleep(1)
assert worker.is_responsible_peer(hostname)
for entry in alias:
assert worker.is_responsible_peer(entry)
worker.handover_connection(test_server_sync[1], TLS_1_2, hostname)
assert len(test_client_sync.recv(1048)) == 32
assert worker.peer_size == 1
worker.shutdown()
assert worker.peer_size == 0
NabuCasa-snitun-b72e8b6/tests/utils/ 0000775 0000000 0000000 00000000000 14730517453 0017441 5 ustar 00root root 0000000 0000000 NabuCasa-snitun-b72e8b6/tests/utils/__init__.py 0000664 0000000 0000000 00000000027 14730517453 0021551 0 ustar 00root root 0000000 0000000 """Tests for utils."""
NabuCasa-snitun-b72e8b6/tests/utils/test_aes.py 0000664 0000000 0000000 00000000670 14730517453 0021625 0 ustar 00root root 0000000 0000000 """Test aes generator function."""
from snitun.utils import aes
from snitun.multiplexer.crypto import CryptoTransport
def test_aes_function():
"""Test crypto with generated keys."""
key, iv = aes.generate_aes_keyset()
assert CryptoTransport(key, iv)
def test_unique_aes():
"""Test unique aes function."""
keyset_1 = aes.generate_aes_keyset()
keyset_2 = aes.generate_aes_keyset()
assert keyset_1 != keyset_2
NabuCasa-snitun-b72e8b6/tests/utils/test_aiohttp_client.py 0000664 0000000 0000000 00000001623 14730517453 0024062 0 ustar 00root root 0000000 0000000 """Tests for aiohttp snitun client."""
from unittest.mock import patch
from snitun.utils.aiohttp_client import SniTunClientAioHttp
async def test_init_client():
"""Init aiohttp client for test."""
with patch("snitun.utils.aiohttp_client.SockSite"):
client = SniTunClientAioHttp(None, None, "127.0.0.1")
assert not client.is_connected
async def test_client_stop_no_wait():
"""Test that we do not wait if wait is not passed to the stop"""
with patch("snitun.utils.aiohttp_client.SockSite"):
client = SniTunClientAioHttp(None, None, "127.0.0.1")
with patch(
"snitun.utils.aiohttp_client._async_waitfor_socket_closed"
) as waitfor_socket_closed:
waitfor_socket_closed.assert_not_called()
await client.stop()
waitfor_socket_closed.assert_not_called()
await client.stop(wait=True)
waitfor_socket_closed.assert_called()
NabuCasa-snitun-b72e8b6/tests/utils/test_ipaddress.py 0000664 0000000 0000000 00000001012 14730517453 0023022 0 ustar 00root root 0000000 0000000 """Test ipaddress module."""
from ipaddress import ip_address
from snitun.utils import ipaddress as ip_modul
def test_ipaddress_to_binary():
"""Test ip address to binary."""
my_ip = ip_address("192.168.1.1")
my_ip_bin = b"\xc0\xa8\x01\x01"
assert ip_modul.ip_address_to_bytes(my_ip) == my_ip_bin
def test_binary_to_ipaddress():
"""Test ip address to binary."""
my_ip = ip_address("192.168.1.1")
my_ip_bin = b"\xc0\xa8\x01\x01"
assert ip_modul.bytes_to_ip_address(my_ip_bin) == my_ip
NabuCasa-snitun-b72e8b6/tests/utils/test_server.py 0000664 0000000 0000000 00000003404 14730517453 0022361 0 ustar 00root root 0000000 0000000 """Test server utils."""
import asyncio
from datetime import timedelta
import os
import pytest
from snitun.client.client_peer import ClientPeer
from snitun.client.connector import Connector
from snitun.utils import server
from snitun.exceptions import SniTunConnectionError
from ..server.const_fernet import FERNET_TOKENS
async def test_fernet_token(peer_listener, peer_manager, test_endpoint):
"""Test fernet token created by server."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = timedelta(days=1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = server.generate_client_token(
FERNET_TOKENS, valid, hostname, aes_key, aes_iv
)
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert peer_manager.peer_available("localhost")
await client.stop()
await asyncio.sleep(0.1)
assert not peer_manager.peer_available("localhost")
async def test_fernet_token_date(peer_listener, peer_manager, test_endpoint):
"""Test fernet token created by server as invalid."""
client = ClientPeer("127.0.0.1", "8893")
connector = Connector("127.0.0.1", "8822")
assert not peer_manager.peer_available("localhost")
valid = timedelta(days=-1)
aes_key = os.urandom(32)
aes_iv = os.urandom(16)
hostname = "localhost"
fernet_token = server.generate_client_token(
FERNET_TOKENS, valid, hostname, aes_key, aes_iv
)
with pytest.raises(SniTunConnectionError):
await client.start(connector, fernet_token, aes_key, aes_iv)
await asyncio.sleep(0.1)
assert not peer_manager.peer_available("localhost")