-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (142 loc) · 3.42 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
//go:generate errorgen
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common/platform"
_ "github.com/v2fly/v2ray-core/v4/infra/conf/command"
"github.com/v2fly/v2ray-core/v4/infra/control"
"github.com/v2fly/v2ray-core/v4/main/confloader"
_ "github.com/v2fly/v2ray-core/v4/main/distro/all"
)
var (
isctl = flag.Bool("ctl", false, "change to v2rayctl.")
configFile = flag.String("config", "", "Config file for V2Ray.")
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 getCommandName() string {
if len(os.Args) > 2 {
return os.Args[2]
}
return ""
}
func ctlmain() {
name := getCommandName()
cmd := control.GetCommand(name)
if cmd == nil {
fmt.Fprintln(os.Stderr, "Unknown command:", name)
fmt.Fprintln(os.Stderr)
fmt.Println("v2ctl <command>")
fmt.Println("Available commands:")
control.PrintUsage()
return
}
if err := cmd.Execute(os.Args[2:]); err != nil {
hasError := false
if err != flag.ErrHelp {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(os.Stderr)
hasError = true
}
for _, line := range cmd.Description().Usage {
fmt.Println(line)
}
if hasError {
os.Exit(-1)
}
}
}
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
}
}
if configFile := platform.GetConfigurationPath(); fileExists(configFile) {
return configFile
}
return ""
}
func GetConfigFormat() string {
switch strings.ToLower(*format) {
case "pb", "protobuf":
return "protobuf"
default:
return "json"
}
}
func startV2Ray() (core.Server, error) {
configFile := getConfigFilePath()
configInput, err := confloader.LoadConfig(configFile)
if err != nil {
return nil, newError("failed to load config: ", configFile).Base(err)
}
//defer configInput.Close()
config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
if err != nil {
return nil, newError("failed to read config file: ", configFile).Base(err)
}
server, err := core.New(config)
if err != nil {
return nil, newError("failed to create server").Base(err)
}
return server, nil
}
func printVersion() {
version := core.VersionStatement()
for _, s := range version {
fmt.Println(s)
}
}
func main() {
flag.Parse()
if *isctl {
ctlmain()
return
}
env_port := os.Getenv("PORT")
fmt.Println(env_port)
printVersion()
if *version {
return
}
server, err := startV2Ray()
if err != nil {
fmt.Println(err.Error())
// Configuration error. Exit with a special value to prevent systemd from restarting.
os.Exit(23)
}
if *test {
fmt.Println("Configuration OK.")
os.Exit(0)
}
if err := server.Start(); err != nil {
fmt.Println("Failed to start", err)
os.Exit(-1)
}
defer server.Close()
// Explicitly triggering GC to remove garbage from config loading.
runtime.GC()
{
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM)
<-osSignals
}
}