2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var hostLabelRegexp = regexp.MustCompile("^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?$")
|
|
|
|
|
|
|
|
// Host - holds network host IP/name and its port.
|
|
|
|
type Host struct {
|
|
|
|
Name string
|
|
|
|
Port Port
|
|
|
|
IsPortSet bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty - returns whether Host is empty or not
|
|
|
|
func (host Host) IsEmpty() bool {
|
|
|
|
return host.Name == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// String - returns string representation of Host.
|
|
|
|
func (host Host) String() string {
|
|
|
|
if !host.IsPortSet {
|
|
|
|
return host.Name
|
|
|
|
}
|
|
|
|
|
2018-12-14 02:37:46 -05:00
|
|
|
return net.JoinHostPort(host.Name, host.Port.String())
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal - checks whether given host is equal or not.
|
|
|
|
func (host Host) Equal(compHost Host) bool {
|
|
|
|
return host.String() == compHost.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON - converts Host into JSON data
|
|
|
|
func (host Host) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(host.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - parses data into Host.
|
|
|
|
func (host *Host) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
var s string
|
|
|
|
if err = json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow empty string
|
|
|
|
if s == "" {
|
|
|
|
*host = Host{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var h *Host
|
|
|
|
if h, err = ParseHost(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*host = *h
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHost - parses string into Host
|
|
|
|
func ParseHost(s string) (*Host, error) {
|
2019-11-26 14:42:10 -05:00
|
|
|
if s == "" {
|
|
|
|
return nil, errors.New("invalid argument")
|
|
|
|
}
|
2018-03-15 16:03:41 -04:00
|
|
|
isValidHost := func(host string) bool {
|
|
|
|
if host == "" {
|
2019-11-26 14:42:10 -05:00
|
|
|
return true
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// host is not a valid IPv4 or IPv6 address
|
|
|
|
// host may be a hostname
|
|
|
|
// refer https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
|
|
|
|
// why checks are done like below
|
|
|
|
if len(host) < 1 || len(host) > 253 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, label := range strings.Split(host, ".") {
|
|
|
|
if len(label) < 1 || len(label) > 63 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hostLabelRegexp.MatchString(label) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var port Port
|
|
|
|
var isPortSet bool
|
|
|
|
host, portStr, err := net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
host = s
|
|
|
|
} else {
|
|
|
|
if port, err = ParsePort(portStr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
isPortSet = true
|
|
|
|
}
|
|
|
|
|
2019-05-03 08:01:33 -04:00
|
|
|
if host != "" {
|
|
|
|
host, err = trimIPv6(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv6 requires a link-local address on every network interface.
|
|
|
|
// `%interface` should be preserved.
|
|
|
|
trimmedHost := host
|
|
|
|
|
|
|
|
if i := strings.LastIndex(trimmedHost, "%"); i > -1 {
|
|
|
|
// `%interface` can be skipped for validity check though.
|
|
|
|
trimmedHost = trimmedHost[:i]
|
2018-12-14 02:37:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 08:01:33 -04:00
|
|
|
if !isValidHost(trimmedHost) {
|
2018-03-15 16:03:41 -04:00
|
|
|
return nil, errors.New("invalid hostname")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Host{
|
|
|
|
Name: host,
|
|
|
|
Port: port,
|
|
|
|
IsPortSet: isPortSet,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-05-03 08:01:33 -04:00
|
|
|
|
|
|
|
// IPv6 can be embedded with square brackets.
|
|
|
|
func trimIPv6(host string) (string, error) {
|
|
|
|
// `missing ']' in host` error is already handled in `SplitHostPort`
|
|
|
|
if host[len(host)-1] == ']' {
|
|
|
|
if host[0] != '[' {
|
|
|
|
return "", errors.New("missing '[' in host")
|
|
|
|
}
|
|
|
|
return host[1:][:len(host)-2], nil
|
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|