2015-09-12 11:51:42 +02:00
|
|
|
package main
|
|
|
|
|
2017-12-03 01:04:57 +01:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg main -path Main
|
2017-04-09 01:43:25 +02:00
|
|
|
|
2015-09-12 20:36:21 +02:00
|
|
|
import (
|
|
|
|
"flag"
|
2015-09-18 11:59:36 +02:00
|
|
|
"fmt"
|
2015-10-18 13:14:34 +02:00
|
|
|
"os"
|
2016-05-10 15:51:38 -07:00
|
|
|
"os/signal"
|
2015-10-18 13:14:34 +02:00
|
|
|
"path/filepath"
|
2016-10-16 14:22:21 +02:00
|
|
|
"strings"
|
2016-08-09 22:10:44 +02:00
|
|
|
"syscall"
|
2015-09-12 20:36:21 +02:00
|
|
|
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core"
|
2017-12-05 11:58:12 +01:00
|
|
|
"v2ray.com/core/common/platform"
|
2018-04-09 11:43:13 +02:00
|
|
|
"v2ray.com/core/main/confloader"
|
2016-12-16 23:38:58 +01:00
|
|
|
_ "v2ray.com/core/main/distro/all"
|
2015-09-12 20:36:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-12-05 11:58:12 +01:00
|
|
|
configFile = flag.String("config", "", "Config file for V2Ray.")
|
2015-09-18 11:59:36 +02:00
|
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
2016-01-24 12:58:04 +01:00
|
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
2016-10-03 11:18:24 +02:00
|
|
|
format = flag.String("format", "json", "Format of input file.")
|
2017-11-22 23:00:46 +01:00
|
|
|
plugin = flag.Bool("plugin", false, "True to load plugins.")
|
2015-09-12 20:36:21 +02:00
|
|
|
)
|
|
|
|
|
2017-12-05 11:58:12 +01:00
|
|
|
func fileExists(file string) bool {
|
|
|
|
info, err := os.Stat(file)
|
|
|
|
return err == nil && !info.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigFilePath() string {
|
|
|
|
if len(*configFile) > 0 {
|
|
|
|
return *configFile
|
|
|
|
}
|
|
|
|
|
|
|
|
if workingDir, err := os.Getwd(); err == nil {
|
|
|
|
configFile := filepath.Join(workingDir, "config.json")
|
|
|
|
if fileExists(configFile) {
|
|
|
|
return configFile
|
|
|
|
}
|
2015-10-18 13:14:34 +02:00
|
|
|
}
|
2017-12-05 11:58:12 +01:00
|
|
|
|
|
|
|
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
|
|
|
|
return configFile
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2015-10-18 13:14:34 +02:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:57:40 +01:00
|
|
|
func GetConfigFormat() string {
|
2016-10-16 14:22:21 +02:00
|
|
|
switch strings.ToLower(*format) {
|
|
|
|
case "pb", "protobuf":
|
2018-02-14 23:57:40 +01:00
|
|
|
return "protobuf"
|
2016-10-16 14:22:21 +02:00
|
|
|
default:
|
2018-02-14 23:57:40 +01:00
|
|
|
return "json"
|
2016-10-16 14:22:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:30:52 +01:00
|
|
|
func startV2Ray() (core.Server, error) {
|
2017-12-05 11:58:12 +01:00
|
|
|
configFile := getConfigFilePath()
|
2018-04-09 11:43:13 +02:00
|
|
|
configInput, err := confloader.LoadConfig(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to load config: ", configFile).Base(err)
|
2016-10-03 11:18:24 +02:00
|
|
|
}
|
2018-04-09 11:43:13 +02:00
|
|
|
defer configInput.Close()
|
|
|
|
|
2018-02-14 23:57:40 +01:00
|
|
|
config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
|
2015-09-12 20:36:21 +02:00
|
|
|
if err != nil {
|
2017-04-09 01:43:25 +02:00
|
|
|
return nil, newError("failed to read config file: ", configFile).Base(err)
|
2015-09-12 20:36:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 23:38:37 +02:00
|
|
|
server, err := core.New(config)
|
2015-09-12 20:36:21 +02:00
|
|
|
if err != nil {
|
2017-11-27 23:00:36 +01:00
|
|
|
return nil, newError("failed to create server").Base(err)
|
2015-09-12 20:36:21 +02:00
|
|
|
}
|
|
|
|
|
2017-04-05 23:38:37 +02:00
|
|
|
return server, nil
|
2016-07-10 23:11:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 14:03:55 +01:00
|
|
|
func printVersion() {
|
|
|
|
version := core.VersionStatement()
|
|
|
|
for _, s := range version {
|
|
|
|
fmt.Println(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 23:11:42 +02:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2018-02-16 14:03:55 +01:00
|
|
|
printVersion()
|
2016-07-10 23:11:42 +02:00
|
|
|
|
|
|
|
if *version {
|
2015-10-07 10:05:11 +02:00
|
|
|
return
|
2015-09-12 20:36:21 +02:00
|
|
|
}
|
2015-09-12 11:51:42 +02:00
|
|
|
|
2017-11-22 23:00:46 +01:00
|
|
|
if *plugin {
|
|
|
|
if err := core.LoadPlugins(); err != nil {
|
|
|
|
fmt.Println("Failed to load plugins:", err.Error())
|
2017-11-23 10:39:40 +01:00
|
|
|
os.Exit(-1)
|
2017-11-22 23:00:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:30:52 +01:00
|
|
|
server, err := startV2Ray()
|
2017-02-02 14:42:31 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2017-11-23 10:39:40 +01:00
|
|
|
os.Exit(-1)
|
2017-02-02 14:42:31 +01:00
|
|
|
}
|
2016-05-10 15:51:38 -07:00
|
|
|
|
2017-02-02 14:42:31 +01:00
|
|
|
if *test {
|
2017-04-06 15:13:09 +02:00
|
|
|
fmt.Println("Configuration OK.")
|
2017-11-23 10:39:40 +01:00
|
|
|
os.Exit(0)
|
2016-07-10 23:11:42 +02:00
|
|
|
}
|
2017-02-02 14:42:31 +01:00
|
|
|
|
2017-02-22 11:30:52 +01:00
|
|
|
if err := server.Start(); err != nil {
|
2017-04-06 15:13:09 +02:00
|
|
|
fmt.Println("Failed to start", err)
|
2017-11-23 10:39:40 +01:00
|
|
|
os.Exit(-1)
|
2017-02-02 14:42:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
osSignals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
|
|
|
|
<-osSignals
|
2017-02-22 11:30:52 +01:00
|
|
|
server.Close()
|
2015-09-12 11:51:42 +02:00
|
|
|
}
|