support include http/https url

This commit is contained in:
yuzuki999
2023-09-22 19:26:21 +08:00
parent cecfffd081
commit a1e14d0aca

View File

@@ -2,11 +2,12 @@ package conf
import ( import (
"fmt" "fmt"
"io"
"os"
"github.com/InazumaV/V2bX/common/json5" "github.com/InazumaV/V2bX/common/json5"
"github.com/goccy/go-json" "github.com/goccy/go-json"
"io"
"net/http"
"os"
"strings"
) )
type NodeConfig struct { type NodeConfig struct {
@@ -36,12 +37,29 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) (err error) {
return err return err
} }
if len(rn.Include) != 0 { if len(rn.Include) != 0 {
file, _ := strings.CutPrefix(rn.Include, ":")
switch file {
case "http", "https":
rsp, err := http.Get(file)
if err != nil {
return err
}
defer rsp.Body.Close()
data, err = io.ReadAll(json5.NewTrimNodeReader(rsp.Body))
if err != nil {
return fmt.Errorf("open include file error: %s", err)
}
default:
f, err := os.Open(rn.Include) f, err := os.Open(rn.Include)
if err != nil { if err != nil {
return fmt.Errorf("open include file error: %s", err) return fmt.Errorf("open include file error: %s", err)
} }
defer f.Close() defer f.Close()
data, err = io.ReadAll(json5.NewTrimNodeReader(f)) data, err = io.ReadAll(json5.NewTrimNodeReader(f))
if err != nil {
return fmt.Errorf("open include file error: %s", err)
}
}
err = json.Unmarshal(data, &rn) err = json.Unmarshal(data, &rn)
if err != nil { if err != nil {
return fmt.Errorf("unmarshal include file error: %s", err) return fmt.Errorf("unmarshal include file error: %s", err)