Class: TorClient

Inherits:
Object
  • Object
show all
Includes:
Process
Defined in:
lib/hopi/tor_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(torrc_path = nil) ⇒ TorClient

Returns a new instance of TorClient.



7
8
9
10
# File 'lib/hopi/tor_client.rb', line 7

def initialize torrc_path=nil
  @torrc_path = torrc_path
  @torr_log_output = []
end

Instance Attribute Details

#tor_availableObject (readonly)

Returns the value of attribute tor_available.



5
6
7
# File 'lib/hopi/tor_client.rb', line 5

def tor_available
  @tor_available
end

#tor_masterObject (readonly)

Returns the value of attribute tor_master.



5
6
7
# File 'lib/hopi/tor_client.rb', line 5

def tor_master
  @tor_master
end

#tor_pidObject (readonly)

Returns the value of attribute tor_pid.



5
6
7
# File 'lib/hopi/tor_client.rb', line 5

def tor_pid
  @tor_pid
end

Instance Method Details

#downObject



16
17
18
19
20
21
# File 'lib/hopi/tor_client.rb', line 16

def down
  @tor_master.close
  kill :QUIT, @tor_pid
  puts "🔻Killed Tor instance"
  true
end

#tor_available?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/hopi/tor_client.rb', line 12

def tor_available?
  find_executable 'tor'
end

#upObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hopi/tor_client.rb', line 23

def up
  raise FailedToSpawnTor "[err] Tor executable is unavailable. Consider modifying your PATH env" unless tor_available?

  master, slave = PTY.open
  read,write = IO.pipe

  if @torrc_path.nil?
    pid = spawn "tor", :in=>read, :out=>slave
  else
    pid = spawn "tor -f #{torrc_path}", :in=>read, :out=>slave
  end

  read.close
  slave.close
  write.close
    
  puts "🤞Waiting for tor to spawn ..."

  loop do
    line = master.gets
    raise FailedToSpawnTor.new "[err] Failed to spawn tor process" if line.nil?
    raise FailedToSpawnTor.new line if ["[warn]", "[err]"].any?{|loglevel| line.include? loglevel}
    if line.include? "[notice] Bootstrapped 100% (done)"
      @tor_pid = pid
      @tor_master = master
      puts "🚀 Spawned tor process"
      return @tor_pid
    end
  end
end