Documentation
¶
Index ¶
- Constants
- func Bold(s string) string
- func BoldGreen(s string) string
- func Debug(msg string)
- func Debugf(format string, args ...any)
- func DeleteProfileFromConfig(configPath string, name string) error
- func Error(msg string)
- func Errorf(format string, args ...any)
- func Fatal(args ...any)
- func Fatalf(format string, args ...any)
- func Info(msg string)
- func Infof(format string, args ...any)
- func NewWarnLogger() *log.Logger
- func ReadThemeNames(path string) ([]string, error)
- func SaveProfileToConfig(configPath string, name string, p Profile) error
- func SaveSettingsToConfig(configPath string, server settingsServerConfig, ...) error
- func SaveThemeToConfig(configPath string, themeName string, colors map[string]any) error
- func Serve(ts *TerminalServer, shouldOpenBrowser bool, useTLS bool)
- func SetDebug(enabled bool)
- func UpdateThemeInConfig(configPath string, themeName string, colors map[string]any) error
- func ValidateConfig(path string) error
- func ValidatePortNumber(port int) bool
- func ValidateTheme(t *Theme) error
- func ValidateThemeColor(s string) bool
- func Warn(msg string)
- func Warnf(format string, args ...any)
- func WriteDefaultConfig(themeName string, colors map[string]any) error
- type CSPHeader
- type CSPHeaders
- type Client
- type Profile
- type Server
- type TLS
- type TermConfig
- type TerminalServer
- type Theme
Constants ¶
const B3TTY_CONFIG_PATH = "b3tty"
const BUFFER_SIZE = 4096
const CONFIG_FILE_NAME = "conf.yaml"
const DEFAULT_COLS = 0
const DEFAULT_CURSOR_BLINK = true
const DEFAULT_FONT_FAMILY = "na"
const DEFAULT_FONT_SIZE = 14
const DEFAULT_PROFILE_NAME = "default"
const DEFAULT_ROOT = "/"
const DEFAULT_ROWS = 24
const DEFAULT_SHELL = "$SHELL"
const DEFAULT_TITLE = "b3tty"
const DEFAULT_URI = "localhost"
const DEFAULT_WORKING_DIRECTORY = "$HOME"
const DOT_CONFIG_PATH = ".config"
const MAX_REQUEST_BODY_SIZE = 4096
const TOKEN_LENGTH = 24
Variables ¶
This section is empty.
Functions ¶
func Bold ¶
Bold returns s wrapped in bold ANSI codes when color output is enabled, and returns s unchanged when output is piped or redirected.
func Debug ¶
func Debug(msg string)
Debug logs a debug message. Output is suppressed unless SetDebug(true) has been called.
func Debugf ¶
Debugf logs a debug message. Output is suppressed unless SetDebug(true) has been called.
func DeleteProfileFromConfig ¶ added in v1.7.0
DeleteProfileFromConfig reads the existing config file at configPath, removes the named entry from the profiles section, and writes the file back. No-ops if the profiles section or the named entry is absent.
func NewWarnLogger ¶
NewWarnLogger returns a *log.Logger suitable for http.Server.ErrorLog. Internal HTTP/TLS messages are routed through Warnf so the timestamp and level label appear in the same order as every other log line.
func ReadThemeNames ¶ added in v1.6.0
ReadThemeNames reads the config file at path and returns the names from the themes section, preserving their exact case as written in the YAML.
func SaveProfileToConfig ¶ added in v1.7.0
SaveProfileToConfig reads the existing config file at configPath (creating it if absent), upserts the named profile in the profiles section, and writes the file back.
func SaveSettingsToConfig ¶ added in v1.8.0
func SaveSettingsToConfig(configPath string, server settingsServerConfig, terminal settingsTerminalConfig) error
SaveSettingsToConfig reads the existing config file at configPath (creating it if absent), updates the server and terminal sections with the provided values, and writes the file back. Existing settings not covered by the structs are preserved.
func SaveThemeToConfig ¶ added in v1.6.0
SaveThemeToConfig reads the existing config file at configPath (creating it if absent), sets the active theme name, and writes the theme's color entries to the themes section, overwriting any existing entry for that theme name.
func Serve ¶
func Serve(ts *TerminalServer, shouldOpenBrowser bool, useTLS bool)
Serve wires up the HTTP mux and starts the server.
func UpdateThemeInConfig ¶ added in v1.5.2
UpdateThemeInConfig reads the existing config file at configPath (creating it if absent), sets the active theme name, and adds the theme's color entries to the themes section if they are not already present. Existing settings are preserved.
func ValidateConfig ¶ added in v1.3.1
ValidateConfig opens the YAML file at path, decodes it into typed structs with KnownFields(true) enabled, and returns a descriptive error (including the line number from the YAML parser) if any field has the wrong type or any unrecognised key is present.
func ValidatePortNumber ¶ added in v1.4.1
ValidatePortNumber reports whether port is a valid TCP/UDP port number (1–65535).
func ValidateTheme ¶
ValidateTheme checks every color field in t against validateThemeColor. It returns an error naming the first invalid field and value, or nil when all fields are valid. Fields are identified by their JSON tag name.
func ValidateThemeColor ¶ added in v1.3.1
validateThemeColor reports whether s is a valid theme color value. Valid values are an empty string (field not set), a CSS hex color with 3 or 6 hex digits (e.g. "#fff" or "#14181d"), or a string of only ASCII letters representing a named CSS color (e.g. "red" or "cornflowerblue").
Types ¶
type CSPHeader ¶ added in v1.3.1
CSPHeader represents a single Content-Security-Policy directive, consisting of a directive name (e.g. "script-src") and one or more source values (e.g. "self", "nonce-abc123"). Values are rendered without surrounding quotes in Add/Set but wrapped in single quotes by String() to produce valid CSP syntax.
func NewCSPHeader ¶ added in v1.3.1
NewCSPHeader constructs a CSPHeader with the given directive name and initial source values.
func (*CSPHeader) Add ¶ added in v1.3.1
Add appends a single source value to this directive. Returns the receiver for chaining. Mutations are reflected in any CSPHeaders map that holds a pointer to this CSPHeader.
type CSPHeaders ¶ added in v1.3.1
CSPHeaders is a collection of CSPHeader directives keyed by directive name. Directive pointers are stored in the map, so mutations via Get().Add() or Get().Set() are reflected in the CSPHeaders value without re-inserting the directive. Use String() to serialize the full policy for use in a Content-Security-Policy response header.
func GetCSPHeaders ¶ added in v1.3.1
func GetCSPHeaders() CSPHeaders
GetCSPHeaders returns the baseline Content-Security-Policy directives used by b3tty's HTTP handlers. The returned CSPHeaders value contains the following directives:
- default-src 'none'
- script-src 'self' 'wasm-unsafe-eval' (callers must add a per-request nonce)
- style-src 'self' 'unsafe-inline'
- connect-src 'self'
- img-src 'self'
- frame-ancestors 'none'
- base-uri 'self'
script-src: allow same-origin module scripts plus the one inline script
that sets window.B3TTY, identified by its per-request nonce. 'wasm-unsafe-eval' is required for xterm.js which uses WebAssembly internally; it is more targeted than 'unsafe-eval' and does not permit JS eval().
style-src: allow same-origin stylesheets plus 'unsafe-inline' for the
dynamic <style> element the JS injects for theme background gradients.
connect-src 'self': covers same-origin fetch and ws:/wss: connections. frame-ancestors 'none': prevents the terminal from being embedded in an
iframe on any other page.
base-uri 'self': blocks <base> tag injection that could redirect relative
URLs to an attacker-controlled origin.
Callers that render HTML (e.g. displayTermHandler) should call csp.Get("script-src").Add("nonce-<value>") on the returned value to inject a per-request nonce before writing the CSP header to the response.
func NewCSPHeders ¶ added in v1.3.1
func NewCSPHeders(headers ...*CSPHeader) *CSPHeaders
NewCSPHeders constructs a CSPHeaders collection from the provided directives, keyed by each directive's Name field.
func (CSPHeaders) Add ¶ added in v1.3.1
func (chs CSPHeaders) Add(key string, header *CSPHeader) *CSPHeaders
Add inserts or replaces the directive stored under key. Returns a pointer to the (possibly updated) CSPHeaders for chaining.
func (CSPHeaders) Get ¶ added in v1.3.1
func (chs CSPHeaders) Get(key string) *CSPHeader
Get returns a pointer to the CSPHeader for the given directive name, or nil if no such directive exists. Mutating the returned pointer updates the entry in place, which is reflected in subsequent calls to String().
func (CSPHeaders) String ¶ added in v1.3.1
func (chs CSPHeaders) String() string
String serializes all directives into a single Content-Security-Policy header value, with each directive separated by a space. Directive order is not guaranteed because the underlying storage is a map.
type Client ¶
type Profile ¶
type Profile struct {
Root string
WorkingDirectory string
Shell string
Title string
Commands []string
}
func NewProfile ¶
func (*Profile) ApplyToCommand ¶
ApplyToCommand applies the Profile's settings to the given exec.Cmd. It sets the working directory based on the Profile's WorkingDirectory field, expanding $HOME and ~ to the user's home directory. If a custom shell is specified in the Profile, it replaces the last argument of the command with the custom shell. Returns the modified exec.Cmd and any error encountered.
func (*Profile) ParseCommands ¶
ParseCommands processes the Profile Commands and returns a slice of string slices. Each command in the Commands slice is trimmed of whitespace and split into arguments.
Returns:
- [][]string: A slice of string slices, where each inner slice represents a parsed command with its arguments.
- error: An error if any occurs during the parsing process, nil otherwise.
type TermConfig ¶
type TermConfig struct {
TLS bool `json:"tls"`
CursorBlink bool `json:"cursorBlink"`
FontFamily string `json:"fontFamily"`
FontSize int `json:"fontSize"`
Rows int `json:"rows"`
Columns int `json:"columns"`
Theme Theme `json:"theme"`
Uri string `json:"uri"`
Port int `json:"port"`
Debug bool `json:"debug"`
HasBackgroundImage bool `json:"backgroundImage"`
ThemeNames []string `json:"themeNames"`
AllThemeNames []string `json:"allThemeNames"`
BuiltinThemeNames []string `json:"builtinThemeNames"`
ProfileNames []string `json:"profileNames"`
ActiveTheme string `json:"activeTheme"`
}
type TerminalServer ¶
type TerminalServer struct {
Client *Client
Server *Server
Profiles map[string]Profile
Themes map[string]Theme
Token string
OrgCols uint16
OrgRows uint16
ProfileName string
StartupProfile string
ActiveTheme string
ConfigFile string
FailedAttempts int
FirstRun bool
NoBrowser bool
BackoffMu sync.Mutex
// AuthSleep is the function used to pause on auth failures. It defaults to
// time.Sleep and can be replaced in tests with a no-op to avoid real delays.
AuthSleep func(time.Duration)
}
TerminalServer bundles all mutable per-session state used by the HTTP handlers, making them independent of package-level globals and straightforward to test.
type Theme ¶
type Theme struct {
Foreground string `json:"foreground,omitempty"`
Background string `json:"background,omitempty"`
Cursor string `json:"cursor,omitempty"`
CursorAccent string `json:"cursorAccent,omitempty"`
SelectionForeground string `json:"selectionForeground,omitempty"`
SelectionBackground string `json:"selectionBackground,omitempty"`
Black string `json:"black,omitempty"`
BrightBlack string `json:"brightBlack,omitempty"`
Red string `json:"red,omitempty"`
BrightRed string `json:"brightRed,omitempty"`
Yellow string `json:"yellow,omitempty"`
BrightYellow string `json:"brightYellow,omitempty"`
Green string `json:"green,omitempty"`
BrightGreen string `json:"brightGreen,omitempty"`
Blue string `json:"blue,omitempty"`
BrightBlue string `json:"brightBlue,omitempty"`
Magenta string `json:"magenta,omitempty"`
BrightMagenta string `json:"brightMagenta,omitempty"`
Cyan string `json:"cyan,omitempty"`
BrightCyan string `json:"brightCyan,omitempty"`
White string `json:"white,omitempty"`
BrightWhite string `json:"brightWhite,omitempty"`
// BackgroundImage is a server-side file path and is intentionally excluded
// from JSON serialization to avoid exposing local paths to the browser.
BackgroundImage string `json:"-"`
}
func (*Theme) MapToTheme ¶
MapToTheme maps the key-value pairs from the given map to the corresponding fields of the Theme struct. It uses reflection to set the values of the struct fields based on the map keys. The map keys are expected to be in a format that can be converted to the struct field names. Only string values from the map are set to the corresponding struct fields.
Parameters:
- m: A map[string]any containing the theme properties to be set.
Note: This method modifies the Theme struct in-place.