github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/main/json/json.go (about)

     1  package json
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/xtls/xray-core/common"
     7  	"github.com/xtls/xray-core/common/cmdarg"
     8  	"github.com/xtls/xray-core/core"
     9  	"github.com/xtls/xray-core/infra/conf"
    10  	"github.com/xtls/xray-core/infra/conf/serial"
    11  	"github.com/xtls/xray-core/main/confloader"
    12  )
    13  
    14  func init() {
    15  	common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
    16  		Name:      "JSON",
    17  		Extension: []string{"json"},
    18  		Loader: func(input interface{}) (*core.Config, error) {
    19  			switch v := input.(type) {
    20  			case cmdarg.Arg:
    21  				cf := &conf.Config{}
    22  				for i, arg := range v {
    23  					newError("Reading config: ", arg).AtInfo().WriteToLog()
    24  					r, err := confloader.LoadConfig(arg)
    25  					if err != nil {
    26  						return nil, newError("failed to read config: ", arg).Base(err)
    27  					}
    28  					c, err := serial.DecodeJSONConfig(r)
    29  					if err != nil {
    30  						return nil, newError("failed to decode config: ", arg).Base(err)
    31  					}
    32  					if i == 0 {
    33  						// This ensure even if the muti-json parser do not support a setting,
    34  						// It is still respected automatically for the first configure file
    35  						*cf = *c
    36  						continue
    37  					}
    38  					cf.Override(c, arg)
    39  				}
    40  				return cf.Build()
    41  			case io.Reader:
    42  				return serial.LoadJSONConfig(v)
    43  			default:
    44  				return nil, newError("unknow type")
    45  			}
    46  		},
    47  	}))
    48  }