Wednesday, November 19, 2014

Getting MacPorts to Work Behind a Firewall

Link.


MacPorts is an extremely useful command line utility for managing open-source software (ports) on your Mac. It makes it a breeze to to find, download, compile and install open-source software on your machine, as well as upgrade to the latest versions without wasting a lot of time.

Unfortunately those of us behind corporate firewalls are usually prevented from using the tool due to network security restrictions of the rsyncd protocol.

I hope this post helps you solve some of the most common issues encountered when running MacPorts selfupdate or sync commands behind a corporate firewall.

What you will need
  • A recent copy of MacPorts installed. If you have not done this yet, please head over to MacPorts to get the latest version 
  • Your corporate proxy server name, port and proxy user credentials if necessary. 
  • The ability to (sudo) temporarily increase your account privileges to those of an administrator on your Mac. 
Step 1: Switch MacPorts to use HTTP

MacPorts is configured to synchronize its list of ports via rsync. We will change it so it uses HTTP instead.

Open a terminal window and edit the MacPorts source configuration file. Enter the superuser password when prompted.
sudo vim /opt/local/etc/macports/sources.conf
Comment out the line at the bottom of the file and add a new one that uses the HTTP address. When you are done, it should look something like this:
#rsync://rsync.macports.org/release/ports/ [default]
http://distfiles.macports.org/ports.tar.gz [default]
Save the file and exit the editor.

Step 2: Setup your Proxies

Edit your profile script, this could be in the form ~/.profile, ~/.bash_profile, etc.
vim ~/.profile

Add the following to the bottom of the file:
################################### CORP PROXY ###################################
PROXY_SERVER=corporate-proxy-server-here
PROXY_PORT=8888
PROXY_USERNAME=proxy_account
PROXY_PASSWORD=proxy_password
PROXY=$PROXY_USERNAME:$PROXY_PASSWORD@$PROXY_SERVER:$PROXY_PORT
# HTTP and HTTPS
export http_proxy=http://$PROXY
export HTTP_PROXY=http://$PROXY
export https_proxy=http://$PROXY
export HTTPS_PROXY=http://$PROXY
# RSYNC
export rsync_proxy=$PROXY
export RSYNC_PROXY=$PROXY
##################################################################################

Save the file and exit the editor.

Step 3: Edit the sudoers file

This is required to forward your proxies to the superuser account every time you run sudo port commands.
sudo visudo

Append the following to the end of the list of Defaults:
Defaults env_keep += "http_proxy rsync_proxy HTTP_PROXY RSYNC_PROXY"

Save the file and exit the editor.

Restart the Shell

Exit the terminal shell and start a new session to load the new settings.

Test your new proxy environment variables are now available:
env | grep http
You should see something like this:
$ env | grep http
http_proxy=http://proxy_account:proxy_password@corporate-proxy-server-here:8888
HTTP_PROXY=http://proxy_account:proxy_password@corporate-proxy-server-here:8888

Update your Ports

You should now be able to load the latest ports using the http proxy. Test this by typing the following command. Note that the -d switch invokes the command in debug mode. We do this so you can see what is happening behind the scenes.
sudo port -d sync

You should see a long list of ports being downloaded.

No comments:

Post a Comment