{"id":37006547,"url":"https://github.com/tourze/workerman-connection-pipe","last_synced_at":"2026-01-14T00:45:35.199Z","repository":{"id":285607193,"uuid":"958737297","full_name":"tourze/workerman-connection-pipe","owner":"tourze","description":"[READ-ONLY] Enhance workerman connection pipe","archived":false,"fork":false,"pushed_at":"2025-11-01T19:34:10.000Z","size":110,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-01T21:15:17.478Z","etag":null,"topics":["connection","pipe","workerman"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tourze.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-04-01T17:20:01.000Z","updated_at":"2025-11-01T19:34:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"b7f4c560-2c45-4ffa-9dac-409652c46195","html_url":"https://github.com/tourze/workerman-connection-pipe","commit_stats":null,"previous_names":["tourze/workerman-connection-pipe"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/tourze/workerman-connection-pipe","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-connection-pipe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-connection-pipe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-connection-pipe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-connection-pipe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tourze","download_url":"https://codeload.github.com/tourze/workerman-connection-pipe/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tourze%2Fworkerman-connection-pipe/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28406534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connection","pipe","workerman"],"created_at":"2026-01-14T00:45:35.119Z","updated_at":"2026-01-14T00:45:35.180Z","avatar_url":"https://github.com/tourze.png","language":"PHP","readme":"# Workerman Connection Pipe\n\n[English](README.md) | [中文](README.zh-CN.md)\n\n[![Latest Version](https://img.shields.io/packagist/v/tourze/workerman-connection-pipe.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-connection-pipe)\n[![Total Downloads](https://img.shields.io/packagist/dt/tourze/workerman-connection-pipe.svg?style=flat-square)](https://packagist.org/packages/tourze/workerman-connection-pipe)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n[![PHP Version](https://img.shields.io/badge/php-%5E8.2-blue.svg)](https://php.net/)\n[![Coverage Status](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/tourze/workerman-connection-pipe)\n\nA high-performance, highly customizable connection forwarding framework based on Workerman, supporting data transmission and protocol conversion between TCP and UDP connections.\n\n## Table of Contents\n\n- [Features](#features)\n- [Installation](#installation)\n- [Quick Start](#quick-start)\n  - [Configuration](#configuration)\n  - [TCP to TCP Proxy Example](#tcp-to-tcp-proxy-example)\n  - [UDP to UDP Forwarding Example](#udp-to-udp-forwarding-example)\n- [Listening to Events](#listening-to-events)\n- [Custom Data Processing](#custom-data-processing)\n- [Detailed Workflows](#detailed-workflows)\n- [Advanced Configuration](#advanced-configuration)\n  - [Setting Buffer Size](#setting-buffer-size)\n  - [Enabling Encrypted Transport](#enabling-encrypted-transport)\n- [Performance Optimization Tips](#performance-optimization-tips)\n- [Troubleshooting](#troubleshooting)\n- [Contributing](#contributing)\n- [Changelog](#changelog)\n- [License](#license)\n\n## Features\n\n- **Multi-protocol Support**\n  - TCP → TCP: Transparent TCP proxying, load balancing, and reverse proxying\n  - TCP → UDP: Access UDP services from TCP clients, such as DNS proxying\n  - UDP → TCP: Access TCP services from UDP clients, such as game server frontend\n  - UDP → UDP: UDP relay, stream forwarding, with full-cone NAT support\n\n- **Advanced Connection Management**\n  - Automatic connection lifecycle and resource release management\n  - Smart buffer control to prevent memory overflow\n  - Timeout cleanup mechanism that automatically releases inactive connections\n\n- **Powerful Extension Functions**\n  - Complete event system for monitoring all forwarding events\n  - Flexible logging interface for recording detailed connection and transmission information\n  - Custom data processing logic for protocol conversion and content modification\n\n## Installation\n\n```bash\ncomposer require tourze/workerman-connection-pipe\n```\n\n## Quick Start\n\n### Configuration\n\nFirst, configure the dependency container, setting up the logger and event dispatcher:\n\n```php\nuse Tourze\\Workerman\\ConnectionPipe\\Container;\nuse Monolog\\Logger;\nuse Monolog\\Handler\\StreamHandler;\nuse Symfony\\Component\\EventDispatcher\\EventDispatcher;\n\n// Configure logging system\n$logger = new Logger('pipe');\n$logger-\u003epushHandler(new StreamHandler('path/to/your/app.log', Logger::DEBUG));\nContainer::$logger = $logger;\n\n// Configure event dispatcher\n$eventDispatcher = new EventDispatcher();\nContainer::$eventDispatcher = $eventDispatcher;\n```\n\n### TCP to TCP Proxy Example\n\nCreate a simple TCP proxy that forwards traffic from local port 8443 to a remote server:\n\n```php\nuse Workerman\\Worker;\nuse Workerman\\Connection\\TcpConnection;\nuse Workerman\\Connection\\AsyncTcpConnection;\nuse Tourze\\Workerman\\ConnectionPipe\\PipeFactory;\n\n// Create local listening service\n$worker = new Worker('tcp://0.0.0.0:8443');\n\n$worker-\u003eonConnect = function(TcpConnection $connection) {\n    // Create connection to target server\n    $targetConnection = new AsyncTcpConnection('tcp://example.com:443');\n    $targetConnection-\u003econnect();\n\n    // Create and start pipe from source to target\n    $forwardPipe = PipeFactory::createTcpToTcp($connection, $targetConnection);\n    $forwardPipe-\u003epipe();\n\n    // Create and start pipe from target to source\n    $backwardPipe = PipeFactory::createTcpToTcp($targetConnection, $connection);\n    $backwardPipe-\u003epipe();\n\n    // Save pipe references for later cleanup\n    $connection-\u003epipeTo = $forwardPipe;\n    $connection-\u003epipeFrom = $backwardPipe;\n\n    // Set cleanup logic when source connection closes\n    $connection-\u003eonClose = function() use ($connection) {\n        if (isset($connection-\u003epipeTo)) {\n            $connection-\u003epipeTo-\u003eunpipe();\n        }\n        if (isset($connection-\u003epipeFrom)) {\n            $connection-\u003epipeFrom-\u003eunpipe();\n        }\n    };\n};\n\nWorker::runAll();\n```\n\n### UDP to UDP Forwarding Example\n\nCreate a UDP forwarding service that forwards UDP traffic from local port 8053 to Google's DNS server:\n\n```php\nuse Workerman\\Worker;\nuse Workerman\\Connection\\AsyncUdpConnection;\nuse Tourze\\Workerman\\ConnectionPipe\\PipeFactory;\n\n// Create UDP source server\n$sourceWorker = new Worker('udp://0.0.0.0:8053');\n\n$sourceWorker-\u003eonWorkerStart = function($worker) {\n    // Create UDP connection to target DNS server\n    $targetConnection = new AsyncUdpConnection('udp://8.8.8.8:53');\n\n    $targetConnection-\u003eonConnect = function($connection) use ($worker) {\n        echo \"Connected to target UDP server\\n\";\n        // Store the target connection\n        $worker-\u003etargetConnection = $connection;\n\n        // Get UDP connection objects\n        $sourceConnection = $worker-\u003egetMainSocket();\n\n        // Create UDP to UDP pipe\n        $pipe = PipeFactory::createUdpToUdp($sourceConnection, $connection);\n\n        // Store pipe reference\n        $worker-\u003epipe = $pipe;\n\n        // Start the pipe\n        $pipe-\u003epipe();\n    };\n\n    $targetConnection-\u003econnect();\n};\n\n$sourceWorker-\u003eonMessage = function($connection, $data, $sourceAddress, $sourcePort) {\n    $worker = $connection-\u003eworker;\n\n    // Check if target connection is available\n    if (!isset($worker-\u003epipe)) {\n        return;\n    }\n\n    // Forward the data with source address information\n    $worker-\u003epipe-\u003eforward($data, $sourceAddress, $sourcePort);\n};\n\nWorker::runAll();\n```\n\n## Listening to Events\n\nYou can listen to data forwarding related events for custom processing:\n\n```php\nuse Tourze\\Workerman\\ConnectionPipe\\Event\\DataForwardedEvent;\nuse Tourze\\Workerman\\ConnectionPipe\\Event\\ForwardFailedEvent;\nuse Tourze\\Workerman\\ConnectionPipe\\Container;\n\n// Listen for successful data forwarding events\nContainer::$eventDispatcher-\u003eaddListener(\n    DataForwardedEvent::class, \n    function(DataForwardedEvent $event) {\n        $pipe = $event-\u003egetPipe();\n        $data = $event-\u003egetData();\n        $sourceProtocol = $event-\u003egetSourceProtocol();\n        $targetProtocol = $event-\u003egetTargetProtocol();\n        $context = $event-\u003egetContext();\n\n        // Process successfully forwarded data, e.g., statistics or logging\n        echo \"Forwarded \" . strlen($data) . \" bytes from {$sourceProtocol} to {$targetProtocol}\\n\";\n    }\n);\n\n// Listen for data forwarding failure events\nContainer::$eventDispatcher-\u003eaddListener(\n    ForwardFailedEvent::class,\n    function(ForwardFailedEvent $event) {\n        $errorMessage = $event-\u003egetErrorMessage();\n\n        // Handle forwarding failure, e.g., warning or retry\n        echo \"Forwarding failed: {$errorMessage}\\n\";\n    }\n);\n```\n\n## Custom Data Processing\n\nIf you need to modify or process data during forwarding, you can extend existing pipe classes and override relevant methods:\n\n```php\nuse Tourze\\Workerman\\ConnectionPipe\\Pipe\\TcpToTcpPipe;\n\nclass EncryptedTcpPipe extends TcpToTcpPipe\n{\n    public function forward(string $data, string $sourceAddress = '', int $sourcePort = 0): bool\n    {\n        // Encrypt or process data before forwarding\n        $processedData = $this-\u003eencryptData($data);\n\n        // Call parent method to complete actual forwarding\n        return parent::forward($processedData, $sourceAddress, $sourcePort);\n    }\n\n    protected function encryptData(string $data): string\n    {\n        // Implement your data encryption or processing logic\n        return $data; // No actual processing in this example\n    }\n}\n\n// Use custom pipe class\n$pipe = new EncryptedTcpPipe();\n$pipe-\u003esetSource($sourceConnection);\n$pipe-\u003esetTarget($targetConnection);\n$pipe-\u003epipe();\n```\n\n## Detailed Workflows\n\nFor detailed workflows of each connection combination, see the [workflows](./workflows) directory:\n\n- [TCP to TCP Forwarding Workflow](./workflows/tcp_to_tcp_workflow.md)\n- [TCP to UDP Forwarding Workflow](./workflows/tcp_to_udp_workflow.md)\n- [UDP to TCP Forwarding Workflow](./workflows/udp_to_tcp_workflow.md)\n- [UDP to UDP Forwarding Workflow](./workflows/udp_to_udp_workflow.md)\n\n## Advanced Configuration\n\n### Setting Buffer Size\n\nFor high-traffic applications, you can adjust the buffer size of TCP connections:\n\n```php\nuse Workerman\\Connection\\TcpConnection;\n\n// Set global maximum send buffer size to 10MB\nTcpConnection::$defaultMaxSendBufferSize = 10 * 1024 * 1024;\n\n// Set buffer size for a single connection\n$targetConnection-\u003emaxSendBufferSize = 5 * 1024 * 1024;\n```\n\n### Enabling Encrypted Transport\n\nYou can enable SSL/TLS encryption on TCP connections:\n\n```php\n$targetConnection = new AsyncTcpConnection('tcp://example.com:443');\n\n// Enable SSL\n$targetConnection-\u003etransport = 'ssl';\n\n$targetConnection-\u003econnect();\n```\n\n## Performance Optimization Tips\n\n1. **Connection Reuse**: For frequently established connections, consider implementing a connection pool to reduce connection establishment overhead.\n\n2. **Multi-process Mode**: On multi-core servers, allocate an appropriate number of Worker processes:\n\n   ```php\n   $worker-\u003ecount = 4; // Adjust based on CPU cores and load\n   ```\n\n3. **Memory Optimization**: For long-running applications, periodically check memory usage to prevent memory leaks.\n\n4. **Timeout Settings**: Set appropriate timeout values for different application scenarios to ensure resources are released in a timely manner.\n\n## Troubleshooting\n\n1. **Connection Cannot Be Established**: Check network connectivity, firewall settings, and whether the target server is reachable.\n\n2. **Data Forwarding Failure**: Ensure pipes are correctly created and started, check error messages in event listeners.\n\n3. **High Memory Usage**: Check if connections are properly closed and NAT mappings are periodically cleaned up.\n\n4. **Unstable UDP Communication**: UDP does not have connection state, ensure proper handling of packet loss and out-of-order situations.\n\n## Contributing\n\nPlease see [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.\n\n## Changelog\n\nSee [CHANGELOG.md](CHANGELOG.md) for details on what has changed in each version.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftourze%2Fworkerman-connection-pipe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftourze%2Fworkerman-connection-pipe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftourze%2Fworkerman-connection-pipe/lists"}