README
SockMITM :satellite:
A simple package made to intercept any HTTP request transiting via a socks5 proxy
<script type="module">
import sockmitm from 'https://cdn.skypack.dev/sockmitm';
</script>
gt; npm install sockmitm
SockMITM is an utility package that gives you a way to quickly setup socks proxies, that you can then use to intercept both HTTP requests and HTTP responses coming from and to your applications. Intercepted messages can be dynamically changed before being forwarded to their destination, and HTTP responses can be manually crafted and returned, without even opening a connection to the remote server.
A common use case for SockMITM is to use it your testsuites, in order to easily monitor and mock your application's requests straight from your Node.js tests.
var SockMitm = require( 'sockmitm' );
SockMitm.startFilteredProxy( function ( parameters ) {
var request = parameters.request;
var response = parameters.response;
if ( ! response )
// We tell the server to only send us uncompressed data
delete request.headers.acceptEncoding;
if ( ! response ) {
// You can change the request before it is sent to the remote server
// You can also return an HTTP response object - in such a case, the request won't forwarded at all, and your response will be returned to the client
console.log( '[REQ] ' + request.headers.host + request.url );
} else {
// It is now too late to alter the request, since the server already answered us!
// However, you can still alter the response that has been sent by the server before returning it to the client
console.log( '[RES] ' + request.headers.host + request.url + ' (' + response.status.code + ' ' + response.status.message + ')' );
}
// Note that you can return either a promise or a plain object, as you want
return { request : request, response : response };
return Promise.resolve( { request : request, response : response } );
} ).listen( 6666 );
gt; curl --socks5 localhost:6666 perdu.com
The body
field of the intercepted requests/responses will always be a Buffer
instance. However, you can set it to either a new buffer, a string, a JSON structure, or null. Be aware that if you set the body to be a JSON structure, the content type will be automatically be set to application/json
. To avoid this, you can do the stringification yourself.
Should you want to alter the content of a request (or response), be aware that the Content-Length header will need to be changed accordingly. Fortunately, SockMITM can handle this automatically, and you probably won't have to deal with it at all. However, because of the way it is implemented, it means that you can't rely on the Content-Length header (it will be null): uses body.length
instead.
Note that you can also force a specific Content-Length if you need to - just set it to a value other than null
.
You may want to ensure that the Accept-Encoding
header is stripped if you wish to intercept the body of the response. Otherwise, the server might send you gzipped data, that you would then have to unzip using the native zlib api. Stripping the header altogether is the best way to ensure that the remote server will only send you back uncompressed data.
method / url / version / headers / body
version / status / headers / body
Copyright © 2016 Wisembly
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.