Skip to main content

Transfer data across multiple virtual channels over a single socket connection using multiplexing

Project description

PyMultiplex

Overview

PyMultiplex is a Python package that allows you to open several virtual channels over a single socket connection, similar to how SSH handles multiple logical sessions over one connection.

Originally built to support reverse tunneling, PyMultiplex is versatile and suitable for any environment where only one socket connection is allowed between endpoints. Simply run the Multiplex server on one end and the Multiplex client on the other, and you can open many "virtual sockets." This is completely transparent to your application.

The server listens for incoming ports requested by the client. For each new connection on those forwarded ports, it creates a new virtual channel over the shared socket.

You can learn more about reverse tunneling here:

How It Works

PyMultiplex consists of two main components:

  1. MultiplexServer
    Listens for incoming client connections and spawns a thread per client. It also listens for port-forwarding requests from the client, and for each new connection on a forwarded port, it creates a virtual pipe (a new thread) over the existing connection.

  2. MultiplexClient
    Connects to the MultiplexServer, requests remote port forwarding, and forwards data received on that port to a specified target server (functionally identical to ssh -R).

Installation

To install the package:

pip install PyMultiplex

Or install directly from GitHub:

pip install git+https://github.com/GefenAltshuler/PyMultiplex@main

Make sure the pip binary is in your system PATH — this will give you access to the multiplex command.

Usage

Command Line

PyMultiplex supports two modes: server and client. Here's how to use them in reverse tunnel mode:

Server

multiplex server --bind 0.0.0.0 --port 8080

Client

multiplex client \
  --host 127.0.0.1 \
  --port 8080 \
  --to-host wtfismyip.com \
  --to-port 80 \
  --remote-forward-port 1726

Now you can connect to the server's port 1726, and the traffic will be tunneled to wtfismyip.com:80 via the client:

curl http://127.0.0.1:1726/json -H "Host: wtfismyip.com"

Example output:

{
  "YourFuckingLocation": "Santa Clara, CA, United States",
  "YourFuckingCity": "Santa Clara",
  "YourFuckingCountry": "United States",
  "YourFuckingCountryCode": "US"
}

Python Code

You can also use PyMultiplex programmatically in Python. (Additional examples are in the Examples/ folder.)

Server

from PyMultiplex import MultiplexServer

def main():
    server = MultiplexServer(('0.0.0.0', 8080))
    server.start()

if __name__ == '__main__':
    main()

Client

from PyMultiplex import MultiplexClient

def main():
    client = MultiplexClient(('127.0.0.1', 8080), ('wtfismyip.com', 80), 1726)
    client.start()

if __name__ == '__main__':
    main()

This sets up a remote port 1726 on the server. Each connection to this port results in a new virtual channel being created between the server and client. Data is piped bidirectionally between the real and virtual sockets, ensuring full transport transparency.

Architecture Diagram

As in the example above, let's assume the client requested to remote-forward all traffic from port 1726 to google.com on port 443

Program Flow

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymultiplex-1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymultiplex-1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file pymultiplex-1.1.tar.gz.

File metadata

  • Download URL: pymultiplex-1.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for pymultiplex-1.1.tar.gz
Algorithm Hash digest
SHA256 186f23af3a796266a5f245c2cf35c57a6189186f89016152ada88350230446a8
MD5 97d623c92c9a3e8a10cf16bee6ad8a21
BLAKE2b-256 66102362e3540b1dddca47d9f8502dc80e496b12c4335f6f60760b72b805d63f

See more details on using hashes here.

File details

Details for the file pymultiplex-1.1-py3-none-any.whl.

File metadata

  • Download URL: pymultiplex-1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for pymultiplex-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5330b2c85bd8cfb5acc45025ee185671924455c3d41fd82430bf50aee570343d
MD5 e14995f859c21dd8a917a0680ad8c2dd
BLAKE2b-256 0690f5d5b2211c949da0574a6cb8a6775da1e753c86c05937ac6ebcc5af34f52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page