DEV Community

Cover image for Connect to a Private network via proxy server from macOS
Muhammad Hewedy
Muhammad Hewedy

Posted on • Edited on

Connect to a Private network via proxy server from macOS

image

In this post, we will see how to connect from mac to another machine (as a forwarding proxy) then to the internet (or to a private network 😉).

The steps will focus to macos client, but it can be any other OS.

First, You need to install squid proxy on your Windows/Linux box (the jump box). there are many ways to do so, in my case I used a docker image for squid proxy.

docker run -d -p 3128:3128 --restart unless-stopped cosmicq/docker-squid
Enter fullscreen mode Exit fullscreen mode

Now, go to macOS, and you need to change the proxy settings in two places, the System Preferences > Network and in the shell.

System Preferences

For System Preferences > Network, Click Advanced, then go to the proxy tab and in the http and https sections, enter the ip/port of the proxy:

image

Note, you can use commands to set the proxy instead of using GUI, as follows:

networksetup -setwebproxy wi-fi 192.168.100.39 3128
networksetup -setwebproxystate wi-fi on
networksetup -setsecurewebproxystate wi-fi on
Enter fullscreen mode Exit fullscreen mode

This will make it easy to enable/disable the proxy setting on a user basis.

The shell

The last step is to set the http_proxy and https_proxy in your shell.

You can append the following at the end of your ~/.zshrc file:

export http_proxy="192.168.100.39:3128"
export https_proxy=$http_proxy
Enter fullscreen mode Exit fullscreen mode

Happy proxying!

Update

The squid proxy works well for HTTP/HTTPS traffic. however, some traffic is TCP traffic. There are different solutions to this problem, but I fixed it by using a native port-forward solution in windows using the command netsh.

In my case, I needed to access a DB server accessible from windows machine by 192.168.100.100/1433, so I run the following command in windows cmd (as administrator)

netsh interface portproxy add v4tov4 listenport=14330 connectport=1433 connectaddress=192.168.100.100
Enter fullscreen mode Exit fullscreen mode

which will open port 14330 on the windows machine and forward traffic to the DB server at 192.168.100.100 on port 1433.

Now I can connect to the DB server from mac on the windows IP address (in my case 192.168.100.39) on port 14330.

To delete the port-forwarding:

netsh interface portproxy delete v4tov4 listenport=14330
Enter fullscreen mode Exit fullscreen mode

To list port-forwarding:

netsh interface portproxy show v4tov4
Enter fullscreen mode Exit fullscreen mode

References:
https://github.com/CosmicQ/docker-squid
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc731068(v=ws.10)?redirectedfrom=MSDN

Top comments (8)

Collapse
 
abdennour profile image
abdennour • Edited

Great ! and for DNS resolution, you need to set it also :

# networksetup -setdnsservers <name> <defaut-dns> <dns-of-windows>
networksetup -setdnsservers wi-fi 192.168.1.1 10.31.65.51
Enter fullscreen mode Exit fullscreen mode
Collapse
 
abdennour profile image
abdennour

Wonderful!

Collapse
 
mhewedy profile image
Muhammad Hewedy

Thanks 🙂

Collapse
 
abdennour profile image
abdennour

Could you please explain/refer also how to make the squid host machine (windows) running with low resources ( economic usage) ? For example, i don't need to keep Windows screen Opened while my work is on Mac.

Thread Thread
 
mhewedy profile image
Muhammad Hewedy

I don't figure out how to fix this issue till the moment.

However In my setup, I use winawake to keep my windows always awake, then I lock the windows screen and the proxy keep working.

After I finish/pause my work on mac, then I can close the win laptop lid. And If I resume work, I go and open the lid. that's it for the moment.

So, it is a lid-oriented way 😂

Collapse
 
khaledannajar profile image
Khaled Annajar

Great tutorial

Collapse
 
mhewedy profile image
Muhammad Hewedy

Thanks 🙂

Collapse
 
abdennour profile image
abdennour

for sshing, github.com/patpadgett/corkscrew needs to be installed .example: squins.com/knowledge/squid-http-ht...