2015-09-12 09:51:42 +00:00
|
|
|
package main
|
|
|
|
|
2018-09-30 21:08:41 +00:00
|
|
|
//go:generate errorgen
|
2017-04-08 23:43:25 +00:00
|
|
|
|
2015-09-12 18:36:21 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
2015-09-18 09:59:36 +00:00
|
|
|
"fmt"
|
2020-01-03 01:26:48 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-10-18 11:14:34 +00:00
|
|
|
"os"
|
2016-05-10 22:51:38 +00:00
|
|
|
"os/signal"
|
2020-01-03 01:26:48 +00:00
|
|
|
"path"
|
2015-10-18 11:14:34 +00:00
|
|
|
"path/filepath"
|
2018-11-16 16:17:11 +00:00
|
|
|
"runtime"
|
2016-10-16 12:22:21 +00:00
|
|
|
"strings"
|
2016-08-09 20:10:44 +00:00
|
|
|
"syscall"
|
2015-09-12 18:36:21 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core"
|
2020-01-03 01:26:48 +00:00
|
|
|
"v2ray.com/core/common/cmdarg"
|
2017-12-05 10:58:12 +00:00
|
|
|
"v2ray.com/core/common/platform"
|
2016-12-16 22:38:58 +00:00
|
|
|
_ "v2ray.com/core/main/distro/all"
|
2015-09-12 18:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-01-03 01:26:48 +00:00
|
|
|
configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main
|
|
|
|
configDir string
|
|
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
|
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
|
|
|
format = flag.String("format", "json", "Format of input file.")
|
2020-03-23 03:34:53 +00:00
|
|
|
|
2020-04-15 01:51:38 +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.
|
|
|
|
*/
|
2020-03-23 03:34:53 +00:00
|
|
|
_ = func() error {
|
|
|
|
|
|
|
|
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.StringVar(&configDir, "confdir", "", "A dir with multiple json config")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
2015-09-12 18:36:21 +00:00
|
|
|
)
|
|
|
|
|
2017-12-05 10:58:12 +00:00
|
|
|
func fileExists(file string) bool {
|
|
|
|
info, err := os.Stat(file)
|
|
|
|
return err == nil && !info.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:26:48 +00:00
|
|
|
func dirExists(file string) bool {
|
2020-03-23 08:33:49 +00:00
|
|
|
if file == "" {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-03 01:26:48 +00:00
|
|
|
info, err := os.Stat(file)
|
|
|
|
return err == nil && info.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfDir(dirPath string) {
|
|
|
|
confs, err := ioutil.ReadDir(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
for _, f := range confs {
|
|
|
|
if strings.HasSuffix(f.Name(), ".json") {
|
|
|
|
configFiles.Set(path.Join(dirPath, f.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigFilePath() (cmdarg.Arg, error) {
|
|
|
|
if dirExists(configDir) {
|
2020-03-23 08:33:49 +00:00
|
|
|
log.Println("Using confdir from arg:", configDir)
|
2020-01-03 01:26:48 +00:00
|
|
|
readConfDir(configDir)
|
2020-03-23 08:33:49 +00:00
|
|
|
} else {
|
|
|
|
if envConfDir := platform.GetConfDirPath(); dirExists(envConfDir) {
|
|
|
|
log.Println("Using confdir from env:", envConfDir)
|
|
|
|
readConfDir(envConfDir)
|
|
|
|
}
|
2020-01-03 01:26:48 +00:00
|
|
|
}
|
2020-03-23 08:33:49 +00:00
|
|
|
|
2020-01-03 01:26:48 +00:00
|
|
|
if len(configFiles) > 0 {
|
|
|
|
return configFiles, nil
|
2017-12-05 10:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
|
if fileExists(configFile) {
|
2020-01-03 01:26:48 +00:00
|
|
|
log.Println("Using default config: ", configFile)
|
|
|
|
return cmdarg.Arg{configFile}, nil
|
2017-12-05 10:58:12 +00:00
|
|
|
}
|
2015-10-18 11:14:34 +00:00
|
|
|
}
|
2017-12-05 10:58:12 +00:00
|
|
|
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
2020-01-03 01:26:48 +00:00
|
|
|
log.Println("Using config from env: ", configFile)
|
|
|
|
return cmdarg.Arg{configFile}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Using config from STDIN")
|
|
|
|
return cmdarg.Arg{"stdin:"}, nil
|
2015-10-18 11:14:34 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 22:57:40 +00:00
|
|
|
func GetConfigFormat() string {
|
2016-10-16 12:22:21 +00:00
|
|
|
switch strings.ToLower(*format) {
|
|
|
|
case "pb", "protobuf":
|
2018-02-14 22:57:40 +00:00
|
|
|
return "protobuf"
|
2016-10-16 12:22:21 +00:00
|
|
|
default:
|
2018-02-14 22:57:40 +00:00
|
|
|
return "json"
|
2016-10-16 12:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:30:52 +00:00
|
|
|
func startV2Ray() (core.Server, error) {
|
2020-01-03 01:26:48 +00:00
|
|
|
configFiles, err := getConfigFilePath()
|
2019-12-16 01:33:41 +00:00
|
|
|
if err != nil {
|
2020-01-03 01:26:48 +00:00
|
|
|
return nil, err
|
2019-12-14 15:29:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 01:26:48 +00:00
|
|
|
config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles)
|
2015-09-12 18:36:21 +00:00
|
|
|
if err != nil {
|
2020-01-03 01:26:48 +00:00
|
|
|
return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err)
|
2015-09-12 18:36:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 21:38:37 +00:00
|
|
|
server, err := core.New(config)
|
2015-09-12 18:36:21 +00:00
|
|
|
if err != nil {
|
2017-11-27 22:00:36 +00:00
|
|
|
return nil, newError("failed to create server").Base(err)
|
2015-09-12 18:36:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 21:38:37 +00:00
|
|
|
return server, nil
|
2016-07-10 21:11:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 13:03:55 +00:00
|
|
|
func printVersion() {
|
|
|
|
version := core.VersionStatement()
|
|
|
|
for _, s := range version {
|
|
|
|
fmt.Println(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 21:11:42 +00:00
|
|
|
func main() {
|
2020-03-23 03:34:53 +00:00
|
|
|
|
2016-07-10 21:11:42 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-02-16 13:03:55 +00:00
|
|
|
printVersion()
|
2016-07-10 21:11:42 +00:00
|
|
|
|
|
|
|
if *version {
|
2015-10-07 08:05:11 +00:00
|
|
|
return
|
2015-09-12 18:36:21 +00:00
|
|
|
}
|
2015-09-12 09:51:42 +00:00
|
|
|
|
2017-02-22 10:30:52 +00:00
|
|
|
server, err := startV2Ray()
|
2017-02-02 13:42:31 +00:00
|
|
|
if err != nil {
|
2020-01-03 01:26:48 +00:00
|
|
|
fmt.Println(err)
|
2018-07-17 09:53:36 +00:00
|
|
|
// Configuration error. Exit with a special value to prevent systemd from restarting.
|
|
|
|
os.Exit(23)
|
2017-02-02 13:42:31 +00:00
|
|
|
}
|
2016-05-10 22:51:38 +00:00
|
|
|
|
2017-02-02 13:42:31 +00:00
|
|
|
if *test {
|
2017-04-06 13:13:09 +00:00
|
|
|
fmt.Println("Configuration OK.")
|
2017-11-23 09:39:40 +00:00
|
|
|
os.Exit(0)
|
2016-07-10 21:11:42 +00:00
|
|
|
}
|
2017-02-02 13:42:31 +00:00
|
|
|
|
2017-02-22 10:30:52 +00:00
|
|
|
if err := server.Start(); err != nil {
|
2017-04-06 13:13:09 +00:00
|
|
|
fmt.Println("Failed to start", err)
|
2017-11-23 09:39:40 +00:00
|
|
|
os.Exit(-1)
|
2017-02-02 13:42:31 +00:00
|
|
|
}
|
2018-12-04 13:16:31 +00:00
|
|
|
defer server.Close()
|
2017-02-02 13:42:31 +00:00
|
|
|
|
2018-11-16 16:17:11 +00:00
|
|
|
// Explicitly triggering GC to remove garbage from config loading.
|
|
|
|
runtime.GC()
|
|
|
|
|
2018-12-04 13:15:35 +00:00
|
|
|
{
|
|
|
|
osSignals := make(chan os.Signal, 1)
|
2020-04-18 00:00:40 +00:00
|
|
|
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
|
2018-12-04 13:15:35 +00:00
|
|
|
<-osSignals
|
|
|
|
}
|
2015-09-12 09:51:42 +00:00
|
|
|
}
|