v1.0.0
Golang lint / docker-smoke (push) Successful in 17s
Golang lint / lint (push) Failing after 18s

This commit is contained in:
2026-08-06 12:22:44 +03:00
parent a47ac72db0
commit bb2fa57bbe
21 changed files with 1075 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
// Package config loads est tunnel configuration and command-line options.
package config
import (
"errors"
"os"
"time"
"github.com/BurntSushi/toml"
)
// ProxyDirection identifies the direction of an SSH port forward.
type ProxyDirection string
const (
// RemoteToLocal creates a local SSH forward with the -L option.
RemoteToLocal ProxyDirection = "rtl"
// LocalToRemote creates a remote SSH forward with the -R option.
LocalToRemote ProxyDirection = "ltr"
)
const (
// DefaultRetryCount is the number of reconnect attempts used when retry is
// not specified.
DefaultRetryCount = 5
// DefaultRetryDelay is the wait between reconnect attempts when retry is
// not specified.
DefaultRetryDelay = 5 * time.Second
)
var (
// ErrInvalidRetryCount is returned when retry count is less than -1.
ErrInvalidRetryCount = errors.New("retry count must be -1 or greater")
// ErrInvalidRetryDelay is returned when retry delay is invalid or not positive.
ErrInvalidRetryDelay = errors.New("retry delay must be a positive duration")
)
// RetryConfig configures tunnel reconnection behavior.
// Count is the number of attempts after the initial SSH process exits; -1
// retries indefinitely. Delay uses time.ParseDuration syntax, such as "5s".
type RetryConfig struct {
Count *int `toml:"count"`
Delay string `toml:"delay"`
}
// RetrySettings is a validated retry configuration ready for execution.
type RetrySettings struct {
Count int
Delay time.Duration
}
// Settings resolves RetryConfig fields with est's default retry values.
func (retry RetryConfig) Settings() (RetrySettings, error) {
settings := RetrySettings{Count: DefaultRetryCount, Delay: DefaultRetryDelay}
if retry.Count != nil {
settings.Count = *retry.Count
}
if settings.Count < -1 {
return RetrySettings{}, ErrInvalidRetryCount
}
if retry.Delay != "" {
delay, err := time.ParseDuration(retry.Delay)
if err != nil || delay <= 0 {
return RetrySettings{}, ErrInvalidRetryDelay
}
settings.Delay = delay
}
return settings, nil
}
// ProxyEntry defines one SSH tunnel.
type ProxyEntry struct {
Direction ProxyDirection `toml:"direction"`
RemotePort uint16 `toml:"remotePort"`
RemoteIP string `toml:"remoteIP"`
LocalPort uint16 `toml:"localPort"`
LocalIP string `toml:"localIP"`
Host string `toml:"host"`
IdentityFile string `toml:"identityFile,omitempty"`
Retry *RetryConfig `toml:"retry"`
}
// Config is the TOML configuration used by est.
type Config struct {
SSHConfig string `toml:"sshConfig"`
Retry *RetryConfig `toml:"retry"`
Entries []ProxyEntry `toml:"entry"`
}
// RetrySettings resolves the retry configuration for entry. An entry retry
// block fully overrides the top-level retry block.
func (cfg Config) RetrySettings(entry ProxyEntry) (RetrySettings, error) {
if entry.Retry != nil {
return entry.Retry.Settings()
}
if cfg.Retry != nil {
return cfg.Retry.Settings()
}
return RetryConfig{}.Settings()
}
// LoadConfig decodes a TOML configuration file from path.
func LoadConfig(path string) (Config, error) {
var config Config
file, err := os.Open(path)
if err != nil {
return config, err
}
defer file.Close()
if _, err := toml.NewDecoder(file).Decode(&config); err != nil {
return config, err
}
return config, nil
}
+105
View File
@@ -0,0 +1,105 @@
package config
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
)
func intPtr(value int) *int {
return &value
}
func TestLoadConfig(t *testing.T) {
path := filepath.Join(t.TempDir(), "tunnels.toml")
contents := `sshConfig = "/etc/ssh/est.conf"
[retry]
count = 3
delay = "2s"
[[entry]]
direction = "ltr"
host = "vps"
localPort = 22
remotePort = 2222
identityFile = "/keys/vps"
[entry.retry]
count = -1
delay = "100ms"
`
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatalf("write config: %v", err)
}
cfg, err := LoadConfig(path)
if err != nil {
t.Fatalf("LoadConfig() error = %v", err)
}
if cfg.SSHConfig != "/etc/ssh/est.conf" {
t.Errorf("SSHConfig = %q, want %q", cfg.SSHConfig, "/etc/ssh/est.conf")
}
if len(cfg.Entries) != 1 {
t.Fatalf("entries = %d, want 1", len(cfg.Entries))
}
entry := cfg.Entries[0]
if entry.Direction != LocalToRemote || entry.Host != "vps" || entry.LocalPort != 22 || entry.RemotePort != 2222 || entry.IdentityFile != "/keys/vps" {
t.Errorf("entry = %#v, want decoded tunnel values", entry)
}
retry, err := cfg.RetrySettings(entry)
if err != nil {
t.Fatalf("RetrySettings() error = %v", err)
}
if retry.Count != -1 || retry.Delay != 100*time.Millisecond {
t.Errorf("entry retry = %#v, want count=-1 delay=100ms", retry)
}
}
func TestConfigRetrySettings(t *testing.T) {
tests := []struct {
name string
cfg Config
entry ProxyEntry
want RetrySettings
}{
{name: "defaults", want: RetrySettings{Count: DefaultRetryCount, Delay: DefaultRetryDelay}},
{name: "top level", cfg: Config{Retry: &RetryConfig{Count: intPtr(2), Delay: "1s"}}, want: RetrySettings{Count: 2, Delay: time.Second}},
{name: "entry override", cfg: Config{Retry: &RetryConfig{Count: intPtr(2), Delay: "1s"}}, entry: ProxyEntry{Retry: &RetryConfig{Count: intPtr(0), Delay: "50ms"}}, want: RetrySettings{Count: 0, Delay: 50 * time.Millisecond}},
{name: "entry override uses defaults", cfg: Config{Retry: &RetryConfig{Count: intPtr(-1), Delay: "1s"}}, entry: ProxyEntry{Retry: &RetryConfig{}}, want: RetrySettings{Count: DefaultRetryCount, Delay: DefaultRetryDelay}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.cfg.RetrySettings(tt.entry)
if err != nil {
t.Fatalf("RetrySettings() error = %v", err)
}
if got != tt.want {
t.Errorf("RetrySettings() = %#v, want %#v", got, tt.want)
}
})
}
}
func TestRetryConfigSettingsRejectsInvalidValues(t *testing.T) {
for _, retry := range []RetryConfig{
{Count: intPtr(-2)},
{Delay: "not-a-duration"},
{Delay: "0s"},
} {
if _, err := retry.Settings(); err == nil {
t.Errorf("Settings() error = nil for %#v", retry)
}
}
}
func TestLoadConfigMissingFile(t *testing.T) {
_, err := LoadConfig(filepath.Join(t.TempDir(), "missing.toml"))
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("LoadConfig() error = %v, want not-exist error", err)
}
}
+22
View File
@@ -0,0 +1,22 @@
package config
import (
"errors"
"github.com/spf13/pflag"
)
// Flags contains command-line options accepted by est.
type Flags struct {
ConfigPath string
}
// ParseFlags parses est command-line flags.
func ParseFlags() (*Flags, error) {
configPath := pflag.StringP("config", "c", "./config.toml", "toml config path")
pflag.Parse()
if configPath == nil {
return nil, errors.New("no config path")
}
return &Flags{ConfigPath: *configPath}, nil
}