cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.1.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

edgediscovery/protocol.go

45lines · modecode

1package edgediscovery
2
3import (
4 "fmt"
5 "net"
6 "strconv"
7 "strings"
8)
9
10const (
11 protocolRecord = "protocol.argotunnel.com"
12)
13
14var (
15 errNoProtocolRecord = fmt.Errorf("No TXT record found for %s to determine connection protocol", protocolRecord)
16)
17
18func 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
30func 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}
46