summaryrefslogtreecommitdiffhomepage
path: root/cmd/connector-gen/github.go
blob: a0162aa06cae3af216e6c102a83d7ac83acc0b56 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/netip"
	"slices"
	"strings"

	"go4.org/netipx"
)

// See https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-githubs-ip-addresses

type GithubMeta struct {
	VerifiablePasswordAuthentication bool `json:"verifiable_password_authentication"`
	SSHKeyFingerprints               struct {
		Sha256Ecdsa   string `json:"SHA256_ECDSA"`
		Sha256Ed25519 string `json:"SHA256_ED25519"`
		Sha256Rsa     string `json:"SHA256_RSA"`
	} `json:"ssh_key_fingerprints"`
	SSHKeys                  []string `json:"ssh_keys"`
	Hooks                    []string `json:"hooks"`
	Web                      []string `json:"web"`
	API                      []string `json:"api"`
	Git                      []string `json:"git"`
	GithubEnterpriseImporter []string `json:"github_enterprise_importer"`
	Packages                 []string `json:"packages"`
	Pages                    []string `json:"pages"`
	Importer                 []string `json:"importer"`
	Actions                  []string `json:"actions"`
	Dependabot               []string `json:"dependabot"`
	Domains                  struct {
		Website    []string `json:"website"`
		Codespaces []string `json:"codespaces"`
		Copilot    []string `json:"copilot"`
		Packages   []string `json:"packages"`
	} `json:"domains"`
}

func github() {
	r, err := http.Get("https://api.github.com/meta")
	if err != nil {
		log.Fatal(err)
	}

	var ghm GithubMeta

	if err := json.NewDecoder(r.Body).Decode(&ghm); err != nil {
		log.Fatal(err)
	}
	r.Body.Close()

	var ips netipx.IPSetBuilder

	var lists []string
	lists = append(lists, ghm.Hooks...)
	lists = append(lists, ghm.Web...)
	lists = append(lists, ghm.API...)
	lists = append(lists, ghm.Git...)
	lists = append(lists, ghm.GithubEnterpriseImporter...)
	lists = append(lists, ghm.Packages...)
	lists = append(lists, ghm.Pages...)
	lists = append(lists, ghm.Importer...)
	lists = append(lists, ghm.Actions...)
	lists = append(lists, ghm.Dependabot...)

	for _, s := range lists {
		ips.AddPrefix(netip.MustParsePrefix(s))
	}

	set, err := ips.IPSet()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(`"routes": [`)
	for _, pfx := range set.Prefixes() {
		fmt.Printf(`"%s": ["tag:connector"],%s`, pfx.String(), "\n")
	}
	fmt.Println(`]`)

	fmt.Println()

	var domains []string
	domains = append(domains, ghm.Domains.Website...)
	domains = append(domains, ghm.Domains.Codespaces...)
	domains = append(domains, ghm.Domains.Copilot...)
	domains = append(domains, ghm.Domains.Packages...)
	slices.Sort(domains)
	domains = slices.Compact(domains)

	var bareDomains []string
	for _, domain := range domains {
		trimmed := strings.TrimPrefix(domain, "*.")
		if trimmed != domain {
			bareDomains = append(bareDomains, trimmed)
		}
	}
	domains = append(domains, bareDomains...)
	slices.Sort(domains)
	domains = slices.Compact(domains)

	fmt.Println(`"domains": [`)
	for _, domain := range domains {
		fmt.Printf(`"%s",%s`, domain, "\n")
	}
	fmt.Println(`]`)

	advertiseRoutes(set)
}