At the height of the British Empire, colonial subjects from the Raj to Malaya to the Caribbean modeled themselves in part on Englishmen: Nehru and Lee Kuan Yew went to Cambridge, Gandhi, to University College, London. Ho Chi Minh wrote in French for magazines in Paris. These days the world is full of businesspeople, bureaucrats, and scientists who have trained in the United States. — James Fallows, http://www.theatlantic.com/magazine/archive/2010/01/how-america-can-rise-again/7839/?single_page=true

Using SSH to bypass The Great Firewall/GFW

Posted on 09 December 2009 by Erwin

This looks like a long process, how does it work after I’m done?

  • You’re ssh tunnel to your server outside the GFW will be active *whenever* your online
  • When you need to bypass the GFW, simply select “SSH” from your locations menu
  • When you don’t need to use your proxy, simply switch your location back to Automatic

What you need:

Configuring SSH key based authentication can be very difficult if you haven’t configured it previously. The idea is:

  • Generate a key set on your local computer (with ssh-keygen on the local system)
  • The Key Set will make two files on your hard drive, a “Private Key” and a “Public Key” (~/.ssh/id_rsa.pub on the local system)
  • Note that the “Private Key” is many “lines” long, but the “Public Key” is a single line (no newlines/returns)
  • The server that you’re logging into keeps a list of keys that are authorized to access the system (in ~/.ssh/authorized_keys on the server)
  • Copy the single line of your “public key” file onto any line of your “authorized keys” file.
  • Make sure the permissions of both your public keys and private keys are “correct”. “Insecure” file permissions are the most common cause of SSH key’s failing to authenticate.
  • Always keep your .ssh dir and all your keys chmodded to 700 and 600 respectively.
  • Use “ssh -vvv” if you have trouble logging in, this will give you diagnostic output.

Verify that you’re able to connect to the system you’ll be using to proxy via SSH.

If you’ve got the Apple Developer Tools installed, then go directly to the AutoSSH website, download the source, compile and install. In case you don’t, I’ve complied a copy that you can download here.

Just extract with: [shell]tar zxvf autossh.tgz -C /[/shell]

Use foreground mode to verify that you’re able to connect to the system of your choice via AutoSSH.

[shell]autossh -M 19999 -D 9999 -N example.com[/shell]

Use cURL to verify that your proxy is working

[shell]curl —socks4a localhost:9999 -v www.facebook.com[/shell]

Now that your proxy is online, the next step is to define a new location in your Mac OS X “Network” Profiles.

In System Preferences / Network:

  • From the Locations dropdown choose “Edit Locations” then add a new location called “SSH”
    Screen shot 2009-12-01 at 上午01.07.36.png
  • Return to the main Network window and choose “Advanced” then “Proxies”
    Screen shot 2009-12-01 at 上午01.07.28.png
  • Enable “SOCKS Proxy” setting the proxy server to “localhost” and port “9999”
    Screen shot 2009-12-01 at 上午01.08.09.png
  • Under bypass proxy setting for these hosts and domains, you can enter any sites that will be slowed down by proxing via your SSH connection:
    [plain]*.local, 169.254/16, *.cn, *.163.com, *.baidu.com, *.youku.com, *.toudou.com, *.sina.com, *.chinesepod.com[/plain]

Last, we just need to configure autossh to start when our Mac boots up. We’ll use a Login Hook so that the script will run regardless of which user logs in.

http://support.apple.com/kb/HT2420

You’ll need to create the shell script to use as the Login Hook, save it as /usr/local/bin/loginhook, and make it executable (chmod 755).

[shell] #! /bin/bash

if [ "$(ps ax | grep autossh | grep -vc grep)" -lt 1 ]; then
  sudo -u {USER} /usr/local/bin/autossh -f -M 19999 -D 9999 -N -o ServerAliveInterval=3 {SERVER}
fi

[/shell]

If you have trouble connecting and need to debug the autossh line, first try disabling the “-f” so that the program runs in the foreground and returns output. If you need to send the output to a log file, you can edit the loginhook script to something like:

[shell]sudo -u {USER} autossh -M 19999 -D 9999 -N {SERVER} 2&>1 >> /tmp/loginhook.log[/shell]

(Note that you’ll need to replace {USER} with the short name of the user account that is providing the SSH public key and {SERVER} with the host your connecting to)

[shell]sudo defaults write com.apple.loginwindow LoginHook /usr/local/bin/loginhook[/shell]

If your ever in doubt as to weather or not your new Proxy is running correctly, save the following script as /usr/local/bin/gfw. Run this script to instantly check on the status of your proxy.

[shell]curl –socks4a localhost:9999 -v www.facebook.com[/shell]

Reboot, Test and Enjoy!

4 Comments For This Post

  1. Weston Says:

    Switching Network Locations From the Menu Bar http://networklocationapp.com/

    使用更方便

  2. Jonell Vittum Says:

    Seit 2005 haben über 6100 Personen von 600 unterschiedlichen Firmen zu der Entwicklung beigetragen und damit dafür gesorgt, dass etwa alle 2 bis 3 Monate eine neue Version des Linux-Kerns veröffentlicht wird.

  3. Build Solar Panels Says:

    I’ve been searching for this for quite a while. I’m writing a college paper on this and this is going to help me. Thanks.

  4. Male Impotence Pills Says:

    Way of life Changes to help Reduce Erectile DysfunctionChange your Prescription DrugDiscuss with your physician whether an alternative drug would suit your needs and minimize erectile dysfunction unwanted effects.If you have were built with a cardiac arrest, stroke or life-threatening heart rhythm through the last 6 months or if you are on a nitrate medication, do not take them.

Leave a Reply