~ancarda/tls-redirector

ref: 5f0b9ad56965a4fa1f18e3734f574c80e6e7340f tls-redirector/util.go -rw-r--r-- 593 bytes
5f0b9ad5Mark Dain Add a basic Docker healthcheck 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import (
	"net"
	"strings"
)

func removeSquareBrackets(s string) string {
	if !strings.HasPrefix(s, "[") {
		return s
	}

	if !strings.HasSuffix(s, "]") {
		return s
	}

	return s[1 : len(s)-1]
}

func isIPAddress(host string) bool {
	// Addresses without a port number attached can be checked as-is
	if net.ParseIP(removeSquareBrackets(host)) != nil {
		return true
	}

	// Try to strip the port number off, then check again.
	if host, _, err := net.SplitHostPort(host); err == nil {
		if net.ParseIP(removeSquareBrackets(host)) != nil {
			return true
		}
	}

	return false
}