Skip to content

Commit 4bf99fe

Browse files
committed
Merge pull request #3 from github/cloudflare
Port Cloudflare Updates
2 parents 74af17b + 935253e commit 4bf99fe

24 files changed

+165
-8
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
*.gem
1+
/*.gem
22
*.lock
33
.bundle
4+
vendor/gems
5+
/bin

config/cloudflare-ips.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
199.27.128.0/21
2+
173.245.48.0/20
3+
103.21.244.0/22
4+
103.22.200.0/22
5+
103.31.4.0/22
6+
141.101.64.0/18
7+
108.162.192.0/18
8+
190.93.240.0/20
9+
188.114.96.0/20
10+
197.234.240.0/22
11+
198.41.128.0/17
12+
162.158.0.0/15
13+
104.16.0.0/12

github-pages-health-check.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Gem::Specification.new do |s|
1313
s.license = "MIT"
1414
s.files = [
1515
"lib/github-pages-health-check.rb",
16-
"lib/github-pages-health-check/version.rb"
16+
"lib/github-pages-health-check/version.rb",
17+
"lib/github-pages-health-check/cloudflare.rb"
1718
]
1819

1920
s.add_dependency("net-dns", "~> 0.6")

lib/github-pages-health-check.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
require 'net/dns/resolver'
33
require 'ipaddr'
44
require 'public_suffix'
5+
require 'singleton'
56
require_relative 'github-pages-health-check/version'
7+
require_relative 'github-pages-health-check/cloudflare'
68

79
class GitHubPages
810
class HealthCheck
@@ -18,14 +20,12 @@ class InvalidCNAME < StandardError; end
1820
199.27.73.133
1921
]
2022

21-
CLOUDFLARE_v4 = IPAddr.new("108.162.192.0/18")
22-
2323
def initialize(domain)
2424
@domain = domain
2525
end
2626

2727
def cloudflare_ip?
28-
dns.all? { |answer| answer.class == Net::DNS::RR::A && CLOUDFLARE_v4.include?(answer.address.to_s) }
28+
dns.all? { |answer| answer.class == Net::DNS::RR::A && CloudFlare.controls_ip?(answer.address) }
2929
end
3030

3131
# Returns an array of DNS answers
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class GitHubPages
2+
class HealthCheck
3+
class CloudFlare
4+
include Singleton
5+
6+
CONFIG_PATH = File.expand_path("../../config/cloudflare-ips.txt", File.dirname(__FILE__))
7+
8+
# Public: Does cloudflare control this address?
9+
def self.controls_ip?(address)
10+
instance.controls_ip?(address)
11+
end
12+
13+
# Internal: Create a new cloudflare info instance.
14+
def initialize(options = {})
15+
@path = options.fetch(:path) { CONFIG_PATH }
16+
end
17+
18+
# Internal: The path of the config file.
19+
attr_reader :path
20+
21+
# Internal: Does cloudflare control this address?
22+
def controls_ip?(address)
23+
ranges.any? { |range| range.include?(address) }
24+
end
25+
26+
# Internal: The IP address ranges that cloudflare controls.
27+
def ranges
28+
@ranges ||= load_ranges
29+
end
30+
31+
# Internal: Load IPAddr ranges from #path
32+
def load_ranges
33+
File.read(path).lines.map { |line| IPAddr.new(line.chomp) }
34+
end
35+
end
36+
end
37+
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class GitHubPages
22
class HealthCheck
3-
VERSION = '0.0.2'
3+
VERSION = '0.0.3'
44
end
55
end

script/bootstrap

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@
22

33
set -ex
44

5-
bundle install
5+
cd "$(dirname "$0")/.."
6+
7+
if [ -f .ruby-version ] ; then
8+
export RBENV_VERSION=$(cat .ruby-version)
9+
fi
10+
11+
#set ruby version, see https://github.com/github/pages-jekyll/pull/17
12+
export PATH=/usr/share/rbenv/shims:$PATH BUNDLE_GEMFILE=$(pwd)/Gemfile
13+
14+
bundle install --path vendor/gems --binstubs --local --standalone --clean "$@"

script/check-cloudflare-ips

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash -e
2+
3+
script/update-cloudflare-ips >/dev/null 2>&1
4+
5+
# `git diff --quiet` suppresses output and sets a return code
6+
# 0 - no changes
7+
# 1 - changes
8+
if git diff -w --quiet --cached config/cloudflare-ips.txt
9+
then
10+
echo CloudFlare IP list is up-to-date.
11+
exit 0
12+
else
13+
echo git reset config/cloudflare-ips.txt
14+
git reset --quiet config/cloudflare-ips.txt
15+
echo '*** CloudFlare IP list is out of date! Run script/update-cloudflare-ips!'
16+
exit 1
17+
fi

script/cibuild

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,18 @@
22

33
set -ex
44

5-
bundle exec rspec
5+
script/bootstrap
6+
7+
if [ -L $0 ]; then
8+
root=$(cd "$(dirname "$(readlink "$0")")"/.. && pwd)
9+
else
10+
root=$(cd "$(dirname "$0")"/.. && pwd)
11+
fi
12+
13+
export PATH="$root/bin:/usr/share/rbenv/shims:$PATH"
14+
15+
export RBENV_VERSION=$(cat $root/.ruby-version)
16+
export BUNDLE_GEMFILE="$root/Gemfile"
17+
18+
bundle exec bin/rspec
19+
script/check-cloudflare-ips

script/update-cloudflare-ips

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash -e
2+
#/ Usage script/update-cloudflare-ips
3+
#/ updates config/cloudflare-ips.txt
4+
5+
source=https://www.cloudflare.com/ips-v4
6+
dest=config/cloudflare-ips.txt
7+
8+
echo '====>' Downloading $source
9+
curl --silent --fail $source | tee $dest
10+
echo # cover for a missing newline
11+
12+
echo '====>' git add $dest
13+
git diff -w $dest
14+
git add --verbose $dest
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'spec_helper'
2+
require 'json'
3+
require 'tempfile'
4+
require 'ipaddr'
5+
6+
describe(GitHubPages::HealthCheck::CloudFlare) do
7+
8+
let(:instance) { GitHubPages::HealthCheck::CloudFlare.send(:new, :path => ipaddr_path) }
9+
let(:tempfile) { Tempfile.new("pages-jekyll-alarmist-cloudflare-ips").tap { |f| f.sync = true } }
10+
let(:ipaddr_path) { tempfile.path }
11+
12+
context "default" do
13+
let(:instance) { GitHubPages::HealthCheck::CloudFlare.instance }
14+
15+
it "loads the default config" do
16+
path = File.expand_path(instance.path)
17+
expected = File.expand_path("../config/cloudflare-ips.txt", File.dirname(__FILE__))
18+
expect(path).to eql(expected)
19+
end
20+
end
21+
22+
context "no config file" do
23+
before { tempfile.unlink }
24+
25+
it "raises an error" do
26+
expect { instance.ranges }.to raise_error
27+
end
28+
end
29+
30+
context "parses config" do
31+
before { tempfile.write("199.27.128.0/21\n173.245.48.0/20") }
32+
33+
it "has two IPs" do
34+
expect(instance.ranges.size).to eql(2)
35+
end
36+
37+
it "loads the IP addresses" do
38+
expect(instance.ranges).to include(IPAddr.new("199.27.128.0/21"))
39+
expect(instance.ranges).to include(IPAddr.new("173.245.48.0/20"))
40+
end
41+
42+
it("controls? 199.27.128.55") { expect(instance.controls_ip?(IPAddr.new("199.27.128.55"))).to be_truthy }
43+
it("controls? 173.245.48.55") { expect(instance.controls_ip?(IPAddr.new("173.245.48.55"))).to be_truthy }
44+
it("controls? 200.27.128.55") { expect(instance.controls_ip?(IPAddr.new("200.27.128.55"))).to be_falsey }
45+
end
46+
47+
it "works as a singleton" do
48+
expect(GitHubPages::HealthCheck::CloudFlare.controls_ip?("108.162.196.20")).to be(true)
49+
end
50+
end

vendor/cache/coderay-1.1.0.gem

93 KB
Binary file not shown.

vendor/cache/diff-lcs-1.2.5.gem

48 KB
Binary file not shown.

vendor/cache/method_source-0.8.2.gem

13 KB
Binary file not shown.

vendor/cache/net-dns-0.8.0.gem

49.5 KB
Binary file not shown.

vendor/cache/pry-0.10.0.gem

133 KB
Binary file not shown.

vendor/cache/public_suffix-1.4.5.gem

64.5 KB
Binary file not shown.

vendor/cache/rspec-3.0.0.gem

9.5 KB
Binary file not shown.

vendor/cache/rspec-core-3.0.4.gem

105 KB
Binary file not shown.
57.5 KB
Binary file not shown.

vendor/cache/rspec-mocks-3.0.4.gem

61.5 KB
Binary file not shown.

vendor/cache/rspec-support-3.0.4.gem

18.5 KB
Binary file not shown.

vendor/cache/slop-3.5.0.gem

23 KB
Binary file not shown.

0 commit comments

Comments
 (0)