slowproxy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f7ea6418d70a1dd7b806e74b654902b6c62e175
4
+ data.tar.gz: dacd479083a700b4dbb63ba98559cb445688cbe4
5
+ SHA512:
6
+ metadata.gz: b0e6842cac9bf037cf1c836150a8509f27ac60fb4dbbe458242770675bb66b22085b7081603192e0da3977ac2b9279f313b2e59cc170df7e2f16bf8232d424bf
7
+ data.tar.gz: 22ff37cdb2b0941d2df6b64b047861f0c0f225df1ab9f59cf81cb185ab101d2fddeec2b45744c760cbcd4682b368bd2ab1c7330e95ef1d090a4947f0ecea14ab
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slowproxy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 labocho
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # slowproxy
2
+
3
+ HTTP proxy server that communicates origin server slowly to emulate slow client.
4
+ Do not access any server except managed by you.
5
+
6
+ ## Installation
7
+
8
+ Requires Ruby 2.0.0 or later.
9
+
10
+ $ gem install slowproxy
11
+
12
+ ## Usage
13
+
14
+ Run `slowproxy`.
15
+
16
+ $ slowproxy
17
+
18
+ And configure your application to use proxy server on `127.0.0.1:8989`.
19
+
20
+ You can set speed by argument (default: 128kbps).
21
+
22
+ $ slowproxy 1mbps
23
+
24
+ You can set listening port (default: 8989).
25
+
26
+ $ slowproxy --port 8080
27
+
28
+ Or view help.
29
+
30
+ $ slowproxy --help
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( http://github.com/labocho/slowproxy/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require "slowproxy"
3
+ Slowproxy::CLI.start(ARGV)
@@ -0,0 +1,7 @@
1
+ require "slowproxy/version"
2
+
3
+ module Slowproxy
4
+ require "slowproxy/cli"
5
+ require "slowproxy/server"
6
+ require "slowproxy/slow_buffered_io"
7
+ end
@@ -0,0 +1,56 @@
1
+ require "optparse"
2
+ module Slowproxy
3
+ class CLI
4
+ def self.start(argv)
5
+ new.run(argv)
6
+ end
7
+
8
+ def run(argv)
9
+ options = {
10
+ port: 8989,
11
+ bps: 128 * 1024,
12
+ debug: false,
13
+ }
14
+
15
+ OptionParser.new do |o|
16
+ o.banner = "Usage: #{$0} [options] [speed(g|m|k)[bps]]"
17
+ o.on("-p PORT", "--port=PORT", Integer){|i| options[:port] = i }
18
+ o.on("--debug", TrueClass){|b| options[:debug] = b }
19
+ o.parse!(argv)
20
+ options[:bps] = parse_bps(argv.first) unless argv.empty?
21
+ end
22
+
23
+ logger = WEBrick::Log::new(STDOUT, options[:debug] ? WEBrick::Log::DEBUG : WEBrick::Log::INFO)
24
+
25
+ server = Server.new(
26
+ Logger: logger,
27
+ Port: options[:port],
28
+ BPS: options[:bps],
29
+ )
30
+
31
+ Signal.trap('INT') do
32
+ server.shutdown
33
+ end
34
+
35
+ server.start
36
+ end
37
+
38
+ def parse_bps(str)
39
+ return unless str
40
+ case str.strip.downcase
41
+ when /\A(\d+)(g|m|k|)(bps)?\z/
42
+ num, order, * = $~.captures
43
+ num.to_i * case order
44
+ when ""
45
+ 1
46
+ when "k"
47
+ 1024
48
+ when "m"
49
+ 1024 ** 2
50
+ when "g"
51
+ 1024 ** 3
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,60 @@
1
+ require 'webrick/httpproxy'
2
+
3
+ module Slowproxy
4
+ class Server < WEBrick::HTTPProxyServer
5
+ def initialize(config, default = WEBrick::Config::HTTP)
6
+ @bps = config.delete(:BPS)
7
+ SlowBufferedIO.bps = @bps if @bps
8
+ super
9
+ logger.info "#{number_to_human_size(@bps)}bps"
10
+ SlowBufferedIO.logger = logger
11
+ end
12
+
13
+ def number_to_human_size(n)
14
+ suffixes = ["", "K", "M", "G"]
15
+ suffixes.each_with_index.to_a.reverse.each do |suffix, index|
16
+ one = 1024 ** index
17
+ return "#{n / one} #{suffix}" if n >= one
18
+ end
19
+ end
20
+
21
+ def perform_proxy_request(req, res)
22
+ uri = req.request_uri
23
+ path = uri.path.dup
24
+ path << "?" << uri.query if uri.query
25
+ header = setup_proxy_header(req, res)
26
+ upstream = setup_upstream_proxy_authentication(req, res, header)
27
+ response = nil
28
+
29
+ http = Net::HTTP.new(uri.host, uri.port, upstream.host, upstream.port)
30
+ http.start do
31
+ ########## prepend Net::SlowBufferedIO
32
+ http.instance_eval do
33
+ class << @socket
34
+ prepend Slowproxy::SlowBufferedIO
35
+ end
36
+ end
37
+ ########## /prepend Net::SlowBufferedIO
38
+ if @config[:ProxyTimeout]
39
+ ################################## these issues are
40
+ http.open_timeout = 30 # secs # necessary (maybe because
41
+ http.read_timeout = 60 # secs # Ruby's bug, but why?)
42
+ ##################################
43
+ end
44
+ response = yield(http, path, header)
45
+ end
46
+
47
+ # Persistent connection requirements are mysterious for me.
48
+ # So I will close the connection in every response.
49
+ res['proxy-connection'] = "close"
50
+ res['connection'] = "close"
51
+
52
+ # Convert Net::HTTP::HTTPResponse to WEBrick::HTTPResponse
53
+ res.status = response.code.to_i
54
+ choose_header(response, res)
55
+ set_cookie(response, res)
56
+ set_via(res)
57
+ res.body = response.body
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,52 @@
1
+ # Extension for Net::BufferedIO
2
+ module Slowproxy
3
+ module SlowBufferedIO
4
+ BUFSIZE = Net::BufferedIO::BUFSIZE # 1024 * 16
5
+
6
+ def self.bps=(bps)
7
+ @bps = bps
8
+ @wait = nil
9
+ end
10
+
11
+ def self.bps
12
+ @bps ||= 128 * 1024
13
+ end
14
+
15
+ def self.logger=(logger)
16
+ @logger = logger
17
+ end
18
+
19
+ def self.logger
20
+ @logger
21
+ end
22
+
23
+ def self.wait
24
+ @wait ||= 1 / ((bps / 8.0) / BUFSIZE)
25
+ end
26
+
27
+ def rbuf_fill
28
+ logger.info "wait for read (#{SlowBufferedIO.wait}s)" if logger
29
+ sleep SlowBufferedIO.wait
30
+ super
31
+ end
32
+
33
+ def write0(str)
34
+ if str.bytesize > BUFSIZE
35
+ logger.info "wait for write (#{str.bytesize * 8.0 / SlowBufferedIO.bps}s)" if logger
36
+ len = 0
37
+ str.each_byte.each_slice(BUFSIZE) do |bytes|
38
+ len += super(bytes.pack("C*"))
39
+ sleep SlowBufferedIO.wait
40
+ end
41
+ len
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ def logger
48
+ SlowBufferedIO.logger
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,3 @@
1
+ module Slowproxy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slowproxy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slowproxy"
8
+ spec.version = Slowproxy::VERSION
9
+ spec.authors = ["labocho"]
10
+ spec.email = ["labocho@penguinlab.jp"]
11
+ spec.summary = %q{HTTP proxy server that communicates origin server slowly to emulate slow client.}
12
+ spec.description = %q{HTTP proxy server that communicates origin server slowly to emulate slow client.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slowproxy do
4
+ it 'should have a version number' do
5
+ Slowproxy::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'should do something useful' do
9
+ false.should eq(true)
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'slowproxy'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slowproxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - labocho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-18 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: HTTP proxy server that communicates origin server slowly to emulate slow
56
+ client.
57
+ email:
58
+ - labocho@penguinlab.jp
59
+ executables:
60
+ - slowproxy
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/slowproxy
72
+ - lib/slowproxy.rb
73
+ - lib/slowproxy/cli.rb
74
+ - lib/slowproxy/server.rb
75
+ - lib/slowproxy/slow_buffered_io.rb
76
+ - lib/slowproxy/version.rb
77
+ - slowproxy.gemspec
78
+ - spec/slowproxy_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.2.0
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: HTTP proxy server that communicates origin server slowly to emulate slow
104
+ client.
105
+ test_files:
106
+ - spec/slowproxy_spec.rb
107
+ - spec/spec_helper.rb