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.0.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.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymultiplex-1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 e0d8465a959bad0e73762705cf8c7e293d61afedec10455bd55f5d8da40f52fc
MD5 f5040c14f3ae6b7a01d42131d607fca7
BLAKE2b-256 27fc50f5e750cafadba391af8d459806e0d2d01fbf3e699aaaf3d74cb3dc3820

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymultiplex-1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec4d6c8bdc13ddb34553d284fb2438c73b027e494f98b37d3aa1e0a03a5a44cf
MD5 b211e3f75ee68a66a39c0fbaa4e2740d
BLAKE2b-256 496af6b66328667770d8dfb03704bab6a45f3539a475eaf27634737fb458779e

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