github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/control/config.go (about) 1 package control 2 3 import ( 4 "bytes" 5 "io" 6 "io/ioutil" 7 "os" 8 "strings" 9 10 "github.com/golang/protobuf/proto" 11 "v2ray.com/core/common" 12 "v2ray.com/core/infra/conf" 13 "v2ray.com/core/infra/conf/serial" 14 ) 15 16 // ConfigCommand is the json to pb convert struct 17 type ConfigCommand struct{} 18 19 // Name for cmd usage 20 func (c *ConfigCommand) Name() string { 21 return "config" 22 } 23 24 // Description for help usage 25 func (c *ConfigCommand) Description() Description { 26 return Description{ 27 Short: "merge multiple json config", 28 Usage: []string{"v2ctl config config.json c1.json c2.json <url>.json"}, 29 } 30 } 31 32 // Execute real work here. 33 func (c *ConfigCommand) Execute(args []string) error { 34 if len(args) < 1 { 35 return newError("empty config list") 36 } 37 38 conf := &conf.Config{} 39 for _, arg := range args { 40 ctllog.Println("Read config: ", arg) 41 r, err := c.LoadArg(arg) 42 common.Must(err) 43 c, err := serial.DecodeJSONConfig(r) 44 if err != nil { 45 ctllog.Fatalln(err) 46 } 47 conf.Override(c, arg) 48 } 49 50 pbConfig, err := conf.Build() 51 if err != nil { 52 return err 53 } 54 55 bytesConfig, err := proto.Marshal(pbConfig) 56 if err != nil { 57 return newError("failed to marshal proto config").Base(err) 58 } 59 60 if _, err := os.Stdout.Write(bytesConfig); err != nil { 61 return newError("failed to write proto config").Base(err) 62 } 63 64 return nil 65 } 66 67 // LoadArg loads one arg, maybe an remote url, or local file path 68 func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) { 69 70 var data []byte 71 if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { 72 data, err = FetchHTTPContent(arg) 73 } else if arg == "stdin:" { 74 data, err = ioutil.ReadAll(os.Stdin) 75 } else { 76 data, err = ioutil.ReadFile(arg) 77 } 78 79 if err != nil { 80 return 81 } 82 out = bytes.NewBuffer(data) 83 return 84 } 85 86 func init() { 87 common.Must(RegisterCommand(&ConfigCommand{})) 88 }