node.js - 如何在 Node 中运行 PAC 代理脚本?

标签 node.js proxy http-proxy

我正在尝试编写一个 Node 程序(欢迎建议其他语言),它可以解析和运行企业代理脚本 PAC 并返回适当的代理服务器以编程方式使用。

现有的解决方案可以做到这一点吗? (或者这只能通过浏览器实现)

PAC 文件似乎假设执行上下文中存在某些全局函数,例如

shExpMatch()
myIpAddress() // interestingly the nodejs ip package return the true LAN DHCP assigned IP instead of a VPN IP

目标是每次启动 shell 时解析正确的代理服务器(或者如果不在代理后面则根本不设置它)

任何提示都非常感谢。

最佳答案

如果您坚持使用 Node,我建议使用类似 pac-resolver 的东西。为此。

const pac = require('pac-resolver');
const fetch = require('node-fetch');
const ip = require('ip');

fetch('http://<proxy_host>:<proxy_port>')
  .then(res => res.text())
  .then(body => {
    // to handle a bug when pinging 8.8.8.8 will time out
    // see: https://github.com/TooTallNate/node-pac-resolver/issues/18
    findProxy = pac(body, { sandbox: { myIpAddress: ip.address }, });
    return findProxy('http://google.com/'));
  })
  .then(proxy => console.log(proxy))
  .catch(err => console.error(err));

无论如何,你可以看看the repo查看需要定义哪些全局函数以及如何在 JavaScript 中完成定义。

如果您可以使用其他语言,请查看 pacparser .

顺便说一下,标准全局函数的列表是 here :

  • dnsDomainIs
  • shExpMatch
  • isInNet
  • 我的IP地址
  • dns解析
  • isPlainHostName
  • 本地主机或域
  • 可解析
  • dnsDomainLevels
  • 工作日范围
  • 日期范围
  • 时间范围
  • 提醒

关于node.js - 如何在 Node 中运行 PAC 代理脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289466/

相关文章:

java - 本地 Jenkins Maven 没有互联网连接

emacs - 是否可以告诉 Windows 上的 emacs 使用 IE http 代理设置?

javascript - Node http-proxy 不在代理后面工作

javascript - DM'ing 一个特定的用户 ID

web-services - 绑定(bind)和代理 IIS 和 Tomcat 参数不起作用

ssl - EKS - Envoy 动态转发代理 CA 验证错误

python - websocket._exceptions.WebSocketProxyException : failed CONNECT via proxy status: 503

javascript - 如何使用 javascript fetch api 和 express multer 上传图片

node.js - 没有多个Redis的多个服务器上的socket.io

javascript - PM2 记录到带有时间戳的文件