Class: Selenium::WebDriver::Tor::TorProcess

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

Overview

Respresentation of a tor process

Constant Summary collapse

BOOTSTRAP_SUCCESS_REGEX =
/Bootstrapped 100% \(done\): Done$/
BOOTSTRAP_FAIL_REGEX =
/^[A-Z][a-z]{2} \d{2} \d{2}:\d{2}:\d{2}\.\d{3} \[err\] .*/
SOCKS_PORT_REGEX =
/Opened Socks listener connection \(ready\) on 127\.0\.0\.1:(\d+)$/
DIR_MSG =
'data_dir must exist and be a dir'
DATA_FILES =
%w[cached-certs cached-microdesc-consensus cached-microdescs.new].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, opts = {}) ⇒ TorProcess

Returns a new instance of TorProcess.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
# File 'lib/tor/tor_process.rb', line 25

def initialize(data_dir, opts = {})
  setup_data_dir data_dir
  raise ArgumentError, 'TorProcess.new takes an options hash' unless opts.is_a? Hash

  @opts = map_opts_to_torrc_keys opts
  @torrc = Torrc.new(@data_dir)
  @config ||= setup_config # Hash to store torrc config
  @socks_port = 0
end

Class Attribute Details

.cached_certsObject

set when tor first started



20
21
22
# File 'lib/tor/tor_process.rb', line 20

def cached_certs
  @cached_certs
end

.cached_microdesc_consensusObject

set when tor first started



20
21
22
# File 'lib/tor/tor_process.rb', line 20

def cached_microdesc_consensus
  @cached_microdesc_consensus
end

.cached_microdescsObject

set when tor first started



20
21
22
# File 'lib/tor/tor_process.rb', line 20

def cached_microdescs
  @cached_microdescs
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/tor/tor_process.rb', line 23

def config
  @config
end

#pidObject (readonly)

Returns the value of attribute pid.



23
24
25
# File 'lib/tor/tor_process.rb', line 23

def pid
  @pid
end

#socks_portObject (readonly)

Returns the value of attribute socks_port.



23
24
25
# File 'lib/tor/tor_process.rb', line 23

def socks_port
  @socks_port
end

Instance Method Details

#control_portObject



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

def control_port
  File.read(@torrc.control_port_file_path).split(':').last.to_i # "PORT=127.0.0.1:<port>"
end

#start_tor(timeout: 120) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tor/tor_process.rb', line 35

def start_tor(timeout: 120)
  r, io = IO.pipe
  pid = Process.spawn tor_command, out: io, err: :out
  io.close
  parse_tor_bootstrap_with_timeout(r, timeout: timeout)
  # tor process now bootstrapped
  read_from_data_files
  @pid = pid
rescue Timeout::Error
  timeout_with pid, timeout
ensure
  r.close
end

#stop_torObject



49
50
51
52
53
# File 'lib/tor/tor_process.rb', line 49

def stop_tor
  Process.kill 'KILL', pid if pid
  FileUtils.rm_rf @data_dir if @data_dir
  @pid = nil
end