kytan-py: Python Wrapper for kytan VPN
A Python interface to the kytan high-performance peer-to-peer VPN application.
Features
- Easy Integration: Simple Python API for embedding VPN functionality in applications
- Client & Server Support: Both client and server modes supported
- Process Management: Automatic process lifecycle management with proper cleanup
- Logging Integration: Built-in logging support with configurable levels
- Context Manager Support: Use with
withstatements for automatic cleanup - Command Line Interface: Can be used as a standalone CLI tool
- Self-Contained: Automatically builds and bundles the kytan binary during installation
Requirements
- Python 3.7+
- Rust/Cargo (for building during installation)
- Root/sudo privileges (required by kytan)
- Linux (server mode) or Linux/macOS (client mode)
Installation
The package will automatically build the kytan Rust binary during installation:
# Install from source (builds kytan binary automatically)
cd kytan-py
pip install .
# Install in development mode (builds kytan binary automatically)
pip install -e .
# Install with development dependencies
pip install -e ".[dev]"
Build Requirements
During installation, the package will:
- Check for Rust/Cargo availability
- Build the kytan binary from source using
cargo build --release - Bundle the binary with the Python package
- Make it available to the Python wrapper
If you don't have Rust installed, you can install it from https://rustup.rs/.
Quick Start
As a Python Library
Client Mode
from kytan import create_client
# Create and connect client (uses bundled kytan binary automatically)
client = create_client()
client.connect(
server="your-server.com",
port=9527,
key="your-secret-key"
)
# Check status
status = client.get_status()
print(f"Connected: {status['running']}")
# Disconnect
client.disconnect()
Server Mode
from kytan import create_server
# Create and start server (uses bundled kytan binary automatically)
server = create_server()
server.serve(
port=9527,
key="your-secret-key",
bind="0.0.0.0",
dns="8.8.8.8"
)
# Check status
status = server.get_status()
print(f"Server running: {status['running']}")
# Shutdown
server.shutdown()
Using Context Managers
from kytan import create_client, KytanContextManager
# Automatic cleanup when exiting context
with KytanContextManager(create_client()) as client:
client.connect("server.com", 9527, "secret-key")
# Do work...
# Client automatically disconnects when exiting
As a Command Line Tool
# Client mode (uses bundled binary)
sudo kytan-py client -s server.com -p 9527 -k secret-key
# Server mode
sudo kytan-py server -p 9527 -k secret-key -d 8.8.8.8
# With custom log level
sudo RUST_LOG=debug kytan-py client -s server.com -p 9527 -k secret-key --log-level debug
API Reference
Classes
KytanClient
Main client class for connecting to kytan VPN servers.
Methods:
connect(server, port, key, no_default_route=False, log_level="info"): Connect to serverdisconnect(timeout=10): Disconnect from serverget_status(): Get connection statusget_logs(lines=50): Get recent log output
KytanServer
Main server class for running kytan VPN servers.
Methods:
serve(port=9527, key="", bind="0.0.0.0", dns="8.8.8.8", log_level="info"): Start servershutdown(timeout=10): Shutdown serverget_status(): Get server statusget_logs(lines=50): Get recent log output
ClientConfig / ServerConfig
Configuration dataclasses for client and server settings.
Functions
create_client(binary_path=None): Create a new client instancecreate_server(binary_path=None): Create a new server instance
Exceptions
KytanError: Base exception for kytan-related errors
Configuration
Binary Path
The wrapper will automatically use the bundled kytan binary that was built during installation. If you need to use a different binary, you can specify a custom path:
client = create_client(binary_path="/path/to/custom/kytan")
The search order is:
- Bundled binary (built during pip install)
- Custom path if specified
- PATH environment variable
- Common system locations
Logging
Set the RUST_LOG environment variable or use the log_level parameter:
# Environment variable
import os
os.environ['RUST_LOG'] = 'debug'
# Or via parameter
client.connect(server="...", port=9527, key="...", log_level="debug")
Available log levels: error, warn, info, debug, trace
Development
Building from Source
If you're developing or want to build manually:
# Clone the repository
git clone https://github.com/changlan/kytan.git
cd kytan
# Install in development mode (builds kytan automatically)
cd kytan-py
pip install -e .
# Or build kytan manually first
cd ..
cargo build --release
cd kytan-py
pip install -e .
Running Tests
# Install with development dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run with coverage
pytest --cov=kytan
Examples
Advanced Client Usage
import time
import logging
from kytan import create_client, KytanError
# Set up logging
logging.basicConfig(level=logging.INFO)
try:
# Uses bundled binary automatically
client = create_client()
# Connect with custom settings
client.connect(
server="vpn.example.com",
port=9527,
key="my-secret-key",
no_default_route=True, # Don't change default route
log_level="info"
)
print("Connected successfully!")
# Monitor connection
while True:
status = client.get_status()
if not status["running"]:
print("Connection lost!")
break
print(f"Status: Connected (PID: {status['pid']})")
time.sleep(10)
except KytanError as e:
print(f"VPN Error: {e}")
except KeyboardInterrupt:
print("Disconnecting...")
finally:
client.disconnect()
Server with Custom DNS
from kytan import create_server
# Uses bundled binary automatically
server = create_server()
try:
server.serve(
port=9527,
key="server-secret-key",
bind="0.0.0.0",
dns="1.1.1.1", # Use Cloudflare DNS
log_level="info"
)
print("Server started on port 9527")
# Keep server running
while server.get_status()["running"]:
time.sleep(1)
except KeyboardInterrupt:
print("Shutting down server...")
finally:
server.shutdown()
Security Notes
- Always use strong, unique encryption keys
- Run with minimal required privileges
- Consider using a dedicated user account for VPN operations
- Monitor logs for suspicious activity
- Keep the package updated to get the latest kytan binary
Troubleshooting
Common Issues
-
"Cargo (Rust) is required to build kytan"
- Install Rust from https://rustup.rs/
- Ensure
cargois in your PATH
-
"Failed to build kytan"
- Check that you have the necessary build dependencies
- Ensure you're in the correct directory structure
- Try building kytan manually first:
cargo build --release
-
"kytan requires root privileges"
- Run with
sudoor as root user - VPN operations require elevated privileges
- Run with
-
Connection timeouts
- Check firewall settings
- Verify server is running and accessible
- Ensure correct port and encryption key
Debug Mode
Enable debug logging to troubleshoot issues:
client.connect(..., log_level="debug")
License
This Python wrapper follows the same Apache 2.0 license as the original kytan project.
Contributing
Contributions are welcome! Please ensure:
- Code follows PEP 8 style guidelines
- Include tests for new functionality
- Update documentation as needed
Related Projects
- kytan - The original Rust VPN implementation
Metadata
Release files for kytan-py 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| kytan_py-0.1.0.tar.gz | 1.2 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kytan_py-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 2.3 MB
Release files / kytan_py-0.1.0.tar.gz
| Download URL | kytan_py-0.1.0.tar.gz |
|---|---|
| Size | 1.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
674ec45ce33ac2c82dc536ea79069e623c2fca11b0109893620ad4a2cad8193f
|
|
BLAKE2b-256 checksum How to use checksums |
b3607bb0cec6b7d33efee2f6c46b67b27f4e411f9a4737567a83fedfb8a2384d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.2
|
Release files / kytan_py-0.1.0-py3-none-any.whl
| Download URL | kytan_py-0.1.0-py3-none-any.whl |
|---|---|
| Size | 1.2 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2fb72c54a696237f3f7fe9fcfe963556e9da03b3e17656e087ef33458bbe6adf
|
|
BLAKE2b-256 checksum How to use checksums |
dc6fb3d4789ba79368ab5f2bddb9469316482184bbeb5ab391187f9075649b7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.2
|