Class: Sheepsafe::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/sheepsafe/installer.rb

Constant Summary collapse

PLIST_FILE =
File.expand_path("~/Library/LaunchAgents/sheepsafe.plist")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, network = nil, controller = nil) ⇒ Installer

Returns a new instance of Installer.



7
8
9
10
11
12
13
14
# File 'lib/sheepsafe/installer.rb', line 7

def initialize(config = nil, network = nil, controller = nil)
  check_config_path
  require 'highline/import'
  @config  = config  || (File.readable?(Sheepsafe::Config::FILE) ? Sheepsafe::Config.new : Sheepsafe::Config.new({}))
  @network = network || Sheepsafe::Network.new(@config)
  @controller = controller || Sheepsafe::Controller.new(@config, @network, Logger.new(Sheepsafe::Controller::LOG_FILE))
  update_config_with_network
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/sheepsafe/installer.rb', line 5

def config
  @config
end

#controllerObject (readonly)

Returns the value of attribute controller.



5
6
7
# File 'lib/sheepsafe/installer.rb', line 5

def controller
  @controller
end

#networkObject (readonly)

Returns the value of attribute network.



5
6
7
# File 'lib/sheepsafe/installer.rb', line 5

def network
  @network
end

Instance Method Details

#addObject



170
171
172
173
174
175
176
# File 'lib/sheepsafe/installer.rb', line 170

def add
  @config.trusted_names << @network.ssid
  @config.last_network = nil
  say "Adding #{@network.ssid} to your trusted locations"
  write_config
  @controller.run
end

#announce_doneObject



145
146
147
148
# File 'lib/sheepsafe/installer.rb', line 145

def announce_done
  controller.run   # Choose the right network and get things going
  say("Sheepsafe installation done!")
end

#check_config_pathObject



27
28
29
30
31
# File 'lib/sheepsafe/installer.rb', line 27

def check_config_path
  if !File.directory? "#{ENV['HOME']}/.sheepsafe/"
    Dir.mkdir("#{ENV['HOME']}/.sheepsafe/")
  end
end

#config_promptsObject



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
# File 'lib/sheepsafe/installer.rb', line 57

def config_prompts
  say "First thing we need is the name of a server you can reach via SSH."

  config.ssh_host = ask "SSH connection (server name or user@server) >\n" do |q|
    q.default = config.ssh_host
  end

  config.ssh_port = ask "SSH Port >\n" do |q|
    q.default = config.ssh_port || 22
  end

  say "Testing connectivitity to #{config.ssh_host}..."
  system "ssh -p #{config.ssh_port} #{config.ssh_host} true"
  unless $?.success?
    abort "Sorry! that ssh host was no good."
  end

  config.socks_port = ask "Ok, next we need to pick a port on localhost where the proxy runs >\n" do |q|
   q.default = config.socks_port || 9999
  end

  config.trusted_location = ask "Next, a name for the \"trusted\" network location >\n" do |q|
    q.default = config.trusted_location
  end

  config.trusted_names = ask "Next, one or more trusted network names/SSIDs (comma-separated) >\n" do |q|
    q.default = @names.join(',')
  end.split(",").map(&:strip)
end

#installObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/sheepsafe/installer.rb', line 16

def install
  migrate_old_config_path
  intro_message
  config_prompts
  setup_network_location
  write_config
  write_launchd_plist
  register_launchd_task
  announce_done
end

#intro_messageObject



48
49
50
51
52
53
54
55
# File 'lib/sheepsafe/installer.rb', line 48

def intro_message
  say("Welcome to Sheepsafe!\n\nSo you want to protect yourself from FireSheep snoopers like me, eh?\nFollow the prompts to get started.\n")
end

#listObject



178
179
180
181
# File 'lib/sheepsafe/installer.rb', line 178

def list
  say "Currently trusted locations:"
  puts @config.trusted_names
end

#migrate_old_config_pathObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sheepsafe/installer.rb', line 33

def migrate_old_config_path
  reinitialize = false
  if File.exist?("#{ENV['HOME']}/.sheepsafe.log")
    reinitialize = true
    say "Moving old sheepsafe.log file to new directory."
    system "mv ~/.sheepsafe.log ~/.sheepsafe/sheepsafe.log"
  end
  if File.exist?("#{ENV['HOME']}/.sheepsafe.yml")
    reinitialize = true
    say "Moving old sheepsafe.yml file to new directory."
    system "mv ~/.sheepsafe.yml ~/.sheepsafe/sheepsafe.yml"
  end
  initialize if reinitialize
end

#register_launchd_taskObject

Register the task with launchd.



140
141
142
143
# File 'lib/sheepsafe/installer.rb', line 140

def register_launchd_task
  say "Registering #{PLIST_FILE}"
  system "launchctl load #{PLIST_FILE}"
end

#setup_network_locationObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sheepsafe/installer.rb', line 87

def setup_network_location
  if `networksetup -listlocations` !~ /Untrusted/m &&
      agree("Next, I'll create and switch to the \"Untrusted\" location in Network Preferences. OK\? (yes/no)\n")
    system "networksetup -createlocation Untrusted populate"
  end

  if agree "Next, I'll set up the SOCKS proxy in the \"Untrusted\" location for you. OK\? (yes/no)\n"
    system "networksetup -switchtolocation Untrusted"
    wifi_service = `networksetup -listallnetworkservices`.lines.detect {|l| l[/(AirPort|Wi-Fi)/, 1] }
    system "networksetup -setsocksfirewallproxy wifi_service localhost #{config.socks_port}"
  end
end

#uninstallObject



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sheepsafe/installer.rb', line 150

def uninstall
  if controller.proxy_running?
    say "Shutting down SOCKS proxy..."
    controller.bring_socks_proxy 'down'
  end
  if File.exist?(PLIST_FILE)
    say "Uninstalling Sheepsafe from launchd..."
    system "launchctl unload #{PLIST_FILE}"
    File.unlink PLIST_FILE rescue nil
  end
  system "rm -r ~/.sheepsafe"
  say "Uninstall finished."
end

#updateObject



164
165
166
167
168
# File 'lib/sheepsafe/installer.rb', line 164

def update
  system "launchctl unload #{PLIST_FILE}"
  write_launchd_plist
  register_launchd_task
end

#write_configObject



100
101
102
103
# File 'lib/sheepsafe/installer.rb', line 100

def write_config
  say "Saving configuration to #{Sheepsafe::Config::FILE}..."
  config.write
end

#write_launchd_plistObject

Write a launchd plist file to .~/Library/LaunchAgents/sheepsafe.plist.

For details see tech.inhelsinki.nl/locationchanger/



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/installer.rb', line 108

def write_launchd_plist
  say "Setting up launchd configuration file #{PLIST_FILE}..."
  Dir.mkdir(File.dirname(PLIST_FILE)) unless File.directory?(File.dirname(PLIST_FILE))
  plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"\n  \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n  <key>Label</key>\n  <string>org.rubygems.sheepsafe</string>\n  <key>ProgramArguments</key>\n  <array>\n    <string>\#{sheepsafe_bin_path}</string>\n  </array>\n  <key>WatchPaths</key>\n  <array>\n    <string>/Library/Preferences/SystemConfiguration</string>\n  </array>\n    <!-- We specify PATH here because /usr/local/bin, where grownotify -->\n    <!-- is usually installed, is not in the script path by default. -->\n    <key>EnvironmentVariables</key>\n    <dict>\n            <key>PATH</key><string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/bin</string>\n    </dict>\n</dict>\n</plist>\n"
  File.open(PLIST_FILE, "w") {|f| f << plist }
end