mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 05:23:04 +00:00
5fb9df1640
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package envoy
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
"github.com/hashicorp/go-sockaddr/template"
|
|
)
|
|
|
|
const defaultGatewayPort int = 8443
|
|
|
|
// ServiceAddressValue implements a flag.Value that may be used to parse an
|
|
// addr:port string into an api.ServiceAddress.
|
|
type ServiceAddressValue struct {
|
|
value api.ServiceAddress
|
|
}
|
|
|
|
func (s *ServiceAddressValue) String() string {
|
|
if s == nil || (s.value.Port == 0 && s.value.Address == "") {
|
|
return fmt.Sprintf(":%d", defaultGatewayPort)
|
|
}
|
|
return fmt.Sprintf("%v:%d", s.value.Address, s.value.Port)
|
|
}
|
|
|
|
func (s *ServiceAddressValue) Value() api.ServiceAddress {
|
|
if s == nil || s.value.Port == 0 && s.value.Address == "" {
|
|
return api.ServiceAddress{Port: defaultGatewayPort}
|
|
}
|
|
return s.value
|
|
}
|
|
|
|
func (s *ServiceAddressValue) Set(raw string) error {
|
|
var err error
|
|
s.value, err = parseAddress(raw)
|
|
return err
|
|
}
|
|
|
|
func parseAddress(raw string) (api.ServiceAddress, error) {
|
|
result := api.ServiceAddress{}
|
|
x, err := template.Parse(raw)
|
|
if err != nil {
|
|
return result, fmt.Errorf("Error parsing address %q: %v", raw, err)
|
|
}
|
|
|
|
addr, portStr, err := net.SplitHostPort(x)
|
|
// Error message from Go's net/ipsock.go
|
|
if err != nil {
|
|
if !strings.Contains(err.Error(), "missing port in address") {
|
|
return result, fmt.Errorf("Error parsing address %q: %v", x, err)
|
|
}
|
|
|
|
// Use the whole input as the address if there wasn't a port.
|
|
if ip := net.ParseIP(x); ip == nil {
|
|
return result, fmt.Errorf("Error parsing address %q: not an IP address", x)
|
|
}
|
|
addr = x
|
|
}
|
|
|
|
port := defaultGatewayPort
|
|
if portStr != "" {
|
|
port, err = strconv.Atoi(portStr)
|
|
if err != nil {
|
|
return result, fmt.Errorf("Error parsing port %q: %v", portStr, err)
|
|
}
|
|
}
|
|
|
|
result.Address = addr
|
|
result.Port = port
|
|
return result, nil
|
|
}
|
|
|
|
var _ flag.Value = (*ServiceAddressValue)(nil)
|
|
|
|
type ServiceAddressMapValue struct {
|
|
value map[string]api.ServiceAddress
|
|
}
|
|
|
|
func (s *ServiceAddressMapValue) String() string {
|
|
buf := new(strings.Builder)
|
|
for k, v := range s.value {
|
|
buf.WriteString(fmt.Sprintf("%v=%v:%d,", k, v.Address, v.Port))
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func (s *ServiceAddressMapValue) Set(raw string) error {
|
|
if s.value == nil {
|
|
s.value = make(map[string]api.ServiceAddress)
|
|
}
|
|
idx := strings.Index(raw, "=")
|
|
if idx == -1 {
|
|
return fmt.Errorf(`Missing "=" in argument: %s`, raw)
|
|
}
|
|
key, value := raw[0:idx], raw[idx+1:]
|
|
var err error
|
|
s.value[key], err = parseAddress(value)
|
|
return err
|
|
}
|
|
|
|
var _ flag.Value = (*ServiceAddressMapValue)(nil)
|