cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.7.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

edgediscovery/protocol.go

45lines · modeblame

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