cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
edgediscovery/protocol.go
45lines · modeblame
b5cdf3b2cthuang5 years ago | 1 | package edgediscovery |
| 2 | | |
| 3 | import ( | |
| 4 | "fmt" | |
| 5 | "net" | |
| 6 | "strconv" | |
| 7 | "strings" | |
| 8 | ) | |
| 9 | | |
| 10 | const ( | |
| 11 | protocolRecord = "protocol.argotunnel.com" | |
| 12 | ) | |
| 13 | | |
| 14 | var ( | |
| 15 | errNoProtocolRecord = fmt.Errorf("No TXT record found for %s to determine connection protocol", protocolRecord) | |
| 16 | ) | |
| 17 | | |
| 18 | func HTTP2Percentage() (int32, error) { | |
| 19 | records, err := net.LookupTXT(protocolRecord) | |
| 20 | if err != nil { | |
| 21 | return 0, err | |
| 22 | } | |
| 23 | if len(records) == 0 { | |
| 24 | return 0, errNoProtocolRecord | |
| 25 | } | |
| 26 | return parseHTTP2Precentage(records[0]) | |
| 27 | } | |
| 28 | | |
| 29 | // The record looks like http2=percentage | |
| 30 | func parseHTTP2Precentage(record string) (int32, error) { | |
| 31 | const key = "http2" | |
| 32 | slices := strings.Split(record, "=") | |
| 33 | if len(slices) != 2 { | |
| 34 | return 0, fmt.Errorf("Malformed TXT record %s, expect http2=percentage", record) | |
| 35 | } | |
| 36 | if slices[0] != key { | |
| 37 | return 0, fmt.Errorf("Incorrect key %s, expect %s", slices[0], key) | |
| 38 | } | |
| 39 | percentage, err := strconv.ParseInt(slices[1], 10, 32) | |
| 40 | if err != nil { | |
| 41 | return 0, err | |
| 42 | } | |
| 43 | return int32(percentage), nil | |
| 44 | | |
| 45 | } |