接受Undici Fetth的自我签名证书

发布于 2025-01-22 16:38:26 字数 102 浏览 2 评论 0 原文

如何进行 fetch('https:// localhost:8888')对于本地运行的http服务器( fetch 来自UNDICI)?

How do I do fetch('https://localhost:8888') for a locally running HTTP server with a self-signed cert (fetch coming from undici)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

将军与妓 2025-01-29 16:38:26

诀窍是使用 setGlobaldisPatcher getGlobaldisPatcher 可以用于抓住原始的以供以后修复)。

import {
  fetch,
  setGlobalDispatcher,
  Agent,
} from 'undici'

setGlobalDispatcher(new Agent({
  connect: {
    rejectUnauthorized: false
  }
}))

fetch('https://localhost:8888').then(...)

The trick is to use setGlobalDispatcher (getGlobalDispatcher can be used to grab the original one for later restoration).

import {
  fetch,
  setGlobalDispatcher,
  Agent,
} from 'undici'

setGlobalDispatcher(new Agent({
  connect: {
    rejectUnauthorized: false
  }
}))

fetch('https://localhost:8888').then(...)
Oo萌小芽oO 2025-01-29 16:38:26

如果您使用的是node.js的内置 fetch ,则可以在初始化选项中指定 dispatcher

您仍然需要安装 UNDICI NPM安装UNDICI )才能使用自定义 agent

import { Agent } from 'undici';

await fetch('https://localhost:8888', {
  dispatcher: new Agent({
    connect: {
      rejectUnauthorized: false,
    },
  }),
});

If you are using Node.js's built-in fetch, you can specify the dispatcher in the init options.

You still need to install undici (npm install undici) to be able to use a custom Agent.

import { Agent } from 'undici';

await fetch('https://localhost:8888', {
  dispatcher: new Agent({
    connect: {
      rejectUnauthorized: false,
    },
  }),
});
鲜肉鲜肉永远不皱 2025-01-29 16:38:26

提示使用 vellyunauthorization的答案的问题:false 是,这使请求变得不安全。

我建议您从您要尝试到的服务器/网站获得PEM Base64'd证书,并在提出请求时通过代理包含它:

const { Agent } = require('undici');

const PEM_CERTS = `{base64 certificate for https://localhost:8888}`;

/* 
    The certificate content should look like:
    -----BEGIN CERTIFICATE-----
    base64string
    -----END CERTIFICATE-----
*/

fetch('https://localhost:8888', {
    dispatcher: new Agent({
        connect: {
            ca: PEM_CERTS
        }
    })
}).then(response => {
    return response.text();
}).then(text => {
    console.log(text);
});

您可以通过访问https:// localhost:8888通过您的浏览器,单击URL旁边的“安全”/锁定图标,然后遵循浏览器的步骤,将证书下载为CRT或PEM。将其作为纯文本打开以检验base64含量。

或通过命令行导出它,但是您必须自己将其从CER转换为Base64格式。

另外,您也可以在计算机上安装证书,您可能不必完全担心此证书。

The issue with answers that suggest using rejectUnauthorized: false is that this makes the request unsecure.

I'd suggest you get the PEM base64'd certificate from the server/site you're trying to reach and include it through an agent when making your request:

const { Agent } = require('undici');

const PEM_CERTS = `{base64 certificate for https://localhost:8888}`;

/* 
    The certificate content should look like:
    -----BEGIN CERTIFICATE-----
    base64string
    -----END CERTIFICATE-----
*/

fetch('https://localhost:8888', {
    dispatcher: new Agent({
        connect: {
            ca: PEM_CERTS
        }
    })
}).then(response => {
    return response.text();
}).then(text => {
    console.log(text);
});

You can get the base64 certificate by accessing https://localhost:8888 through your browser, clicking on the "secure"/lock icon next to the URL and following your browser's steps to download the certificate as CRT or PEM. Open it as plain text to retrive the base64 content.

Or export it via command line, but you'll have to convert it from CER to base64 format yourself.

Alternatively, you can also install the certificate on your machine and you may not have to worry about this altogether.

任谁 2025-01-29 16:38:26

您也可以使用前类。近视类具有options.requesttls and options.proxytls,在文档中未描述。为了使用venuctunauthorizatization:false选项,只有以下代码:

import { ProxyAgent, request } from 'undici'

const proxyAgent = new ProxyAgent('my.proxy.server',{
    requestTls: {  // this is the key point
         rejectUnauthorized: false,
    }
})

const {  statusCode,  body } = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })
for await (const data of body) {
  console.log('data', data.toString('utf8'))
}

还检查测试用例通过http到https endpoint 和代理和proxy-agent 源代码。这是

You can also use the ProxyAgent class. The ProxyAgent class has an options.requestTls and options.proxyTls, which are not described in document. In order to use rejectUnauthorized : false options, just code like below:

import { ProxyAgent, request } from 'undici'

const proxyAgent = new ProxyAgent('my.proxy.server',{
    requestTls: {  // this is the key point
         rejectUnauthorized: false,
    }
})

const {  statusCode,  body } = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })
for await (const data of body) {
  console.log('data', data.toString('utf8'))
}

Also check the test case Proxy via HTTP to HTTPS endpoint and the proxy-agent source code.This is the fixed pr

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文