| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "net/http" |
| 8 | "net/netip" |
| 9 | "syscall" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | var errBlockedAddress = errors |
| 14 | |
| 15 | var ( |
| 16 | thisNetwork = netip. |
| 17 | sharedCGNAT = netip. |
| 18 | ) |
| 19 | |
| 20 | // Link-local covers the cloud metadata address 169.254.169.254, so it stays blocked even when private networks are allowed. |
| 21 | func blocked(a netip |
| 22 | a = a. |
| 23 | if a.IsLinkLocalUnicast() |
| 24 | a.IsUnspecified() || |
| 25 | return true |
| 26 | } |
| 27 | if allowPrivate { |
| 28 | return false |
| 29 | } |
| 30 | return a.IsLoopback() |
| 31 | } |
| 32 | |
| 33 | // The check runs on the IP actually dialed, after DNS, so a name that rebinds to an internal address is refused. No proxy, since the check would see the proxy's IP; no redirects, since one could point anywhere. |
| 34 | func newWebhookClient(allowPrivate bool |
| 35 | dialer := &net |
| 36 | Timeout: 5 * |
| 37 | Control: func(network |
| 38 | ap, err := |
| 39 | if err != nil |
| 40 | return err |
| 41 | } |
| 42 | if blocked(ap. |
| 43 | return fmt.Errorf( |
| 44 | } |
| 45 | return nil |
| 46 | }, |
| 47 | } |
| 48 | return &http.Client |
| 49 | Transport: &http |
| 50 | Proxy: nil, |
| 51 | DialContext: dialer.DialContext |
| 52 | TLSHandshakeTimeout: 5 * |
| 53 | MaxIdleConnsPerHost: 2, |
| 54 | IdleConnTimeout: 90 * |
| 55 | }, |
| 56 | CheckRedirect: func(*http |
| 57 | return http.ErrUseLastResponse |
| 58 | }, |
| 59 | } |
| 60 | } |