core

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DoctorOK    = "ok"
	DoctorWarn  = "warn"
	DoctorError = "error"
)
View Source
const (
	ConfigExportVersion = 1
	ConfigExportApp     = "sshc"
)
View Source
const (
	HostKeyCheckKnownHosts = "known_hosts"
	HostKeyCheckInsecure   = "insecure"

	DefaultConnectTimeout  = "20s"
	DefaultRemoteScriptDir = "/tmp"
	DefaultKnownHostsPath  = "~/.ssh/known_hosts"
)
View Source
const (
	DefaultSSHPort  = 22
	DefaultGroup    = "default"
	ConfigVersion   = 1
	ConfigEnvKey    = "SSHC_CONFIG"
	ConfigDirEnvKey = "SSHC_CONFIG_DIR"
	ConfigFileName  = "sshc.config.json"

	HostBackendSSH          = "ssh"
	HostBackendCommandProxy = "command_proxy"
	CommandProxyCmdToken    = "{{cmd}}"
)
View Source
const MaskedSecret = "***"

Variables

This section is empty.

Functions

func AppendRunLog

func AppendRunLog(host Host, rec RunLogRecord) error

func ApplyHostImport added in v0.7.0

func ApplyHostImport(config *Config, plan HostImportPlan) error

func AuthLabel added in v0.6.0

func AuthLabel(host Host) string

func BackupConfigFile added in v0.7.0

func BackupConfigFile(now time.Time) (string, error)

func BuildRemoteCommand

func BuildRemoteCommand(command string, env map[string]string) (string, error)

func BuildRemoteCommandWithCWD

func BuildRemoteCommandWithCWD(command string, env map[string]string, cwd string) (string, error)

func BuildRemoteRunCommand added in v0.8.0

func BuildRemoteRunCommand(command string, opts RunOptions) (string, error)

func ConfigPathSource added in v0.9.0

func ConfigPathSource() string

func DecryptPassword added in v0.5.0

func DecryptPassword(value string) (string, error)

func EncryptConfigExport added in v0.7.0

func EncryptConfigExport(config Config, key string, now time.Time) ([]byte, error)

func EncryptPassword added in v0.5.0

func EncryptPassword(password string) (string, error)

func ErrorString

func ErrorString(err error) string

func ExecuteCommandProxy added in v0.8.0

func ExecuteCommandProxy(host Host, command string, opts RunOptions) ([]byte, error)

func ExecuteRemote

func ExecuteRemote(host Host, command string, opts RunOptions) ([]byte, error)

func GenerateExportKey added in v0.7.0

func GenerateExportKey() (string, error)

func HasDoctorErrors added in v0.6.0

func HasDoctorErrors(issues []DoctorIssue) bool

func HostBackend added in v0.8.0

func HostBackend(host Host) string

func HostGroupName

func HostGroupName(host Host) string

func HostLogName

func HostLogName(host Host) string

func IsCommandProxyHost added in v0.8.0

func IsCommandProxyHost(host Host) bool

func JoinRemotePath

func JoinRemotePath(base, elem string) string

func LoadRunEnv

func LoadRunEnv(file string, inline []string) (map[string]string, error)

func LocalDirPath

func LocalDirPath(remotePath, localPath string) string

func LocalFilePath

func LocalFilePath(remotePath, localPath string) string

func LoginCommandProxy added in v0.8.0

func LoginCommandProxy(host Host, opts LoginOptions) error

func LoginRemote

func LoginRemote(host Host) error

func LoginRemoteWithOptions

func LoginRemoteWithOptions(host Host, opts LoginOptions) error

func LogsDir added in v0.9.0

func LogsDir() (string, error)

func MergeImportedConfig added in v0.7.0

func MergeImportedConfig(current, imported Config, strategy ImportStrategy) (Config, ImportResult, error)

func NewRemoteScriptPath

func NewRemoteScriptPath(value time.Time) string

func NewRemoteScriptPathInDir

func NewRemoteScriptPathInDir(value time.Time, dir string) string

func NewRunTaskID added in v0.6.0

func NewRunTaskID(startedAt time.Time, host Host, rec RunLogRecord) string

func NormalizeHostFields added in v0.8.0

func NormalizeHostFields(host *Host)

func Now

func Now() time.Time

func ParseHostImport added in v0.7.0

func ParseHostImport(reader io.Reader, format HostImportFormat, defaults HostImportDefaults) ([]Host, []HostImportError)

func ParseHostImportCSV added in v0.7.0

func ParseHostImportCSV(reader io.Reader, defaults HostImportDefaults) ([]Host, []HostImportError)

func ParseHostImportIPs added in v0.7.0

func ParseHostImportIPs(reader io.Reader, defaults HostImportDefaults) ([]Host, []HostImportError)

func ParseHostImportPlain added in v0.7.0

func ParseHostImportPlain(reader io.Reader, defaults HostImportDefaults) ([]Host, []HostImportError)

func ParseHostKV added in v0.8.0

func ParseHostKV(text string, defaults HostImportDefaults) (Host, []HostImportError)

func ParseLogLineRange added in v0.6.0

func ParseLogLineRange(spec string) (start int, end int, ok bool, err error)

func ParseTimeout

func ParseTimeout(value string) (time.Duration, error)

func PasswordKeyPath added in v0.5.0

func PasswordKeyPath() (string, error)

func ReadConfigFile added in v0.6.0

func ReadConfigFile() (string, []byte, error)

func ReadHostsFile added in v0.6.0

func ReadHostsFile(path string) ([]string, error)

func ReadRunLogOutputByID added in v0.6.0

func ReadRunLogOutputByID(target, taskID string) ([]byte, error)

func ReadRunLogs

func ReadRunLogs(target, match string, tail int) ([]string, error)

func ReadRunLogsSelected added in v0.6.0

func ReadRunLogsSelected(target, match string, tail int, rangeSpec string) ([]string, error)

func RejectCommandProxyTransfer added in v0.8.0

func RejectCommandProxyTransfer(host Host) error

func RemoteFilePath

func RemoteFilePath(localPath, remotePath string) string

func RemoteRelPath

func RemoteRelPath(root, current string) string

func RenderCommandProxyRun added in v0.8.0

func RenderCommandProxyRun(template, finalCommand string) (string, error)

func RunOutputPath added in v0.6.0

func RunOutputPath(taskID string, startedAt time.Time) (string, string, error)

func RunStatus

func RunStatus(err error) string

func RuntimeCommandName added in v0.9.0

func RuntimeCommandName(args []string) string

func RuntimeDurationMS added in v0.9.0

func RuntimeDurationMS(start time.Time) int64

func RuntimeLogArgs added in v0.9.0

func RuntimeLogArgs(args []string) []string

func RuntimeLogPath added in v0.9.0

func RuntimeLogPath() (string, error)

func SaveConfig added in v0.6.0

func SaveConfig(config *Config) error

func SaveStore

func SaveStore(store *Store) error

func SelectLogLines added in v0.6.0

func SelectLogLines(lines []string, rangeSpec string, tail int) ([]string, error)

func SetNowForTest

func SetNowForTest(fn func() time.Time) func()

func SetUserHomeDirForTest

func SetUserHomeDirForTest(fn func() (string, error)) func()

func SinceMS

func SinceMS(start time.Time) int64

func StorePath

func StorePath() (string, error)

func ValidateCommandProxyTemplate added in v0.8.0

func ValidateCommandProxyTemplate(host Host) error

func ValidateSudoUser

func ValidateSudoUser(user string) error

Types

type AuthProfile added in v0.6.0

type AuthProfile struct {
	Name        string `json:"name"`
	User        string `json:"user,omitempty"`
	Password    string `json:"password,omitempty"`
	PasswordEnc string `json:"password_enc,omitempty"`
	KeyPath     string `json:"key_path,omitempty"`
	Remark      string `json:"remark,omitempty"`
}

func MaskAuthProfile added in v0.6.0

func MaskAuthProfile(profile AuthProfile) AuthProfile

type BatchHostSource added in v0.6.0

type BatchHostSource struct {
	Hosts     []string
	HostsFile string
	Group     string
	Overrides HostOverrides
	AuthRef   string
	AllowRaw  bool
}

type CommandProxyLoginPlan added in v0.8.0

type CommandProxyLoginPlan struct {
	Via          Host
	LoginCommand string
}

func PlanCommandProxyLogin added in v0.8.0

func PlanCommandProxyLogin(host Host) (CommandProxyLoginPlan, error)

type CommandProxyRunPlan added in v0.8.0

type CommandProxyRunPlan struct {
	Via            Host
	ProxiedCommand string
}

func PlanCommandProxyRun added in v0.8.0

func PlanCommandProxyRun(host Host, command string, opts RunOptions) (CommandProxyRunPlan, error)

type Config added in v0.6.0

type Config struct {
	Version      int           `json:"version"`
	LogsPath     string        `json:"logs_path,omitempty"`
	Defaults     Defaults      `json:"defaults,omitempty"`
	AuthProfiles []AuthProfile `json:"auth_profiles"`
	Hosts        []Host        `json:"hosts"`
}

func DecryptConfigExport added in v0.7.0

func DecryptConfigExport(data []byte, key string) (Config, error)

func LoadConfig added in v0.6.0

func LoadConfig() (*Config, error)

func LoadConfigWithSSHConfig added in v0.6.0

func LoadConfigWithSSHConfig() (*Config, error)

func MaskConfig added in v0.6.0

func MaskConfig(config Config) Config

func (Config) EffectiveHost added in v0.6.0

func (c Config) EffectiveHost(host Host, overrides HostOverrides) (EffectiveHost, bool, error)

func (Config) FindAuthProfile added in v0.6.0

func (c Config) FindAuthProfile(name string) (AuthProfile, bool)

func (Config) ResolveConnection added in v0.6.0

func (c Config) ResolveConnection(host Host) (ResolvedConnection, error)

func (Config) ResolveEffectiveHost added in v0.6.0

func (c Config) ResolveEffectiveHost(target string, overrides HostOverrides) (EffectiveHost, bool, error)

type ConfigExportFile added in v0.7.0

type ConfigExportFile struct {
	Version   int    `json:"version"`
	Cipher    string `json:"cipher"`
	KDF       string `json:"kdf"`
	CreatedAt string `json:"created_at"`
	Nonce     string `json:"nonce"`
	Payload   string `json:"payload"`
}

type ConfigExportPayload added in v0.7.0

type ConfigExportPayload struct {
	Version       int    `json:"version"`
	App           string `json:"app"`
	ConfigVersion int    `json:"config_version"`
	ExportedAt    string `json:"exported_at"`
	Config        Config `json:"config"`
}

type Defaults added in v0.6.0

type Defaults struct {
	User            string `json:"user,omitempty"`
	Port            int    `json:"port,omitempty"`
	ConnectTimeout  string `json:"connect_timeout,omitempty"`
	RunTimeout      string `json:"run_timeout,omitempty"`
	RemoteScriptDir string `json:"remote_script_dir,omitempty"`
	HostKeyCheck    string `json:"host_key_check,omitempty"`
	KnownHostsPath  string `json:"known_hosts_path,omitempty"`
}

type DoctorIssue added in v0.6.0

type DoctorIssue struct {
	Level   string `json:"level"`
	Item    string `json:"item"`
	Message string `json:"message"`
}

func CheckConfig added in v0.6.0

func CheckConfig(config Config) []DoctorIssue

type EffectiveHost added in v0.6.0

type EffectiveHost struct {
	Host
	ConnectTimeout  string
	RunTimeout      string
	RemoteScriptDir string
	HostKeyCheck    string
	KnownHostsPath  string
}

func (EffectiveHost) ToHost added in v0.6.0

func (h EffectiveHost) ToHost() Host

type Host

type Host struct {
	Name         string `json:"name"`
	IP           string `json:"ip"`
	AuthRef      string `json:"auth_ref,omitempty"`
	User         string `json:"user"`
	Password     string `json:"password,omitempty"`
	PasswordEnc  string `json:"password_enc,omitempty"`
	KeyPath      string `json:"key_path,omitempty"`
	Remark       string `json:"remark,omitempty"`
	Group        string `json:"group,omitempty"`
	Port         int    `json:"port,omitempty"`
	Jump         string `json:"jump,omitempty"`
	Backend      string `json:"backend,omitempty"`
	Via          string `json:"via,omitempty"`
	RunTemplate  string `json:"run_template,omitempty"`
	LoginCommand string `json:"login_command,omitempty"`

	ConnectTimeout  string `json:"connect_timeout,omitempty"`
	RunTimeout      string `json:"run_timeout,omitempty"`
	RemoteScriptDir string `json:"remote_script_dir,omitempty"`
	HostKeyCheck    string `json:"host_key_check,omitempty"`
	KnownHostsPath  string `json:"known_hosts_path,omitempty"`
}

func LoadSSHConfigHosts

func LoadSSHConfigHosts() ([]Host, error)

func MaskHost added in v0.6.0

func MaskHost(host Host) Host

func ParseSSHConfig

func ParseSSHConfig(reader io.Reader) ([]Host, error)

func ResolveBatchHosts added in v0.6.0

func ResolveBatchHosts(source BatchHostSource) ([]Host, error)

func ResolveHostWithSSHConfig added in v0.6.0

func ResolveHostWithSSHConfig(target string, overrides HostOverrides) (Host, bool, error)

type HostImportAction added in v0.7.0

type HostImportAction string
const (
	HostImportActionAdd    HostImportAction = "add"
	HostImportActionUpdate HostImportAction = "update"
	HostImportActionSkip   HostImportAction = "skip"
)

type HostImportChange added in v0.7.0

type HostImportChange struct {
	Action        HostImportAction
	Host          Host
	ExistingIndex int
	Reason        string
}

type HostImportConflict added in v0.7.0

type HostImportConflict struct {
	Host   Host
	Field  string
	Value  string
	Reason string
}

type HostImportDefaults added in v0.7.0

type HostImportDefaults struct {
	AuthRef         string
	User            string
	KeyPath         string
	Group           string
	Remark          string
	Port            int
	Jump            string
	Backend         string
	Via             string
	RunTemplate     string
	LoginCommand    string
	ConnectTimeout  string
	RunTimeout      string
	RemoteScriptDir string
	HostKeyCheck    string
	KnownHostsPath  string
}

type HostImportError added in v0.7.0

type HostImportError struct {
	Line    int
	Field   string
	Message string
}

func (HostImportError) Error added in v0.7.0

func (e HostImportError) Error() string

type HostImportFormat added in v0.7.0

type HostImportFormat string
const (
	HostImportIPs   HostImportFormat = "ips"
	HostImportPlain HostImportFormat = "plain"
	HostImportCSV   HostImportFormat = "csv"
)

type HostImportOptions added in v0.7.0

type HostImportOptions struct {
	Format       HostImportFormat
	Defaults     HostImportDefaults
	Overwrite    bool
	SkipExisting bool
}

type HostImportPlan added in v0.7.0

type HostImportPlan struct {
	Hosts     []Host
	Changes   []HostImportChange
	Added     int
	Updated   int
	Skipped   int
	Conflicts []HostImportConflict
	Invalid   []HostImportError
}

func PlanHostImport added in v0.7.0

func PlanHostImport(config Config, hosts []Host, opts HostImportOptions) (HostImportPlan, error)

type HostKeyTrustOptions added in v0.9.0

type HostKeyTrustOptions struct {
	Force bool
}

type HostKeyTrustResult added in v0.8.0

type HostKeyTrustResult struct {
	Host           Host
	Address        string
	KnownHostsPath string
	KeyType        string
	Fingerprint    string
	Status         string
}

func TrustHostKey added in v0.8.0

func TrustHostKey(host Host) (HostKeyTrustResult, error)

func TrustHostKeyWithOptions added in v0.9.0

func TrustHostKeyWithOptions(host Host, opts HostKeyTrustOptions) (HostKeyTrustResult, error)

type HostOverrides added in v0.6.0

type HostOverrides struct {
	User            string
	Password        string
	KeyPath         string
	Port            int
	ConnectTimeout  string
	RunTimeout      string
	RemoteScriptDir string
	HostKeyCheck    string
	KnownHostsPath  string
}

type ImportResult added in v0.7.0

type ImportResult struct {
	BackupPath   string
	HostsAdded   int
	HostsUpdated int
	AuthAdded    int
	AuthUpdated  int
}

type ImportStrategy added in v0.7.0

type ImportStrategy string
const (
	ImportMerge     ImportStrategy = "merge"
	ImportOverwrite ImportStrategy = "overwrite"
	ImportReplace   ImportStrategy = "replace"
)

type LoginOptions

type LoginOptions struct {
	Stdin  *os.File
	Stdout io.Writer
	Stderr io.Writer
	Term   string
}

type RemoteClient added in v0.6.0

type RemoteClient interface {
	Run(string) ([]byte, error)
	RunContext(context.Context, string) ([]byte, error)
	NewSession() (*ssh.Session, error)
	NewSftp(...sftp.ClientOption) (*sftp.Client, error)
	Close() error
}

type ResolveConnectionOptions added in v0.6.0

type ResolveConnectionOptions struct {
	Jump string
}

type ResolvedConnection added in v0.6.0

type ResolvedConnection struct {
	Target Host
	Jump   *Host
}

func ResolveConnectionForHost added in v0.6.0

func ResolveConnectionForHost(host Host) (ResolvedConnection, error)

func ResolveConnectionWithSSHConfig added in v0.6.0

func ResolveConnectionWithSSHConfig(target string, opts ResolveConnectionOptions) (ResolvedConnection, bool, error)

type RunLogRecord

type RunLogRecord struct {
	Target           string
	TaskID           string
	Command          string
	Status           string
	StartedAt        time.Time
	DurationMS       int64
	Output           string
	OutputFile       string
	OutputBytes      int64
	OutputSHA256     string
	OutputInline     bool
	OutputPreview    string
	Error            string
	CWD              string
	Script           string
	RemoteScript     string
	KeepRemoteScript bool
	Backend          string
	Via              string
	ProxiedCommand   string
}

type RunOptions

type RunOptions struct {
	Timeout          time.Duration
	KillAfter        time.Duration
	Env              map[string]string
	CWD              string
	Sudo             bool
	SudoUser         string
	ScriptPath       string
	RemoteScriptDir  string
	RemoteScriptPath string
	KeepRemoteScript bool
}

type RuntimeLogger added in v0.9.0

type RuntimeLogger struct {
	// contains filtered or unexported fields
}

func InitRuntimeLogger added in v0.9.0

func InitRuntimeLogger() (*RuntimeLogger, error)

func (*RuntimeLogger) Close added in v0.9.0

func (l *RuntimeLogger) Close() error

type Store

type Store struct {
	LogsPath string `json:"logs_path,omitempty"`
	Hosts    []Host `json:"hosts"`
}

func LoadConfigSettings added in v0.5.0

func LoadConfigSettings() (Store, error)

func LoadStore

func LoadStore() (*Store, error)

func LoadStoreWithSSHConfig

func LoadStoreWithSSHConfig() (*Store, error)

func (Store) Find

func (s Store) Find(target string) (Host, bool)

func (Store) MatchHosts

func (s Store) MatchHosts(target string) []Host

func (Store) ResolveHost

func (s Store) ResolveHost(target string) (Host, bool, error)

func (*Store) Upsert

func (s *Store) Upsert(host Host) error

type TransferJob

type TransferJob struct {
	LocalPath  string
	RemotePath string
	RemoteDir  bool
}

type TransferOptions

type TransferOptions struct {
	SHA256    bool
	RemoveDir bool
}

type TransferResult

type TransferResult struct {
	Bytes        int64
	Files        int
	Directories  int
	Elapsed      time.Duration
	LocalSHA256  string
	RemoteSHA256 string
	SHA256OK     bool
}

func FetchRemote

func FetchRemote(host Host, remotePath, localPath string, opts TransferOptions) (result TransferResult, err error)

func UploadRemote

func UploadRemote(host Host, localPath, remotePath string, opts TransferOptions) (result TransferResult, err error)

func UploadRemoteBatch

func UploadRemoteBatch(host Host, jobs []TransferJob, opts TransferOptions) (result TransferResult, err error)

type UnknownHostKey added in v0.8.0

type UnknownHostKey struct {
	Hostname       string
	Remote         net.Addr
	Key            ssh.PublicKey
	KnownHostsPath string
}

type WebPTY added in v0.9.0

type WebPTY interface {
	io.Reader
	io.Writer
	Resize(cols, rows int) error
	Close() error
	Wait() error
}

func OpenWebPTY added in v0.9.0

func OpenWebPTY(host Host, opts WebPTYOptions) (WebPTY, error)

type WebPTYOptions added in v0.9.0

type WebPTYOptions struct {
	Cols int
	Rows int
	Term string
}

Jump to

Keyboard shortcuts

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