Class: Pincerna::Vpn

Inherits:
Base
  • Object
show all
Defined in:
lib/pincerna/vpn.rb

Overview

Connects or disconnects from system’s VPNs.

Constant Summary

MATCHER =

The expression to match.

/^(?<all>.*)$/i
ICON =

The icon to show for each feedback item.

Pincerna::Base::ROOT + "/images/vpn.png"

Constants inherited from Base

Base::CACHE_ROOT, Base::FULL_NAME, Base::RELEVANT_MATCHES, Base::ROOT, Base::TYPES, Base::WORKFLOW_ROOT

Instance Attribute Summary

Attributes inherited from Base

#format, #format_content_type, #output

Instance Method Summary (collapse)

Methods inherited from Base

#add_feedback_item, execute!, #filter, #format_float, #initialize, #output_feedback, #round_float

Constructor Details

This class inherits a constructor from Pincerna::Base

Instance Method Details

- (Array) perform_filtering(query)

Connects to or disconnects from system VPN.

Parameters:

  • query (Array)

    A query to match against VPNs names.

Returns:

  • (Array)

    A list of items to process.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pincerna/vpn.rb', line 20

def perform_filtering(query)
  rv = []
  interface_filter ||= query.empty? ? /.+/ : /#{query}/i

  execute_command("/usr/sbin/networksetup", "-listnetworkserviceorder").split(/\n\n/).each do |i|
    # Scan every interface
    token = StringScanner.new(i)

    if token.scan_until(/^\(\d+\)/) then
      name = token.scan_until(/\n/).strip # Get VPN name
      next if !interface_filter.match(name)

      # Get the type
      token.scan_until(/Hardware Port:\s/)

      # If type matches
      rv << {name: name, connected: vpn_connected?(name)} if is_vpn_service?(token.scan_until(/,/))
    end
  end

  rv
end

- (Array) process_results(results)

Processes items to obtain feedback items.

Parameters:

  • results (Array)

    The items to process.

Returns:

  • (Array)

    The feedback items.



47
48
49
50
51
52
53
54
55
# File 'lib/pincerna/vpn.rb', line 47

def process_results(results)
  results.map do |result|
    title = "#{result[:connected] ? "Disconnect from" : "Connect to"} #{result[:name]}"
    subtitle = "Action this item to #{result[:connected] ? "disconnect from" : "connect to"} the VPN service."
    arg = "#{result[:connected] ? "disconnect" : "connect"} service \"#{result[:name]}\""

    {title: title, arg: arg, subtitle: subtitle, icon: ICON}
  end
end

- (Boolean) vpn_connected?(name)

Checks if a VPN is connected.

Parameters:

  • name (String)

    The VPN’s name.

Returns:

  • (Boolean)

    true if the VPN is connected, false otherwise.



61
62
63
# File 'lib/pincerna/vpn.rb', line 61

def vpn_connected?(name)
  execute_command("/usr/sbin/networksetup", "-showpppoestatus", "\"#{name}\"").strip == "connected"
end