Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirecting a request to another server #268

Open
Jiri-Mihal opened this issue Oct 8, 2022 · 0 comments
Open

Redirecting a request to another server #268

Jiri-Mihal opened this issue Oct 8, 2022 · 0 comments

Comments

@Jiri-Mihal
Copy link

Hi,

I need to prevent a request from being sent to the target server, instead I would like to send it (unchanged, including all data) to another server and return the data from that server.

Client --> Proxy ----------------> Target
curl -k -x 'http://127.0.0.1:8081' 'https://httpbin.org/anything'

So the above CURL command should, for example, send/return data to/from 'https://127.0.0.1:443' instead of 'https://httpbin.org/anything'. Is it possible to do it with 'http-mitm-proxy'? My current code is as follows:

const Proxy = require('http-mitm-proxy');
const proxy = Proxy();

proxy.onError( (ctx, err) => {
    console.error('Proxy error: ', err);
});

async function sendAndGetData(ctx, host) {
    // !!!!! Todo !!!!!: Ideally, just passing the request to `host` without reconstructing it. Maybe via connect?
}

async function getResponse(ctx) {

    // Get proxy headers
    const sym = Object.getOwnPropertySymbols(ctx.connectRequest).find(function(s) {
        return String(s) === 'Symbol(kHeaders)';
    });
    console.log('Proxy headers: ', ctx.connectRequest[sym]);

    // Modify request headers
    // Add Proxy-Authorization header to request headers
    ctx.proxyToServerRequestOptions.headers['Proxy-Authorization'] = ctx.connectRequest[sym]['proxy-authorization'];

    // !!!!! Todo !!!!!: Here I want to send a request to https://127.0.0.1:443 and return a response from it
    return await sendAndGetData(ctx, 'https://127.0.0.1:443');
}

proxy.onConnect(async (req, socket, head, callback) => {
    // Hack: Add proxy headers to ctx. Without this ctx does not include proxy headers
    req.headers;
    return callback();
});

proxy.onRequest( async (ctx, callback) => {
    // Get request headers
    console.log('Request headers: ', ctx.clientToProxyRequest.headers);

    // Intercepting proxy
    // No callback() so proxy request is not sent to the server
    ctx.proxyToClientResponse.end(await getResponse(ctx));

    // MITM proxy
    // return callback();
});

proxy.listen({port: 8081});

Thank you in advance for any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant