How to easily forward/redirect all network traffics through local socks port 1080?

How to easily forward/redirect all network traffics through local socks port 1080? I did google this before asking question. I found this link and this link and others too. But there is no simple solution. (The system preferences network proxy setup just don't work for all apps. ) Is that so hard to do it? I think just some iptables rules should do it. (although I don't know what is the equivalent iptables command in macos, maybe pfctl?). Can anyone show me some simple shell scripts which can achieve this? Thank you very much.


Solution 1:

Since nobody posts a shell script (or easy way) to solve my question, I posted my current workaround solution:

  1. Install virtualbox
  2. Install openwrt as the guest OS to virtualbox
  3. Configure the openwrt to become a router which has the lan IP of 192.168.56.2. The lan interface is a Host-only adapter. The wan interface is Bridged adapter.
  4. Configure the openwrt router to do the transparent proxy job that I want.
  5. Change both gateway and DNS to 192.168.56.2 in the host.
    • to change the gateway: sudo route change default 192.168.56.2
    • to change the DNS: must use the UI way. System Preferences --> Network --> Adavanced --> DNS

That's all. So far so good.

Solution 2:

@sgon00, Thank you for an idea and solution! I can't comment, but I want to share a bit of continuation here:

To use your Virtual OpenWRT router from different VM on the same host you should:

  1. On OpenWRT VM follow the intruction: https://openwrt.org/docs/guide-user/virtualization/virtualbox-vm

but instead of adding phisical adapter (eth0, wlps0) (3d adapter in manual), add virtual one (virbr0) as Bridged Adapter (Linux).

then ssh to openwrt:

root@OpenWrt:~# uci set network.lan.type='bridge' 
root@OpenWrt:~# uci set network.lan.proto='static' 
root@OpenWrt:~# uci set network.lan.netmask='255.255.255.0' 
root@OpenWrt:~# uci set network.lan.ip6assign='60' 
root@OpenWrt:~# uci set network.lan.ipaddr='192.168.1.1'

uci commit && reboot

  1. On another VM you want to route over OpenWRT:

add the same virtual adapter (virbr0) as on step 1 the same way. Start VM, it should automatticaly connect to the DHCP on [1] and connect to internet.