aws-ip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b8a8763f0c0b8b7d6e6d5226a20b43acfa8bca5
4
+ data.tar.gz: 7ee5ad3cb96483fed021ae2e8f3e2563dc65ebe6
5
+ SHA512:
6
+ metadata.gz: fa9e6c7e692f66626c0a87868692bf23b045ca726618a86c0d87db8f50b12c11aab3f5158f6bca8398c59119bd8aa61d5f0533d553ea7c7790834c6f0730c702
7
+ data.tar.gz: 5b2ca93776681140cd52c648a6d151a64f4a9cc2511e4eafdf8bf88db7fb843f2dffb53f0d5c0d13869017ef33884b2ad8f6a537df834da37512a0348b24e9e8
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
4
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws-ip.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 rochefort
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Aws::Ip
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'aws-ip'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install aws-ip
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/aws-ip/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task test: :spec
7
+ task default: :spec
data/aws-ip.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws_ip/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'aws-ip'
8
+ spec.version = AwsIp::VERSION
9
+ spec.authors = ['rochefort']
10
+ spec.email = ['terasawan@gmail.com']
11
+ spec.summary = %q{find out AWS IP address}
12
+ spec.description = spec.summary
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.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'ipaddress', '~> 0.8.0'
24
+
25
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
26
+ spec.add_development_dependency 'webmock', '~> 1.20.4'
27
+ end
data/bin/aws-ip ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
4
+ require 'aws_ip'
5
+
6
+ fail ArgumentError, 'invalid argument' if ARGV.size == 0
7
+
8
+ aws = AwsIp.new
9
+ puts aws.env
10
+ puts aws.fetch ARGV[0]
@@ -0,0 +1,49 @@
1
+ require 'ipaddress'
2
+
3
+ module AwsIp
4
+ class Base
5
+ def initialize
6
+ @range = Range.new
7
+ @ranges ||= @range.get
8
+ end
9
+
10
+ def update
11
+ @ranges = @range.get
12
+ end
13
+
14
+ def ranges
15
+ @ranges['prefixes']
16
+ end
17
+ alias_method :all_ranges, :ranges
18
+
19
+ def env
20
+ "syncToken : #{@ranges['syncToken']}\n" \
21
+ "createDate: #{@ranges['createDate']}\n\n"
22
+ end
23
+
24
+ def fetch(ip_address)
25
+ ip = parse(ip_address)
26
+ @ranges['prefixes'].find_all do |prefix|
27
+ prefix_ip = IPAddress(prefix['ip_prefix'])
28
+ prefix_ip.include?(ip)
29
+ end
30
+ end
31
+
32
+ def include?(ip_address)
33
+ ip = parse(ip_address)
34
+ result = @ranges['prefixes'].find do |prefix|
35
+ prefix_ip = IPAddress(prefix['ip_prefix'])
36
+ prefix_ip.include?(ip)
37
+ end
38
+ !!result
39
+ end
40
+
41
+ private
42
+ def parse(ip_address)
43
+ IPAddress(ip_address)
44
+ rescue => e
45
+ puts e.stacktrace if ENV['DEBUG']
46
+ abort e.message
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ module AwsIp
6
+ class Range
7
+ BASE_URI = 'https://ip-ranges.amazonaws.com/ip-ranges.json'
8
+
9
+ def initialize
10
+ @uri = URI.parse(BASE_URI)
11
+ @proxy = @uri.find_proxy
12
+ end
13
+
14
+ def get
15
+ connection = if @proxy
16
+ Net::HTTP::Proxy(@proxy.host, @proxy.port, @proxy.user, @proxy.password).new(@uri.host, @uri.port)
17
+ else
18
+ Net::HTTP.new(@uri.host, @uri.port)
19
+ end
20
+ connection.use_ssl = true
21
+ response = connection.start { |http| http.get(@uri) }
22
+ case response
23
+ when Net::HTTPSuccess
24
+ json = response.body
25
+ JSON.parse(json)
26
+ else
27
+ # raise excpetion
28
+ response.value
29
+ end
30
+ rescue => e
31
+ puts e.stacktrace if ENV['DEBUG']
32
+ abort [e.message, @uri].join(' : ')
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module AwsIp
2
+ VERSION = '0.0.1'
3
+ end
data/lib/aws_ip.rb ADDED
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ require 'aws_ip/base'
4
+ require 'aws_ip/range'
5
+ require 'aws_ip/version'
6
+
7
+ module AwsIp
8
+ class << self
9
+ def new
10
+ AwsIp::Base.new
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AwsIp::Base do
4
+ before do
5
+ stub_base_request
6
+ @ip = AwsIp.new
7
+ end
8
+
9
+ describe '#fetch' do
10
+ before do
11
+ @result = [{"ip_prefix"=>"54.240.200.0/24", "region"=>"ap-northeast-1", "service"=>"AMAZON"}]
12
+ end
13
+ context 'in range' do
14
+ it { expect(@ip.fetch('54.240.200.0')).to eq(@result) }
15
+ it { expect(@ip.fetch('54.240.200.255')).to eq(@result) }
16
+ end
17
+ context 'out of range' do
18
+ it { expect(@ip.fetch('54.240.201.0')).to eq([]) }
19
+ end
20
+ end
21
+
22
+ describe '#include?' do
23
+ context 'in range' do
24
+ it { expect(@ip.include?('54.240.200.0')).to be_truthy }
25
+ it { expect(@ip.include?('54.240.200.255')).to be_truthy }
26
+ end
27
+ context 'out of range' do
28
+ it { expect(@ip.include?('54.240.201.0')).to be_falsey }
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Range do
4
+ describe 'request behind proxy' do
5
+ before do
6
+ allow(ENV).to receive(:[]).with('no_proxy').and_return('')
7
+ allow(ENV).to receive(:[]).with('https_proxy')
8
+ .and_return('http://proxy_user:proxy_pass@192.168.1.99:9999')
9
+ ip = AwsIp::Range.new
10
+ @proxy = ip.instance_variable_get(:@proxy)
11
+ end
12
+ it { expect(@proxy.host).to eq '192.168.1.99' }
13
+ it { expect(@proxy.user).to eq 'proxy_user' }
14
+ it { expect(@proxy.password).to eq 'proxy_pass' }
15
+ it { expect(@proxy.port).to eq 9999 }
16
+ end
17
+
18
+ describe '#get' do
19
+ context 'when getting json error occurred' do
20
+ before do
21
+ stub_request(:get, AwsIp::Range::BASE_URI)
22
+ .to_return(status: 500, headers: { content_type: 'application/json' }, body: '')
23
+ end
24
+ it { expect { AwsIp::Range.new.get }.to raise_error(Exception) }
25
+ end
26
+ end
27
+ end