Go API Documentation

github.com/caddyserver/caddy/v2/internal

No package summary is available.

Package

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Functions

func PrivateRangesCIDR

PrivateRangesCIDR returns a list of private CIDR range strings, which can be used as a configuration shortcut.

func PrivateRangesCIDR() []string {
	return []string{
		"192.168.0.0/16",
		"172.16.0.0/12",
		"10.0.0.0/8",
		"127.0.0.1/8",
		"fd00::/8",
		"::1",
	}
}

Cognitive complexity: 1, Cyclomatic complexity: 1

func SplitUnixSocketPermissionsBits

SplitUnixSocketPermissionsBits takes a unix socket address in the unusual "path|bits" format (e.g. /run/caddy.sock|0222) and tries to split it into socket path (host) and permissions bits (port). Colons (":") can't be used as separator, as socket paths on Windows may include a drive letter (e.g. unix/c:\absolute\path.sock). Permission bits will default to 0200 if none are specified. Throws an error, if the first carrying bit does not include write perms (e.g. 0422 or 022). Symbolic permission representation (e.g. u=w,g=w,o=w) is not supported and will throw an error for now!

func SplitUnixSocketPermissionsBits(addr string) (path string, fileMode fs.FileMode, err error) {
	addrSplit := strings.SplitN(addr, "|", 2)

	if len(addrSplit) == 2 {
		// parse octal permission bit string as uint32
		fileModeUInt64, err := strconv.ParseUint(addrSplit[1], 8, 32)
		if err != nil {
			return "", 0, fmt.Errorf("could not parse octal permission bits in %s: %v", addr, err)
		}
		fileMode = fs.FileMode(fileModeUInt64)

		// FileMode.String() returns a string like `-rwxr-xr--` for `u=rwx,g=rx,o=r` (`0754`)
		if string(fileMode.String()[2]) != "w" {
			return "", 0, fmt.Errorf("owner of the socket requires '-w-' (write, octal: '2') permissions at least; got '%s' in %s", fileMode.String()[1:4], addr)
		}

		return addrSplit[0], fileMode, nil
	}

	// default to 0200 (symbolic: `u=w,g=,o=`)
	// if no permission bits are specified
	return addr, 0o200, nil
}

Cognitive complexity: 6, Cyclomatic complexity: 4

Uses: fmt.Errorf, fs.FileMode, strconv.ParseUint, strings.SplitN.