File: update-cdn-ips

package info (click to toggle)
ruby-github-pages-health-check 1.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ruby: 1,135; sh: 53; makefile: 2
file content (50 lines) | stat: -rwxr-xr-x 1,112 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/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