feat(parser): support HTTP(S) URL config sources with -C flag

Add remote config fetching capability to the -C CLI flag, enabling
centralized fleet management of gost nodes.

- readConfig() now detects http:// and https:// URLs and fetches
  them via HTTP GET with a 30s timeout and shared client pool
- Format auto-detection from Content-Type header (using mime
  parsing) with fallback to URL path extension (.yaml/.json)
- 10 MiB response size limit to prevent memory exhaustion
- URL userinfo stripped from error messages for safety
- Fix ReadFile to force viper config type from file extension,
  preventing format detection ambiguity
- Fix viper config search path fallback: skip cfg.Load() when
  -C flags are explicitly provided, avoiding unwanted merges
- Tests: URL fetch (YAML/JSON), non-200, oversize, detectFormat,
  isHTTPURL, sanitizeURL, and combined file+URL Parse scenario
This commit is contained in:
ginuerzh
2026-06-20 20:11:43 +08:00
parent 9c585e2de8
commit e583f5b538
3 changed files with 297 additions and 4 deletions
+3
View File
@@ -3,6 +3,8 @@ package config
import (
"encoding/json"
"io"
"path/filepath"
"strings"
"sync"
"time"
@@ -658,6 +660,7 @@ func (c *Config) Read(r io.Reader, configType string) error {
func (c *Config) ReadFile(file string) error {
v.SetConfigFile(file)
v.SetConfigType(strings.TrimPrefix(filepath.Ext(file), ".")) // force format from extension
if err := v.ReadInConfig(); err != nil {
return err
}