port-proxy
v0.2.0
Published
Listens to the specified port and forwards to the specified port.(监听指定的端口,并转发到指定端口。)
Maintainers
Readme
port-proxy —— Port Proxy Tool
简体中文 | English
Description
A lightweight port proxy tool that can forward traffic from one port to another, supporting TCP and UDP protocols, and can limit the transmission rate.
Getting Started
Install dependencies
npm i port-proxyImport
import { PortProxy } from 'port-proxy'Usage
// Create a TCP port proxy instance
const proxy = new PortProxy({
// Source, new proxy service
source: '127.0.0.1',
sourcePort: 3001,
// Target, original service being proxied
target: '127.0.0.1',
targetPort: 3000,
// Whether to output detailed logs
verbose: false,
// Protocol type: 'tcp' or 'udp'
protocol: 'tcp',
// Single TCP service rate limit, unit B/s, rate limit has 5% fluctuation for each TCP connection
limiteRate: 400 * 1024
})
// Start the proxy service
await proxy.start()
// Check if the proxy is running
const isRunning = proxy.isRunning()
// Get proxy status
const status = proxy.getStatus()
// Get active connection count
const connectionCount = proxy.getConnectionCount()
// Stop the proxy service
await proxy.stop()Explanation
PortProxy Class
Constructor
new PortProxy(options: ProxyOptions)Parameters
options:Objecttype, required, configuration options objectProperties of
optionsare as follows:source:stringtype, requiredListening address.
sourcePort:numbertype, requiredListening port.
target:stringtype, requiredTarget address.
targetPort:numbertype, requiredTarget port.
verbose:booleantype, optionalWhether to output detailed logs, default is false.
protocol:'tcp' | 'udp'type, optionalProtocol to use, default is 'tcp'.
limiteRate:numbertype, optionalSingle TCP service rate limit, unit B/s, rate limit has 5% fluctuation for each TCP connection. Default is 0 (no limit).
Methods
start() Promise<void>
Start the port forwarding proxy.
stop() Promise<void>
Stop the port forwarding proxy.
getStatus() Object
Get the proxy status.
Return value:
{
listening: boolean, // Whether it is listening
connections: number, // Current number of connections
config: ProxyOptions // Current configuration
}isRunning() boolean
Check if the proxy is running.
getConnectionCount() number
Get the number of active connections.
Command Line Usage
This library can also be used as a command line tool. After installing the package globally, you can use it directly:
npm install -g port-proxy
portproxy --source 127.0.0.1 --source-port 3001 --target 127.0.0.1 --target-port 3000 --verbose --protocol tcp --limite-rate 409600Or use it in your project:
npx port-proxy --source 127.0.0.1 --source-port 3001 --target 127.0.0.1 --target-port 3000Options
--source <IP>: Listening address--source-port <port>: Listening port--target <IP>: Target address--target-port <port>: Target port--protocol <protocol>: Protocol (default: tcp)--verbose: Detailed output mode--limite-rate <rate>: Single TCP service rate limit, unit B/s, default is 0 (no limit)--help: Display help information
Example
portproxy --source 192.168.196.2 --source-port 14491 --target 127.0.0.1 --target-port 14490🤝 Support
Enjoying this project? Show your support by giving it a star! ⭐
Your stars help the project gain visibility and encourage further development.
