Sha256: 9bb5f20120b0d3b90fb501485e1489b665992fe35bbdbea7e2d53b174839e2e6

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true
#
# Usage script/update-ips
# updates config/cloudflare-ips.txt and config/fastly-ips.txt

require "open-uri"
require "json"

SOURCES = {
  :cloudflare => ["https://www.cloudflare.com/ips-v4", "https://www.cloudflare.com/ips-v6"],
  :fastly => ["https://api.fastly.com/public-ip-list"]
}.freeze

def parse_fastly(data)
  json_data = JSON.parse(data)
  (json_data["addresses"] + json_data["ipv6_addresses"]).join("\n")
end

def parse_cloudflare(data)
  data
end

def fetch_ips_from_cdn(urls)
  urls.map do |url|
    puts "Fetching #{url}..."
    URI.parse(url).open.read
  end.join("\n")
end

def update_cdn_file(source, data)
  file = "config/#{source}-ips.txt"
  File.write(file, data)
  puts "Writing contents to #{file} and staging changes."
  `git add --verbose #{file}`
end

def parse_cdn_response(source, ips)
  send("parse_#{source}", ips)
end

def update_cdn_ips(source, urls)
  ips = fetch_ips_from_cdn(urls)
  data = parse_cdn_response(source, ips)
  update_cdn_file(source, data)
end

SOURCES.each do |source, urls|
  update_cdn_ips(source, urls)
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
github-pages-health-check-1.18.5 script/update-cdn-ips
github-pages-health-check-1.18.4 script/update-cdn-ips
github-pages-health-check-1.18.2 script/update-cdn-ips
github-pages-health-check-1.18.1 script/update-cdn-ips
github-pages-health-check-1.18.0 script/update-cdn-ips
github-pages-health-check-1.17.9 script/update-cdn-ips
github-pages-health-check-1.17.8 script/update-cdn-ips