333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# tcp-proxy.js
 
a simple tcp proxy
 
[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Appveyor status][appveyor-image]][appveyor-url]
[![Coverage Status][coveralls-image]][coveralls-url]
 
[npm-image]: https://img.shields.io/npm/v/tcp-proxy.js.svg?style=flat-square
[npm-url]: https://npmjs.org/package/tcp-proxy.js
[travis-url]: https://travis-ci.org/whxaxes/tcp-proxy.js
[travis-image]: http://img.shields.io/travis/whxaxes/tcp-proxy.js.svg
[appveyor-url]: https://ci.appveyor.com/project/whxaxes/tcp-proxy.js/branch/master
[appveyor-image]: https://ci.appveyor.com/api/projects/status/github/whxaxes/tcp-proxy.js?branch=master&svg=true
[coveralls-url]: https://coveralls.io/r/whxaxes/tcp-proxy.js
[coveralls-image]: https://img.shields.io/coveralls/whxaxes/tcp-proxy.js.svg
 
## Usage
 
```bash
$ npm i tcp-proxy.js --save
```
 
create proxy
 
```js
const TCPProxy = require('tcp-proxy.js');
const proxy = new TCPProxy({ port: 9229 });
proxy.createProxy({
  forwardPort: 9999,
  forwardHost: 'localhost',
});
```
 
end proxy
 
```js
proxy.end();
```
 
interceptor
 
```js
proxy.createProxy({
  forwardPort: 9999,
  interceptor: {
    client(chunk) {
      // request => proxy server => interceptor.client => forward server
      const data = chunk.toString();
      const newData = data.replace('GET / ', 'GET /tom ');
      return Buffer.from(newData);
    },
    server(chunk) {
      // forward server => interceptor.server => proxy server => response
      const data = chunk.toString();
      const newData = data.replace('hello tom', 'bello tom');
      return Buffer.from(newData);
    },
  },
});
```
 
async interceptor
 
```js
proxy.createProxy({
  forwardPort: 9999,
  interceptor: {
    client(chunk) {
      // request => proxy server => interceptor.client => forward server
      const data = chunk.toString();
      return new Promise(resolve => {
        setTimeout(() => {
          const newData = data.replace('GET / ', 'GET /tom ');
          resolve(Buffer.from(newData));
        }, 200);
      });
    },
  },
});
```
 
## License
 
MIT