跳到主要内容

2 篇博文 含有标签「proxy」

查看所有标签

WSL 网络设置

· 阅读需 1 分钟
1adybug
子虚伊人

wsl 默认使用的是 NAT 模式的网络,无法直接访问外部网络,可以通过修改 wsl 的网络设置,将其设置为 mirrored,使其能够直接访问外部网络。

C:\Users\用户名 目录下创建 .wslconfig 文件,内容如下:

[wsl2]
networkingMode=mirrored

wsl 中查看网络配置:

ifconfig

如果 ip 地址已经和主机在同一个网段,那么网络设置已经生效。

重启 wsl 使设置生效:

wsl --shutdown

代理可能需要重新设置,或者重启代理软件或者主机

如果要在 NAT 模式下使用 clash 代理,可以参考 获取 Windows 在 wsl 中的 ip 这篇文章

vi ~/.bashrc

~/.bashrc 文件中添加以下内容:

export http_proxy=http://172.30.160.1:7890
export https_proxy=http://172.30.160.1:7890
export all_proxy=http://172.30.160.1:7890
export HTTP_PROXY=http://172.30.160.1:7890
export HTTPS_PROXY=http://172.30.160.1:7890
export ALL_PROXY=http://172.30.160.1:7890

重启终端,或者执行 source ~/.bashrc 使设置生效。

在 Node.js 中为 fetch 配置代理

· 阅读需 1 分钟
1adybug
子虚伊人

Node.js 中,原生的 fetch API 并不直接支持代理功能。可以使用 node-fetch 库和 https-proxy-agent 库来实现通过代理服务器发送请求的功能:

import { HttpsProxyAgent } from "https-proxy-agent"
import fetch from "node-fetch"

// 代理服务器的URL
const proxyUrl = "http://your-proxy-server:port"

// 目标URL,即你想要通过代理服务器访问的URL
const targetUrl = "http://example.com"

// 配置代理服务器
const proxyAgent = new HttpsProxyAgent(proxyUrl)

// 使用fetch发起请求,通过代理服务器访问目标URL
fetch(targetUrl, { agent: proxyAgent })
注意

与原生的 fetch 不同,node-fetchresponse.json() 返回值类型为 Promise<unknown>