Class: Clients::TorClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/clients/tor_client.rb

Constant Summary collapse

DEFAULT_PORT =
9050
DEFAULT_CONTROL_PORT =
9051
DEFAULT_HTTP_PORT =
8080
OK_STATUS =
"250 OK\n".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TorClient

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/clients/tor_client.rb', line 22

def initialize(options = {})
  options = {
    tor_host:        "localhost",
    tor_port:        (ENV["TOR_PORT"] || DEFAULT_PORT).to_i,
    control_port:    (ENV["TOR_CONTROL_PORT"] || DEFAULT_CONTROL_PORT).to_i,
    host:            "localhost",
    port:            (ENV["HTTP_TOR_PORT"] || DEFAULT_HTTP_PORT).to_i,
    circuit_timeout: 10,
    throttle_by:     10, # .seconds implied
    pool_num:        nil
  }.merge(options)

  @pool_num = options.delete(:pool_num)
  @config = OpenStruct.new options

  setup_pool
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/clients/tor_client.rb', line 16

def config
  @config
end

#pool_numObject (readonly)

Returns the value of attribute pool_num.



16
17
18
# File 'lib/clients/tor_client.rb', line 16

def pool_num
  @pool_num
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



16
17
18
# File 'lib/clients/tor_client.rb', line 16

def threshold
  @threshold
end

Instance Method Details

#hostObject



40
41
42
# File 'lib/clients/tor_client.rb', line 40

def host
  @config[:host]
end

#passwordObject



52
53
54
# File 'lib/clients/tor_client.rb', line 52

def password
  nil
end

#portObject



44
45
46
# File 'lib/clients/tor_client.rb', line 44

def port
  @config[:port]
end

#switch_identityObject Also known as: reset!



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/clients/tor_client.rb', line 60

def switch_identity
  throttle do
    client = Net::Telnet.new(
      "Host"    => config.tor_host,
      "Port"    => config.control_port,
      "Timeout" => config.circuit_timeout,
      "Prompt"  => Regexp.new(OK_STATUS)
    )

    authenticate client
    new_route client

    client.close
  end
end

#to_sObject



56
57
58
# File 'lib/clients/tor_client.rb', line 56

def to_s
  "#{host}:#{port}"
end

#userObject



48
49
50
# File 'lib/clients/tor_client.rb', line 48

def user
  nil
end