proxyprotocol 0.0.2

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: 01ac713c25f1bebcb9c0587123404d74e1f7bfbc
4
+ data.tar.gz: 7b12f6133e9837b80489dcf494fe8a1f1edab73e
5
+ SHA512:
6
+ metadata.gz: 394d402fee3b672047a50061bcf90c4c2e74b9fe0305d36a56da27fda320d126ee150ee8ca3192fc11685d4e58247d2957c2bbf1f137a0aa02bba1681c13702f
7
+ data.tar.gz: 8a6a6d886a196eafc355ec568f68d1b7973ab5794c6835ed405f0465dc0b636f679a94dee82805ace25eb6d68a4233b21a8a4046825590424b685cd4024c1f28
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .#*
19
+ #*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in proxyprotocol.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,29 @@
1
+ Copyright (c) 2014, Heroku Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ * Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ * The names of its contributors may not be used to endorse or promote
16
+ products derived from this software without specific prior written
17
+ permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Proxyprotocol
2
+
3
+ A `Proxyprotocol::TCPSocket` that inherits `TCPSocket` to provide the
4
+ [proxy protocol](http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt)
5
+ header on connect.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'proxyprotocol'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install proxyprotocol
20
+
21
+ ## Usage
22
+
23
+ Use it like `TCPSocket`.
24
+
25
+ ``` ruby
26
+ socket = ProxyProtocol::TCPSocket.new(RemoteHost, RemotePort, SourceIp,
27
+ SourcePort, DestIp, DestPort)
28
+ ```
29
+
30
+ ## Run the tests
31
+
32
+ ``` bash
33
+ $ bundle exec rspec
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( http://github.com/omarkj/proxyprotocol/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ require "proxyprotocol/version"
2
+ require "proxyprotocol/tcpsocket"
3
+ require "proxyprotocol/socket"
4
+
5
+ module ProxyProtocol
6
+ end
@@ -0,0 +1,24 @@
1
+ require "ipaddr"
2
+
3
+ module ProxyProtocol
4
+ module Header
5
+ def proxy_protocol_header(source_ip, source_port, dest_ip, dest_port)
6
+ if [source_ip, source_port, dest_ip, dest_port].include?(nil)
7
+ "PROXY UNKNOWN\r\n"
8
+ else
9
+ source_ip = IPAddr.new(source_ip) unless source_ip.kind_of?(IPAddr)
10
+ dest_ip = IPAddr.new(dest_ip) unless dest_ip.kind_of?(IPAddr)
11
+ version = ip_version(source_ip, dest_ip)
12
+ "PROXY #{version} #{source_ip.to_s} #{dest_ip.to_s} #{source_port} #{dest_port}\r\n"
13
+ end
14
+ end
15
+
16
+ def ip_version(source_ip, dest_ip)
17
+ if source_ip.ipv4? and dest_ip.ipv4?
18
+ "TCP4"
19
+ elsif source_ip.ipv6? and dest_ip.ipv6?
20
+ "TCP6"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ require "socket"
2
+
3
+ module ProxyProtocol
4
+ class Socket < Socket
5
+ include Header
6
+ def initialize(domain, socktype, protocol=0, source_ip=nil, source_port=nil,
7
+ dest_ip=nil, dest_port=nil, local_host=nil, local_port=nil)
8
+ @header = proxy_protocol_header(source_ip, source_port, dest_ip,
9
+ dest_port)
10
+ super(domain, socktype, protocol=0)
11
+ end
12
+
13
+ def connect(remote_sockaddr)
14
+ super
15
+ write(@header)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ require "proxyprotocol/header"
2
+
3
+ module ProxyProtocol
4
+ class TCPSocket < TCPSocket
5
+ include Header
6
+ def initialize(remote_host, remote_port, source_ip=nil, source_port=nil,
7
+ dest_ip=nil, dest_port=nil, local_host=nil, local_port=nil)
8
+ super(remote_host, remote_port, local_host, local_port)
9
+ header = proxy_protocol_header(source_ip, source_port, dest_ip,
10
+ dest_port)
11
+ write(header)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module ProxyProtocol
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'proxyprotocol/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "proxyprotocol"
8
+ spec.version = ProxyProtocol::VERSION
9
+ spec.authors = ["Ómar Kjartan Yasin"]
10
+ spec.email = ["omarkj@heroku.com"]
11
+ spec.summary = "A wrapper around TCPSocket that support the Proxy Protocol"
12
+ spec.homepage = "https://github.com/omarkj/proxyprotocol"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,68 @@
1
+ require 'proxyprotocol'
2
+ require 'socket'
3
+
4
+ describe "ProxyProtocol::TCPSocket" do
5
+ it "should send the proxy protocol header via TCPSocket" do
6
+ server_socket = TCPServer.new("127.0.0.1", 0)
7
+ result = nil
8
+ thread = Thread.new do
9
+ client = server_socket.accept
10
+ result = client.gets
11
+ client.close
12
+ end
13
+ server_port = server_socket.addr[1]
14
+ socket = ProxyProtocol::TCPSocket.new("127.0.0.1", server_port,
15
+ "10.0.0.1", 1000,
16
+ "192.168.1.1", 80)
17
+ thread.join
18
+ expect(result).to eq("PROXY TCP4 10.0.0.1 192.168.1.1 1000 80\r\n")
19
+ end
20
+
21
+ it "should send unknown protocol header via TCPSocket" do
22
+ server_socket = TCPServer.new("127.0.0.1", 0)
23
+ result = nil
24
+ thread = Thread.new do
25
+ client = server_socket.accept
26
+ result = client.gets
27
+ client.close
28
+ end
29
+ server_port = server_socket.addr[1]
30
+ socket = ProxyProtocol::TCPSocket.new("127.0.0.1", server_port)
31
+ thread.join
32
+ expect(result).to eq("PROXY UNKNOWN\r\n")
33
+ end
34
+
35
+ it "should send the proxy protocol header via Socket" do
36
+ server_socket = TCPServer.new("127.0.0.1", 0)
37
+ result = nil
38
+ thread = Thread.new do
39
+ client = server_socket.accept
40
+ result = client.gets
41
+ client.close
42
+ end
43
+ server_port = server_socket.addr[1]
44
+ socket = ProxyProtocol::Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0,
45
+ "10.0.0.1", 1000, "192.168.1.1", 80)
46
+ sockaddr = ProxyProtocol::Socket.pack_sockaddr_in(server_port, "127.0.0.1")
47
+ socket.connect(sockaddr)
48
+ thread.join
49
+ expect(result).to eq("PROXY TCP4 10.0.0.1 192.168.1.1 1000 80\r\n")
50
+ end
51
+
52
+ it "should send the proxy protocol header via Socket" do
53
+ server_socket = TCPServer.new("127.0.0.1", 0)
54
+ result = nil
55
+ thread = Thread.new do
56
+ client = server_socket.accept
57
+ result = client.gets
58
+ client.close
59
+ end
60
+ server_port = server_socket.addr[1]
61
+ socket = ProxyProtocol::Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
62
+ sockaddr = ProxyProtocol::Socket.pack_sockaddr_in(server_port, "127.0.0.1")
63
+ socket.connect(sockaddr)
64
+ thread.join
65
+ expect(result).to eq("PROXY UNKNOWN\r\n")
66
+ end
67
+
68
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxyprotocol
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ómar Kjartan Yasin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-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.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:
56
+ email:
57
+ - omarkj@heroku.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/proxyprotocol.rb
68
+ - lib/proxyprotocol/header.rb
69
+ - lib/proxyprotocol/socket.rb
70
+ - lib/proxyprotocol/tcpsocket.rb
71
+ - lib/proxyprotocol/version.rb
72
+ - proxyprotocol.gemspec
73
+ - spec/proxyprotocol_spec.rb
74
+ homepage: https://github.com/omarkj/proxyprotocol
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A wrapper around TCPSocket that support the Proxy Protocol
98
+ test_files:
99
+ - spec/proxyprotocol_spec.rb