microsoft / dev-proxy

Dev Proxy is an API simulator that helps you effortlessly test your app beyond the happy path.
https://aka.ms/devproxy
MIT License
471 stars 56 forks source link

Followed instruction - DevProxy not intercepting localhost addresses #837

Closed cfelstead closed 1 month ago

cfelstead commented 1 month ago

Hi,

I have looked at the instructions here and believe I am doing everything right. But I am not seeing any requests intercepted by devproxy that are on localhost addresses (public addresses are logged and intercepted correctly).

Configuration file (I've only added one new url)

{
  "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.1/rc.schema.json",
  "plugins": [
    {
      "name": "RetryAfterPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
    },
    {
      "name": "GenericRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
      "configSection": "genericRandomErrorPlugin"
    }
  ],
  "urlsToWatch": [
    "https://localhost:7271/*",
    "https://jsonplaceholder.typicode.com/*"
  ],
  "genericRandomErrorPlugin": {
    "errorsFile": "devproxy-errors.json"
  },
  "rate": 50,
  "logLevel": "information",
  "newVersionNotification": "stable"
}

Starting visual studio and running my project in debug. Testing all is working correctly.

Starting devproxy via (actual path ommitted) devproxy --config-file "C:\.....\devproxyrc.json"

Launching edge by doing Win + R and using the command msedge --proxy-bypass-list="<-loopback>" (have also tried replacing the <-loopback> with localhost and localhost:7271)

I'm sure I am missing something, but if I am, it is not clear what. Help appreciated.

garrytrinder commented 1 month ago

Hey @cfelstead 👋

Thanks for trying Dev Proxy, apologies for the trouble.

Command-line options for proxy settings suggests that you also need to specify the --proxy-server option.

--proxy-bypass-list=(|)[:][;...]

Tells Microsoft Edge to bypass any specified proxy for the specified semicolon-separated list of hosts. This flag must be used with --proxy-server.

Note

Trailing-domain matching doesn't require "." separators, "microsoft.com" will match "imicrosoft.com". For example, --proxy-server="proxy2:8080" --proxy-bypass-list=".microsoft.com;example.com;127.0.0.1:8080" will use the proxy server "proxy2" on port 8080 for all hosts except requests for .microsoft.com, example.com, and 127.0.0.1 on port 8080. In the previous example, imicrosoft.com requests will still be proxied. However, iexample.com requests will bypass the proxy because example.com was specified instead of .example.com.

Try adding the --proxy-server option to the command line arguments.

msedge --proxy-bypass-list="<-loopback>" --proxy-server "127.0.0.1:8000"
cfelstead commented 1 month ago

Thanks @garrytrinder.

Sadly no change. I am also trying via powershell using the below with no luck. curl -proxy http://127.0.0.1:8000 https://localhost:7271/Person

garrytrinder commented 1 month ago

Can you tell me more about the local web service? I'll see if I can repro.

cfelstead commented 1 month ago

When I experienced the issue I wanted to rule out it being my API somehow. So I’ve replaced it with the default .net 8 weather forecast api.

This is a corporate machine so I’m not ruling out it being something to do with the setup there.

garrytrinder commented 1 month ago

I did some investigation today and can repro, I'll do some more digging tomorrow.

garrytrinder commented 1 month ago

@cfelstead I tested this scenario with a simple nodejs express API setup running on http://localhost:3000, here are my findings.

index.js

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.json({ date: new Date() });
});

app.listen(3000, () => {
  console.log(`Server is running on port 3000`);
});

devproxyrc.json

{
    "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.1/rc.schema.json",
    "plugins": [
        {
            "name": "GenericRandomErrorPlugin",
            "enabled": true,
            "pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
            "configSection": "genericRandomErrorPlugin"
        }
    ],
    "genericRandomErrorPlugin": {
        "errorsFile": "errors.json"
    },
    "urlsToWatch": [
        "http://localhost:3000*"
    ]
}

errors.json

{
    "$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.1/genericrandomerrorplugin.schema.json",
    "responses": [
        {
            "statusCode": 500,
            "body": "Internal Server Error",
            "headers": [
                {
                    "name": "Content-Type",
                    "value": "application/json"
                }
            ]
        }
    ]
}

Program.cs

var client = new HttpClient();
var response = await client.GetAsync("http://localhost:3000");

Console.WriteLine(response.StatusCode);
Console.WriteLine(await response.Content.ReadAsStringAsync());

Tests

Command Passed Through
curl http://localhost:3000
curl -x http://127.0.0.1:8000 http://localhost:3000
dotnet run ✅ *
Invoke-WebRequest -Uri http://localhost:3000 ✅*
Invoke-WebRequest -Uri http://localhost:3000 -ProxyServer http://127.0.0.1:800

image

garrytrinder commented 1 month ago

Screenshot of web server, proxy and tests

image

cfelstead commented 1 month ago

Thank you so much for you efforts @garrytrinder. I now have the application working as it should, however, only under certain conditions. Here are my notes. I am not ruling out some of these being specific to the work laptop in some way, however, hopefully they will help people in future.

  1. On a windows machine, Powershell overrules the curl command as an alias for invoke-webrequest. As a result, I have needed to call the exe built into windows directly by referencing "curl.exe" and not just "curl".

  2. HTTPS appears to be an issue. However, switching the API to load over HTTP works correctly.


So, in order to get work I did the following.

  1. Inside devproxyrc.json make sure the urlsToWatch contains the API url as HTTP.
"urlsToWatch": [
  "http://localhost:5256/*",
  .......
]
  1. Start devproxy in the windows terminal

  2. Start the project (debug mode or not using a HTTP profile)

  3. In a separate terminal running powershell, run the curl command as follows

curl.exe -x http://127.0.0.1:8000 http://localhost:5256/people


It is worth noting, I have not yet got it working without using curl.exe and specifying the proxy, for example, using a swagger UI. However, I am continuing to test and will report back with any news.

cfelstead commented 1 month ago

I can get Firefox to route through devproxy by setting the proxy server to 127.0.0.1:8000 and, in about:config changing the network.proxy.allow_hijacking_localhost setting to true.

I cannot get Chrome or Edge to behave with the launch command

msedge --proxy-bypass-list="<-loopback>" --proxy-server="127.0.0.1:8000"

garrytrinder commented 1 month ago

Thanks for the updated info @cfelstead

I've been able to confirm that msedge --proxy-bypass-list="<-loopback>" --proxy-server="127.0.0.1:8000" works for me on Windows 11 and Edge Beta (128.0.2739.5) this morning.

image

I noticed however that Dev Proxy did not intercept requests unless I used the --proxy-server option, so we should update our docs to include that.

cfelstead commented 1 month ago

Interesting. I still can’t get it working.

I did get the vs debugger working correctly (so dotnet run) as long as the proxy was running prior to the debugger launching.

cfelstead commented 1 month ago

I think I’m in a place where it is working enough for me to proceed. It i can provide more assistance, please let me know. Otherwise, I’m happy to close this.

garrytrinder commented 1 month ago

Thanks @cfelstead, I'll close this, but will work on updating the page to reference some of our findings in this issue. Thanks again for raising the issue, please let us know if there is anything we can help with.

garrytrinder commented 1 month ago

https://github.com/MicrosoftDocs/microsoft-cloud-pr/pull/332