2020-11-23 10:38:43 -05:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/commands/base"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/cmdarg"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdRun runs V2Ray with config
|
|
|
|
var CmdRun = &base.Command{
|
|
|
|
CustomFlags: true,
|
2020-11-28 09:06:03 -05:00
|
|
|
UsageLine: "{{.Exec}} run [-c config.json] [-d dir]",
|
2020-11-23 10:38:43 -05:00
|
|
|
Short: "Run V2Ray with config",
|
|
|
|
Long: `
|
|
|
|
Run V2Ray with config.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-c, -config
|
|
|
|
Config file for V2Ray. Multiple assign is accepted.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-d, -confdir
|
|
|
|
A dir with config files. Multiple assign is accepted.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-r
|
|
|
|
Load confdir recursively.
|
2020-11-23 10:38:43 -05:00
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
-format
|
2020-11-23 10:38:43 -05:00
|
|
|
Format of input files. (default "json")
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
Examples:
|
|
|
|
|
|
|
|
{{.Exec}} {{.LongName}} -c config.json
|
|
|
|
{{.Exec}} {{.LongName}} -d path/to/dir
|
|
|
|
|
|
|
|
Use "{{.Exec}} help format-loader" for more information about format.
|
|
|
|
`,
|
|
|
|
Run: executeRun,
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-11-28 09:06:03 -05:00
|
|
|
configFiles cmdarg.Arg
|
|
|
|
configDirs cmdarg.Arg
|
|
|
|
configFormat *string
|
|
|
|
configDirRecursively *bool
|
2020-11-23 10:38:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func setConfigFlags(cmd *base.Command) {
|
2020-11-28 09:06:03 -05:00
|
|
|
configFormat = cmd.Flag.String("format", "json", "")
|
|
|
|
configDirRecursively = cmd.Flag.Bool("r", false, "")
|
2020-11-23 10:38:43 -05:00
|
|
|
|
|
|
|
cmd.Flag.Var(&configFiles, "config", "")
|
|
|
|
cmd.Flag.Var(&configFiles, "c", "")
|
2020-11-28 09:06:03 -05:00
|
|
|
cmd.Flag.Var(&configDirs, "confdir", "")
|
|
|
|
cmd.Flag.Var(&configDirs, "d", "")
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
2020-11-28 09:06:03 -05:00
|
|
|
|
2020-11-23 10:38:43 -05:00
|
|
|
func executeRun(cmd *base.Command, args []string) {
|
|
|
|
setConfigFlags(cmd)
|
|
|
|
cmd.Flag.Parse(args)
|
|
|
|
printVersion()
|
|
|
|
server, err := startV2Ray()
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("Failed to start: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Start(); err != nil {
|
|
|
|
base.Fatalf("Failed to start: %s", err)
|
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
// Explicitly triggering GC to remove garbage from config loading.
|
|
|
|
runtime.GC()
|
|
|
|
|
|
|
|
{
|
|
|
|
osSignals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-osSignals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileExists(file string) bool {
|
|
|
|
info, err := os.Stat(file)
|
|
|
|
return err == nil && !info.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dirExists(file string) bool {
|
|
|
|
if file == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
info, err := os.Stat(file)
|
|
|
|
return err == nil && info.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
func readConfDir(dirPath string, extension []string) cmdarg.Arg {
|
2020-11-23 10:38:43 -05:00
|
|
|
confs, err := ioutil.ReadDir(dirPath)
|
|
|
|
if err != nil {
|
2020-11-28 09:06:03 -05:00
|
|
|
base.Fatalf("failed to read dir %s: %s", dirPath, err)
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
files := make(cmdarg.Arg, 0)
|
|
|
|
for _, f := range confs {
|
2020-11-28 09:06:03 -05:00
|
|
|
ext := filepath.Ext(f.Name())
|
|
|
|
for _, e := range extension {
|
|
|
|
if strings.EqualFold(e, ext) {
|
|
|
|
files.Set(filepath.Join(dirPath, f.Name()))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFolderFiles get files in the folder and it's children
|
|
|
|
func readConfDirRecursively(dirPath string, extension []string) cmdarg.Arg {
|
|
|
|
files := make(cmdarg.Arg, 0)
|
|
|
|
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
for _, e := range extension {
|
|
|
|
if strings.EqualFold(e, ext) {
|
|
|
|
files.Set(path)
|
|
|
|
break
|
|
|
|
}
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
2020-11-28 09:06:03 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf("failed to read dir %s: %s", dirPath, err)
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
func getLoaderExtension() ([]string, error) {
|
|
|
|
firstFile := ""
|
|
|
|
if len(configFiles) > 0 {
|
|
|
|
firstFile = configFiles[0]
|
|
|
|
}
|
|
|
|
loader, err := core.GetConfigLoader(*configFormat, firstFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return loader.Extension, nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:38:43 -05:00
|
|
|
func getConfigFilePath() cmdarg.Arg {
|
2020-11-28 09:06:03 -05:00
|
|
|
extension, err := getLoaderExtension()
|
|
|
|
if err != nil {
|
|
|
|
base.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
dirReader := readConfDir
|
|
|
|
if *configDirRecursively {
|
|
|
|
dirReader = readConfDirRecursively
|
|
|
|
}
|
|
|
|
if len(configDirs) > 0 {
|
|
|
|
for _, d := range configDirs {
|
|
|
|
log.Println("Using confdir from arg:", d)
|
|
|
|
configFiles = append(configFiles, dirReader(d, extension)...)
|
|
|
|
}
|
2020-11-23 10:38:43 -05:00
|
|
|
} else if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
|
|
|
|
log.Println("Using confdir from env:", envConfDir)
|
2020-11-28 09:06:03 -05:00
|
|
|
configFiles = append(configFiles, dirReader(envConfDir, extension)...)
|
2020-11-23 10:38:43 -05:00
|
|
|
}
|
|
|
|
if len(configFiles) > 0 {
|
|
|
|
return configFiles
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
if len(configFiles) == 0 && len(configDirs) > 0 {
|
|
|
|
base.Fatalf("no config file found with extension: %s", extension)
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:38:43 -05:00
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
|
if fileExists(configFile) {
|
|
|
|
log.Println("Using default config: ", configFile)
|
|
|
|
return cmdarg.Arg{configFile}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
|
|
log.Println("Using config from env: ", configFile)
|
|
|
|
return cmdarg.Arg{configFile}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Using config from STDIN")
|
|
|
|
return cmdarg.Arg{"stdin:"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startV2Ray() (core.Server, error) {
|
|
|
|
configFiles := getConfigFilePath()
|
|
|
|
|
2020-11-28 09:06:03 -05:00
|
|
|
config, err := core.LoadConfig(*configFormat, configFiles[0], configFiles)
|
2020-11-23 10:38:43 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := core.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create server").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, nil
|
|
|
|
}
|