diff --git a/pkg/config/legacy/value.go b/pkg/config/legacy/value.go index ecf805c9..637129d9 100644 --- a/pkg/config/legacy/value.go +++ b/pkg/config/legacy/value.go @@ -16,6 +16,9 @@ package legacy import ( "bytes" + "fmt" + "io" + "net/http" "os" "strings" "text/template" @@ -45,6 +48,21 @@ func GetValues() *Values { } } +func readBytesFromPath(path string) ([]byte, error) { + if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { + resp, err := http.Get(path) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to fetch config from url %s, status code: %d", path, resp.StatusCode) + } + return io.ReadAll(resp.Body) + } + return os.ReadFile(path) +} + func RenderContent(in []byte) (out []byte, err error) { tmpl, errRet := template.New("frp").Parse(string(in)) if errRet != nil { @@ -64,7 +82,7 @@ func RenderContent(in []byte) (out []byte, err error) { func GetRenderedConfFromFile(path string) (out []byte, err error) { var b []byte - b, err = os.ReadFile(path) + b, err = readBytesFromPath(path) if err != nil { return } diff --git a/pkg/config/load.go b/pkg/config/load.go index bb050b40..3432dd10 100644 --- a/pkg/config/load.go +++ b/pkg/config/load.go @@ -18,6 +18,8 @@ import ( "bytes" "encoding/json" "fmt" + "io" + "net/http" "os" "path/filepath" "strings" @@ -36,6 +38,21 @@ import ( "github.com/fatedier/frp/pkg/util/util" ) +func readBytesFromPath(path string) ([]byte, error) { + if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { + resp, err := http.Get(path) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("failed to fetch config from url %s, status code: %d", path, resp.StatusCode) + } + return io.ReadAll(resp.Body) + } + return os.ReadFile(path) +} + var glbEnvs map[string]string func init() { @@ -72,7 +89,7 @@ func DetectLegacyINIFormat(content []byte) bool { } func DetectLegacyINIFormatFromFile(path string) bool { - b, err := os.ReadFile(path) + b, err := readBytesFromPath(path) if err != nil { return false } @@ -96,7 +113,7 @@ func RenderWithTemplate(in []byte, values *Values) ([]byte, error) { } func LoadFileContentWithTemplate(path string, values *Values) ([]byte, error) { - b, err := os.ReadFile(path) + b, err := readBytesFromPath(path) if err != nil { return nil, err }