我正在尝试编写一个 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/