File
Blob: tun/client/config.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | "os" |
| 8 | "runtime" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "go.miragespace.co/specter/spec/pki" |
| 13 | |
| 14 | "go.yaml.in/yaml/v3" |
| 15 | ) |
| 16 | |
| 17 | type Tunnel struct { |
| 18 | parsed *url.URL |
| 19 | Target string `yaml:"target" json:"target"` |
| 20 | Hostname string `yaml:"hostname,omitempty" json:"hostname,omitempty"` |
| 21 | Insecure bool `yaml:"insecure,omitempty" json:"insecure"` |
| 22 | ProxyHeaderTimeout time.Duration `yaml:"headerTimeout,omitempty" json:"headerTimeout,omitempty"` |
| 23 | ProxyHeaderHost string `yaml:"headerHost,omitempty" json:"headerHost,omitempty"` |
| 24 | ProxyHeaderMode string `yaml:"headerMode,omitempty" json:"headerMode,omitempty"` |
| 25 | } |
| 26 | |
| 27 | func (t *Tunnel) UnmarshalYAML(value *yaml.Node) error { |
| 28 | type tunnelAlias Tunnel |
| 29 | var aux struct { |
| 30 | tunnelAlias `yaml:",inline"` |
| 31 | // legacy names for backward compatibility |
| 32 | OldHeaderTimeout time.Duration `yaml:"proxyHeaderTimeout,omitempty"` |
| 33 | OldHeaderHost string `yaml:"proxyHeaderHost,omitempty"` |
| 34 | OldHeaderMode string `yaml:"proxyHeaderMode,omitempty"` |
| 35 | } |
| 36 | if err := value.Decode(&aux); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | *t = Tunnel(aux.tunnelAlias) |
| 40 | |
| 41 | // If new header* fields are not set, fall back to legacy proxyHeader* values. |
| 42 | // This ensures configs using header* take precedence when both are present. |
| 43 | if aux.OldHeaderTimeout != 0 && t.ProxyHeaderTimeout == 0 { |
| 44 | t.ProxyHeaderTimeout = aux.OldHeaderTimeout |
| 45 | } |
| 46 | if aux.OldHeaderHost != "" && t.ProxyHeaderHost == "" { |
| 47 | t.ProxyHeaderHost = aux.OldHeaderHost |
| 48 | } |
| 49 | if aux.OldHeaderMode != "" && t.ProxyHeaderMode == "" { |
| 50 | t.ProxyHeaderMode = aux.OldHeaderMode |
| 51 | } |
| 52 | |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | type Config struct { |
| 57 | path string |
| 58 | Version int `yaml:"version" json:"version"` |
| 59 | Apex string `yaml:"apex" json:"apex"` |
| 60 | Certificate string `yaml:"certificate,omitempty" json:"certificate,omitempty"` |
| 61 | PrivKey string `yaml:"privKey,omitempty" json:"privKey,omitempty"` |
| 62 | Tunnels []Tunnel `yaml:"tunnels,omitempty" json:"tunnels,omitempty"` |
| 63 | } |
| 64 | |
| 65 | func NewConfig(path string) (*Config, error) { |
| 66 | cfg := &Config{ |
| 67 | path: path, |
| 68 | } |
| 69 | if err := cfg.readFile(); err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | if err := cfg.checkVersion(); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | if err := cfg.validate(); err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | return cfg, nil |
| 79 | } |
| 80 | |
| 81 | func (c *Config) clone() *Config { |
| 82 | cfg := *c |
| 83 | cfg.Tunnels = make([]Tunnel, len(c.Tunnels)) |
| 84 | for i := range c.Tunnels { |
| 85 | cfg.Tunnels[i] = c.Tunnels[i] |
| 86 | cfg.Tunnels[i].parsed = nil |
| 87 | } |
| 88 | cfg.validate() |
| 89 | return &cfg |
| 90 | } |
| 91 | |
| 92 | func (c *Config) checkVersion() error { |
| 93 | if c.Version != 2 { |
| 94 | return fmt.Errorf("expecting config version 2, got %v; migration is needed", c.Version) |
| 95 | } |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | func (c *Config) validate() error { |
| 100 | for i, tunnel := range c.Tunnels { |
| 101 | u, err := parseTarget(tunnel.Target) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | switch tunnel.ProxyHeaderMode { |
| 107 | case "", "target", "hostname", "custom": |
| 108 | default: |
| 109 | return fmt.Errorf("unsupported proxyHeaderMode %q", tunnel.ProxyHeaderMode) |
| 110 | } |
| 111 | |
| 112 | if tunnel.ProxyHeaderMode == "custom" && tunnel.ProxyHeaderHost == "" { |
| 113 | return fmt.Errorf("proxyHeaderMode 'custom' requires proxyHeaderHost to be set") |
| 114 | } |
| 115 | |
| 116 | if (u.Scheme == "unix" || u.Scheme == "winio") && tunnel.ProxyHeaderMode == "target" { |
| 117 | return fmt.Errorf("proxyHeaderMode 'target' is not valid for pipe targets; use 'hostname' or 'custom'") |
| 118 | } |
| 119 | c.Tunnels[i].parsed = u |
| 120 | } |
| 121 | if c.PrivKey == "" { |
| 122 | _, c.PrivKey = pki.GeneratePrivKey() |
| 123 | } |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | func (c *Config) reloadFile() error { |
| 128 | f, err := os.Open(c.path) |
| 129 | if err != nil { |
| 130 | return fmt.Errorf("error opening config file for reading: %w", err) |
| 131 | } |
| 132 | defer f.Close() |
| 133 | |
| 134 | next := &Config{} |
| 135 | if err := yaml.NewDecoder(f).Decode(next); err != nil { |
| 136 | return fmt.Errorf("error decoding config file: %w", err) |
| 137 | } |
| 138 | |
| 139 | if err := next.validate(); err != nil { |
| 140 | return fmt.Errorf("error validating config file: %w", err) |
| 141 | } |
| 142 | |
| 143 | c.Tunnels = next.Tunnels |
| 144 | return nil |
| 145 | } |
| 146 | |
| 147 | func (c *Config) readFile() error { |
| 148 | f, err := os.Open(c.path) |
| 149 | if err != nil { |
| 150 | return fmt.Errorf("error opening config file for reading: %w", err) |
| 151 | } |
| 152 | defer f.Close() |
| 153 | return yaml.NewDecoder(f).Decode(c) |
| 154 | } |
| 155 | |
| 156 | func (c *Config) writeFile() (err error) { |
| 157 | f, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
| 158 | if err != nil { |
| 159 | return fmt.Errorf("error opening config file for writing: %w", err) |
| 160 | } |
| 161 | defer func() { err = errors.Join(err, f.Close()) }() |
| 162 | |
| 163 | encoder := yaml.NewEncoder(f) |
| 164 | encoder.SetIndent(2) |
| 165 | if err := encoder.Encode(c); err != nil { |
| 166 | return err |
| 167 | } |
| 168 | if err := encoder.Close(); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | return f.Sync() |
| 172 | } |
| 173 | |
| 174 | func parseTarget(target string) (*url.URL, error) { |
| 175 | u, err := url.Parse(target) |
| 176 | if err != nil { |
| 177 | return nil, fmt.Errorf("error parsing target %s: %w", target, err) |
| 178 | } |
| 179 | switch u.Scheme { |
| 180 | case "http", "https", "tcp", "unix": |
| 181 | default: |
| 182 | if strings.HasPrefix(u.Path, "\\\\.\\pipe") { |
| 183 | u.Scheme = "winio" |
| 184 | break |
| 185 | } |
| 186 | return nil, fmt.Errorf("unsupported scheme. valid schemes: http, https, tcp, or unix; got %s", u.Scheme) |
| 187 | } |
| 188 | switch u.Scheme { |
| 189 | case "winio": |
| 190 | if runtime.GOOS != "windows" { |
| 191 | return nil, errors.New("named pipe is not supported on non-Windows platform") |
| 192 | } |
| 193 | case "unix": |
| 194 | if runtime.GOOS == "windows" { |
| 195 | return nil, errors.New("unix socket is not supported on non-Unix platform") |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return u, nil |
| 200 | } |