proxy_daemon 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5a416d6040e9373aa1122adcc4d8296aabb0a5a
4
- data.tar.gz: 71c900c16cc20eee03080fcf5dc753743fac1469
3
+ metadata.gz: d2b9074ac6f2540105b251e49fb530bfedf864e7
4
+ data.tar.gz: 814414fde598324b3a3b2a06c2bcd88b2744a340
5
5
  SHA512:
6
- metadata.gz: f150d04c4b701356fc10f768f504f20c3fdcea86750a39dc84160acec3f35f5f1190d06709990d6bb5d39b23d10c4f555a6be360b21c954f05f7d53021680d00
7
- data.tar.gz: 8b0098c2d990165b68cfe172c705dd4de560a1799b7e12ec4a06bf833c003a385c7251d955e3fb4e520b1ba47e22a452d609866d452a3c1ada59eb1041f83917
6
+ metadata.gz: 4d06f6fa08f208a4537406a82dd359a134b2a8822999636c5bb2e5a58a094f65a00df7135c4d57c162c320b00d909c5acd007f4235972c1bf59002c9e19a416c
7
+ data.tar.gz: 40c533a7e7930e23fd0b3bb14179bdc86704763878365441143b8062bac340e6ea00a491ee14ded9fb7abcc8c90c12b4afcadbf16db0b3368a377d3520c6a844
@@ -5,14 +5,17 @@ require 'colorize'
5
5
 
6
6
  module ProxyDaemon
7
7
  class Daemon
8
+ attr_accessor :list
9
+
8
10
  def initialize(script, options)
9
11
  @script = script
10
12
  @proxies = ((options[:proxies].map { |proxy| proxy.gsub /\s+/, ' ' }) || []).shuffle
11
13
  @urls = options[:urls] || []
12
- @workers = options[:workers] || 10
14
+ @workers = [(options[:workers] || 10), @urls.count].min
13
15
  @tries = options[:tries] || 4
14
16
 
15
17
  @threads = []
18
+ @list = {}
16
19
  @semaphore = Mutex.new
17
20
  end
18
21
 
@@ -37,6 +40,7 @@ module ProxyDaemon
37
40
  elsif answer == 'timeout' || answer == 'error'
38
41
  raise "Process: '#{answer}'"
39
42
  elsif Thread.current[:command] == :url
43
+ if (buf = answer.match(/^set (.+?)[\s]*:[\s]*?(.+)$/i)); @list[buf[1]] = buf[2] end
40
44
  log "Process: #{Thread.current[:url].cyan}: '#{answer.green}'", pipe
41
45
  else
42
46
  log "Answer: #{answer}".green, pipe
@@ -45,34 +49,44 @@ module ProxyDaemon
45
49
 
46
50
  def worker
47
51
  IO.popen("#{@script}", 'r+') { |p|
48
- proxy = getProxy
49
- log "Starting loop with new proxy: ".green + "#{(proxy || 'nil').yellow}", p
50
- command :proxy, p, proxy
52
+ if (@script == '-' && p.nil?) # child process
53
+ if @block.nil?
54
+ $stderr.puts "[#{Process.pid}]".magenta + ' Empty block for parsing content'.red
55
+ Kernel.exit!
56
+ end
57
+
58
+ worker = ProxyDaemon::Worker.new
59
+ worker.call(&@block)
60
+ else
61
+ proxy = getProxy
62
+ log "Starting loop with new proxy: ".green + "#{(proxy || 'nil').yellow}", p
63
+ command :proxy, p, proxy
51
64
 
52
- begin
53
- loop do
54
- sleep(0.1)
55
- url = getUrl
65
+ begin
66
+ loop do
67
+ sleep(0.1)
68
+ url = getUrl
56
69
 
57
- if url.nil?
58
- finished "Links are finished! exitting...".green, p
59
- command :exit, p
60
- break
61
- else
62
- log 'Urls count: ' + "#{@urls.length}".green + ", #{url.green}"
63
- Thread.current[:url] = url
64
- command :url, p, url
65
- listen p
70
+ if url.nil?
71
+ finished "Links are finished! exitting...".green, p
72
+ command :exit, p
73
+ break
74
+ else
75
+ log 'Urls count: ' + "#{@urls.length}".green + ", #{url.green}"
76
+ Thread.current[:url] = url
77
+ command :url, p, url
78
+ listen p
79
+ end
66
80
  end
67
- end
68
81
 
69
- log "Finishing loop".green, p
70
- rescue Exception => e
71
- @semaphore.synchronize {
72
- log "Exception in main: " + "#{e.message.red}, '#{Thread.current[:url]}'".red, p
73
- command :exit, p
74
- #puts e.backtrace
75
- }
82
+ log "Finishing loop".green, p
83
+ rescue Exception => e
84
+ @semaphore.synchronize {
85
+ log "Exception in main: " + "#{e.message.red}, '#{Thread.current[:url]}'".red, p
86
+ command :exit, p
87
+ #puts e.backtrace
88
+ }
89
+ end
76
90
  end
77
91
  }
78
92
 
@@ -83,10 +97,16 @@ module ProxyDaemon
83
97
  end
84
98
  end
85
99
 
86
- def start
87
- puts "[main] Starting " + "#{@workers}".yellow + " workers:"
88
- @workers.times { |i| @threads << Thread.new(&(->{worker})) }
89
- @threads.each { |t| t.join }
100
+ def start(&block)
101
+ @block = block if block_given?
102
+
103
+ begin
104
+ puts "[main] Starting " + "#{@workers}".yellow + " workers:"
105
+ @workers.times { |i| @threads << Thread.new(&(->{worker})) }
106
+ @threads.each { |t| t.join }
107
+ rescue Interrupt => e
108
+ puts "[main] Interrupted by user".yellow
109
+ end
90
110
  end
91
111
 
92
112
  private
@@ -1,3 +1,3 @@
1
1
  module ProxyDaemon
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -5,6 +5,8 @@ require 'openssl'
5
5
 
6
6
  module ProxyDaemon
7
7
  class Worker
8
+ attr_accessor :url
9
+
8
10
  def initialize
9
11
  @client = Net::HTTP.Proxy(nil, nil)
10
12
  @url = ''
@@ -17,7 +19,7 @@ module ProxyDaemon
17
19
  command = ($stdin.gets || String.new).strip
18
20
 
19
21
  if command.empty?
20
- log "Got empty answer by daemon, exiting...".yellow
22
+ log "Got empty answer from daemon, exiting...".yellow
21
23
  raise Timeout::Error
22
24
  end
23
25
  }
@@ -67,7 +69,11 @@ module ProxyDaemon
67
69
  raise Net::HTTPBadResponse
68
70
  end
69
71
 
70
- answer(parse(@page.body) ? 'ok' : 'error')
72
+ res = parse(@page.body)
73
+
74
+ if !!res == res || res.nil?; answer(res ? 'ok' : 'error')
75
+ elsif res.is_a? Array; answer("set #{res[0]}:#{res[1]}")
76
+ else; answer('error') end
71
77
  rescue Timeout::Error, Errno::ETIMEDOUT, Errno::ECONNREFUSED,
72
78
  Errno::EINVAL, Errno::ECONNRESET, Errno::ENETUNREACH, SocketError, EOFError,
73
79
  TypeError, Net::HTTPExceptions, Net::HTTPBadResponse, OpenSSL::SSL::SSLError => e
@@ -77,7 +83,7 @@ module ProxyDaemon
77
83
  log 'Interrupted by user, exiting...'.yellow
78
84
  Kernel.exit!
79
85
  rescue Exception => e
80
- log "rescue in #{'process'.yellow}: #{e.inspect}, #{e.backtrace.reverse.join.red}"
86
+ log "rescue in #{'process'.yellow}: #{e.inspect},\n#{e.backtrace.reverse.join("\n").red}"
81
87
  answer 'error'
82
88
  end
83
89
  end
@@ -91,10 +97,14 @@ module ProxyDaemon
91
97
  end
92
98
 
93
99
  def parse(body)
94
- raise NotImplementedError
100
+ raise NotImplementedError if @block.nil?
101
+
102
+ @block.call(self, body)
103
+ # instance_exec body, &@block
95
104
  end
96
105
 
97
- def call
106
+ def call(&block)
107
+ @block = block if block_given?
98
108
  proxy = nil
99
109
 
100
110
  loop do
@@ -119,7 +129,6 @@ module ProxyDaemon
119
129
  end
120
130
  end
121
131
 
122
- private
123
132
  def log(msg)
124
133
  $stderr.puts "[child #{Process.pid}]".magenta + " #{msg}"
125
134
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxy_daemon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michail Volkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-10 00:00:00.000000000 Z
11
+ date: 2016-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler