2020-10-14 10:28:07 +00:00
|
|
|
package edgediscovery
|
|
|
|
|
|
|
|
import (
|
2021-10-11 10:31:05 +00:00
|
|
|
"encoding/json"
|
2020-10-14 10:28:07 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-11 10:31:05 +00:00
|
|
|
protocolRecord = "protocol-v2.argotunnel.com"
|
2020-10-14 10:28:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNoProtocolRecord = fmt.Errorf("No TXT record found for %s to determine connection protocol", protocolRecord)
|
|
|
|
)
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
// ProtocolPercent represents a single Protocol Percentage combination.
|
|
|
|
type ProtocolPercent struct {
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
Percentage int32 `json:"percentage"`
|
2021-10-13 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
// ProtocolPercents represents the preferred distribution ratio of protocols when protocol isn't specified.
|
|
|
|
type ProtocolPercents []ProtocolPercent
|
|
|
|
|
|
|
|
// GetPercentage returns the threshold percentage of a single protocol.
|
|
|
|
func (p ProtocolPercents) GetPercentage(protocol string) int32 {
|
|
|
|
for _, protocolPercent := range p {
|
|
|
|
if strings.ToLower(protocolPercent.Protocol) == strings.ToLower(protocol) {
|
|
|
|
return protocolPercent.Percentage
|
|
|
|
}
|
2021-10-13 18:06:31 +00:00
|
|
|
}
|
2021-10-11 10:31:05 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProtocolPercentage returns the ratio of protocols and a specification ratio for their selection.
|
|
|
|
func ProtocolPercentage() (ProtocolPercents, error) {
|
|
|
|
records, err := net.LookupTXT(protocolRecord)
|
2021-10-13 18:06:31 +00:00
|
|
|
if err != nil {
|
2021-10-11 10:31:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(records) == 0 {
|
|
|
|
return nil, errNoProtocolRecord
|
2020-10-14 10:28:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 10:31:05 +00:00
|
|
|
var protocolsWithPercent ProtocolPercents
|
|
|
|
err = json.Unmarshal([]byte(records[0]), &protocolsWithPercent)
|
|
|
|
return protocolsWithPercent, err
|
2020-10-14 10:28:07 +00:00
|
|
|
}
|