forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (91 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"v2ray.com/core"
"v2ray.com/core/common/log"
_ "v2ray.com/core/main/distro/all"
)
var (
configFile string
version = flag.Bool("version", false, "Show current version of V2Ray.")
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
}
}
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
}
vPoint, err := core.NewPoint(config)
if err != nil {
log.Error("Failed to create Point server: ", err)
return nil
}
if *test {
fmt.Println("Configuration OK.")
return nil
}
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)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
<-osSignals
point.Close()
}
log.Close()
}