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.15.tar.gz (9.1 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.15-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pymultiplex-1.15.tar.gz
Algorithm Hash digest
SHA256 023c11655019aa8b91238e50e60a4748abdd75b3adf0edcf31dde96c15f1eb8f
MD5 f9cf436485167310365df997ad9f4ec8
BLAKE2b-256 f20cc57f50ce84305c2ed2d37991de2ae2607a7ecc66ff1ef5095ca5c5197569

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymultiplex-1.15-py3-none-any.whl
  • Upload date:
  • Size: 11.4 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.15-py3-none-any.whl
Algorithm Hash digest
SHA256 fe0545fdc15a404e32519261b127f50d2bb5bae05c2e2e758c4e371f7ca307f9
MD5 730a1a8584e929fdc5df4f47c8ffd038
BLAKE2b-256 90c76d50ac0cbf4535f0d60a97e105bb6efabf225377673449b4b68bc51bfbf8

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