rest-tor 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 392136defab833843bd5a37a571a6e59e312ab10
4
- data.tar.gz: cdaeca6e88c8269015156c35964a2cfc38013f31
3
+ metadata.gz: 8a66c1d52d12d7e095cfd1c06e8c275468d93272
4
+ data.tar.gz: 03a50fdc74e6a84cfdf110005ede9eae7da3b9ae
5
5
  SHA512:
6
- metadata.gz: 6d7521af54efeedaffb4cb7e59e6276b77edea2a8042760d59fb5c5543c8712a6c0a46a923be76adc54ef4f9f6ea2cb61101b87a4cfd5c6ba89c8f1a48798a7d
7
- data.tar.gz: 04d56a5f7473453a56861a33448a1a0b23fb13f1b2c0d755cea19c2120f78fab09abf4db0fb0ab33322dab95b8f69f71bec30ef85b1c45c0f2d8bc7a3e4481d0
6
+ metadata.gz: f4ac758c7cf4f12df10741936f0a2d069e5b1d926652473aae3fde612c21676a47b8f41b3b3d7a30becd56c2c8f40620cbbdfae530ea569a05e181711ebcd1a6
7
+ data.tar.gz: aa2b087b9e731387f816535c59b9a8f9f8350d77667ba13efb8260d1776362bc8bde93db44af12468f219f30cd499338bf7d552d76eb733dbf9fe6cb17901740
@@ -2,4 +2,5 @@
2
2
 
3
3
  - Open tor without sleep
4
4
  - Tor.init use Multithreading
5
- - Tor.request allow pass custom proxy
5
+ - Tor.request allow pass custom proxy
6
+ - Tor.setup
data/README.md CHANGED
@@ -35,90 +35,117 @@ Or install it yourself as:
35
35
 
36
36
  ## Usage
37
37
 
38
+ #### SETUP
39
+
38
40
  ```ruby
39
- Tor.request(url: 'http://ip.plusor.cn/')
40
- => #<Nokogiri::HTML::Document:0x3fcd64ec6eb0 name="document" children=[#<Nokogiri::XML::DTD:0x3fcd64ec6af0 name="html">, #<Nokogiri::XML::Element:0x3fcd64ec67f8 name="html" children=[#<Nokogiri::XML::Element:0x3fcd64ec6618 name="body" children=[#<Nokogiri::XML::Element:0x3fcd64ec6438 name="p" children=[#<Nokogiri::XML::Text:0x3fcd64ec6258 "185.100.85.101\n">]>]>]>]>
41
-
42
- Tor.request(url: 'http://ip.plusor.cn/', raw: false)
43
- "64.113.32.29\n"
44
-
45
- Tor.request(url: 'http://ip.plusor.cn/', mobile: true) # RestClient.get "http://ip.plusor.cn/", "", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"0", "Content-Type"=>"application/x-www-form-urlencoded", "User-Agent"=>"ANDROID_KFZ_COM_2.0.9_M6 Note_7.1.2"
46
- Tor.request(url: 'http://ip.plusor.cn/') # RestClient.get "http://ip.plusor.cn/", "", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"0", "Content-Type"=>"application/x-www-form-urlencoded", "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"
47
-
48
- Tor.request(method: :post, url: '...', format: :json, mobile: true)
49
- => {'hello' => 'world'}
50
-
51
- Tor.request(url: '...', timeout: 10)
52
-
53
- Tor.request(url: '...', mode: :default) # Priority to use the highest number of successes, default is :default
54
- Tor.request(url: '...', mode: :order) # in order
55
-
56
-      # Customize an mode
57
- Tor::Dispatcher.register :custom do
58
- Tor.store.all.sort_by do |(port, tor)|
59
- tor.c_success
60
- end
61
- end
62
- Tor.request(url: '...', mode: :custom)
63
-
64
- # show all instances
65
- Tor.store.all
66
- => {"9001"=>#<Tor::Instance:0x007fb7798d2498 @port="9001", @ip="64.113.32.29", @using=nil, @counter=#<Counter success: 5, fail: 1, succss_at: 2017-12-15 19:52:44 +0800, fail_at:2017-12-15 19:52:26 +0800>>}
67
- # Clear instances
68
- Tor.clear
69
- # Stop instance
70
- Tor.stop(port)
71
-
72
- # proxy default is true
73
- Tor.request(url: 'http://ip.plusor.cn',raw: false, proxy: false)
74
- [2017-12-16 16:19:34.545] [95646-70129455200740] Started GET "http://ip.plusor.cn" (port:rest-client | mode:default)
75
- [2017-12-16 16:19:34.785] [95646-70129455200740] Completed 200 OK in 0.2s (Size: 15 Bytes)
76
- => "172.104.89.163\n"
77
-
78
- # Initialize tors
79
- Tor.init
80
- I, [2017-12-15T20:40:36.189835 #60422] INFO -- : Open tor with port:9001
81
- I, [2017-12-15T20:40:41.242452 #60422] INFO -- : Testing tor 9001
82
- I, [2017-12-15T20:40:42.881857 #60422] INFO -- : IP: 163.172.67.180
83
- I, [2017-12-15T20:40:42.882737 #60422] INFO -- : Open tor with port:9002
84
- I, [2017-12-15T20:40:47.931555 #60422] INFO -- : Testing tor 9002
85
- I, [2017-12-15T20:41:18.924004 #60422] INFO -- : IP: 155.4.230.97
86
- I, [2017-12-15T20:41:18.925154 #60422] INFO -- : Open tor with port:9003
87
- I, [2017-12-15T20:41:23.971647 #60422] INFO -- : Testing tor 9003
88
- I, [2017-12-15T20:41:46.111952 #60422] INFO -- : IP: 46.17.97.112
89
- I, [2017-12-15T20:41:46.112896 #60422] INFO -- : Open tor with port:9004
90
- I, [2017-12-15T20:41:51.164009 #60422] INFO -- : Testing tor 9004
91
- I, [2017-12-15T20:42:20.096998 #60422] INFO -- : IP: 109.201.133.100
92
- I, [2017-12-15T20:42:20.097992 #60422] INFO -- : Open tor with port:9005
93
- I, [2017-12-15T20:42:25.148808 #60422] INFO -- : Testing tor 9005
94
- I, [2017-12-15T20:43:01.699053 #60422] INFO -- : IP: 62.210.129.246
95
- I, [2017-12-15T20:43:01.700151 #60422] INFO -- : Open tor with port:9006
96
- I, [2017-12-15T20:43:06.756239 #60422] INFO -- : Testing tor 9006
97
- I, [2017-12-15T20:43:39.118469 #60422] INFO -- : IP: 163.172.160.182
98
- I, [2017-12-15T20:43:39.119310 #60422] INFO -- : Open tor with port:9007
99
- I, [2017-12-15T20:43:44.194975 #60422] INFO -- : Testing tor 9007
100
- I, [2017-12-15T20:44:23.526274 #60422] INFO -- : IP: 89.144.12.14
101
- I, [2017-12-15T20:44:23.527287 #60422] INFO -- : Open tor with port:9008
102
- I, [2017-12-15T20:44:28.575018 #60422] INFO -- : Testing tor 9008
103
- I, [2017-12-15T20:44:51.746405 #60422] INFO -- : IP: 195.22.126.147
104
- I, [2017-12-15T20:44:51.747258 #60422] INFO -- : Open tor with port:9009
105
- I, [2017-12-15T20:44:56.793446 #60422] INFO -- : Testing tor 9009
106
- I, [2017-12-15T20:45:32.469774 #60422] INFO -- : IP: 62.210.129.246
107
- I, [2017-12-15T20:45:32.470560 #60422] INFO -- : Open tor with port:9010
108
- I, [2017-12-15T20:45:37.528114 #60422] INFO -- : Testing tor 9010
109
- I, [2017-12-15T20:46:37.533066 #60422] INFO -- : Stop tor port:9010
110
- I, [2017-12-15T20:46:37.533927 #60422] INFO -- : RestClient::Exceptions::OpenTimeout:Timed out connecting to server
111
- I, [2017-12-15T20:46:37.534015 #60422] INFO -- : Open tor with port:9010
112
- I, [2017-12-15T20:46:42.588824 #60422] INFO -- : Testing tor 9010
113
- I, [2017-12-15T20:47:02.179464 #60422] INFO -- : IP: 173.254.216.66
41
+ # Default setup
42
+ Tor.setup do
43
+ config.count = 10, # Init count
44
+ config.port = 9000, # Listen port start with 9000
45
+ config.dir = Pathname.new('/tmp/tor'), #
46
+ config.ipApi = 'http://ip.plusor.cn/', # get tor ip
47
+ config.ipParser = -> (body) { body[/\d{,3}\.\d{,3}\.\d{,3}\.\d{,3}/] }, # get tor ip
48
+ config.command = -> (port) { "tor --RunAsDaemon 1 --CookieAuthentication 0 --HashedControlPassword \"\" --ControlPort auto --PidFile #{Tor.dir(port)}/tor.pid --SocksPort #{port} --DataDirectory #{Tor.dir(port)} --CircuitBuildTimeout 5 --KeepalivePeriod 60 --NewCircuitPeriod 15 --NumEntryGuards 8 --quiet" }
49
+ end
50
+ ```
51
+
52
+ #### Tor.request Options
53
+ - `method` | Default get
54
+ - `url`. Request url
55
+ - `payload`. Request data
56
+ - `header` or `headers`. Default: `Tor::USER_AGENT`)
57
+ - `mobile`. Default is false. If true, use the Mobile-side User-Agent
58
+ - `proxy` Default use tor proxy. IF false, do not use proxy. You can specify proxy as `host:port`.
59
+ - `raw` Default true. The result is Nokogiri::HTML(req.body). If false, then req.body
60
+ - `format`. Default `html`. Avaliable formats: `html`, `json`, `string`.
61
+ - `mode` Default is `:default`.
62
+ - `timeout` Request timeout
114
63
 
64
+
65
+ ```ruby
66
+ Tor.request(url: 'http://ip.plusor.cn/')
67
+ => #<Nokogiri::HTML::Document:0x3fcd64ec6eb0 name="document" children=[#<Nokogiri::XML::DTD:0x3fcd64ec6af0 name="html">, #<Nokogiri::XML::Element:0x3fcd64ec67f8 name="html" children=[#<Nokogiri::XML::Element:0x3fcd64ec6618 name="body" children=[#<Nokogiri::XML::Element:0x3fcd64ec6438 name="p" children=[#<Nokogiri::XML::Text:0x3fcd64ec6258 "185.100.85.101\n">]>]>]>]>
68
+
69
+ Tor.request(url: 'http://ip.plusor.cn/', raw: false)
70
+ "64.113.32.29\n"
71
+
72
+ Tor.request(url: 'http://ip.plusor.cn/', mobile: true) # ..."User-Agent"=>"ANDROID_KFZ_COM_2.0.9_M6 Note_7.1.2"
73
+ Tor.request(url: 'http://ip.plusor.cn/') # ..."User-Agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko)
74
+
75
+ Tor.request(method: :post, url: '...', format: :json, mobile: true)
76
+ => {'hello' => 'world'}
77
+
78
+ Tor.request(url: '...', timeout: 10)
79
+
80
+ Tor.request(url: '...', mode: :default) # Priority to use the highest number of successes, default is :default
81
+ Tor.request(url: '...', mode: :order) # in order
82
+
83
+      # Customize an mode
84
+ Tor::Dispatcher.register :custom do
85
+ Tor.store.all.sort_by do |(port, tor)|
86
+ tor.c_success
87
+ end
88
+ end
89
+ Tor.request(url: '...', mode: :custom)
90
+
91
+ # show all instances
92
+ Tor.store.all
93
+ => {"9001"=>#<Tor::Instance:0x007fb7798d2498 @port="9001", @ip="64.113.32.29", @using=nil, @counter=#<Counter success: 5, fail: 1, succss_at: 2017-12-15 19:52:44 +0800, fail_at:2017-12-15 19:52:26 +0800>>}
94
+ # Clear instances
95
+ Tor.clear
96
+ # Stop instance
97
+ Tor.stop(port)
98
+
99
+ # proxy default is true
100
+ Tor.request(url: 'http://ip.plusor.cn',raw: false, proxy: false)
101
+ [2017-12-16 16:19:34.545] [95646-70129455200740] Started GET "http://ip.plusor.cn" (port:rest-client | mode:default)
102
+ [2017-12-16 16:19:34.785] [95646-70129455200740] Completed 200 OK in 0.2s (Size: 15 Bytes)
103
+ => "172.104.89.163\n"
104
+
105
+ # Initialize tors
106
+ Tor.init
107
+ I, [2017-12-15T20:40:36.189835 #60422] INFO -- : Open tor with port:9001
108
+ I, [2017-12-15T20:40:41.242452 #60422] INFO -- : Testing tor 9001
109
+ I, [2017-12-15T20:40:42.881857 #60422] INFO -- : IP: 163.172.67.180
110
+ I, [2017-12-15T20:40:42.882737 #60422] INFO -- : Open tor with port:9002
111
+ I, [2017-12-15T20:40:47.931555 #60422] INFO -- : Testing tor 9002
112
+ I, [2017-12-15T20:41:18.924004 #60422] INFO -- : IP: 155.4.230.97
113
+ I, [2017-12-15T20:41:18.925154 #60422] INFO -- : Open tor with port:9003
114
+ I, [2017-12-15T20:41:23.971647 #60422] INFO -- : Testing tor 9003
115
+ I, [2017-12-15T20:41:46.111952 #60422] INFO -- : IP: 46.17.97.112
116
+ I, [2017-12-15T20:41:46.112896 #60422] INFO -- : Open tor with port:9004
117
+ I, [2017-12-15T20:41:51.164009 #60422] INFO -- : Testing tor 9004
118
+ I, [2017-12-15T20:42:20.096998 #60422] INFO -- : IP: 109.201.133.100
119
+ I, [2017-12-15T20:42:20.097992 #60422] INFO -- : Open tor with port:9005
120
+ I, [2017-12-15T20:42:25.148808 #60422] INFO -- : Testing tor 9005
121
+ I, [2017-12-15T20:43:01.699053 #60422] INFO -- : IP: 62.210.129.246
122
+ I, [2017-12-15T20:43:01.700151 #60422] INFO -- : Open tor with port:9006
123
+ I, [2017-12-15T20:43:06.756239 #60422] INFO -- : Testing tor 9006
124
+ I, [2017-12-15T20:43:39.118469 #60422] INFO -- : IP: 163.172.160.182
125
+ I, [2017-12-15T20:43:39.119310 #60422] INFO -- : Open tor with port:9007
126
+ I, [2017-12-15T20:43:44.194975 #60422] INFO -- : Testing tor 9007
127
+ I, [2017-12-15T20:44:23.526274 #60422] INFO -- : IP: 89.144.12.14
128
+ I, [2017-12-15T20:44:23.527287 #60422] INFO -- : Open tor with port:9008
129
+ I, [2017-12-15T20:44:28.575018 #60422] INFO -- : Testing tor 9008
130
+ I, [2017-12-15T20:44:51.746405 #60422] INFO -- : IP: 195.22.126.147
131
+ I, [2017-12-15T20:44:51.747258 #60422] INFO -- : Open tor with port:9009
132
+ I, [2017-12-15T20:44:56.793446 #60422] INFO -- : Testing tor 9009
133
+ I, [2017-12-15T20:45:32.469774 #60422] INFO -- : IP: 62.210.129.246
134
+ I, [2017-12-15T20:45:32.470560 #60422] INFO -- : Open tor with port:9010
135
+ I, [2017-12-15T20:45:37.528114 #60422] INFO -- : Testing tor 9010
136
+ I, [2017-12-15T20:46:37.533066 #60422] INFO -- : Stop tor port:9010
137
+ I, [2017-12-15T20:46:37.533927 #60422] INFO -- : RestClient::Exceptions::OpenTimeout:Timed out connecting to server
138
+ I, [2017-12-15T20:46:37.534015 #60422] INFO -- : Open tor with port:9010
139
+ I, [2017-12-15T20:46:42.588824 #60422] INFO -- : Testing tor 9010
140
+ I, [2017-12-15T20:47:02.179464 #60422] INFO -- : IP: 173.254.216.66
115
141
  ```
116
142
 
117
143
  ## TODO
118
144
 
119
145
  - Test
120
146
  - Optimization code
121
- - Add config
147
+ - Configurable http client
148
+ - Add chrome headless
122
149
  - Configurable restart strategy
123
150
 
124
151
  ## Development
@@ -129,7 +156,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
129
156
 
130
157
  ## Contributing
131
158
 
132
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rest-tor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
159
+ Bug reports and pull requests are welcome on GitHub at https://github.com/plusor/rest-tor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
133
160
 
134
161
 
135
162
  ## License
@@ -7,10 +7,22 @@ Dir.glob(File.expand_path('../rest_tor/**/*.rb', __FILE__)).each { |file| requir
7
7
 
8
8
  module Tor
9
9
  def self.logger
10
- Logger.new(STDOUT).tap do |log|
10
+ @logger ||= Logger.new(STDOUT).tap do |log|
11
11
  log.formatter = proc do |severity, datetime, progname, msg|
12
12
  "[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%3N')}] [#{Process.pid}-#{Thread.current.object_id}] #{msg}\n"
13
13
  end
14
14
  end
15
15
  end
16
+
17
+ def self.logger=(log)
18
+ @logger = log
19
+ end
20
+
21
+ def self.setup(&block)
22
+ instance_exec(&block)
23
+ end
24
+
25
+ def self.config
26
+ @config ||= Configuration.new
27
+ end
16
28
  end
@@ -0,0 +1,24 @@
1
+ module Tor
2
+ class Configuration
3
+ def initialize
4
+ @config = {
5
+ count: 10, # Init count
6
+ port: 9000, # Listen port start with 9000
7
+ dir: Pathname.new('/tmp/tor'), #
8
+ ipApi: 'http://ip.plusor.cn/',
9
+ ipParser: ->(body) { body[/\d{,3}\.\d{,3}\.\d{,3}\.\d{,3}/] },
10
+ command: -> (port) { "tor --RunAsDaemon 1 --CookieAuthentication 0 --HashedControlPassword \"\" --ControlPort auto --PidFile #{Tor.dir(port)}/tor.pid --SocksPort #{port} --DataDirectory #{Tor.dir(port)} --CircuitBuildTimeout 5 --KeepalivePeriod 60 --NewCircuitPeriod 15 --NumEntryGuards 8 --quiet" }
11
+ }
12
+ end
13
+
14
+ def method_missing(method_id, *args, &block)
15
+ if /(?<name>.*?)=$/ =~ method_id.to_s
16
+ @config[:"#{name}"] = args.first
17
+ elsif @config.key?(:"#{method_id}")
18
+ @config[:"#{method_id}"]
19
+ else
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
@@ -12,7 +12,7 @@ module Tor
12
12
  Tor.lock("tor:pick", expires: 10) do
13
13
  port, tor = run(mode)
14
14
  if port.blank? || tor.blank?
15
- port, _ = Tor.store.max {|a,b | a[0] <=> b[0] } || Tor::TOR_PORT_START_WITH
15
+ port, _ = Tor.store.max {|a,b | a[0] <=> b[0] } || Tor.config.port
16
16
  tor = Tor.listen(port=port.next)
17
17
  end
18
18
  return [port, tor]
@@ -6,7 +6,7 @@ module Tor
6
6
  include Strategy::Restart
7
7
  extend Forwardable
8
8
 
9
- ATTRS = %i(ip port using counter)
9
+ ATTRS = %i(ip port using counter created_at)
10
10
 
11
11
  attr_accessor *ATTRS
12
12
 
@@ -14,22 +14,23 @@ module Tor
14
14
  def_delegator :@counter, :success, :c_success
15
15
  def_delegator :@counter, :fail, :c_fail
16
16
 
17
- def initialize(port, ip: nil, using: nil, counter: nil)
18
- @port = port
19
- @ip = ip
20
- @using = using
21
- @counter= Counter.new(self, counter || {})
17
+ def initialize(port, ip: nil, using: nil, counter: nil, created_at: Time.now)
18
+ @port = port
19
+ @ip = ip
20
+ @using = using
21
+ @counter = Counter.new(self, counter || {})
22
+ @created_at = created_at
22
23
  end
23
24
 
24
25
  def pid
25
- path = Tor::TOR_DIR.join("#{port}/tor.pid")
26
+ path = Tor.config.dir.join("#{port}/tor.pid")
26
27
  if File.exists?(path)
27
28
  File.read(path).chomp.to_i
28
29
  end
29
30
  end
30
31
 
31
32
  def attributes
32
- { ip: @ip, port: @port, using: @using, counter: @counter.to_h }
33
+ { ip: @ip, port: @port, using: @using, counter: @counter.to_h, created_at: @created_at }
33
34
  end
34
35
 
35
36
  ATTRS.each do |name|
@@ -48,7 +49,7 @@ module Tor
48
49
  self.using = nil
49
50
 
50
51
  if Tor.locked?("#{port}:restart")
51
- logger.info "The tor(#{port}) already processing!"
52
+ Tor.logger.info "The tor(#{port}) already processing!"
52
53
  else
53
54
  if error=died?
54
55
  restart!("Died(#{error})")
@@ -58,7 +59,7 @@ module Tor
58
59
  end
59
60
 
60
61
  def restart!(message="")
61
- logger.info "#{message} Restart => #{ip}:#{port}"
62
+ Tor.logger.info "#{message} Restart => #{ip}:#{port}"
62
63
  Tor.restart(port)
63
64
  end
64
65
 
@@ -79,7 +80,7 @@ module Tor
79
80
  def apply(&block)
80
81
  Tor.lock("tor:#{port}:update", expires: 1.minutes) do
81
82
  if not Tor.store.has_key?(port)
82
- logger.info "Has been destroyed"
83
+ Tor.logger.info "Has been destroyed"
83
84
  return
84
85
  end
85
86
  if block_given?
@@ -4,7 +4,7 @@ module Tor
4
4
  DEFAULT_EXCEPTION_COUNT = 50
5
5
  EXCEPTIONS = {
6
6
  ::Net::OpenTimeout => 20,
7
- ::Errno::ECONNREFUSED => 50,
7
+ ::Errno::ECONNREFUSED => 1,
8
8
  ::RestClient::Exceptions::OpenTimeout => 20
9
9
  }
10
10
 
@@ -8,14 +8,11 @@ module Tor extend self
8
8
  class InvalidFormat < Error; end
9
9
  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'
10
10
  MOBILE_AGENT = 'ANDROID_KFZ_COM_2.0.9_M6 Note_7.1.2'
11
- TOR_COUNT = 10
12
- TOR_PORT_START_WITH = 9000
13
- TOR_DIR = Pathname.new('/tmp/tor')
14
11
 
15
12
  def init
16
13
  lock("tor:init", expires: 1.minutes) do
17
14
  threads = []
18
- TOR_COUNT.times { |i| threads << Thread.new { listen(TOR_PORT_START_WITH + i + 1) } }
15
+ config.count.times { |i| threads << Thread.new { listen(config.port + i + 1) } }
19
16
  threads.map(&:join)
20
17
  end
21
18
  end
@@ -46,6 +43,7 @@ module Tor extend self
46
43
  url = options[:url]
47
44
  mobile = options[:mobile]
48
45
  proxy = options[:proxy]
46
+ proxy = nil if proxy == true
49
47
  raw = options[:raw].nil? ? true : false
50
48
  mode = options[:mode] || :default
51
49
  method = options[:method] || :get
@@ -55,9 +53,10 @@ module Tor extend self
55
53
  headers = options[:headers] || options[:header] || {}
56
54
  default_header = { 'User-Agent' => mobile ? MOBILE_AGENT : USER_AGENT }
57
55
  time, body = Time.now, nil
58
- rest = proxy == false
56
+ rest = proxy != nil
59
57
 
60
58
  hold_tor(mode: mode, rest: rest) do |port, tor|
59
+ Thread.current[:tor] = tor if tor.present?
61
60
 
62
61
  proxy ||= "socks5://127.0.0.1:#{port}" if not rest
63
62
 
@@ -74,15 +73,19 @@ module Tor extend self
74
73
 
75
74
  begin
76
75
  response = RestClient::Request.execute(params) do |res, req, headers|
77
- yield(res, req, headers ) if block_given?
78
- res
76
+ if res.code == 302
77
+ res.follow_redirection
78
+ else
79
+ yield(res, req, headers ) if block_given?
80
+ res
81
+ end
79
82
  end
80
83
  tor&.success!
81
84
  body = response.body
82
- logger.info "Completed #{response.try(:code)} OK in #{(Time.now-time).round(1)}s (Size: #{Utils.number_to_human_size(body.bytesize)})"
85
+ logger.info "Completed #{response.try(:code)} OK in #{(Time.now-time).round(1)}s (size: #{Utils.number_to_human_size(body.bytesize)})"
83
86
  rescue Exception => e
84
- tor.fail!(e)
85
87
  if tor
88
+ tor.fail!(e)
86
89
  logger.info "#{e.class}: #{e.message}, <Tor#(success: #{tor.counter.success}, fail: #{tor.counter.fail}, port: #{tor.port})>"
87
90
  end
88
91
  raise e
@@ -103,7 +106,7 @@ module Tor extend self
103
106
  if instance && instance.pid
104
107
  Process.kill("KILL", instance.pid)
105
108
  end
106
- FileUtils.rm_rf(TOR_DIR.join(port.to_s))
109
+ FileUtils.rm_rf dir(port)
107
110
  rescue Exception
108
111
 
109
112
  ensure
@@ -115,13 +118,8 @@ module Tor extend self
115
118
 
116
119
  logger.info "Open tor with port:#{port}"
117
120
 
118
- control_port = 6000 + port.to_i
121
+ system config.command.call(port)
119
122
 
120
- tor = 'tor --RunAsDaemon 1 --CookieAuthentication 0 --HashedControlPassword ""'
121
- tor+= " --ControlPort #{ control_port } --PidFile tor.pid --SocksPort #{port} --DataDirectory #{dir(port)}"
122
- tor+= " --CircuitBuildTimeout 5 --KeepalivePeriod 60 --NewCircuitPeriod 15 --NumEntryGuards 8"# make tor faster
123
- tor+= " --quiet" # unless Rails.env.production?
124
- system tor
125
123
  if ip=test(port)
126
124
  store.insert(port, ip)
127
125
  else
@@ -143,7 +141,7 @@ module Tor extend self
143
141
  end
144
142
 
145
143
  def dir(port)
146
- TOR_DIR.join("#{port}").tap do |dir|
144
+ config.dir.join("#{port}").tap do |dir|
147
145
  FileUtils.mkpath(dir) if not Dir.exists?(dir)
148
146
  end
149
147
  end
@@ -151,11 +149,9 @@ module Tor extend self
151
149
  def test(port)
152
150
  logger.info "Testing tor #{port}"
153
151
 
154
- url = 'http://ip.plusor.cn/'
155
-
156
- req = RestClient::Request.execute({method: :get, url: url, proxy: "socks5://127.0.0.1:#{port}"})
157
- req.body.chomp.tap do |body|
158
- logger.info " IP: #{body} "
152
+ req = RestClient::Request.execute({method: :get, url: config.ipApi, proxy: "socks5://127.0.0.1:#{port}"})
153
+ config.ipParser.call(req.body).tap do |ip|
154
+ logger.info " IP: #{ip} "
159
155
  end
160
156
  rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNREFUSED, SOCKSError, SOCKSError::TTLExpired, Errno::ECONNRESET => e
161
157
  logger.error "#{e.class}: #{e.message}"
@@ -163,20 +159,20 @@ module Tor extend self
163
159
  end
164
160
 
165
161
  def clear
166
- Dir.glob(TOR_DIR.join("**/*.pid")).each do |path|
162
+ Dir.glob(config.dir.join("**/*.pid")).each do |path|
167
163
  begin
168
164
  Process.kill("KILL", File.read(path).chomp.to_i)
169
165
  rescue Errno::ESRCH
170
166
  end
171
167
  end
172
- FileUtils.rm_rf(TOR_DIR)
168
+ FileUtils.rm_rf(config.dir)
173
169
  true
174
170
  ensure
175
171
  store.clear
176
172
  end
177
173
 
178
174
  def count
179
- Dir.glob(TOR_DIR.join("*")).count
175
+ Dir.glob(config.dir.join("*")).count
180
176
  end
181
177
 
182
178
  def unused
@@ -202,7 +198,7 @@ module Tor extend self
202
198
  private
203
199
  def safe_value(value)
204
200
  counter = value.try(:[], :counter) || {}
205
- value.slice(*[:ip, :using]).merge({
201
+ value.slice(*[:ip, :using, :created_at]).merge({
206
202
  counter: counter.slice(*[:success, :fail, :success_at, :fail_at, :errors])
207
203
  })
208
204
  rescue NoMethodError => e
@@ -3,7 +3,7 @@ module Tor
3
3
  def to_json(body)
4
4
  JSON.parse(body)
5
5
  rescue Exception => e
6
- logger.info "#{e.class}:#{e.message}"
6
+ Tor.logger.info "#{e.class}:#{e.message} (Tor.request)"
7
7
  {}
8
8
  end
9
9
 
@@ -18,7 +18,7 @@ module Tor
18
18
  begin
19
19
  body = body.dup.force_encoding(encode).encode("utf-8", invalid: :replace, under: :replace)
20
20
  rescue Exception => e
21
- logger.info "#{e.class}:#{e.message}(Tor.request)"
21
+ Tor.logger.info "#{e.class}:#{e.message} (Tor.request)"
22
22
  end
23
23
  end
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module Tor
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-tor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - plusor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-20 00:00:00.000000000 Z
11
+ date: 2018-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -140,6 +140,7 @@ files:
140
140
  - bin/console
141
141
  - bin/setup
142
142
  - lib/rest-tor.rb
143
+ - lib/rest_tor/configuration.rb
143
144
  - lib/rest_tor/dispatcher.rb
144
145
  - lib/rest_tor/instance.rb
145
146
  - lib/rest_tor/lock.rb