SOCKS5 Proxy on MacOS

2023/08/05

Ever experienced network restrictions or just needed to enhance network security? You may find configuring proxies on MacOS challenging. The intricacies lie in MacOS’s proxy mechanism, which can lead to instances where certain applications, like Google Chrome, might utilize a proxy while others, like Terminal, operate without one. To add to the versatility, SOCKS5 proxies stand out from traditional HTTP proxies, as they offer the capability to handle any type of network traffic, transcending the limitations of HTTP and HTTPS webpages.

In this comprehensive guide, we delve into the art of seamlessly setting up a SOCKS5 proxy that transcends application boundaries, catering to your browsing needs, terminal activities, and even elevating your development experience on the VS Code platform.

Browser

Configuring a SOCKS5 proxy for your web browser is a straightforward process, made even more accessible with the support of modern SOCKS5 proxy tools, such as Shadowsocks. While it serves as a crucial prerequisite for setting up proxies on Terminal and VS Code, this initial step deserves its place of prominence for seamless integration.

Terminal

Upon initializing your SOCKS5 proxy tool on MacOS, you may encounter a common challenge: the proxy might not seamlessly extend its reach to the Terminal. To ascertain its status, a simple test involves using the curl command to access websites and retrieve public IP data from their responses.

For instance, executing:

curl ipinfo.io

will yield output akin to:

{
  "ip": "█.█.█.█",
  "city": "Zhangjiakou",
  "region": "Hebei",
  "country": "CN",
  "loc": "█,█",
  "org": "█",
  "timezone": "Asia/Shanghai",
  "readme": "https://ipinfo.io/missingauth"
}

Analyzing this data will enable you to discern if the current Terminal is utilizing the proxy. If not, investigate the SOCKS5 tool’s local address and port settings. Assuming the tool is listening on 127.0.0.1:1086, navigate to Terminal and input:

export all_proxy=socks5://127.0.0.1:1086

Re-running curl ipinfo.io should present output akin to:

{
  "ip": "█.█.█.█",
  "city": "Los Angeles",
  "region": "California",
  "country": "US",
  "loc": "█,█",
  "org": "█",
  "timezone": "America/Los_Angeles",
  "readme": "https://ipinfo.io/missingauth"
}

Compare the retrieved IP address, location, organization, and timezone with the previous results to confirm if the SOCKS5 proxy is now active on this Terminal. Keep in mind that opening another Terminal will not default to proxy usage.

Resetting the proxy can be accomplished with the command:

unset all_proxy

However, this is typically unnecessary, as simply closing and reopening the Terminal will suffice for subsequent operations.

VS Code

In the dynamic world of VS Code, seamless interaction with remote servers ensures smooth updates of internal packages. From the side of Settings in VS Code, it only gives us an option of HTTP proxies. To harness the true potential of SOCKS5 proxies, a slightly unconventional yet easily attainable approach first requires integrating VS Code’s start command into the PATH environment.

While traditional methods like export PATH="your-dir:$PATH" may suffice, the ingenious minds behind VS Code have blessed users with a one-click solution for effortless configuration.
Open VS Code and press Command + Shift + P to invoke the search bar. Then, type shell to uncover the following option, and choose the one below.

A success notification shall greet you, and you are now ready to open a new Terminal and execute the command:

code --proxy-server="socks5://127.0.0.1:1086"

Then a new VS Code window will emerge, seamlessly channeling all its traffic through the SOCKS5 server address. Congratulations!