Module: Tor

Extended by:
Tor, Lock
Included in:
Tor
Defined in:
lib/rest-tor.rb,
lib/rest_tor/tor.rb,
lib/rest_tor/lock.rb,
lib/rest_tor/utils.rb,
lib/rest_tor/version.rb,
lib/rest_tor/instance.rb,
lib/rest_tor/dispatcher.rb,
lib/rest_tor/strategy/restart.rb

Defined Under Namespace

Modules: Builder, Dispatcher, Lock, Strategy, Utils Classes: Counter, DiedPortError, Error, Instance, InvalidFormat, UnvaliablePort

Constant Summary collapse

USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
MOBILE_AGENT =
'ANDROID_KFZ_COM_2.0.9_M6 Note_7.1.2'
TOR_COUNT =
10
TOR_PORT_START_WITH =
9000
TOR_DIR =
Pathname.new('/tmp/tor')
VERSION =
"0.1.6"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lock

lock, locked?, unlock!

Class Method Details

.loggerObject



9
10
11
12
13
14
15
# File 'lib/rest-tor.rb', line 9

def self.logger
  Logger.new(STDOUT).tap do |log|
    log.formatter = proc do |severity, datetime, progname, msg|
      "[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%3N')}] [#{Process.pid}-#{Thread.current.object_id}] #{msg}\n"
    end
  end
end

Instance Method Details

#clearObject



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rest_tor/tor.rb', line 165

def clear
  Dir.glob(TOR_DIR.join("**/*.pid")).each do |path|
    begin
      Process.kill("KILL", File.read(path).chomp.to_i)
    rescue Errno::ESRCH
    end
  end
  FileUtils.rm_rf(TOR_DIR)
  true
ensure
  store.clear
end

#countObject



178
179
180
# File 'lib/rest_tor/tor.rb', line 178

def count
  Dir.glob(TOR_DIR.join("*")).count
end

#dir(port) ⇒ Object



145
146
147
148
149
# File 'lib/rest_tor/tor.rb', line 145

def dir(port)
  TOR_DIR.join("#{port}").tap do |dir|
    FileUtils.mkpath(dir) if not Dir.exists?(dir)
  end
end

#hold_tor(mode: :default, rest: false, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rest_tor/tor.rb', line 27

def hold_tor(mode: :default, rest: false, &block)
  return yield if rest
  if tor=Thread.current[:tor]
    port = tor.port
  else
    port, tor = Dispatcher.take(mode: mode)
  end

  Thread.current[:tor] = tor

  if block_given?
    yield port, tor
  end
ensure
  Thread.current[:tor] = nil
  tor && tor.release!
end

#initObject



15
16
17
18
19
20
21
# File 'lib/rest_tor/tor.rb', line 15

def init
  lock("tor:init", expires: 1.minutes) do
    threads = []
    TOR_COUNT.times { |i| threads << Thread.new { listen(TOR_PORT_START_WITH + i + 1) }  }
    threads.map(&:join)
  end
end

#listen(port) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rest_tor/tor.rb', line 113

def listen(port)
  return if port.blank? || !port.to_s.match(/^\d+$/)

  logger.info "Open tor with port:#{port}"

  control_port = 6000 + port.to_i

  tor = 'tor --RunAsDaemon 1 --CookieAuthentication 0 --HashedControlPassword ""'
  tor+= " --ControlPort #{ control_port } --PidFile tor.pid --SocksPort #{port} --DataDirectory #{dir(port)}"
  tor+= " --CircuitBuildTimeout 5 --KeepalivePeriod 60 --NewCircuitPeriod 15 --NumEntryGuards 8"# make tor faster
  tor+= " --quiet" # unless Rails.env.production?
  system tor
  if ip=test(port)
    store.insert(port, ip)
  else
    tor = Tor.store[port]
    raise DiedPortError if tor&.died?
    raise UnvaliablePort if !tor
  end
rescue Error, RestClient::Exception => e
  stop(port)
  logger.info "#{e.class}:#{e.message}"
  retry
end

#request(options = {}, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rest_tor/tor.rb', line 45

def request(options={}, &block)
  url     = options[:url]
  mobile  = options[:mobile]
  proxy   = options[:proxy]
  raw     = options[:raw].nil? ? true : false
  mode    = options[:mode]    || :default
  method  = options[:method]  || :get
  payload = options[:payload] || {}
  timeout = options[:timeout] || 10
  format  = options[:format]  || (raw ? :html : :string)
  headers = options[:headers] || options[:header] || {}
  default_header = { 'User-Agent' => mobile ? MOBILE_AGENT : USER_AGENT }
  time, body = Time.now, nil
  rest    = proxy == false

  hold_tor(mode: mode, rest: rest) do |port, tor|

    proxy  ||= "socks5://127.0.0.1:#{port}" if not rest

    logger.info "Started #{method.to_s.upcase} #{url.inspect} (proxy:#{proxy} | mode:#{mode})"

    params  = {
      method:   method,
      url:      url,
      payload:  payload,
      proxy:    proxy,
      timeout:  timeout,
      headers:  default_header.merge(headers)
    }

    begin
      response = RestClient::Request.execute(params) do |res, req, headers|
         yield(res, req, headers ) if block_given?
         res
      end
      tor&.success!
      body = response.body
      logger.info "Completed #{response.try(:code)} OK in #{(Time.now-time).round(1)}s (Size: #{Utils.number_to_human_size(body.bytesize)})"
    rescue Exception => e
      tor.fail!(e)
      if tor
        logger.info "#{e.class}: #{e.message}, <Tor#(success: #{tor.counter.success}, fail: #{tor.counter.fail}, port: #{tor.port})>"
      end
      raise e
    end
  end
  case format.to_s
    when "html"    then Nokogiri::HTML(Utils.encode_html(body))
    when "json"    then Utils.to_json(body)
    when "string"  then body
  else
    raise InvalidFormat, format.to_s
  end
end

#restart(port) ⇒ Object



138
139
140
141
142
143
# File 'lib/rest_tor/tor.rb', line 138

def restart(port)
  lock("tor:#{port}:restart", expires: 1.minutes) do
    stop(port)
    listen(port)
  end
end

#stop(port) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rest_tor/tor.rb', line 100

def stop(port)
  logger.info "Stop tor port:#{port}"
  instance = store[port]
  if instance && instance.pid
    Process.kill("KILL", instance.pid)
  end
  FileUtils.rm_rf(TOR_DIR.join(port.to_s))
rescue Exception
  
ensure
  store.delete(port)
end

#storeObject



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

def store
  @store ||= Redis::HashKey.new('tor', marshal: true).tap { |s| s.send(:extend, Builder) }
end

#test(port) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rest_tor/tor.rb', line 151

def test(port)
  logger.info  "Testing tor #{port}"

  url = 'http://ip.plusor.cn/'

  req = RestClient::Request.execute({method: :get, url: url, proxy: "socks5://127.0.0.1:#{port}"})
  req.body.chomp.tap do |body|
    logger.info "  IP: #{body} "
  end
rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNREFUSED, SOCKSError, SOCKSError::TTLExpired, Errno::ECONNRESET => e
  logger.error "#{e.class}: #{e.message}"
  false
end

#unusedObject



182
183
184
# File 'lib/rest_tor/tor.rb', line 182

def unused
  store.select {|_, options| !options.using? }
end