angular-cliwindows-authenticationwebpack-dev-server

How to configure ng serve proxy with Windows authentication *and HTTPS*


I want to use ng serve with a backend server secured with Windows Authentication (NTLM). This is pretty much the same situation as in these posts (example 1, example 2), except the server is accessed over HTTPS.

When I try to use the same proposed solution (which works fine with HTTP), I get a 404 error (but of course the server is accessible at this URL, which I can test directly with the browser).

const Agent = require("agentkeepalive");

module.exports = {
    '/api': {
        target: "https://my-server.example.com",
        secure: false,
        changeOrigin: true,
        agent: new Agent({
            maxSockets: 100,
            keepAlive: true,
            maxFreeSockets: 10,
            keepAliveMsecs: 100000,
            timeout: 6000000,
            keepAliveTimeout: 90000
        }),
        onProxyRes: proxyRes => {
            const key = "www-authenticate";
            proxyRes.headers[key] = proxyRes.headers[key] &&
                proxyRes.headers[key].split(",");
        }
    }
}

enter image description here

Any help would be appreciated, including some methodology to diagnose the problem.

UPDATE: with logLevel = "debug" I get the following stacktrace (which I do not understand, since the same proxy config works fine with HTTPS, if it weren't for the Windows config):

TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"
    at new ClientRequest (_http_client.js:119:11)
    at Object.request (https.js:289:10)
    at Array.stream (C:\myproject\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:126:74)      
    at ProxyServer.<anonymous> (C:\myproject\node_modules\http-proxy\lib\http-proxy\index.js:81:21)
    at middleware (C:\myproject\node_modules\http-proxy-middleware\lib\index.js:46:13)
    at handle (C:\myproject\node_modules\webpack-dev-server\lib\Server.js:322:18)
    at app.use (C:\myproject\node_modules\webpack-dev-server\lib\Server.js:330:47)
    at Layer.handle_error (C:\myproject\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (C:\myproject\node_modules\express\lib\router\index.js:315:13)
    at C:\myproject\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\myproject\node_modules\express\lib\router\index.js:335:12)
    at next (C:\myproject\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle [as handle_request] (C:\myproject\node_modules\express\lib\router\layer.js:97:5)
    at trim_prefix (C:\myproject\node_modules\express\lib\router\index.js:317:13)
    at C:\myproject\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\myproject\node_modules\express\lib\router\index.js:335:12)

Solution

  • As often, when I post a question on Stack Overflow after days of struggle, I find the solution by myself within the next hour...

    The problem is that Agent is for HTTP only. For HTTPS, we must use HttpsAgent:

    const HttpsAgent = require('agentkeepalive').HttpsAgent;
    
    module.exports = {
        '/api': {
            target: "https://my-server.example.com",
            secure: false,
            changeOrigin: true,
            agent: new HttpsAgent({
                maxSockets: 100,
                keepAlive: true,
                maxFreeSockets: 10,
                keepAliveMsecs: 100000,
                timeout: 6000000,
                keepAliveTimeout: 90000
            }),
            onProxyRes: proxyRes => {
                const key = "www-authenticate";
                proxyRes.headers[key] = proxyRes.headers[key] &&
                    proxyRes.headers[key].split(",");
            }
        }
    }