Class: VpnService

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

Class Method Summary collapse

Class Method Details

.connect(address) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/vpn_service.rb', line 2

def self.connect(address)
   system(
      %x{
          osascript <<APPLESCRIPT
          tell application "Tunnelblick"
            connect "#{address}"
            get state of first configuration where name = "#{address}"
            repeat until result = "CONNECTED"
              delay 1
              get state of first configuration where name = "#{address}"
            end repeat
          end tell
          APPLESCRIPT}
    )

  return "Connected to #{address}"
end

.connected?Boolean



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vpn_service.rb', line 40

def self.connected?

    output = `
        osascript <<APPLESCRIPT
        tell application "Tunnelblick"
           get state of first configuration
           return result
        end tell
        APPLESCRIPT
    `

  return output == "CONNECTED\n"
end

.disconnect(address) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vpn_service.rb', line 20

def self.disconnect(address)
  raise 'No connection exists' unless connected?
  system(
    %x{
        osascript <<APPLESCRIPT
        tell application "Tunnelblick"
           disconnect "#{address}"
        end tell
        APPLESCRIPT}
  )
  return 'Disconnected'
end

.switch(address) ⇒ Object



33
34
35
36
37
38
# File 'lib/vpn_service.rb', line 33

def self.switch(address)
  raise 'No connection exists' unless connected?
  self.disconnect
  self.connect(address)
  return "Connected to #{address}"
end