在 macOS 或 Linux 发行版中,是否可以从 Golang 应用程序设置系统代理?

huangapple go评论94阅读模式
英文:

Is it possible to set the system proxy from golang app in macOS or Linux distributes?

问题

你好!你想在你的golang应用程序中创建一个代理服务器,并且希望能够将其设置为系统代理。这是可能的吗?如何实现呢?

英文:

I'm creating golang app with a proxy server as a pet project, and I want to have the possibility to set himself as a system proxy. Is it possible? And how?

答案1

得分: 2

代理通常通过HTTP_PROXYHTTPS_PROXYNO_PROXY环境变量进行配置。在类Unix操作系统中,这些变量会从父进程继承给其子进程。

话虽如此:

  • 您的程序可以修改自己的环境并启动其他进程作为子进程(传递_PROXY变量)。
  • 您的程序可以修改shell启动脚本(如.bashrc.cshrc),新启动的shell将获取这些值并将它们传递给新启动的进程。

但我不知道修改其他(活动的)进程的环境是否可行。

英文:

Proxies are often configured through the HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables. In Unix-style operating systems, these variables are inherited from the parent process to its child processes.

This being said:

  • your program can modify its own environment and start other processes as child processes (passing in the _PROXY variables).
  • your program may modify the shell startup scripts (like .bashrc, .cshrc), and a newly started shell will pick the values up and pass them to newly started processes.

But I am not aware that modifying the environment of other (active) processes is possible.

huangapple
  • 本文由 发表于 2022年3月4日 05:13:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71343863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定