You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/main/main.go

108 lines
2.1 KiB

package main
import (
"flag"
9 years ago
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
8 years ago
"syscall"
"v2ray.com/core"
"v2ray.com/core/common/log"
8 years ago
_ "v2ray.com/core/main/distro/all"
)
var (
configFile string
9 years ago
version = flag.Bool("version", false, "Show current version of V2Ray.")
9 years ago
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
format = flag.String("format", "json", "Format of input file.")
)
func init() {
defaultConfigFile := ""
workingDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err == nil {
defaultConfigFile = filepath.Join(workingDir, "config.json")
}
flag.StringVar(&configFile, "config", defaultConfigFile, "Config file for this Point server.")
}
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
}
}
8 years ago
func startV2Ray() *core.Point {
if len(configFile) == 0 {
log.Error("Config file is not set.")
return nil
}
var configInput io.Reader
if configFile == "stdin:" {
configInput = os.Stdin
} else {
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
log.Error("Config file not readable: ", err)
return nil
}
defer file.Close()
configInput = file
}
config, err := core.LoadConfig(GetConfigFormat(), configInput)
if err != nil {
log.Error("Failed to read config file (", configFile, "): ", configFile, err)
return nil
}
8 years ago
vPoint, err := core.NewPoint(config)
if err != nil {
log.Error("Failed to create Point server: ", err)
return nil
}
9 years ago
if *test {
fmt.Println("Configuration OK.")
return nil
9 years ago
}
err = vPoint.Start()
if err != nil {
log.Error("Error starting Point server: ", err)
return nil
}
return vPoint
}
func main() {
flag.Parse()
core.PrintVersion()
if *version {
return
}
if point := startV2Ray(); point != nil {
osSignals := make(chan os.Signal, 1)
8 years ago
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
<-osSignals
point.Close()
}
log.Close()
}