#!/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