proxy_daemon 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92ab7dba218ec3dc7b687a8539ccb1bc0639875a
4
+ data.tar.gz: fb9416fd34e1da07431a5b4060895ef72ed91a7b
5
+ SHA512:
6
+ metadata.gz: 9b14a8a2843644dc54116a941e6cdbb56ff3cb0671652cbdb2d148bccb13492a3763f24adebb73e801e9cccc3d5846783cb84fe43a837ee1ea7bee6fc0be9f24
7
+ data.tar.gz: bde510d4d99e0d1e009f8fc376293d46e63c64aa9a0fce3117db2d8fed1a49854248fe18be4536d9458a02f6d628c0020631b5cb15b6c3598be5cafe4e9cf265
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at xbiznet@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in proxy_daemon.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Michail
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # ProxyDaemon
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/proxy_daemon`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'proxy_daemon'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install proxy_daemon
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/proxy_daemon. 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.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "proxy_daemon"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,122 @@
1
+ require 'thread'
2
+ require 'timeout'
3
+ require 'json'
4
+ require 'colorize'
5
+
6
+ module ProxyDaemon
7
+ class Daemon
8
+ def initialize(script, options)
9
+ @script = script
10
+ @proxies = ((options[:proxies].map { |proxy| proxy.gsub /\s+/, ' ' }) || []).shuffle
11
+ @urls = options[:urls] || []
12
+ @workers = options[:workers] || 10
13
+ @tries = options[:tries] || 4
14
+
15
+ @threads = []
16
+ @semaphore = Mutex.new
17
+ end
18
+
19
+ def command(cmd, pipe, *params)
20
+ Thread.current[:command] = cmd
21
+ pipe.syswrite("#{cmd} #{params.join(' ')}\n")
22
+ end
23
+
24
+ def listen(pipe, try = 0)
25
+ answer = (pipe.sysread(30) || '').strip
26
+
27
+ if answer == 'proxy'
28
+ raise "Broken url: #{Thread.current[:url]}" if try > @tries
29
+
30
+ proxy = getProxy
31
+ log "Choosing new proxy: #{(proxy || 'nil').yellow}", pipe
32
+ command :proxy, pipe, proxy
33
+ command :url, pipe, Thread.current[:url]
34
+ listen pipe, try+1
35
+ elsif answer.empty?
36
+ raise "Empty answer from worker process"
37
+ elsif answer == 'timeout' || answer == 'error'
38
+ raise "Process: '#{answer}'"
39
+ elsif Thread.current[:command] == :url
40
+ log "Process: #{Thread.current[:url].cyan}: '#{answer.green}'", pipe
41
+ else
42
+ log "Answer: #{answer}".green, pipe
43
+ end
44
+ end
45
+
46
+ def worker
47
+ 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
51
+
52
+ begin
53
+ loop do
54
+ sleep(0.1)
55
+ url = getUrl
56
+
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
66
+ end
67
+ end
68
+
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
+ }
76
+ end
77
+ }
78
+
79
+ if @urls.length > 0
80
+ # @threads << Thread.new(&(->{worker}))
81
+ # @threads.last.join
82
+ worker
83
+ end
84
+ end
85
+
86
+ def start
87
+ puts "[main] Starting " + "#{@workers}".yellow + " workers:"
88
+ @workers.times { |i| @threads << Thread.new(&(->{worker})) }
89
+ @threads.each { |t| t.join }
90
+ end
91
+
92
+ private
93
+ def getProxy
94
+ proxy = String.new
95
+ @semaphore.synchronize {
96
+ proxy = @proxies.first
97
+ @proxies.rotate!
98
+ }
99
+
100
+ proxy
101
+ end
102
+
103
+ def getUrl
104
+ url = String.new
105
+ @semaphore.synchronize { url = @urls.shift }
106
+
107
+ url
108
+ end
109
+
110
+ def finished(msg, pipe = 0)
111
+ unless @finished
112
+ @finished = true
113
+ @semaphore.synchronize { log msg, pipe }
114
+ end
115
+ end
116
+
117
+ def log(msg, pipe = nil)
118
+ unless pipe.nil? then puts "[#{pipe.pid}]".blue + " #{msg}"
119
+ else puts msg end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,3 @@
1
+ module ProxyDaemon
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,127 @@
1
+ require 'colorize'
2
+ require 'timeout'
3
+ require 'net/http'
4
+ require 'openssl'
5
+
6
+ module ProxyDaemon
7
+ class Worker
8
+ def initialize
9
+ @client = Net::HTTP.Proxy(nil, nil)
10
+ @url = ''
11
+ end
12
+
13
+ def listen
14
+ begin
15
+ command = ''
16
+ Timeout::timeout(6) {
17
+ command = ($stdin.gets || String.new).strip
18
+
19
+ if command.empty?
20
+ log "Got empty answer by daemon, exiting...".yellow
21
+ raise Timeout::Error
22
+ end
23
+ }
24
+ rescue Timeout::Error
25
+ answer 'timeout'
26
+ Kernel.exit!
27
+ end
28
+
29
+ command
30
+ end
31
+
32
+ def answer(command)
33
+ begin
34
+ $stdout.puts "#{command}"
35
+ $stdout.flush
36
+ rescue Errno::EPIPE => e
37
+ log 'Broken pipe with daemon, exiting...'.yellow
38
+ Kernel.exit!
39
+ rescue => e
40
+ log e.inspect.red
41
+ end
42
+ end
43
+
44
+ def process(url)
45
+ begin
46
+ uri = URI(url)
47
+ Timeout::timeout(15) {
48
+ @client = Net::HTTP.new(uri.host, uri.port)
49
+ @client.use_ssl = (uri.scheme == 'https')
50
+ @client.verify_mode = OpenSSL::SSL::VERIFY_NONE if uri.scheme == 'https'
51
+ #@client.set_debug_output($stderr)
52
+
53
+ @client.start { |http|
54
+ req = Net::HTTP::Get.new(uri,
55
+ 'Connection' => 'keep-alive',
56
+ 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
57
+ 'Upgrade-Insecure-Requests' => '1',
58
+ 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.13 Safari/537.36',
59
+ 'Accept-Language' => 'en-US,en;q=0.8,ru;q=0.6'
60
+ )
61
+ @page = http.request(req)
62
+ }
63
+ }
64
+
65
+ if (!@page.is_a?(Net::HTTPOK) || @page.body.empty?)
66
+ log @page
67
+ raise Net::HTTPBadResponse
68
+ end
69
+
70
+ answer(parse(@page.body) ? 'ok' : 'error')
71
+ rescue Timeout::Error, Errno::ETIMEDOUT, Errno::ECONNREFUSED,
72
+ Errno::EINVAL, Errno::ECONNRESET, Errno::ENETUNREACH, SocketError, EOFError,
73
+ TypeError, Net::HTTPExceptions, Net::HTTPBadResponse, OpenSSL::SSL::SSLError => e
74
+ log "proxy".red + " in #{'process'.yellow}: #{e.inspect.red}"
75
+ answer 'proxy'
76
+ rescue Interrupt => e
77
+ log 'Interrupted by user, exiting...'.yellow
78
+ Kernel.exit!
79
+ rescue Exception => e
80
+ log "rescue in #{'process'.yellow}: #{e.inspect}, #{e.backtrace.reverse.join.red}"
81
+ answer 'error'
82
+ end
83
+ end
84
+
85
+ def changeProxy(proxy)
86
+ if proxy == 'localhost' || proxy.nil? || (proxy = proxy.split(/\s+/)).length < 2
87
+ ENV['http_proxy'] = nil
88
+ else
89
+ ENV['http_proxy'] = "http://#{proxy[0]}:#{proxy[1]}"
90
+ end
91
+ end
92
+
93
+ def parse(body)
94
+ raise NotImplementedError
95
+ end
96
+
97
+ def call
98
+ proxy = nil
99
+
100
+ loop do
101
+ begin
102
+ task = listen
103
+ case task
104
+ when /^proxy/
105
+ proxy = task.match(/^proxy\s*(.*)$/)[1]
106
+ changeProxy(proxy)
107
+ when /^url/
108
+ @url = task.match(/^url\s+(.+)$/)[1]
109
+ process(@url)
110
+ when /^exit/
111
+ exit!
112
+ end
113
+
114
+ #$stderr.puts "[child #{Process.pid}]".magenta + ' Task: ' + task.to_s.yellow
115
+ rescue => e
116
+ log "rescue in #{'call'.yellow}: #{e.inspect.red}"
117
+ answer 'error'
118
+ end
119
+ end
120
+ end
121
+
122
+ private
123
+ def log(msg)
124
+ $stderr.puts "[child #{Process.pid}]".magenta + " #{msg}"
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,7 @@
1
+ require 'proxy_daemon/version'
2
+ require 'proxy_daemon/daemon'
3
+ require 'proxy_daemon/worker'
4
+
5
+ module ProxyDaemon
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'proxy_daemon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'proxy_daemon'
8
+ spec.version = ProxyDaemon::VERSION
9
+ spec.authors = ['Michail Volkov']
10
+ spec.email = ['xbiznet@gmail.com']
11
+
12
+ spec.summary = %q{Simple daemon for grabbing sites via proxy servers.}
13
+ spec.description = %q{Daemon and base worker for grabbing site pages via list of proxies in parallel.}
14
+ spec.homepage = 'http://rubygems.org/gems/proxy-daemon'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'bin'
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+
25
+ spec.add_runtime_dependency 'colorize', '~> 0.7.7'
26
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxy_daemon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michail Volkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.7
55
+ description: Daemon and base worker for grabbing site pages via list of proxies in
56
+ parallel.
57
+ email:
58
+ - xbiznet@gmail.com
59
+ executables:
60
+ - console
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/proxy_daemon.rb
74
+ - lib/proxy_daemon/daemon.rb
75
+ - lib/proxy_daemon/version.rb
76
+ - lib/proxy_daemon/worker.rb
77
+ - proxy_daemon.gemspec
78
+ homepage: http://rubygems.org/gems/proxy-daemon
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple daemon for grabbing sites via proxy servers.
102
+ test_files: []
103
+ has_rdoc: