| 9 | "github.com/fatedier/frp/client/configmgmt" |
| 10 | "github.com/fatedier/frp/client/proxy" |
| 11 | "github.com/fatedier/frp/pkg/config" |
| 12 | "github.com/fatedier/frp/pkg/config/source" |
| 13 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 14 | "github.com/fatedier/frp/pkg/config/v1/validation" |
| 15 | "github.com/fatedier/frp/pkg/util/log" |
| 16 | ) |
| 17 |
| 18 | type serviceConfigManager struct { |
| 19 | svr *Service |
| 20 | } |
| 21 |
| 22 | func newServiceConfigManager(svr *Service) configmgmt.ConfigManager { |
| 23 | return &serviceConfigManager{svr: svr} |
| 24 | } |
| 25 |
| 26 | func (m *serviceConfigManager) ReloadFromFile(strict bool) error { |
| 27 | if m.svr.configFilePath == "" { |
| 28 | return fmt.Errorf("%w: frpc has no config file path", configmgmt.ErrInvalidArgument) |
| 29 | } |
| 30 |
| 31 | result, err := config.LoadClientConfigResult(m.svr.configFilePath, strict) |
| 32 | if err != nil { |
| 33 | return fmt.Errorf("%w: %v", configmgmt.ErrInvalidArgument, err) |
| 34 | } |
| 35 |
| 36 | proxyCfgsForValidation, visitorCfgsForValidation := config.FilterClientConfigurers( |
| 37 | result.Common, |
| 38 | result.Proxies, |
| 39 | result.Visitors, |
| 40 | ) |
| 41 | proxyCfgsForValidation = config.CompleteProxyConfigurers(proxyCfgsForValidation) |
| 42 | visitorCfgsForValidation = config.CompleteVisitorConfigurers(visitorCfgsForValidation) |
| 43 |
| 44 | if _, err := validation.ValidateAllClientConfig(result.Common, proxyCfgsForValidation, visitorCfgsForValidation, m.svr.unsafeFeatures); err != nil { |
| 45 | return fmt.Errorf("%w: %v", configmgmt.ErrInvalidArgument, err) |
| 46 | } |
| 47 |
| 48 | if err := m.svr.UpdateConfigSource(result.Common, result.Proxies, result.Visitors); err != nil { |
| 49 | return fmt.Errorf("%w: %v", configmgmt.ErrApplyConfig, err) |
| 50 | } |
| 51 |
| 52 | log.Infof("success reload conf") |
| 53 | return nil |
| 54 | } |
| 55 |
| 56 | func (m *serviceConfigManager) ReadConfigFile() (string, error) { |
| 57 | if m.svr.configFilePath == "" { |
| 58 | return "", fmt.Errorf("%w: frpc has no config file path", configmgmt.ErrInvalidArgument) |
| 59 | } |
| 60 |
| 61 | content, err := os.ReadFile(m.svr.configFilePath) |
| 62 | if err != nil { |
| 63 | return "", fmt.Errorf("%w: %v", configmgmt.ErrInvalidArgument, err) |
| 64 | } |
| 65 | return string(content), nil |
| 66 | } |
| 67 |
| 68 | func (m *serviceConfigManager) WriteConfigFile(content []byte) error { |
| 69 | if len(content) == 0 { |
| 70 | return fmt.Errorf("%w: body can't be empty", configmgmt.ErrInvalidArgument) |
| 71 | } |
| 72 |
| 73 | if err := os.WriteFile(m.svr.configFilePath, content, 0o600); err != nil { |
| 74 | return err |
| 75 | } |
| 76 | return nil |
| 77 | } |
| 78 |
| 79 | func (m *serviceConfigManager) GetProxyStatus() []*proxy.WorkingStatus { |
| 80 | return m.svr.getAllProxyStatus() |
| 81 | } |
| 82 |
| 83 | func (m *serviceConfigManager) GetProxyConfig(name string) (v1.ProxyConfigurer, bool) { |
| 84 | // Try running proxy manager first |
| 85 | ws, ok := m.svr.getProxyStatus(name) |
| 86 | if ok { |
| 87 | return ws.Cfg, true |
| 88 | } |
| 89 |
| 90 | // Fallback to store |
| 91 | m.svr.reloadMu.Lock() |
| 92 | storeSource := m.svr.storeSource |
| 93 | m.svr.reloadMu.Unlock() |
| 94 |
| 95 | if storeSource != nil { |
| 96 | cfg := storeSource.GetProxy(name) |
| 97 | if cfg != nil { |
| 98 | return cfg, true |
| 99 | } |
| 100 | } |
| 101 | return nil, false |
| 102 | } |
| 103 |
| 104 | func (m *serviceConfigManager) GetVisitorConfig(name string) (v1.VisitorConfigurer, bool) { |
| 105 | // Try running visitor manager first |
| 106 | cfg, ok := m.svr.getVisitorCfg(name) |
| 107 | if ok { |
| 108 | return cfg, true |
| 109 | } |
| 110 |
| 111 | // Fallback to store |
| 112 | m.svr.reloadMu.Lock() |
| 113 | storeSource := m.svr.storeSource |
| 114 | m.svr.reloadMu.Unlock() |
| 115 |
| 116 | if storeSource != nil { |
| 117 | vcfg := storeSource.GetVisitor(name) |
| 118 | if vcfg != nil { |
| 119 | return vcfg, true |
| 120 | } |
| 121 | } |
| 122 | return nil, false |
| 123 | } |
| 124 |
| 125 | func (m *serviceConfigManager) IsStoreProxyEnabled(name string) bool { |
| 126 | if name == "" { |
| 127 | return false |
| 128 | } |
| 129 |
| 130 | m.svr.reloadMu.Lock() |
| 131 | storeSource := m.svr.storeSource |
| 132 | m.svr.reloadMu.Unlock() |
| 133 |
| 134 | if storeSource == nil { |
| 135 | return false |
| 136 | } |
| 137 |
| 138 | cfg := storeSource.GetProxy(name) |
| 139 | if cfg == nil { |
| 140 | return false |
| 141 | } |
| 142 | enabled := cfg.GetBaseConfig().Enabled |
| 143 | return enabled == nil || *enabled |
| 144 | } |
| 145 |
| 146 | func (m *serviceConfigManager) StoreEnabled() bool { |
| 147 | m.svr.reloadMu.Lock() |
| 148 | storeSource := m.svr.storeSource |
| 149 | m.svr.reloadMu.Unlock() |
| 150 | return storeSource != nil |
| 151 | } |
| 152 |
| 153 | func (m *serviceConfigManager) ListStoreProxies() ([]v1.ProxyConfigurer, error) { |
| 154 | storeSource, err := m.storeSourceOrError() |
| 155 | if err != nil { |
| 156 | return nil, err |
| 157 | } |
| 158 | return storeSource.GetAllProxies() |
| 159 | } |
| 160 |
| 161 | func (m *serviceConfigManager) GetStoreProxy(name string) (v1.ProxyConfigurer, error) { |
| 162 | if name == "" { |
| 163 | return nil, fmt.Errorf("%w: proxy name is required", configmgmt.ErrInvalidArgument) |
| 164 | } |
| 165 |
| 166 | storeSource, err := m.storeSourceOrError() |
| 167 | if err != nil { |
| 168 | return nil, err |
| 169 | } |
| 170 |
| 171 | cfg := storeSource.GetProxy(name) |
| 172 | if cfg == nil { |
| 173 | return nil, fmt.Errorf("%w: proxy %q", configmgmt.ErrNotFound, name) |
| 174 | } |
| 175 | return cfg, nil |
| 176 | } |
| 177 |
| 178 | func (m *serviceConfigManager) CreateStoreProxy(cfg v1.ProxyConfigurer) (v1.ProxyConfigurer, error) { |
| 179 | if err := m.validateStoreProxyConfigurer(cfg); err != nil { |
| 180 | return nil, fmt.Errorf("%w: validation error: %v", configmgmt.ErrInvalidArgument, err) |
| 181 | } |
| 182 |
| 183 | name := cfg.GetBaseConfig().Name |
| 184 | persisted, err := m.withStoreProxyMutationAndReload(name, func(storeSource *source.StoreSource) error { |
| 185 | if err := storeSource.AddProxy(cfg); err != nil { |
| 186 | if errors.Is(err, source.ErrAlreadyExists) { |
| 187 | return fmt.Errorf("%w: %v", configmgmt.ErrConflict, err) |
| 188 | } |
| 189 | return err |
| 190 | } |
| 191 | return nil |
| 192 | }) |
| 193 | if err != nil { |
| 194 | return nil, err |
| 195 | } |
| 196 | log.Infof("store: created proxy %q", name) |
| 197 | return persisted, nil |
| 198 | } |
| 199 |
| 200 | func (m *serviceConfigManager) UpdateStoreProxy(name string, cfg v1.ProxyConfigurer) (v1.ProxyConfigurer, error) { |
| 201 | if name == "" { |
| 202 | return nil, fmt.Errorf("%w: proxy name is required", configmgmt.ErrInvalidArgument) |
| 203 | } |
| 204 | if cfg == nil { |
| 205 | return nil, fmt.Errorf("%w: invalid proxy config: type is required", configmgmt.ErrInvalidArgument) |
| 206 | } |
| 207 | bodyName := cfg.GetBaseConfig().Name |
| 208 | if bodyName != name { |
| 209 | return nil, fmt.Errorf("%w: proxy name in URL must match name in body", configmgmt.ErrInvalidArgument) |
| 210 | } |
| 211 | if err := m.validateStoreProxyConfigurer(cfg); err != nil { |
| 212 | return nil, fmt.Errorf("%w: validation error: %v", configmgmt.ErrInvalidArgument, err) |
| 213 | } |
| 214 |
| 215 | persisted, err := m.withStoreProxyMutationAndReload(name, func(storeSource *source.StoreSource) error { |
| 216 | if err := storeSource.UpdateProxy(cfg); err != nil { |
| 217 | if errors.Is(err, source.ErrNotFound) { |
| 218 | return fmt.Errorf("%w: %v", configmgmt.ErrNotFound, err) |
| 219 | } |
| 220 | return err |
| 221 | } |
| 222 | return nil |
| 223 | }) |
| 224 | if err != nil { |
| 225 | return nil, err |
| 226 | } |
| 227 |
| 228 | log.Infof("store: updated proxy %q", name) |
| 229 | return persisted, nil |
| 230 | } |
| 231 |
| 232 | func (m *serviceConfigManager) DeleteStoreProxy(name string) error { |
| 233 | if name == "" { |
| 234 | return fmt.Errorf("%w: proxy name is required", configmgmt.ErrInvalidArgument) |
| 235 | } |
| 236 |
| 237 | if err := m.withStoreMutationAndReload(func(storeSource *source.StoreSource) error { |
| 238 | if err := storeSource.RemoveProxy(name); err != nil { |
| 239 | if errors.Is(err, source.ErrNotFound) { |
| 240 | return fmt.Errorf("%w: %v", configmgmt.ErrNotFound, err) |
| 241 | } |
| 242 | return err |
| 243 | } |
| 244 | return nil |
| 245 | }); err != nil { |
| 246 | return err |
| 247 | } |
| 248 |
| 249 | log.Infof("store: deleted proxy %q", name) |
| 250 | return nil |
| 251 | } |
| 252 |
| 253 | func (m *serviceConfigManager) ListStoreVisitors() ([]v1.VisitorConfigurer, error) { |
| 254 | storeSource, err := m.storeSourceOrError() |
| 255 | if err != nil { |
| 256 | return nil, err |
| 257 | } |
| 258 | return storeSource.GetAllVisitors() |
| 259 | } |
| 260 |
| 261 | func (m *serviceConfigManager) GetStoreVisitor(name string) (v1.VisitorConfigurer, error) { |
| 262 | if name == "" { |
| 263 | return nil, fmt.Errorf("%w: visitor name is required", configmgmt.ErrInvalidArgument) |
| 264 | } |
| 265 |
| 266 | storeSource, err := m.storeSourceOrError() |
| 267 | if err != nil { |
| 268 | return nil, err |
| 269 | } |
| 270 |
| 271 | cfg := storeSource.GetVisitor(name) |
| 272 | if cfg == nil { |
| 273 | return nil, fmt.Errorf("%w: visitor %q", configmgmt.ErrNotFound, name) |
| 274 | } |
| 275 | return cfg, nil |
| 276 | } |
| 277 |
| 278 | func (m *serviceConfigManager) CreateStoreVisitor(cfg v1.VisitorConfigurer) (v1.VisitorConfigurer, error) { |
| 279 | if err := m.validateStoreVisitorConfigurer(cfg); err != nil { |
| 280 | return nil, fmt.Errorf("%w: validation error: %v", configmgmt.ErrInvalidArgument, err) |
| 281 | } |
| 282 |
| 283 | name := cfg.GetBaseConfig().Name |
| 284 | persisted, err := m.withStoreVisitorMutationAndReload(name, func(storeSource *source.StoreSource) error { |
| 285 | if err := storeSource.AddVisitor(cfg); err != nil { |
| 286 | if errors.Is(err, source.ErrAlreadyExists) { |
| 287 | return fmt.Errorf("%w: %v", configmgmt.ErrConflict, err) |
| 288 | } |
| 289 | return err |
| 290 | } |
| 291 | return nil |
| 292 | }) |
| 293 | if err != nil { |
| 294 | return nil, err |
| 295 | } |
| 296 |
| 297 | log.Infof("store: created visitor %q", name) |
| 298 | return persisted, nil |
| 299 | } |
| 300 |
| 301 | func (m *serviceConfigManager) UpdateStoreVisitor(name string, cfg v1.VisitorConfigurer) (v1.VisitorConfigurer, error) { |
| 302 | if name == "" { |
| 303 | return nil, fmt.Errorf("%w: visitor name is required", configmgmt.ErrInvalidArgument) |
| 304 | } |
| 305 | if cfg == nil { |
| 306 | return nil, fmt.Errorf("%w: invalid visitor config: type is required", configmgmt.ErrInvalidArgument) |
| 307 | } |
| 308 | bodyName := cfg.GetBaseConfig().Name |
| 309 | if bodyName != name { |
| 310 | return nil, fmt.Errorf("%w: visitor name in URL must match name in body", configmgmt.ErrInvalidArgument) |
| 311 | } |
| 312 | if err := m.validateStoreVisitorConfigurer(cfg); err != nil { |
| 313 | return nil, fmt.Errorf("%w: validation error: %v", configmgmt.ErrInvalidArgument, err) |
| 314 | } |
| 315 |
| 316 | persisted, err := m.withStoreVisitorMutationAndReload(name, func(storeSource *source.StoreSource) error { |
| 317 | if err := storeSource.UpdateVisitor(cfg); err != nil { |
| 318 | if errors.Is(err, source.ErrNotFound) { |
| 319 | return fmt.Errorf("%w: %v", configmgmt.ErrNotFound, err) |
| 320 | } |
| 321 | return err |
| 322 | } |
| 323 | return nil |
| 324 | }) |
| 325 | if err != nil { |
| 326 | return nil, err |
| 327 | } |
| 328 |
| 329 | log.Infof("store: updated visitor %q", name) |
| 330 | return persisted, nil |
| 331 | } |
| 332 |
| 333 | func (m *serviceConfigManager) DeleteStoreVisitor(name string) error { |
| 334 | if name == "" { |
| 335 | return fmt.Errorf("%w: visitor name is required", configmgmt.ErrInvalidArgument) |
| 336 | } |
| 337 |
| 338 | if err := m.withStoreMutationAndReload(func(storeSource *source.StoreSource) error { |
| 339 | if err := storeSource.RemoveVisitor(name); err != nil { |
| 340 | if errors.Is(err, source.ErrNotFound) { |
| 341 | return fmt.Errorf("%w: %v", configmgmt.ErrNotFound, err) |
| 342 | } |
| 343 | return err |
| 344 | } |
| 345 | return nil |
| 346 | }); err != nil { |
| 347 | return err |
| 348 | } |
| 349 |
| 350 | log.Infof("store: deleted visitor %q", name) |
| 351 | return nil |
| 352 | } |
| 353 |
| 354 | func (m *serviceConfigManager) GracefulClose(d time.Duration) { |
| 355 | m.svr.GracefulClose(d) |
| 356 | } |
| 357 |
| 358 | func (m *serviceConfigManager) storeSourceOrError() (*source.StoreSource, error) { |
| 359 | m.svr.reloadMu.Lock() |
| 360 | storeSource := m.svr.storeSource |
| 361 | m.svr.reloadMu.Unlock() |
| 362 |
| 363 | if storeSource == nil { |
| 364 | return nil, fmt.Errorf("%w: store API is disabled", configmgmt.ErrStoreDisabled) |
| 365 | } |
| 366 | return storeSource, nil |
| 367 | } |
| 368 |
| 369 | func (m *serviceConfigManager) withStoreMutationAndReload( |
| 370 | fn func(storeSource *source.StoreSource) error, |
| 371 | ) error { |
| 372 | m.svr.reloadMu.Lock() |
| 373 | defer m.svr.reloadMu.Unlock() |
| 374 |
| 375 | storeSource := m.svr.storeSource |
| 376 | if storeSource == nil { |
| 377 | return fmt.Errorf("%w: store API is disabled", configmgmt.ErrStoreDisabled) |
| 378 | } |
| 379 |
| 380 | if err := fn(storeSource); err != nil { |
| 381 | return err |
| 382 | } |
| 383 |
| 384 | if err := m.svr.reloadConfigFromSourcesLocked(); err != nil { |
| 385 | return fmt.Errorf("%w: failed to apply config: %v", configmgmt.ErrApplyConfig, err) |
| 386 | } |
| 387 | return nil |
| 388 | } |
| 389 |
| 390 | func (m *serviceConfigManager) withStoreProxyMutationAndReload( |
| 391 | name string, |
| 392 | fn func(storeSource *source.StoreSource) error, |
| 393 | ) (v1.ProxyConfigurer, error) { |
| 394 | m.svr.reloadMu.Lock() |
| 395 | defer m.svr.reloadMu.Unlock() |
| 396 |
| 397 | storeSource := m.svr.storeSource |
| 398 | if storeSource == nil { |
| 399 | return nil, fmt.Errorf("%w: store API is disabled", configmgmt.ErrStoreDisabled) |
| 400 | } |
| 401 |
| 402 | if err := fn(storeSource); err != nil { |
| 403 | return nil, err |
| 404 | } |
| 405 | if err := m.svr.reloadConfigFromSourcesLocked(); err != nil { |
| 406 | return nil, fmt.Errorf("%w: failed to apply config: %v", configmgmt.ErrApplyConfig, err) |
| 407 | } |
| 408 |
| 409 | persisted := storeSource.GetProxy(name) |
| 410 | if persisted == nil { |
| 411 | return nil, fmt.Errorf("%w: proxy %q not found in store after mutation", configmgmt.ErrApplyConfig, name) |
| 412 | } |
| 413 | return persisted.Clone(), nil |
| 414 | } |
| 415 |
| 416 | func (m *serviceConfigManager) withStoreVisitorMutationAndReload( |
| 417 | name string, |
| 418 | fn func(storeSource *source.StoreSource) error, |
| 419 | ) (v1.VisitorConfigurer, error) { |
| 420 | m.svr.reloadMu.Lock() |
| 421 | defer m.svr.reloadMu.Unlock() |
| 422 |
| 423 | storeSource := m.svr.storeSource |
| 424 | if storeSource == nil { |
| 425 | return nil, fmt.Errorf("%w: store API is disabled", configmgmt.ErrStoreDisabled) |
| 426 | } |
| 427 |
| 428 | if err := fn(storeSource); err != nil { |
| 429 | return nil, err |
| 430 | } |
| 431 | if err := m.svr.reloadConfigFromSourcesLocked(); err != nil { |
| 432 | return nil, fmt.Errorf("%w: failed to apply config: %v", configmgmt.ErrApplyConfig, err) |
| 433 | } |
| 434 |
| 435 | persisted := storeSource.GetVisitor(name) |
| 436 | if persisted == nil { |
| 437 | return nil, fmt.Errorf("%w: visitor %q not found in store after mutation", configmgmt.ErrApplyConfig, name) |
| 438 | } |
| 439 | return persisted.Clone(), nil |
| 440 | } |
| 441 |
| 442 | func (m *serviceConfigManager) validateStoreProxyConfigurer(cfg v1.ProxyConfigurer) error { |
| 443 | if cfg == nil { |
| 444 | return fmt.Errorf("invalid proxy config") |
| 445 | } |
| 446 | runtimeCfg := cfg.Clone() |
| 447 | if runtimeCfg == nil { |
| 448 | return fmt.Errorf("invalid proxy config") |
| 449 | } |
| 450 | runtimeCfg.Complete() |
| 451 | return validation.ValidateProxyConfigurerForClient(runtimeCfg) |
| 452 | } |
| 453 |
| 454 | func (m *serviceConfigManager) validateStoreVisitorConfigurer(cfg v1.VisitorConfigurer) error { |
| 455 | if cfg == nil { |
| 456 | return fmt.Errorf("invalid visitor config") |
| 457 | } |
| 458 | runtimeCfg := cfg.Clone() |
| 459 | if runtimeCfg == nil { |
| 460 | return fmt.Errorf("invalid visitor config") |
| 461 | } |
| 462 | runtimeCfg.Complete() |
| 463 | return validation.ValidateVisitorConfigurer(runtimeCfg) |
| 464 | } |