hatoo / http-mitm-proxy

A HTTP proxy server library intended to be a backend of application like Burp proxy
MIT License
16 stars 2 forks source link

response from local file #36

Open futurist opened 1 month ago

futurist commented 1 month ago

I have a proxy rule like below:

https://example.com/x.json -> file:///test/x.json

Want to response from local file /test/x.json directly without any actual network request when the uri is https://example.com/x.json.

Is it supported and how to do that?

hatoo commented 1 month ago

It's not supported but looks good. I'll implement it.

cismous commented 1 month ago

I also require this functionality. I need to read response content and alter specific sections of it using regular expressions

futurist commented 1 month ago

Just a discussion, does this feature related to some API changes? For example the API design of rama, it exposed the whole service layer and upstream, but that is more complex and not strait forward to use, but the Matcher System can be reused?

hatoo commented 1 month ago

Yes, actually I'm going to change API fundamentally. I don't know about rama but I'll let users create a custom service that receives Requests and returns Responses. I think it will not be very complex.

futurist commented 1 month ago

I've tested #40, the speed is good, but I cannot find a clue how to response from local file without actually making request out. Can you give an example to do this?

hatoo commented 1 month ago

Could you take a look #45?

cismous commented 1 month ago

Can you provide an example of how to convert the res variable to text for rendering, given the following code:

let (mut res, _upgrade) = client.send_request(req).await?;

I want to replace the base with the content of the return to make modifications @hatoo

hatoo commented 1 month ago

You can receive bodies in proxy and send back full body.

    let server = proxy
        .bind(("127.0.0.1", 3003), move |_client_addr, req| {
            let client = client.clone();
            async move {
                let uri = req.uri().clone();

                // You can modify request here
                // or You can just return response anyware

                let (res, _upgrade) = client.send_request(req).await?;

                println!("{} -> {}", uri, res.status());

                let (parts, body) = res.into_parts();
                let body = body.collect().await?.to_bytes();

                println!("{}", String::from_utf8_lossy(&body));

                // You can modify response here

                Ok::<_, http_mitm_proxy::default_client::Error>(Response::from_parts(
                    parts,
                    http_body_util::Full::new(body),
                ))
            }
        })
        .await
        .unwrap();
cismous commented 1 month ago

@hatoo Hello, I found that when I access it through a browser, the printed content is garbled, but page render is ok. If I use curl -x http://127.0.0.1:7888 https://www.google.com.hk the printed content is correct. The cause has not been identified yet.

error image image

right image image

cismous commented 1 month ago

Solved! When browser send request,the request headers includes content-encoding: gzip,the response body is gzip file, cause garble。