|
|
@ -3,8 +3,6 @@ package main
|
|
|
|
//go:generate errorgen
|
|
|
|
//go:generate errorgen
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"flag"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
@ -15,26 +13,17 @@ import (
|
|
|
|
"syscall"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
|
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/cmdarg"
|
|
|
|
"v2ray.com/core/common/platform"
|
|
|
|
"v2ray.com/core/common/platform"
|
|
|
|
_ "v2ray.com/core/main/distro/all"
|
|
|
|
_ "v2ray.com/core/main/distro/all"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type CmdConfig []string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *CmdConfig) String() string {
|
|
|
|
|
|
|
|
return strings.Join([]string(*c), ",")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *CmdConfig) Set(value string) error {
|
|
|
|
|
|
|
|
*c = append(*c, value)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
configFiles CmdConfig // "Config file for V2Ray.", the option is customed type, parse in main
|
|
|
|
configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
|
|
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
|
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
|
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
|
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
|
|
|
format = flag.String("format", "json", "Format of input file.")
|
|
|
|
format = flag.String("format", "", "Format of input file.")
|
|
|
|
|
|
|
|
errNoConfig = newError("no valid config")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func fileExists(file string) bool {
|
|
|
|
func fileExists(file string) bool {
|
|
|
@ -42,8 +31,7 @@ func fileExists(file string) bool {
|
|
|
|
return err == nil && !info.IsDir()
|
|
|
|
return err == nil && !info.IsDir()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getConfigFilePath() CmdConfig {
|
|
|
|
func getConfigFilePath() cmdarg.Arg {
|
|
|
|
|
|
|
|
|
|
|
|
if len(configFiles) > 0 {
|
|
|
|
if len(configFiles) > 0 {
|
|
|
|
return configFiles
|
|
|
|
return configFiles
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -51,15 +39,15 @@ func getConfigFilePath() CmdConfig {
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
|
if fileExists(configFile) {
|
|
|
|
if fileExists(configFile) {
|
|
|
|
return []string{configFile}
|
|
|
|
return cmdarg.Arg{configFile}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
|
|
return []string{configFile}
|
|
|
|
return cmdarg.Arg{configFile}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return []string{}
|
|
|
|
return configFiles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetConfigFormat() string {
|
|
|
|
func GetConfigFormat() string {
|
|
|
@ -73,8 +61,14 @@ func GetConfigFormat() string {
|
|
|
|
|
|
|
|
|
|
|
|
func startV2Ray() (core.Server, error) {
|
|
|
|
func startV2Ray() (core.Server, error) {
|
|
|
|
configFiles := getConfigFilePath()
|
|
|
|
configFiles := getConfigFilePath()
|
|
|
|
fs, _ := json.Marshal(configFiles)
|
|
|
|
if len(configFiles) == 0 {
|
|
|
|
config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], bytes.NewBuffer(fs))
|
|
|
|
if *format == "" {
|
|
|
|
|
|
|
|
return nil, errNoConfig
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
configFiles = []string{"stdin:"}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
|
|
|
|
return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -96,6 +90,7 @@ func printVersion() {
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
|
|
|
|
flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.")
|
|
|
|
|
|
|
|
flag.Var(&configFiles, "c", "short alias of -config")
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
printVersion()
|
|
|
|
printVersion()
|
|
|
@ -108,6 +103,9 @@ func main() {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
|
|
|
|
|
|
if err == errNoConfig {
|
|
|
|
|
|
|
|
flag.PrintDefaults()
|
|
|
|
|
|
|
|
}
|
|
|
|
os.Exit(23)
|
|
|
|
os.Exit(23)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|