Skip to content

Instantly share code, notes, and snippets.

@techbelly
Created April 17, 2009 23:02
Show Gist options
  • Save techbelly/97330 to your computer and use it in GitHub Desktop.
Save techbelly/97330 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
### CHANGE THIS TO YOUR SERVER OK?
TUNNEL_ENDPOINT = '[email protected]'
### YOU CAN ALSO USE 'Ethernet' et al.
INTERFACE = 'AirPort'
PORT = 3022
def switch_on
start_tunnel && proxy_traffic
end
def switch_off
stop_tunnel && stop_proxying
end
def ssh_command
"ssh -D #{PORT} -f -C -q -N #{TUNNEL_ENDPOINT}"
end
def ssh_process_id
ssh_processes = `ps -o pid,command -h -e | grep '#{ssh_command}' | grep -v grep`
return nil if ssh_processes == ""
return ssh_processes.split(' ').first
end
def start_tunnel
system ssh_command if ssh_process_id.nil?
end
def stop_tunnel
process = ssh_process_id
system "kill #{process}" unless process.nil?
end
def proxy_traffic
system "sudo networksetup -setsocksfirewallproxy #{INTERFACE} 127.0.0.1 #{PORT} off"
end
def stop_proxying
system "sudo networksetup -setsocksfirewallproxystate #{INTERFACE} off"
end
action = ARGV[0] || 'usage'
case (action)
when 'on'
switch_on
when 'off'
switch_off
else
puts "#{$0} (on|off)"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment