我无法将SOCKS5代理与节点获取一起使用(FetchError.原因:套接字已关闭)



我有一个Docker容器在我的机器上运行这个映像(它是一个SOCKS代理服务器(,我已经公开了它工作所需的端口。当我在机器上运行curl -x socks5://localhost:40000 https://ip.me时,请求会顺利通过,一切正常。但当我尝试使用代理从节点发送请求时,它不起作用。这是我的代码:

const fetch = require('node-fetch')
const { SocksProxyAgent } = require('socks-proxy-agent')
const req = await fetch(`https://google.com`, {
agent: new SocksProxyAgent(`socks5://localhost:40000`)
})
console.log(req.ok)
console.log(req.status)

这是我的错误:

/home/______/______/node_modules/node-fetch/lib/index.js:1376
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
^
FetchError: request to https://google.com/ failed, reason: Socket closed
at ClientRequest.<anonymous> (/home/______/______/node_modules/node-fetch/lib/index.js:1376:11)
at ClientRequest.emit (node:events:390:28)
at onerror (/home/______/______/node_modules/socks-proxy-agent/node_modules/agent-base/dist/src/index.js:117:21)
at callbackError (/home/______/______/node_modules/socks-proxy-agent/node_modules/agent-base/dist/src/index.js:136:17) {
type: 'system',
errno: undefined,
code: undefined
}
Node.js v17.0.0

我不知道为什么会发生这种事。有人能帮忙吗?

这是我的代码中的一个错误。在我使用它之前,我没有等待容器完全启动。

相关内容

最新更新