2020-11-25 11:01:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-01-01 12:16:22 +00:00
|
|
|
"regexp"
|
2020-11-25 11:01:53 +00:00
|
|
|
"runtime"
|
2020-12-15 12:27:18 +00:00
|
|
|
"runtime/debug"
|
2021-03-21 09:13:51 +00:00
|
|
|
"strings"
|
2020-11-25 11:01:53 +00:00
|
|
|
"syscall"
|
2023-12-29 16:16:48 +00:00
|
|
|
"time"
|
2020-11-25 11:01:53 +00:00
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/cmdarg"
|
2023-12-29 16:16:48 +00:00
|
|
|
clog "github.com/xtls/xray-core/common/log"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/platform"
|
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
|
"github.com/xtls/xray-core/main/commands/base"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdRun = &base.Command{
|
|
|
|
UsageLine: "{{.Exec}} run [-c config.json] [-confdir dir]",
|
|
|
|
Short: "Run Xray with config, the default command",
|
|
|
|
Long: `
|
|
|
|
Run Xray with config, the default command.
|
|
|
|
|
|
|
|
The -config=file, -c=file flags set the config files for
|
|
|
|
Xray. Multiple assign is accepted.
|
|
|
|
|
|
|
|
The -confdir=dir flag sets a dir with multiple json config
|
|
|
|
|
|
|
|
The -format=json flag sets the format of config files.
|
2021-03-21 09:13:51 +00:00
|
|
|
Default "auto".
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
The -test flag tells Xray to test config files only,
|
2023-12-29 16:16:48 +00:00
|
|
|
without launching the server.
|
|
|
|
|
|
|
|
The -dump flag tells Xray to print the merged config.
|
2020-11-25 11:01:53 +00:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2021-10-19 16:57:14 +00:00
|
|
|
cmdRun.Run = executeRun // break init loop
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
configFiles cmdarg.Arg // "Config file for Xray.", the option is customed type, parse in main
|
|
|
|
configDir string
|
2023-12-29 16:16:48 +00:00
|
|
|
dump = cmdRun.Flag.Bool("dump", false, "Dump merged config only, without launching Xray server.")
|
2020-11-25 11:01:53 +00:00
|
|
|
test = cmdRun.Flag.Bool("test", false, "Test config file only, without launching Xray server.")
|
2021-03-21 09:13:51 +00:00
|
|
|
format = cmdRun.Flag.String("format", "auto", "Format of input file.")
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
/* We have to do this here because Golang's Test will also need to parse flag, before
|
|
|
|
* main func in this file is run.
|
|
|
|
*/
|
|
|
|
_ = func() bool {
|
|
|
|
cmdRun.Flag.Var(&configFiles, "config", "Config path for Xray.")
|
|
|
|
cmdRun.Flag.Var(&configFiles, "c", "Short alias of -config")
|
|
|
|
cmdRun.Flag.StringVar(&configDir, "confdir", "", "A dir with multiple json config")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}()
|
|
|
|
)
|
|
|
|
|
|
|
|
func executeRun(cmd *base.Command, args []string) {
|
2023-12-29 16:16:48 +00:00
|
|
|
if *dump {
|
|
|
|
clog.ReplaceWithSeverityLogger(clog.Severity_Warning)
|
|
|
|
errCode := dumpConfig()
|
|
|
|
os.Exit(errCode)
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
printVersion()
|
|
|
|
server, err := startXray()
|
|
|
|
if err != nil {
|
2020-12-25 10:53:17 +00:00
|
|
|
fmt.Println("Failed to start:", err)
|
|
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
|
|
os.Exit(23)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *test {
|
|
|
|
fmt.Println("Configuration OK.")
|
2020-12-25 10:53:17 +00:00
|
|
|
os.Exit(0)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Start(); err != nil {
|
2020-12-25 10:53:17 +00:00
|
|
|
fmt.Println("Failed to start:", err)
|
|
|
|
os.Exit(-1)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
defer server.Close()
|
|
|
|
|
2021-03-01 01:29:17 +00:00
|
|
|
/*
|
|
|
|
conf.FileCache = nil
|
|
|
|
conf.IPCache = nil
|
|
|
|
conf.SiteCache = nil
|
|
|
|
*/
|
2020-12-15 12:27:18 +00:00
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
// Explicitly triggering GC to remove garbage from config loading.
|
|
|
|
runtime.GC()
|
2020-12-15 12:27:18 +00:00
|
|
|
debug.FreeOSMemory()
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
osSignals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-osSignals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:16:48 +00:00
|
|
|
func dumpConfig() int {
|
|
|
|
files := getConfigFilePath(false)
|
|
|
|
if config, err := core.GetMergedConfig(files); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return 23
|
|
|
|
} else {
|
|
|
|
fmt.Print(config)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-03-21 09:13:51 +00:00
|
|
|
func getRegepxByFormat() string {
|
|
|
|
switch strings.ToLower(*format) {
|
|
|
|
case "json":
|
2023-08-06 06:47:31 +00:00
|
|
|
return `^.+\.(json|jsonc)$`
|
2021-03-21 09:13:51 +00:00
|
|
|
case "toml":
|
|
|
|
return `^.+\.toml$`
|
|
|
|
case "yaml", "yml":
|
|
|
|
return `^.+\.(yaml|yml)$`
|
|
|
|
default:
|
2023-08-06 06:47:31 +00:00
|
|
|
return `^.+\.(json|jsonc|toml|yaml|yml)$`
|
2021-03-21 09:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
func readConfDir(dirPath string) {
|
2021-09-28 18:49:34 +00:00
|
|
|
confs, err := os.ReadDir(dirPath)
|
2020-11-25 11:01:53 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
for _, f := range confs {
|
2021-03-21 09:13:51 +00:00
|
|
|
matched, err := regexp.MatchString(getRegepxByFormat(), f.Name())
|
2021-01-01 12:16:22 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
if matched {
|
2020-11-25 11:01:53 +00:00
|
|
|
configFiles.Set(path.Join(dirPath, f.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:16:48 +00:00
|
|
|
func getConfigFilePath(verbose bool) cmdarg.Arg {
|
2020-11-25 11:01:53 +00:00
|
|
|
if dirExists(configDir) {
|
2023-12-29 16:16:48 +00:00
|
|
|
if verbose {
|
|
|
|
log.Println("Using confdir from arg:", configDir)
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
readConfDir(configDir)
|
|
|
|
} else if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
|
2023-12-29 16:16:48 +00:00
|
|
|
if verbose {
|
|
|
|
log.Println("Using confdir from env:", envConfDir)
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
readConfDir(envConfDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configFiles) > 0 {
|
|
|
|
return configFiles
|
|
|
|
}
|
|
|
|
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
|
if fileExists(configFile) {
|
2023-12-29 16:16:48 +00:00
|
|
|
if verbose {
|
|
|
|
log.Println("Using default config: ", configFile)
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
return cmdarg.Arg{configFile}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
2023-12-29 16:16:48 +00:00
|
|
|
if verbose {
|
|
|
|
log.Println("Using config from env: ", configFile)
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
return cmdarg.Arg{configFile}
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:16:48 +00:00
|
|
|
if verbose {
|
|
|
|
log.Println("Using config from STDIN")
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
return cmdarg.Arg{"stdin:"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigFormat() string {
|
2021-02-12 14:12:58 +00:00
|
|
|
f := core.GetFormatByExtension(*format)
|
|
|
|
if f == "" {
|
2021-03-21 09:13:51 +00:00
|
|
|
f = "auto"
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
2021-02-12 14:12:58 +00:00
|
|
|
return f
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startXray() (core.Server, error) {
|
2023-12-29 16:16:48 +00:00
|
|
|
configFiles := getConfigFilePath(true)
|
2020-11-25 11:01:53 +00:00
|
|
|
|
2021-10-19 16:57:14 +00:00
|
|
|
// config, err := core.LoadConfig(getConfigFormat(), configFiles[0], configFiles)
|
2020-12-24 19:30:26 +00:00
|
|
|
|
2021-02-12 14:12:58 +00:00
|
|
|
c, err := core.LoadConfig(getConfigFormat(), configFiles)
|
2020-11-25 11:01:53 +00:00
|
|
|
if err != nil {
|
2020-12-16 12:35:27 +00:00
|
|
|
return nil, newError("failed to load config files: [", configFiles.String(), "]").Base(err)
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 14:12:58 +00:00
|
|
|
server, err := core.New(c)
|
2020-11-25 11:01:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create server").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return server, nil
|
|
|
|
}
|