Class: Onion::ControlClient

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

Overview

Onion::ControlClient is a client to the Tor Control Protocol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 9051, log_level = Logger::ERROR) ⇒ ControlClient

Returns a new instance of ControlClient.



9
10
11
12
13
14
15
16
17
18
# File 'lib/onion/control_client.rb', line 9

def initialize(host="127.0.0.1", port=9051, log_level=Logger::ERROR)
  @log  = Logger.new(STDOUT)
  @log.level = log_level
  
  @host = host  # Host of the Tor process.
  @port = port  # Port accepting the Tor Control Protocol.
  @sock = TCPSocket.new(host, port)
  
  @log.info("[TOR CONTROL CLIENT ] Connected to #{host}:#{port}.")
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/onion/control_client.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/onion/control_client.rb', line 7

def port
  @port
end

Instance Method Details

#attach_stream(stream_id, circuit_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/onion/control_client.rb', line 44

def attach_stream(stream_id, circuit_id)
  send("attachstream #{stream_id} #{circuit_id}")
  response = @sock.gets
    if response.split[1] == "OK"
      @log.info("[TOR CONTROL CLIENT] #{response}")
    else
      @log.warn("[TOR CONTROL CLIENT] Stream #{stream_id} was not attached to circuit #{circuit_id}.")
      @log.warn("[TOR CONTROL CLIENT] #{response}.")
    end
end

#authenticate(pass = "\"\"") ⇒ Object



25
26
27
28
# File 'lib/onion/control_client.rb', line 25

def authenticate(pass="\"\"")
  send("authenticate #{pass}")
  @log.debug("[TOR CONTROL CLIENT] #{@sock.gets}")
end

#extend_circuit(specs, id = 0) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/onion/control_client.rb', line 30

def extend_circuit(specs, id=0)
  send("extendcircuit #{id} #{specs.join(",")}")
  response = @sock.gets
  if response.split[1] == "EXTENDED"
    @log.debug("[TOR CONTROL CLIENT] #{response}")
    return response.split[2]
  else
    @log.warn("[TOR CONTROL CLIENT] Circuit (#{specs.join(",")}) was not extended.")
    @log.warn("[TOR CONTROL CLIENT] #{response}.")
    
    return -1
  end
end

#get_stream_statusObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/onion/control_client.rb', line 69

def get_stream_status
  send("getinfo stream-status")
  result = []
  line = @sock.gets
  while line != "250 OK\r\n"
    unless line == "250+stream-status=\r\n" or line == ".\r\n"
      line = line.gsub("250-stream-status=", "")
      result << line
    end
    line = @sock.gets
  end
  return result
end

#router_status_info(nicknames = nil, fingerprints = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/onion/control_client.rb', line 55

def router_status_info(nicknames=nil,fingerprints=nil)
  statuses = ""
  
  if nicknames
    nicknames.each do |name| 
      send("getinfo ns/name/#{name}")
      result << @sock.gets
    end
    result << @sock.gets
  end
  
  Onion::RouterList.new(statuses)
end

#send(text) ⇒ Object



20
21
22
23
# File 'lib/onion/control_client.rb', line 20

def send(text)
  @sock << "#{text}\r\n"
  @log.debug("[TOR CONTROL CLIENT] #{text}")
end