aboutsummaryrefslogtreecommitdiff
path: root/pkg/cloudflare/iplist_generate.go
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-21 02:25:01 -0400
committerpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-21 02:25:01 -0400
commit950e311c82c90a00a3b5201b44c0faaaafb19967 (patch)
tree0d1b46d317799ff6b683ae8c6f0cd58a6f3f8b73 /pkg/cloudflare/iplist_generate.go
parent2c983b4561d0d66b3613f32e1e0b6121779b094c (diff)
downloadAtlas-950e311c82c90a00a3b5201b44c0faaaafb19967.tar.gz
Atlas-950e311c82c90a00a3b5201b44c0faaaafb19967.zip
pkg/cloudflare: Implement Cloudflare IP list checks
Diffstat (limited to 'pkg/cloudflare/iplist_generate.go')
-rw-r--r--pkg/cloudflare/iplist_generate.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/pkg/cloudflare/iplist_generate.go b/pkg/cloudflare/iplist_generate.go
new file mode 100644
index 0000000..7e169c4
--- /dev/null
+++ b/pkg/cloudflare/iplist_generate.go
@@ -0,0 +1,52 @@
+//go:build generate
+// +build generate
+
+package main
+
+import (
+ "bytes"
+ "context"
+ "fmt"
+ "go/format"
+ "net/netip"
+ "os"
+ "time"
+ _ "unsafe"
+
+ "github.com/pg9182/atlas/pkg/cloudflare"
+)
+
+//go:linkname iplist github.com/pg9182/atlas/pkg/cloudflare.iplist
+var iplist []netip.Prefix
+
+func main() {
+ if err := cloudflare.UpdateIPs(context.Background()); err != nil {
+ fmt.Fprintf(os.Stderr, "error: fetch ip list: %v\n", err)
+ os.Exit(1)
+ }
+
+ var b bytes.Buffer
+ fmt.Fprintf(&b, "// Code generated by iplist_generate on %s; DO NOT EDIT.\n", time.Now().UTC())
+ fmt.Fprintf(&b, "\n\n")
+ fmt.Fprintf(&b, "package cloudflare\n")
+ fmt.Fprintf(&b, "\n\n")
+ fmt.Fprintf(&b, "import %#v\n", "net/netip")
+ fmt.Fprintf(&b, "\n\n")
+ fmt.Fprintf(&b, "//go:generate go run iplist_generate.go\n")
+ fmt.Fprintf(&b, "\n\n")
+ fmt.Fprintf(&b, "func init() {\n")
+ fmt.Fprintf(&b, "iplist = append(iplist,\n")
+ for _, p := range iplist {
+ fmt.Fprintf(&b, "netip.MustParsePrefix(%#v),\n", p.String())
+ }
+ fmt.Fprintf(&b, ")\n")
+ fmt.Fprintf(&b, "}\n")
+
+ if src, err := format.Source(b.Bytes()); err != nil {
+ fmt.Fprintf(os.Stderr, "error: format generated code: %v\n", err)
+ os.Exit(1)
+ } else if err = os.WriteFile("iplist_generated.go", src, 0666); err != nil {
+ fmt.Fprintf(os.Stderr, "error: write generated code: %v\n", err)
+ os.Exit(1)
+ }
+}