multiplex 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +13 -0
- data/README.md +38 -0
- data/Rakefile +11 -0
- data/lib/multiplex.rb +38 -0
- data/lib/multiplex/version.rb +3 -0
- data/multiplex.gemspec +21 -0
- data/test/multiplex_test.rb +56 -0
- metadata +65 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
2
|
+
Version 2, December 2004
|
3
|
+
|
4
|
+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
5
|
+
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
7
|
+
copies of this license document, and changing it is allowed as long
|
8
|
+
as the name is changed.
|
9
|
+
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
12
|
+
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
Multiplex
|
2
|
+
=========
|
3
|
+
|
4
|
+
Multiplex lets you bind Net::HTTP requests to non-default local IP
|
5
|
+
addresses. If you are craving cURL's interface option, this is what
|
6
|
+
you need.
|
7
|
+
|
8
|
+
[gatling gun](http://upload.wikimedia.org/wikipedia/commons/6/6c/Gatling_gun.jpg)
|
9
|
+
|
10
|
+
Usage
|
11
|
+
-----
|
12
|
+
|
13
|
+
Say the IP of your default interface is 10.1.1.2 and you have a
|
14
|
+
second interface bound to 10.1.1.3.
|
15
|
+
|
16
|
+
require 'multiplex'
|
17
|
+
uri = URI.parse('http://jsonip.com')
|
18
|
+
|
19
|
+
Net::HTTP.bind 10.1.1.3 do
|
20
|
+
res = Net::HTTP.get_response(uri)
|
21
|
+
puts JSON.parse(res.body)['ip'] # 10.1.1.3
|
22
|
+
end
|
23
|
+
|
24
|
+
res = Net::HTTP.get_response(uri)
|
25
|
+
puts JSON.parse(res.body)['ip'] # 10.1.1.2
|
26
|
+
|
27
|
+
Alternatively, you can bind without a block and then unbind
|
28
|
+
manually:
|
29
|
+
|
30
|
+
Net::HTTP.bind 10.1.1.3
|
31
|
+
|
32
|
+
res = Net::HTTP.get_response(uri)
|
33
|
+
puts JSON.parse(res.body)['ip'] # 10.1.1.3
|
34
|
+
|
35
|
+
Net::HTTP.unbind
|
36
|
+
|
37
|
+
res = Net::HTTP.get_response(uri)
|
38
|
+
puts JSON.parse(res.body)['ip'] # 10.1.1.2
|
data/Rakefile
ADDED
data/lib/multiplex.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module Multiplex
|
4
|
+
def bind(local_ip)
|
5
|
+
TCPSocket.instance_eval do
|
6
|
+
(class << self; self; end).instance_eval do
|
7
|
+
alias_method :original_open, :open
|
8
|
+
|
9
|
+
define_method(:open) do |conn_address, conn_port|
|
10
|
+
original_open(conn_address, conn_port, local_ip)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
if block_given?
|
16
|
+
begin
|
17
|
+
yield
|
18
|
+
ensure
|
19
|
+
unbind
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def unbind
|
25
|
+
TCPSocket.instance_eval do
|
26
|
+
(class << self; self; end).instance_eval do
|
27
|
+
alias_method :open, :original_open
|
28
|
+
remove_method :original_open
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module Net
|
35
|
+
class HTTP
|
36
|
+
extend Multiplex
|
37
|
+
end
|
38
|
+
end
|
data/multiplex.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "multiplex/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "multiplex"
|
7
|
+
s.version = Multiplex::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Paper Cavalier"]
|
10
|
+
s.email = ["code@papercavalier.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/multiplex"
|
12
|
+
s.summary = %q{Bind Net::HTTP requests to non-default local IPs.}
|
13
|
+
s.description = %q{Multiplex lets you bind Net::HTTP requests to non-default local IP addresses.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "multiplex"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'json'
|
5
|
+
require 'multiplex'
|
6
|
+
|
7
|
+
class TestMultiplex < Test::Unit::TestCase
|
8
|
+
def find_ip
|
9
|
+
uri = URI.parse('http://jsonip.com')
|
10
|
+
res = Net::HTTP.start(uri.host, uri.port) do |http|
|
11
|
+
http.get('/')
|
12
|
+
end
|
13
|
+
|
14
|
+
JSON.parse(res.body)['ip']
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup
|
18
|
+
unless defined?(@@old_ip)
|
19
|
+
@@old_ip = find_ip
|
20
|
+
print "Change #{@@old_ip} to: "
|
21
|
+
@@new_ip = STDIN.gets.chomp
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def teardown
|
26
|
+
Net::HTTP.unbind if TCPSocket.respond_to? :original_open
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_bind_without_block
|
30
|
+
Net::HTTP.bind(@@new_ip)
|
31
|
+
assert_equal @@new_ip, find_ip
|
32
|
+
|
33
|
+
Net::HTTP.unbind
|
34
|
+
assert_equal @@old_ip, find_ip
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_bind_with_block
|
38
|
+
Net::HTTP.bind(@@new_ip) do
|
39
|
+
assert_equal @@new_ip, find_ip
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal @@old_ip, find_ip
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_do_not_unbind_when_bind_not_given_block
|
46
|
+
Net::HTTP.bind(@@new_ip)
|
47
|
+
assert_respond_to TCPSocket, :original_open
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_unbind_when_bind_given_block
|
51
|
+
Net::HTTP.bind(@@new_ip) {}
|
52
|
+
assert_raise NoMethodError do
|
53
|
+
TCPSocket.original_open
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multiplex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paper Cavalier
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-11 00:00:00 +00:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Multiplex lets you bind Net::HTTP requests to non-default local IP addresses.
|
18
|
+
email:
|
19
|
+
- code@papercavalier.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- LICENSE
|
30
|
+
- README.md
|
31
|
+
- Rakefile
|
32
|
+
- lib/multiplex.rb
|
33
|
+
- lib/multiplex/version.rb
|
34
|
+
- multiplex.gemspec
|
35
|
+
- test/multiplex_test.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://rubygems.org/gems/multiplex
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: multiplex
|
60
|
+
rubygems_version: 1.5.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Bind Net::HTTP requests to non-default local IPs.
|
64
|
+
test_files:
|
65
|
+
- test/multiplex_test.rb
|