Class: Sheepsafe::Controller
- Inherits:
-
Object
- Object
- Sheepsafe::Controller
- Defined in:
- lib/sheepsafe/controller.rb
Defined Under Namespace
Classes: TeeStdout
Constant Summary collapse
- LOG_FILE =
Sheepsafe::Config::FILE.sub(/\.yml/, '.log')
Instance Method Summary collapse
- #bring_socks_proxy(direction) ⇒ Object
-
#initialize(config = nil, network = nil, logger = nil) ⇒ Controller
constructor
A new instance of Controller.
- #log(msg) ⇒ Object
- #network_changed? ⇒ Boolean
- #network_up? ⇒ Boolean
- #notify_ok(msg) ⇒ Object
- #notify_warning(msg) ⇒ Object
- #proxy_running? ⇒ Boolean
- #run(args = ARGV) ⇒ Object
- #switch_to_trusted? ⇒ Boolean
- #switch_to_untrusted? ⇒ Boolean
- #when_growl_available(&block) ⇒ Object
- #with_log_file(&block) ⇒ Object
Constructor Details
#initialize(config = nil, network = nil, logger = nil) ⇒ Controller
Returns a new instance of Controller.
28 29 30 31 32 33 |
# File 'lib/sheepsafe/controller.rb', line 28 def initialize(config = nil, network = nil, logger = nil) @config = config || Sheepsafe::Config.new @network = network || Sheepsafe::Network.new(@config) @logger = logger $stdout = TeeStdout.new(self) end |
Instance Method Details
#bring_socks_proxy(direction) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sheepsafe/controller.rb', line 108 def bring_socks_proxy(direction) cmd = case direction when 'up' then 'start' when 'down' then 'stop' when 'kick' then 'restart' else direction end Daemons.run_proc('sheepsafe.proxy', :ARGV => [cmd], :dir_mode => :normal, :dir => "#{ENV['HOME']}/.sheepsafe") do pid = nil trap("TERM") do Process.kill("TERM", pid) exit 0 end sleep 5 # wait a bit before starting proxy exit_count = 0 ssh_command = "ssh #{@config.ssh_args}" loop do pid = fork do exec(ssh_command) end Process.waitpid(pid) exit_count += 1 if exit_count % 2 == 1 && exit_count < 10 log "command '#{ssh_command}' exited #{exit_count} times:\nlast time with #{$?.exitstatus}" end sleep 1 end end end |
#log(msg) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/sheepsafe/controller.rb', line 157 def log(msg) if @logger @logger.info(msg) else with_log_file {|f| Logger.new(f).info(msg) } end end |
#network_changed? ⇒ Boolean
96 97 98 |
# File 'lib/sheepsafe/controller.rb', line 96 def network_changed? @config.last_network.nil? || @network.ssid != @config.last_network.ssid || @network.bssid != @config.last_network.bssid end |
#network_up? ⇒ Boolean
92 93 94 |
# File 'lib/sheepsafe/controller.rb', line 92 def network_up? @network.up? end |
#notify_ok(msg) ⇒ Object
143 144 145 146 |
# File 'lib/sheepsafe/controller.rb', line 143 def notify_ok(msg) when_growl_available { Growl.notify_ok(msg) } log(msg) end |
#notify_warning(msg) ⇒ Object
148 149 150 151 |
# File 'lib/sheepsafe/controller.rb', line 148 def notify_warning(msg) when_growl_available { Growl.notify_warning(msg) } log(msg) end |
#proxy_running? ⇒ Boolean
139 140 141 |
# File 'lib/sheepsafe/controller.rb', line 139 def proxy_running? File.exist?("#{ENV['HOME']}/.sheepsafe/sheepsafe.proxy.pid") && File.read("#{ENV['HOME']}/.sheepsafe/sheepsafe.proxy.pid").to_i > 0 end |
#run(args = ARGV) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/sheepsafe/controller.rb', line 35 def run(args = ARGV) log("Sheepsafe starting") case args.first when 'proxy' # 'sheepsafe proxy up/down/kick' bring_socks_proxy args[1] return when 'disable' @config.disabled = true @config.write bring_socks_proxy 'down' system "scselect #{@config.trusted_location}" return when 'enable' @config.disabled = nil @config.write when nil true # continue else abort "unknown command #{args.first}. Try 'install', 'update', or 'uninstall'." end # Always recycle the proxy server on network changes bring_socks_proxy 'down' return if @config.disabled if network_up? if network_changed? if switch_to_trusted? notify_ok "Switching to #{@config.trusted_location} location" system "scselect #{@config.trusted_location}" elsif switch_to_untrusted? notified = false loop do require 'open-uri' is_google_com = open("http://www.google.com") {|f| f.['server'] == 'gws'} rescue nil break if is_google_com notify_warning("Waiting for internet connection before switching") unless notified notified = true sleep 5 end notify_warning "Switching to #{@config.untrusted_location} location" system "scselect #{@config.untrusted_location}" bring_socks_proxy 'up' end @config.last_network = @network @config.write elsif !@network.trustworthy? bring_socks_proxy 'up' end else log("AirPort is off") end log("Sheepsafe finished") end |
#switch_to_trusted? ⇒ Boolean
100 101 102 |
# File 'lib/sheepsafe/controller.rb', line 100 def switch_to_trusted? @network.trustworthy? end |
#switch_to_untrusted? ⇒ Boolean
104 105 106 |
# File 'lib/sheepsafe/controller.rb', line 104 def switch_to_untrusted? !@network.trustworthy? end |
#when_growl_available(&block) ⇒ Object
153 154 155 |
# File 'lib/sheepsafe/controller.rb', line 153 def when_growl_available(&block) block.call if defined?(Growl) end |
#with_log_file(&block) ⇒ Object
165 166 167 |
# File 'lib/sheepsafe/controller.rb', line 165 def with_log_file(&block) File.open(LOG_FILE, (File::WRONLY | File::APPEND), &block) end |