Metadata-Version: 2.4
Name: fognode
Version: 0.3.0
Summary: fognode - headless secure encrypted data transmission. TLS + X25519 + AESGCM + PBKDF2.
Project-URL: Repository, https://github.com/reekeer/fognode
Author: reekeer
License: MIT License
        
        Copyright (c) 2026 reekeer
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: cryptography>=44.0.0
Requires-Dist: pycryptodome>=3.20.0
Provides-Extra: dev
Requires-Dist: black>=24.0.0; extra == 'dev'
Requires-Dist: pyright>=1.1.409; extra == 'dev'
Requires-Dist: pytest-asyncio>=1.3.0; extra == 'dev'
Requires-Dist: pytest>=8.0.0; extra == 'dev'
Requires-Dist: ruff>=0.5.0; extra == 'dev'
Description-Content-Type: text/markdown

<h1 align="center">fognode</h1>

<h4 align="center">headless secure encrypted data transmission</h4>

<p align="center">
  <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge&logo=opensourceinitiative&logoColor=FFFFFF" alt="License"></a>
  <img src="https://img.shields.io/badge/Python-3.10%2B-blue?style=for-the-badge&logo=python&logoColor=white" alt="Python">
  <img src="https://img.shields.io/badge/Platform-Linux%20%7C%20macOS%20%7C%20Windows-lightgrey?style=for-the-badge&logo=linux&logoColor=FCC624" alt="Platform">
  <img src="https://img.shields.io/badge/code%20style-black-000000?style=for-the-badge" alt="black">
  <img src="https://img.shields.io/badge/linting-ruff-orange?style=for-the-badge" alt="ruff">
</p>

---

Peer-to-peer encrypted channel library for headless projects. Built for speed and zero-trust networking.

## Security Stack

| Layer | Technology |
|---|---|
| Transport | TLS 1.2+ with self-signed RSA-4096 certs |
| Key Exchange | X25519 ephemeral ECDH |
| Session Keys | HKDF-SHA256 |
| Channel Cipher | mini-MTProto (AES-256-IGE + SHA-256 msg_key) |
| Password Derivation | PBKDF2-HMAC-SHA256 (390k iterations) |
| Auth Challenge | HMAC-SHA256 over random nonce |
| Cert Pinning | SHA-256 fingerprint verification |

## How connection works

<p align="center">
  <img src="./docs/fognode_connection.svg" alt="fognode connection flow" width="900"/>
</p>

## Install

```bash
pip install fognode
```

## Quick Start

### Server

```python
from fognode import Server, MessageEvent, ConnectEvent, DisconnectEvent

server = Server(host="0.0.0.0", port=9443, password="secret")

@server.on_event(MessageEvent)
async def on_message(ctx):
    if ctx.event.text:
        await ctx.answer(f"echo: {ctx.event.text}")

@server.on_event(ConnectEvent)
async def on_connect(ctx):
    print("+ peer connected")

@server.on_event(DisconnectEvent)
async def on_disconnect(ctx):
    print("- peer disconnected")

if __name__ == "__main__":
    server.run()
```

### Client

```python
from fognode import Client, MessageEvent, ClosedEvent, ErrorEvent

client = Client(connect_string="7sf9Tn:9443", password="secret")

@client.on_event(MessageEvent)
async def on_message(ctx):
    print(f"msg: {ctx.event.text}")

@client.on_event(ClosedEvent)
async def on_closed(ctx):
    print("connection closed")

@client.on_event(ErrorEvent)
async def on_error(ctx):
    print(f"error: {ctx.event.exception}")

if __name__ == "__main__":
    client.connect()
```

### Classic API

```python
from fognode import start_server, client_connect

ip, code, fp = start_server("0.0.0.0", 9443, "secret")
print(f"Server running at {code}:9443")  # e.g. 7sf9Tn:9443
```

## Events

| Event | Server | Client | Description |
|---|---|---|---|
| `StartEvent` | yes | yes | Server/client started |
| `ConnectEvent` | yes | yes | Peer connected |
| `DisconnectEvent` | yes | yes | Peer disconnected |
| `MessageEvent` | yes | yes | Message received (`type`, `text`, `ts`) |
| `ClosedEvent` | no | yes | Connection closed |
| `ErrorEvent` | no | yes | Error occurred (`exception`) |

### Decorators

```python
# Specific event
@server.on_event(MessageEvent)
async def handler(ctx):
    pass

# Aliases
@server.on_connect()
@server.on_disconnect()
@server.on_message()
@client.on_closed()
@client.on_error()
```

## How mini-MTProto Works

Each message is encrypted independently using a simplified MTProto 2.0 scheme:

1. **Serialize** JSON payload to bytes
2. **Pad** to AES block size (16 bytes)
3. **Compute msg_key** = `SHA-256(padded)[8:24]` (128 bits)
4. **Derive AES key** = `SHA-256(auth_key + msg_key)`
5. **Derive AES IV** = `SHA-256(msg_key + auth_key)`
6. **Encrypt** with AES-256-IGE
7. **Send** `length(4 bytes) + msg_key(16 bytes) + ciphertext`

Decryption reverses the process and verifies `msg_key` integrity.

## CLI

```bash
# Run server
fognode server --host 0.0.0.0 --port 9443 --password secret

# Interactive client
fognode client 7sf9Tn:9443 --password secret

# Probe TLS fingerprint (no auth)
fognode probe 7sf9Tn:9443

# Server status via authenticated channel
fognode status 7sf9Tn:9443 --password secret

# Send one message and exit
fognode send 7sf9Tn:9443 --password secret --text "hello"

# Monitor messages only
fognode monitor 7sf9Tn:9443 --password secret

# Show certificate info
fognode cert --file fognode_cert.pem
```

## Project Structure

```
src/fognode/
├── app.py              # Server, Client, Context
├── cipher.py           # mini-MTProto AES-256-IGE encrypt/decrypt
├── cli/
│   └── entrypoint.py   # argparse CLI
├── core/
│   ├── client.py       # client_connect()
│   ├── events.py       # Event classes
│   ├── probe.py        # probe_server()
│   └── server.py       # start_server()
├── crypto/
│   ├── cert.py         # Self-signed cert generation
│   ├── channel.py      # SecureChannel (mini-MTProto)
│   ├── kdf.py          # Session key helpers
│   ├── kx.py           # X25519 keypair + shared secret
│   ├── password.py     # PBKDF2 password store
│   └── primitives.py   # pbkdf2, hmac256, hkdf_expand
├── filters/            # BaseFilter, Command, Text
├── handlers/           # HandlerObject
├── types/              # constants, exceptions, protocol
├── utils/              # ipwords, ratelimit, net
└── wire/               # Length-prefixed JSON framing
```

---

<p align="center"><sub><a href="LICENSE">MIT</a> © reekeer</sub></p>
