node-socksInTheMiddle
use a socks server for web request modification
This module starts a socks server and handle HTTP, HTTPS and UDP requests, it can be used to modify these requests and their responses.
TCP requests are only checked whether those are HTTP requests, or the request will be recognized as a HTTPS request, so don't send other protocols' requests to this server.
Install
npm i socksinthemiddle
Examples
Launch a server
const {SocksInTheMiddle}=require('socksInTheMiddle');
const fs=require('fs');
let server=new SocksInTheMiddle({
socksPort:1090,
httpPort:0,
httpsPort:0,
httpsOptions:{
key:fs.readFileSync("/your/path/to/server.key"),
cert:fs.readFileSync('/your/path/to/server.crt'),
}
});
Then set your application's socks5 proxy setting to 127.0.0.1:1090
.
If http(s)Port
is not a number, the server will not be created. You must fill the httpsOptions
option if you want to create a server that supports HTTPS protocol.
About Certificate
You can create a CA yourself and sign a certificate for the target domain then fill TLS options in httpsOptions
.
Modify HTTP request and response
const {BufferModder}=require('socksInTheMiddle');
server.setHTTPModder(async (headers,reqFromClient,resToClient)=>{
console.log('client request headers:',headers);
reqFromClient;
resToClient;
if(reqFromClient.url.startsWith('/favicon.ico')){
delete headers['Etag'];
}
resToClient.end('blocked');
return false;
},(headers,resFromServer,reqFromClient)=>{
console.log('server response headers:',headers);
reqFromClient;
resFromServer;
if(reqFromClient.url.startsWith('/?')){
return new BufferModder(buf=>{
let str=buf.toString();
return str.replace(/更多/g,'超级多');
});
}
});
Modify UDP request and response
server.setUDPModder(async (fromClient,packet)=>{
fromClient;
packet.address;
packet.port;
packet.data;
packet.address='127.0.0.1';
packet.port=12345;
});