1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/main/main.go

128 lines
2.6 KiB
Go
Raw Normal View History

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