2015-09-12 05:51:42 -04:00
|
|
|
package main
|
|
|
|
|
2015-09-12 14:36:21 -04:00
|
|
|
import (
|
|
|
|
"flag"
|
2015-09-18 05:59:36 -04:00
|
|
|
"fmt"
|
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-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"
|
|
|
|
_ "v2ray.com/core/app/router/rules"
|
|
|
|
"v2ray.com/core/common/log"
|
|
|
|
"v2ray.com/core/shell/point"
|
2015-09-12 14:36:21 -04:00
|
|
|
|
2016-02-05 16:04:43 -05:00
|
|
|
// The following are necessary as they register handlers in their init functions.
|
2016-08-20 14:55:45 -04:00
|
|
|
_ "v2ray.com/core/proxy/blackhole"
|
|
|
|
_ "v2ray.com/core/proxy/dokodemo"
|
|
|
|
_ "v2ray.com/core/proxy/freedom"
|
|
|
|
_ "v2ray.com/core/proxy/http"
|
|
|
|
_ "v2ray.com/core/proxy/shadowsocks"
|
|
|
|
_ "v2ray.com/core/proxy/socks"
|
|
|
|
_ "v2ray.com/core/proxy/vmess/inbound"
|
|
|
|
_ "v2ray.com/core/proxy/vmess/outbound"
|
|
|
|
|
|
|
|
_ "v2ray.com/core/transport/internet/kcp"
|
|
|
|
_ "v2ray.com/core/transport/internet/tcp"
|
|
|
|
_ "v2ray.com/core/transport/internet/udp"
|
|
|
|
_ "v2ray.com/core/transport/internet/ws"
|
|
|
|
|
|
|
|
_ "v2ray.com/core/transport/internet/authenticators/noop"
|
|
|
|
_ "v2ray.com/core/transport/internet/authenticators/srtp"
|
|
|
|
_ "v2ray.com/core/transport/internet/authenticators/utp"
|
2015-09-12 14:36:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-10-18 07:14:34 -04:00
|
|
|
configFile string
|
2015-10-07 04:05:11 -04:00
|
|
|
logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
|
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.")
|
2015-09-12 14:36:21 -04:00
|
|
|
)
|
|
|
|
|
2015-10-18 07:14:34 -04: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-07-10 17:11:42 -04:00
|
|
|
func startV2Ray() *point.Point {
|
2015-09-15 18:06:22 -04:00
|
|
|
switch *logLevel {
|
|
|
|
case "debug":
|
|
|
|
log.SetLogLevel(log.DebugLevel)
|
|
|
|
case "info":
|
|
|
|
log.SetLogLevel(log.InfoLevel)
|
|
|
|
case "warning":
|
|
|
|
log.SetLogLevel(log.WarningLevel)
|
|
|
|
case "error":
|
|
|
|
log.SetLogLevel(log.ErrorLevel)
|
2015-10-07 04:05:11 -04:00
|
|
|
default:
|
|
|
|
fmt.Println("Unknown log level: " + *logLevel)
|
2016-07-10 17:11:42 -04:00
|
|
|
return nil
|
2015-09-15 18:06:22 -04:00
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
|
2015-10-18 07:14:34 -04:00
|
|
|
if len(configFile) == 0 {
|
2015-10-07 04:05:11 -04:00
|
|
|
log.Error("Config file is not set.")
|
2016-07-10 17:11:42 -04:00
|
|
|
return nil
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
2016-01-17 15:43:10 -05:00
|
|
|
config, err := point.LoadConfig(configFile)
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Failed to read config file (", configFile, "): ", configFile, err)
|
2016-07-10 17:11:42 -04:00
|
|
|
return nil
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-17 15:43:10 -05:00
|
|
|
if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 {
|
|
|
|
log.InitAccessLogger(config.LogConfig.AccessLog)
|
2015-10-09 11:43:27 -04:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:51:19 -04:00
|
|
|
vPoint, err := point.NewPoint(config)
|
2015-09-12 14:36:21 -04:00
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Failed to create Point server: ", err)
|
2016-07-10 17:11:42 -04:00
|
|
|
return nil
|
2015-09-12 14:36:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-24 06:58:04 -05:00
|
|
|
if *test {
|
|
|
|
fmt.Println("Configuration OK.")
|
2016-07-10 17:11:42 -04:00
|
|
|
return nil
|
2016-01-24 06:58:04 -05:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:36:21 -04:00
|
|
|
err = vPoint.Start()
|
|
|
|
if err != nil {
|
2016-01-18 06:24:33 -05:00
|
|
|
log.Error("Error starting Point server: ", err)
|
2016-07-10 17:11:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return vPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-10 17:11:42 -04:00
|
|
|
if point := startV2Ray(); point != nil {
|
|
|
|
osSignals := make(chan os.Signal, 1)
|
2016-08-09 16:10:44 -04:00
|
|
|
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
|
2016-05-10 18:51:38 -04:00
|
|
|
|
2016-07-10 17:11:42 -04:00
|
|
|
<-osSignals
|
|
|
|
point.Close()
|
|
|
|
}
|
|
|
|
log.Close()
|
2015-09-12 05:51:42 -04:00
|
|
|
}
|