util

package
v0.43.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const HostIPToken = "${{ host_ip }}"

HostIPToken is a template function usable in any address value (e.g. server.agent_endpoint): `${{ host_ip }}:3010` renders to the host's IP address at startup. Agents run inside containers and cannot reach the host via 127.0.0.1, so endpoints advertised to them need the real interface IP. The ${{ }} delimiters match knot's template variable style (used to avoid clashing with Nomad interpolation); any spacing inside them works.

View Source
const KnotCookiePrefix = "__KNOT_"

KnotCookiePrefix starts the name of every cookie knot sets, after the "__Host-" prefix an https server adds.

View Source
const ShellStopTimeout = 2 * time.Second

ShellStopTimeout is how long a shell gets to exit after SIGHUP before it is killed.

Variables

View Source
var Timezones = []string{}/* 575 elements not displayed */

Functions

func CanOpenBrowser added in v0.41.0

func CanOpenBrowser() bool

CanOpenBrowser reports whether OpenBrowser is likely to show a browser to the person at this terminal. Over SSH, or on a Unix with no graphical session, xdg-open may fail or start a text browser that takes over the terminal, so callers should show the URL instead.

func CheckShells

func CheckShells(preferredShell string) string

func ConvertToBytes

func ConvertToBytes(input string) (int64, error)

func CopyFields

func CopyFields(src, dst interface{}, fields []string) error

CopyFields copies specified fields from src to dst using reflection.

func DecryptSSHPrivateKey added in v0.25.0

func DecryptSSHPrivateKey(encryptionKey string, privateKey string) string

func DownloadUnpackTgz

func DownloadUnpackTgz(downloadURL string, destDir string) error

func FixListenAddress

func FixListenAddress(address string) string

func GenerateCertificate

func GenerateCertificate(dnsNames []string, ipAddresses []net.IP) (string, string, error)

GenerateCertificate generates a self-signed certificate and private key in PEM format.

func GetGitHubKeys

func GetGitHubKeys(username string) (string, error)

Download the public keys from GitHub, https://github.com/<username>.keys

func GetGitHubKeysArray

func GetGitHubKeysArray(username string) ([]string, error)

func GetLocalIP

func GetLocalIP() (string, error)

Get the local IP address of the machine we're running on, interfaces are checked for being up.

func HangUpShell added in v0.42.0

func HangUpShell(proc *os.Process, tty io.Reader, exited <-chan struct{})

HangUpShell hangs up a shell whose terminal has gone away so it can save its history on the way out (bash, zsh and fish all do on SIGHUP), and only kills it if it hasn't exited within ShellStopTimeout. Killing it straight away would lose the session's history.

exited must be closed once the process has been waited on. The shell's pty must stay open until then: closing it first makes the kernel hang the session up underneath the shell and bash then exits without saving its history. Its output keeps being read (and discarded) meanwhile, as a shell that can't flush its output to a pty nobody is reading never gets as far as saving. Closing tty afterwards ends that reading.

func HideConsoleIfOwned added in v0.33.0

func HideConsoleIfOwned()

HideConsoleIfOwned is a no-op outside Windows.

func HostIP added in v0.33.0

func HostIP() string

HostIP returns the host's primary outbound IPv4 address. Physical interfaces are preferred — virtual interfaces (VPN/tailscale tunnels, docker/veth bridges) and their address ranges (CGNAT 100.64/10, link-local) are skipped first so advertised endpoints stay on durable addresses. If no physical candidate exists (e.g. a host only reachable via VPN) it falls back to the address the routing table would use for an external destination (UDP dial sends no packets), then to any non-loopback IPv4, then to loopback.

func InArray

func InArray(slice []string, item string) bool

Checks if a string is in a slice of strings.

func IsLocalURL added in v0.41.1

func IsLocalURL(target string) bool

IsLocalURL reports whether target is a path on this server, as LocalRedirect returns: it starts with a single "/", not "//" or "/\", and holds nothing browsers would turn into either.

func LocalRedirect added in v0.41.0

func LocalRedirect(raw string) string

LocalRedirect reduces a user-supplied post-login redirect target to a path on this server: any scheme, host or fragment is dropped and only the path and query survive. It returns "" when the target can't safely be one, or is /logout.

The path stays percent-encoded, so feeding the result back in returns it unchanged: a target kept in a cookie and checked again later can't decode a second time into something else, such as "/%2509/evil.com" becoming "/\t/evil.com", which browsers read as "//evil.com".

func NewCopier

func NewCopier(socket net.Conn, wsConnection *websocket.Conn) *copierConnections

func OpenBrowser added in v0.33.0

func OpenBrowser(url string) error

OpenBrowser opens the specified URL in the default browser of the user. On Windows rundll32 is used rather than `cmd /c start` because cmd is a console application and would flash (or leave) a console window every time the tray menu opens the UI.

func PrettyPrintJSON added in v0.19.0

func PrettyPrintJSON(v interface{}) error

func PrintTable

func PrintTable(table [][]string)

func ReadSSHConfigAgentForwarding added in v0.39.0

func ReadSSHConfigAgentForwarding(alias string) (map[string]bool, error)

ReadSSHConfigAgentForwarding reads the knot block for alias from ~/.ssh/config and reports, for each host in it, whether agent forwarding is on. A missing file or block gives an empty map.

func RedirectToSyslog

func RedirectToSyslog(cmd *exec.Cmd)

func RelaunchHidden added in v0.33.0

func RelaunchHidden() bool

RelaunchHidden is a no-op outside Windows.

func ResolveHostIP added in v0.33.0

func ResolveHostIP(s string) string

ResolveHostIP renders any template functions in an address using the address funcs (currently host_ip). knot-style `${{ func }}` delimiters are normalised to Go template actions first so the $ is consumed. Strings that are not templates — or that fail to parse or execute — are returned unchanged, so plain addresses pass through untouched.

func SplitSSHPublicKeys added in v0.25.0

func SplitSSHPublicKeys(keys string) []string

func StripKnotCookies added in v0.41.0

func StripKnotCookies(h http.Header)

StripKnotCookies removes knot's own cookies from a request headed for a space or tunnel. What runs there is the user's code: given the session cookie of whoever opened the page, it could act as them. The space's own cookies pass through unchanged.

func UpdateAuthorizedKeys

func UpdateAuthorizedKeys(keys []string, githubUsernames []string) error

func UpdateSSHConfig

func UpdateSSHConfig(sshConfig string, alias string) error

func UpdateSSHPrivateKey added in v0.25.0

func UpdateSSHPrivateKey(privateKey string) error

func UpgradeToWS

func UpgradeToWS(w http.ResponseWriter, r *http.Request) *websocket.Conn

Upgrade the connection to a websocket connection

Types

type CommandFrontmatter added in v0.29.0

type CommandFrontmatter struct {
	Name         string
	Description  string
	ArgumentHint string
	AllowedTools []string
}

func ParseCommandFrontmatter added in v0.29.0

func ParseCommandFrontmatter(content string) (*CommandFrontmatter, error)

type SkillFrontmatter added in v0.23.0

type SkillFrontmatter struct {
	Name        string
	Description string
}

func ParseSkillFrontmatter added in v0.23.0

func ParseSkillFrontmatter(content string) (*SkillFrontmatter, error)

Directories

Path Synopsis
Package excludes matches paths against exclude patterns, for the commands that copy directory trees: knot space mirror and knot file sync.
Package excludes matches paths against exclude patterns, for the commands that copy directory trees: knot space mirror and knot file sync.
Package hostlabel makes the first label of a space's web hostnames, <user>--<name>--<port>, fit the 63 characters DNS allows a label: a longer label is never resolved, so the space could not be reached.
Package hostlabel makes the first label of a space's web hostnames, <user>--<name>--<port>, fit the 63 characters DNS allows a label: a longer label is never resolved, so the space could not be reached.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL