chrome in --headless模式:为代理提供凭证/身份验证

cgs*_*gsd 13 google-chrome headless-browser

我通过以下方式运行chrome:

google-chrome --headless --disable-gpu --dump-dom --disable-web-security

由于公司代理,它似乎碰到要求输入凭据的身份验证弹出窗口.如何在命令行上注入这些凭据?

小智 3

他们没有直接的方法来做到这一点。Google 不允许在无头模式下使用登录名/密码连接到代理。

但是,我可以通过使用 MITMProxy 来使用带有凭据的代理。其他技术(如 Squid)的工作原理相同,但使用起来更复杂。

诀窍是使用 MITMProxy 创建本地上游代理,通过您要使用的真实代理传输您的呼叫。MITMProxy 为您进行身份验证。

安装MITMProxy后,您可以通过命令行使用它:

~/mitmdump -p 9000 --mode upstream:http://proxy:port --set upstream_auth=login:password --ssl-insecure
Run Code Online (Sandbox Code Playgroud)

您必须将 ~/mitmdump 替换为 mitmproxy 的真实路径(mitmdump 将在您的控制台上运行它)。-p 9000 表示 MITMProxy 正在侦听http://localhost:9000,因此您必须使用 Chrome headless 调用此代理。--mode upload 将告诉 MITMProxy 将您的流量重定向到您要使用的真实代理。--set upload_auth 允许您使用正确的凭据进行连接。--ssl-insecure 允许您使用 https 并忽略错误的证书验证。

然后,你只需调用 chrome :

google-chrome --headless --disable-gpu --dump-dom --disable-web-security --proxy-server=http://localhost:9000
Run Code Online (Sandbox Code Playgroud)

在能够正确使用它进行 https 调用之前,您必须使用 certutil 导入 MITMProxy 安全证书:

certutil -d  sql:$HOME/.pki/nssdb -A -t "C,," -n mitm -i ~/.mitmproxy/mitmproxy-ca-cert.pem
Run Code Online (Sandbox Code Playgroud)

如果您在使用此技巧时遇到任何问题,MITMProxy 有一份完善的文档: https: //mitmproxy.org/