forked from slandx/v2rayGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
212 lines (190 loc) · 5.18 KB
/
Copy pathmain.go
File metadata and controls
212 lines (190 loc) · 5.18 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/json-iterator/go"
"github.com/pkg/errors"
)
//
const GVersionString = "v0.0.1"
const GConfigFilePath = "GConfig.json"
const V2rayConfigFilePath = "config.json"
const V2rayBinWin32 = "v2ray.exe"
const V2rayBinLinuxOrMac = "v2ray"
var v2rayBinaryName string
func main() {
if runtime.GOOS == "windows" {
v2rayBinaryName = V2rayBinWin32
} else {
v2rayBinaryName = V2rayBinLinuxOrMac
}
var subUrl string
var addVmess string
var port int
var showVersion bool
var needUpdate bool
flag.StringVar(&subUrl, "s", "", "Subscribe URL")
flag.StringVar(&addVmess, "a", "", "String starts with 'vmess://...'")
flag.IntVar(&port, "p", 1080, "Local port")
flag.BoolVar(&needUpdate, "u", false, "Update subscribe URL")
flag.BoolVar(&showVersion, "v", false, "Show version")
flag.Parse()
eColor := color.New(color.FgRed)
if showVersion {
fmt.Println(GVersionString)
return
}
if _, err := os.Stat(v2rayBinaryName); os.IsNotExist(err) {
eColor.Println("v2ray core not found\nDownload and extract to this folder from https://github.com/v2ray/v2ray-core/releases/latest")
return
}
if _, err := os.Stat(GConfigFilePath); os.IsNotExist(err) && len(subUrl) == 0 && len(addVmess) == 0 {
eColor.Println("v2rayGen Config file is not exist. Use -s or -a to add a config file first.")
flag.Usage()
return
}
var confObj ConfigInfo
confObj, err := readConfig(GConfigFilePath)
if err != nil {
//init config
confObj.LocalPort = port
confObj.Protocol = "socks"
confObj.Index = -1
}
if len(confObj.SubURL) == 0 && len(subUrl) == 0 {
eColor.Println("No subscribe URL found.")
flag.Usage()
return
}
if needUpdate && len(confObj.SubURL) > 0 && len(subUrl) == 0 {
subUrl = confObj.SubURL
}
if len(subUrl) != 0 {
confObj.SubURL = subUrl
err := updateBySubscribeUrl(&confObj, subUrl)
if err != nil {
eColor.Println(err)
return
}
}
if len(addVmess) != 0 {
if len(confObj.Vmess) == 0 {
vmObj, err := parseVmessUrl(addVmess)
if err != nil {
eColor.Println("Invalid vmess URL, skip.")
} else {
confObj.Vmess = append(confObj.Vmess, vmObj)
}
}
}
generateV2rayConfig(&confObj)
}
func readConfig(confPath string) (ConfigInfo, error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
confBytes, err := ioutil.ReadFile(confPath)
if err != nil {
return ConfigInfo{}, errors.New("Read config file failed")
}
var confObj ConfigInfo
err = json.Unmarshal(confBytes, &confObj)
if err != nil {
return ConfigInfo{}, errors.New("Parse config file failed")
}
return confObj, nil
}
func updateBySubscribeUrl(conf *ConfigInfo, subUrl string) error {
//fmt.Println(subUrl)
eColor := color.New(color.FgRed)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
resp, err := http.Get(subUrl)
if err != nil {
return errors.New("Cannot get subscribe URL")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Cannot get content of URL")
}
decodeBytes, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return errors.New("Decode failed with subscribe URL content")
}
vms := strings.Split(string(decodeBytes), "\n")
conf.Vmess = make([]VmessInfo, len(vms))
for idx, num := range vms {
if len(num) > 8 {
vmObj, err := parseVmessUrl(num)
if err != nil {
eColor.Println(err)
continue
}
conf.Vmess[idx] = vmObj
}
}
configJsonString, _ := json.Marshal(conf)
err = ioutil.WriteFile(GConfigFilePath, configJsonString, 0644)
if err != nil {
return errors.New("Save config file Failed")
}
return nil
}
func parseVmessUrl(vmessUrl string) (VmessInfo, error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
//fmt.Println(vmessUrl)
if len(vmessUrl) <= 0 {
return VmessInfo{}, errors.New("Empty vmess URL string")
}
if !strings.HasPrefix(vmessUrl, "vmess://") {
return VmessInfo{}, errors.New("Invalid vmess URL string")
}
vmDec, err := base64.StdEncoding.DecodeString(vmessUrl[8:])
if err != nil && len(vmDec) > 0 {
return VmessInfo{}, errors.New("Decode failed with vmess URL content")
}
//fmt.Println(string(vmDec))
var vmObj VmessInfo
err = json.Unmarshal(vmDec, &vmObj)
if err != nil {
return VmessInfo{}, errors.New("Decode json failed")
}
return vmObj, nil
}
func generateV2rayConfig(confObj *ConfigInfo) {
eColor := color.New(color.FgRed)
gColor := color.New(color.FgHiGreen)
wColor := color.New(color.FgWhite)
if len(confObj.Vmess) == 0 {
eColor.Println("No vmess config is found.")
return
}
for true {
for idx, vm := range confObj.Vmess {
if idx == confObj.Index {
gColor.Printf("%d. %s (active)\n", idx+1, vm.Ps)
} else {
wColor.Printf("%d. %s\n", idx+1, vm.Ps)
}
}
wColor.Printf("%d. 退出\n", len(confObj.Vmess)+1)
wColor.Print("Select active server:")
var activeIdx int
_, err := fmt.Scan(&activeIdx)
if err != nil || activeIdx < 1 || activeIdx > (len(confObj.Vmess)+1) {
eColor.Println("Error number")
continue
}
if activeIdx == (len(confObj.Vmess) + 1) {
return
}
confObj.Index = activeIdx
saveV2rayConfigAndRun(V2rayConfigFilePath, confObj)
break
}
}