13

Sometimes I need to send my traffic to a proxy, sometimes I don't.

Currently, I have to go to the settings every time to click it.

Is there any simple way for me to switch this? For instance, use a shell command to do this.

system proxy

1
  • sudo networksetup -setsocksfirewallproxy "USB 10/100/1000 LAN" 127.0.0.1 8888 / sudo networksetup -setsocksfirewallproxystate "USB 10/100/1000 LAN" off / sudo networksetup -setsocksfirewallproxystate "USB 10/100/1000 LAN" on works great. Jun 23, 2017 at 13:57

4 Answers 4

11

This can be done with networksetup.

Get all services:

networksetup -listallnetworkservices

Then with the proper service (e.g Ethernet or the name of your USB 10/100/1000 LAN adapter) enter

sudo networksetup -setsocksfirewallproxy <networkservice> <domain> <port number> <authenticated> <username> <password>

or

sudo networksetup -setwebproxy <networkservice> <domain> <port number> <authenticated> <username> <password>

to configure it (which isn't necessary in your case, because you already entered all necessary values).

e.g.

sudo networksetup -setsocksfirewallproxy Ethernet 127.0.0.1 18080
sudo networksetup -setwebproxy Ethernet 127.0.0.1 18080

To simply toggle it on or off use:

sudo networksetup -setsocksfirewallproxystate networkservice on | off
sudo networksetup -setwebproxystate networkservice on | off

e.g.

sudo networksetup -setsocksfirewallproxystate Ethernet off
sudo networksetup -setwebproxystate Ethernet off

man networksetup will reveal other proxy options (gopher, ftp, etc.)

11

Use "Locations" under Network Preferences.

enter image description here

Set up a location that has the proxy and then a location that doesn't. When you want to use the proxy, pick that location and your settings will change.

0

Take a look at this page at YourMacGuy about the Terminal command networksetup. Although the page dates from 2008, the command exists and works in macOS Sierra. Perhaps one of the proxy-related commands will work for you. You can always create multiple Locations in System Preferences > Network that can enable or disable the proxy. You will still need to open System Preferences, though.

0

I do it by defining shortcuts in Hammerspoon. Now I can toggle the system proxy with just shortcut CommandOptionControlP.

Source code available on GitHub.

Here is the setup steps:

  1. Set the IP and port of http and https proxy manually for the first time, which can be accomplished in command line though.
networksetup -setwebproxy "Wi-fi" 127.0.0.1 8080
networksetup -setsecurewebproxy "Wi-fi" 127.0.0.1 8080
  1. Add the following shell function to your ~/.profile
toggleProxy() {
    e=$(networksetup -getwebproxy wi-fi | grep "No")

    ns=wi-fi
    status=''
    if [ -n "$e" ]; then
      status=on
    else
      status=off
    fi

    echo -n "Turning $status proxy"
    networksetup -setstreamingproxystate $ns $status
    networksetup -setsocksfirewallproxystate $ns $status
    networksetup -setwebproxystate $ns $status
    networksetup -setsecurewebproxystate  $ns $status
}
  1. Add the following to your ~/.hammerspoon/init.lua
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "P", function()
  local output = hs.execute("toggleProxy", true)
  hs.alert.show(output)
end)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .