2015-09-12 09:51:42 +00:00
|
|
|
package main
|
|
|
|
|
2015-09-12 18:36:21 +00:00
|
|
|
import (
|
|
|
|
"flag"
|
2015-09-18 09:59:36 +00:00
|
|
|
"fmt"
|
2016-10-03 09:18:24 +00:00
|
|
|
"io"
|
2015-10-18 11:14:34 +00:00
|
|
|
"os"
|
2016-05-10 22:51:38 +00:00
|
|
|
"os/signal"
|
2015-10-18 11:14:34 +00:00
|
|
|
"path/filepath"
|
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"
|
2017-02-02 13:42:31 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-12-16 22:38:58 +00:00
|
|
|
|
|
|
|
_ "v2ray.com/core/main/distro/all"
|
2015-09-12 18:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-10-18 11:14:34 +00:00
|
|
|
configFile string
|
2015-09-18 09:59:36 +00:00
|
|
|
version = flag.Bool("version", false, "Show current version of V2Ray.")
|
2016-01-24 11:58:04 +00:00
|
|
|
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
|
2016-10-03 09:18:24 +00:00
|
|
|
format = flag.String("format", "json", "Format of input file.")
|
2015-09-12 18:36:21 +00:00
|
|
|
)
|
|
|
|
|
2015-10-18 11:14:34 +00:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
|
2016-10-16 12:22:21 +00: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 10:30:52 +00:00
|
|
|
func startV2Ray() (core.Server, error) {
|
2015-10-18 11:14:34 +00:00
|
|
|
if len(configFile) == 0 {
|
2017-02-02 13:42:31 +00:00
|
|
|
return nil, errors.New("V2Ray: Config file is not set.")
|
2015-09-12 18:36:21 +00:00
|
|
|
}
|
2016-10-03 09:18:24 +00: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-02-02 13:42:31 +00:00
|
|
|
return nil, errors.Base(err).Message("V2Ray: Config file not readable.")
|
2016-10-03 09:18:24 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
configInput = file
|
|
|
|
}
|
2016-10-16 12:22:21 +00:00
|
|
|
config, err := core.LoadConfig(GetConfigFormat(), configInput)
|
2015-09-12 18:36:21 +00:00
|
|
|
if err != nil {
|
2017-02-02 13:42:31 +00:00
|
|
|
return nil, errors.Base(err).Message("V2Ray: Failed to read config file: ", configFile)
|
2015-09-12 18:36:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 10:30:52 +00:00
|
|
|
vPoint, err := core.New(config)
|
2015-09-12 18:36:21 +00:00
|
|
|
if err != nil {
|
2017-02-02 13:42:31 +00:00
|
|
|
return nil, errors.Base(err).Message("V2Ray: Failed to create initialize.")
|
2015-09-12 18:36:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 13:42:31 +00:00
|
|
|
return vPoint, nil
|
2016-07-10 21:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
core.PrintVersion()
|
|
|
|
|
|
|
|
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 {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-05-10 22:51:38 +00:00
|
|
|
|
2017-02-02 13:42:31 +00:00
|
|
|
if *test {
|
|
|
|
fmt.Println("V2Ray: Configuration OK.")
|
|
|
|
return
|
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-02-02 13:42:31 +00:00
|
|
|
fmt.Println("V2Ray: Failed to start. ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
osSignals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
|
|
|
|
|
|
|
|
<-osSignals
|
2017-02-22 10:30:52 +00:00
|
|
|
server.Close()
|
2015-09-12 09:51:42 +00:00
|
|
|
}
|