Setup Charles Proxy on Mac

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives the steps to setup Charles Proxy on Mac OS X Yosemite. It can be used to proxy your CLI traffic to a work proxy server. The main benefit is if you have to pass credentials it can be securely stored in Charles Proxy (One place) and all your CLI commands and Dev tools point to it locally without the need for credentials.


Download and Install


Uncheck Mac OS X Proxy

Since we'll be setting up all the proxy settings in Charles under External Proxy Settings... we'll want to turn this option off.


Setup External Proxy

  1. Select menu Proxy | External Proxy Settings...
  2. Check the box for Use external proxy servers
  3. Check the box for Web Proxy (HTTP)
  4. Enter proxy hostname not URL and Port number
  5. Enter proxy server username and password if required
  6. Check the box for Secure Web Proxy (HTTPS)
  7. Enter proxy hostname not URL and Port number
  8. Enter proxy server username and password if required
  9. Check the box for Always bypass external proxies for localhost
  10. Enter exclusions list and Select OK
    1. One IP address or name per line.
*.local
169.254/16
*.internaldomain.com
10.0.2.*
33.33.33.*

Charles proxy menu

Charles proxy external proxy settings


Proxy Settings

Set the Charles Proxy "Proxy Settings". Basically what port to listen on locally.

  1. Select menu Proxy | Proxy Settings
  2. Set the port to the default 8888 or pick a custom port.
  3. Select the check box for Enable transparent HTTP proxying
  4. Select OK

Charles proxy settings


Configure CLI

Option 1 (Always set)

There's several ways you can get the Charles Proxy settings into your shell environment variables. Here's one way to allows have the settings in place. Meaning you'll always need to be running Charles Proxy while in the shell if you need network access.

Add environment variables to ~/.bash_profile

# Charles Proxy Settings
export ALL_PROXY=http://localhost:8888
export HTTP_PROXY=$ALL_PROXY
export HTTPS_PROXY=$ALL_PROXY
export FTP_PROXY=$ALL_PROXY
export RSYNC_PROXY=$ALL_PROXY
export http_proxy=$ALL_PROXY
export https_proxy=$ALL_PROXY
export ftp_proxy=$ALL_PROXY
export rsync_proxy=$ALL_PROXY


Option 2 (Turn settings on and off)

Another way you can get the Charles Proxy settings into your shell environment variables is to create a function to set and a function to unset. With the following, you can type proxy-on which will set the environment variables and then when done with the need for the proxy settings you can type proxy-off. This may be good for if you are on and off of a proxy network.

Add environment variables to ~/.bash_profile

# Show Current Settings
function proxy-show(){
  echo "ALL_PROXY = ($ALL_PROXY)"
  echo "HTTP_PROXY = ($HTTP_PROXY)"
  echo "http_proxy = ($http_proxy)"
  echo "HTTPS_PROXY = ($HTTPS_PROXY)"
  echo "https_proxy = ($https_proxy)"
  echo "ftp_proxy = ($ftp_proxy)"
  echo "rsync_proxy = ($rsync_proxy)"
  echo "no_proxy = ($no_proxy)"
  echo "NO_PROXY = ($NO_PROXY)"
}

# Enable Proxy
function proxy-on(){
  export ALL_PROXY=http://localhost:8888
  export HTTP_PROXY=$ALL_PROXY
  export http_proxy=$ALL_PROXY
  export HTTPS_PROXY=$ALL_PROXY
  export https_proxy=$ALL_PROXY
  export ftp_proxy=$ALL_PROXY
  export rsync_proxy=$ALL_PROXY
  export NO_PROXY=localhost,.local,169.254.,127.0.0.1,10.0.2.,.bonusbits.com,/var/run/docker.sock
  export no_proxy=$NO_PROXY
  proxy-show
}

# Disable Proxy
function proxy-off(){
  unset HTTP_PROXY HTTPS_PROXY http_proxy https_proxy ftp_proxy rsync_proxy ALL_PROXY NO_PROXY no_proxy
  proxy-show
}

# Turn on by default
proxy-on


Configure Test Kitchen

Configure Test Kitchen to Use Charles Proxy


Icon-Tip-Square-Green.png You must launch Charles Proxy and keep it running pretty much at all times or at least while doing CLI/Dev work that requires network access.


Troubleshooting

  1. If you get an error that you can't save settings to the configuration you may need to change ownership of the application files.
    sudo chown -R username:usergroup /Applications/Charles.app
    
  2. If you don't have a license it will shutdown after about 10 minutes. You can relaunch it, apply a license or maybe cron something.