Simple programmable http man in the middle proxy server
MIT License
It is simple. Without too many features.
decodeHTTPS
, so that all https packet will be forwarded directly with nothing touched.Content-Length
header automatically. Any other header won't be modified. Modify them by yourself if needed.npm install simple-http-mitm-proxy --save
const fs = require('fs');
const simpleProxy = require('simple-http-mitm-proxy');
const SimpleProxyServer = simpleProxy.default;
const server = new SimpleProxyServer();
server.processRequest = (ctx, callback) => {
console.log(ctx);
callback(ctx);
}
server.processResponse = (ctx, callback) => {
ctx.response.headers['content-type'] = 'text/plain';
ctx.response.body = Buffer.from('Hello, world!');
callback(ctx);
}
server.listen(8080, '127.0.0.1', () => {
console.log('Proxy server listening http://127.0.0.1:8080/');
});
server.decodeHTTPS = true;
interface Context {
request: {
protocol: 'http' | 'https',
method: string;
url: string;
httpVersion: string;
headers: http.IncomingHttpHeaders;
host: string;
body: Buffer | null;
isEnd: boolean;
};
processRequestResult: undefined | 'block' | 'response' | 'responseProcess';
response: {
statusCode: number;
statusMessage: string;
headers: { [key: string]: string | string[] };
body: Buffer | null;
isEnd: boolean;
};
processResponseResult: undefined | 'block';
}
class Server {
public processRequest: Handler;
public processResponse: Handler;
public requestBodyMaxLen: number;
public responseBodyMaxLen: number;
public decodeHTTPS: boolean;
public rootCAKey: pki.PrivateKey;
public rootCACert: pki.Certificate;
public sslKeys: { [hostname: string]: string };
public sslCerts: { [hostname: string]: string };
public onHTTPServerError: ErrorHandler;
public onHTTPSServerError: ErrorHandler;
public onClientRequestError: ErrorHandler;
public onClientResponseError: ErrorHandler;
public onServerRequestError: ErrorHandler;
public onServerResponseError: ErrorHandler;
public onConnectClientSocketError: ErrorHandler;
public onConnectServerSocketError: ErrorHandler;
getHTTPServer(): http.Server;
getHTTPSServer(): https.Server;
getHTTPSServerListenPort(): number;
constructor();
generateRandomRootCA();
setRootCA(keyPem: string, certPem: string);
init();
listen(port: number, hostname?: string, listeningListener?: () => void);
}
server.processRequest
, server.processResponse
are two important functions for modification. They use ctx
to pass request and response information and modification result.
processRequest
ctx.processRequestResult = 'block'
: Socket will be closed instantly.ctx.processRequestResult = 'response'
: Send resposne to client according to ctx.response
.ctx.processRequestResult = 'responseProcess'
: Pass ctx
to processResponse
and then response.ctx.processRequestResult
to other value: Send reqyest to server according to ctx.request
.ctx.request.headers.host
is a field in HTTP headers.ctx.request.host
such as www.example.com:80
, 127.0.0.1:8081
is used in TCP connection. Use this field to send the request to another proxy server.ctx.request.isEnd
: When request data too large, this value is false
.processResponse
ctx.processResponseResult = 'block'
: Socket will be closed instantly.ctx.processResponseResult
to other value: Send resposne to client according to ctx.response
.You can use node-web-proxy to set proxy on Windows. Use certutil.exe -addstore -user Root root.crt
import cert on Windows.
MIT License