Module: Tor

Defined in:
lib/tor.rb,
lib/tor/dnsel.rb,
lib/tor/config.rb,
lib/tor/version.rb,
lib/tor/control.rb

Overview

See Also:

Defined Under Namespace

Modules: DNSEL, VERSION Classes: Config, Controller

Class Method Summary (collapse)

Class Method Details

+ (Boolean) available?

Returns true if Tor is available, false otherwise.

Examples:

Tor.available?    #=> true

Returns:

  • (Boolean)


54
55
56
# File 'lib/tor.rb', line 54

def self.available?
  !!program_path
end

+ (String) program_path(program_name = :tor)

Returns the path to the tor executable, or nil if the program could not be found in the user's current PATH environment.

Examples:

Tor.program_path  #=> "/opt/local/bin/tor"

Parameters:

  • (String, #to_s) program_name (defaults to: :tor)

Returns:

  • (String)


80
81
82
83
84
85
86
# File 'lib/tor.rb', line 80

def self.program_path(program_name = :tor)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    program_path = File.join(path, program_name.to_s)
    return program_path if File.executable?(program_path)
  end
  return nil
end

+ (Boolean) running?

Returns true if the Tor process is running locally, false otherwise.

This works by attempting to establish a Tor Control Protocol (TC) connection to the standard control port 9051 on localhost. If Tor hasn't been configured with the ControlPort 9051 option, this will return false.

Examples:

Tor.running?      #=> false

Returns:

  • (Boolean)

Since:

  • 0.1.2



38
39
40
41
42
43
44
45
# File 'lib/tor.rb', line 38

def self.running?
  begin
    Tor::Controller.new.quit
    true
  rescue Errno::ECONNREFUSED
    false
  end
end

+ (String) version

Returns the Tor version number, or nil if Tor is not available.

Examples:

Tor.version       #=> "0.2.1.25"

Returns:

  • (String)


65
66
67
68
69
# File 'lib/tor.rb', line 65

def self.version
  if available? && `#{program_path} --version` =~ /Tor v(\d+)\.(\d+)\.(\d+)\.(\d+)/
    [$1, $2, $3, $4].join('.')
  end
end