| 1 | /* |
| 2 | Copyright 2020 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package util |
| 18 | |
| 19 | import ( |
| 20 | "crypto/tls" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | // containIPv6Addr checks if the given host identity contains an |
| 25 | // IPv6 address |
| 26 | func containIPv6Addr(host string) bool { |
| 27 | // the shortest IPv6 address is :: |
| 28 | return len(strings.Split(host, ":")) > 2 |
| 29 | } |
| 30 | |
| 31 | // containPortIPv6 checks if the host that contains an IPv6 address |
| 32 | // also contains a port number |
| 33 | func containPortIPv6(host string) bool { |
| 34 | // based on to RFC 3986, section 3.2.2, host identified by an |
| 35 | // IPv6 is distinguished by enclosing the IP literal within square |
| 36 | // brackets ("[" and "]") |
| 37 | return strings.ContainsRune(host, '[') |
| 38 | } |
| 39 | |
| 40 | // RemovePortFromHost removes port number from the host address that |
| 41 | // may be of the form "<host>:<port>" where the <host> can be an either |
| 42 | // an IPv4/6 address or a domain name |
| 43 | func RemovePortFromHost(host string) string { |
| 44 | if !containIPv6Addr(host) { |
| 45 | return strings.Split(host, ":")[0] |
| 46 | } |
| 47 | if containPortIPv6(host) { |
| 48 | host = host[:strings.LastIndexByte(host, ':')] |
| 49 | } |
| 50 | return strings.Trim(host, "[]") |
| 51 | } |
| 52 | |
| 53 | // GetAcceptedCiphers returns all the ciphers supported by the crypto/tls package |
| 54 | func GetAcceptedCiphers() map[string]uint16 { |
| 55 | acceptedCiphers := make(map[string]uint16, len(tls.CipherSuites())) |
| 56 | for _, v := range tls.CipherSuites() { |
| 57 | acceptedCiphers[v.Name] = v.ID |
| 58 | } |
| 59 | return acceptedCiphers |
| 60 | } |