|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + |
| 11 | + "npm/internal/config" |
| 12 | + "npm/internal/model" |
| 13 | + |
| 14 | + "github.com/rotisserie/eris" |
| 15 | +) |
| 16 | + |
| 17 | +var commit string |
| 18 | +var version string |
| 19 | +var sentryDSN string |
| 20 | + |
| 21 | +var cloudfrontURL = "https://ip-ranges.amazonaws.com/ip-ranges.json" |
| 22 | +var cloudflare4URL = "https://www.cloudflare.com/ips-v4" |
| 23 | +var cloudflare6URL = "https://www.cloudflare.com/ips-v6" |
| 24 | + |
| 25 | +func main() { |
| 26 | + config.InitArgs(&version, &commit) |
| 27 | + if err := config.InitIPRanges(&version, &commit, &sentryDSN); err != nil { |
| 28 | + fmt.Printf("# Config ERROR: %v\n", err) |
| 29 | + os.Exit(1) |
| 30 | + } |
| 31 | + |
| 32 | + exitCode := 0 |
| 33 | + |
| 34 | + // Cloudfront |
| 35 | + fmt.Printf("# Cloudfront Ranges from: %s\n", cloudfrontURL) |
| 36 | + if ranges, err := parseCloudfront(); err == nil { |
| 37 | + for _, item := range ranges { |
| 38 | + fmt.Printf("set_real_ip_from %s;\n", item) |
| 39 | + } |
| 40 | + } else { |
| 41 | + fmt.Printf("# ERROR: %v\n", err) |
| 42 | + } |
| 43 | + |
| 44 | + // Cloudflare ipv4 |
| 45 | + if !config.Configuration.DisableIPV4 { |
| 46 | + fmt.Printf("\n# Cloudflare Ranges from: %s\n", cloudflare4URL) |
| 47 | + if ranges, err := parseCloudflare(cloudflare4URL); err == nil { |
| 48 | + for _, item := range ranges { |
| 49 | + fmt.Printf("set_real_ip_from %s;\n", item) |
| 50 | + } |
| 51 | + } else { |
| 52 | + fmt.Printf("# ERROR: %v\n", err) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Cloudflare ipv6 |
| 57 | + if !config.Configuration.DisableIPV6 { |
| 58 | + fmt.Printf("\n# Cloudflare Ranges from: %s\n", cloudflare6URL) |
| 59 | + if ranges, err := parseCloudflare(cloudflare6URL); err == nil { |
| 60 | + for _, item := range ranges { |
| 61 | + fmt.Printf("set_real_ip_from %s;\n", item) |
| 62 | + } |
| 63 | + } else { |
| 64 | + fmt.Printf("# ERROR: %v\n", err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Done |
| 69 | + os.Exit(exitCode) |
| 70 | +} |
| 71 | + |
| 72 | +func parseCloudfront() ([]string, error) { |
| 73 | + // nolint: gosec |
| 74 | + resp, err := http.Get(cloudfrontURL) |
| 75 | + if err != nil { |
| 76 | + return nil, eris.Wrapf(err, "Failed to download Cloudfront IP Ranges from %s", cloudfrontURL) |
| 77 | + } |
| 78 | + |
| 79 | + // nolint: errcheck, gosec |
| 80 | + defer resp.Body.Close() |
| 81 | + body, err := io.ReadAll(resp.Body) |
| 82 | + if err != nil { |
| 83 | + return nil, eris.Wrapf(err, "Failed to read Cloudfront IP Ranges body") |
| 84 | + } |
| 85 | + |
| 86 | + var result model.CloudfrontIPRanges |
| 87 | + if err := json.Unmarshal(body, &result); err != nil { |
| 88 | + return nil, eris.Wrapf(err, "Failed to unmarshal Cloudfront IP Ranges file") |
| 89 | + } |
| 90 | + |
| 91 | + ranges := make([]string, 0) |
| 92 | + if !config.Configuration.DisableIPV4 { |
| 93 | + for _, item := range result.IPV4Prefixes { |
| 94 | + ranges = append(ranges, item.Value) |
| 95 | + } |
| 96 | + } |
| 97 | + if !config.Configuration.DisableIPV6 { |
| 98 | + for _, item := range result.IPV6Prefixes { |
| 99 | + ranges = append(ranges, item.Value) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return ranges, nil |
| 104 | +} |
| 105 | + |
| 106 | +func parseCloudflare(url string) ([]string, error) { |
| 107 | + // nolint: gosec |
| 108 | + resp, err := http.Get(url) |
| 109 | + if err != nil { |
| 110 | + return nil, eris.Wrapf(err, "Failed to download Cloudflare IP Ranges from %s", url) |
| 111 | + } |
| 112 | + |
| 113 | + // nolint: errcheck, gosec |
| 114 | + defer resp.Body.Close() |
| 115 | + |
| 116 | + scanner := bufio.NewScanner(resp.Body) |
| 117 | + scanner.Split(bufio.ScanLines) |
| 118 | + |
| 119 | + ranges := make([]string, 0) |
| 120 | + for scanner.Scan() { |
| 121 | + if scanner.Text() != "" { |
| 122 | + ranges = append(ranges, scanner.Text()) |
| 123 | + } |
| 124 | + } |
| 125 | + return ranges, nil |
| 126 | +} |
0 commit comments