port-proxy
Version:
Listens to the specified port and forwards to the specified port.(监听指定的端口,并转发到指定端口。)
165 lines (109 loc) • 3.67 kB
Markdown
[简体中文](readme/README-zh-cn.md) | English
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.
Install dependencies
```bash
npm i port-proxy
```
```javascript
import { PortProxy } from 'port-proxy'
```
```javascript
// 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()
```
```javascript
new PortProxy(options: ProxyOptions)
```
##### Parameters
- `options`: `Object` type, required, configuration options object
Properties of `options` are as follows:
- `source`: `string` type, required
Listening address.
- `sourcePort`: `number` type, required
Listening port.
- `target`: `string` type, required
Target address.
- `targetPort`: `number` type, required
Target port.
- `verbose`: `boolean` type, optional
Whether to output detailed logs, default is false.
- `protocol`: `'tcp' | 'udp'` type, optional
Protocol to use, default is 'tcp'.
- `limiteRate`: `number` type, optional
Single 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:
```javascript
{
listening: boolean, // Whether it is listening
connections: number, // Current number of connections
config: ProxyOptions // Current configuration
}
```
Check if the proxy is running.
Get the number of active connections.
This library can also be used as a command line tool. After installing the package globally, you can use it directly:
```bash
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 409600
```
Or use it in your project:
```bash
npx port-proxy --source 127.0.0.1 --source-port 3001 --target 127.0.0.1 --target-port 3000
```
- `--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
```bash
portproxy --source 192.168.196.2 --source-port 14491 --target 127.0.0.1 --target-port 14490
```
Enjoying this project? Show your support by giving it a star! ⭐
Your stars help the project gain visibility and encourage further development.