github.com/imannamdari/v2ray-core/v5@v5.0.5/infra/conf/v5cfg/init.go (about)

     1  package v5cfg
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	core "github.com/imannamdari/v2ray-core/v5"
     8  	"github.com/imannamdari/v2ray-core/v5/common"
     9  	"github.com/imannamdari/v2ray-core/v5/common/buf"
    10  	"github.com/imannamdari/v2ray-core/v5/common/cmdarg"
    11  	"github.com/imannamdari/v2ray-core/v5/infra/conf/json"
    12  )
    13  
    14  const jsonV5 = "jsonv5"
    15  
    16  func init() {
    17  	common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
    18  		Name:      []string{jsonV5},
    19  		Extension: []string{".v5.json", ".v5.jsonc"},
    20  		Loader: func(input interface{}) (*core.Config, error) {
    21  			switch v := input.(type) {
    22  			case string:
    23  				r, err := cmdarg.LoadArg(v)
    24  				if err != nil {
    25  					return nil, err
    26  				}
    27  				data, err := buf.ReadAllToBytes(&json.Reader{
    28  					Reader: r,
    29  				})
    30  				if err != nil {
    31  					return nil, err
    32  				}
    33  				return loadJSONConfig(data)
    34  			case []byte:
    35  				r := &json.Reader{
    36  					Reader: bytes.NewReader(v),
    37  				}
    38  				data, err := buf.ReadAllToBytes(r)
    39  				if err != nil {
    40  					return nil, err
    41  				}
    42  				return loadJSONConfig(data)
    43  			case io.Reader:
    44  				data, err := buf.ReadAllToBytes(&json.Reader{
    45  					Reader: v,
    46  				})
    47  				if err != nil {
    48  					return nil, err
    49  				}
    50  				return loadJSONConfig(data)
    51  			default:
    52  				return nil, newError("unknown type")
    53  			}
    54  		},
    55  	}))
    56  }