Skip to content

Commit 7459e2c

Browse files
authored
Fix: vprotogen loop dependency (v2fly#797)
* Chore: remove protoc-gen-gofast * Fix: loop dependency * Chore: regenerate VLESS pb.go file * Fix: compare test
1 parent 9e9173a commit 7459e2c

File tree

4 files changed

+210
-363
lines changed

4 files changed

+210
-363
lines changed

infra/vprotogen/main.go

+80-13
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,99 @@ package main
22

33
import (
44
"fmt"
5+
"go/build"
6+
"io/ioutil"
57
"os"
68
"os/exec"
79
"path/filepath"
810
"runtime"
911
"strings"
10-
11-
core "github.com/v2fly/v2ray-core/v4"
12-
"github.com/v2fly/v2ray-core/v4/common"
1312
)
1413

14+
// envFile returns the name of the Go environment configuration file.
15+
// Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166
16+
func envFile() (string, error) {
17+
if file := os.Getenv("GOENV"); file != "" {
18+
if file == "off" {
19+
return "", fmt.Errorf("GOENV=off")
20+
}
21+
return file, nil
22+
}
23+
dir, err := os.UserConfigDir()
24+
if err != nil {
25+
return "", err
26+
}
27+
if dir == "" {
28+
return "", fmt.Errorf("missing user-config dir")
29+
}
30+
return filepath.Join(dir, "go", "env"), nil
31+
}
32+
33+
// GetRuntimeEnv returns the value of runtime environment variable,
34+
// that is set by running following command: `go env -w key=value`.
35+
func GetRuntimeEnv(key string) (string, error) {
36+
file, err := envFile()
37+
if err != nil {
38+
return "", err
39+
}
40+
if file == "" {
41+
return "", fmt.Errorf("missing runtime env file")
42+
}
43+
var data []byte
44+
var runtimeEnv string
45+
data, readErr := ioutil.ReadFile(file)
46+
if readErr != nil {
47+
return "", readErr
48+
}
49+
envStrings := strings.Split(string(data), "\n")
50+
for _, envItem := range envStrings {
51+
envItem = strings.TrimSuffix(envItem, "\r")
52+
envKeyValue := strings.Split(envItem, "=")
53+
if strings.EqualFold(strings.TrimSpace(envKeyValue[0]), key) {
54+
runtimeEnv = strings.TrimSpace(envKeyValue[1])
55+
}
56+
}
57+
return runtimeEnv, nil
58+
}
59+
60+
// GetGOBIN returns GOBIN environment variable as a string. It will NOT be empty.
61+
func GetGOBIN() string {
62+
// The one set by user explicitly by `export GOBIN=/path` or `env GOBIN=/path command`
63+
GOBIN := os.Getenv("GOBIN")
64+
if GOBIN == "" {
65+
var err error
66+
// The one set by user by running `go env -w GOBIN=/path`
67+
GOBIN, err = GetRuntimeEnv("GOBIN")
68+
if err != nil {
69+
// The default one that Golang uses
70+
return filepath.Join(build.Default.GOPATH, "bin")
71+
}
72+
if GOBIN == "" {
73+
return filepath.Join(build.Default.GOPATH, "bin")
74+
}
75+
return GOBIN
76+
}
77+
return GOBIN
78+
}
79+
1580
func main() {
1681
pwd, wdErr := os.Getwd()
1782
if wdErr != nil {
1883
fmt.Println("Can not get current working directory.")
1984
os.Exit(1)
2085
}
2186

22-
GOBIN := common.GetGOBIN()
87+
GOBIN := GetGOBIN()
2388
binPath := os.Getenv("PATH")
24-
pathSlice := []string{binPath, GOBIN, pwd}
89+
pathSlice := []string{pwd, GOBIN, binPath}
2590
binPath = strings.Join(pathSlice, string(os.PathListSeparator))
2691
os.Setenv("PATH", binPath)
2792

28-
EXE := ""
93+
suffix := ""
2994
if runtime.GOOS == "windows" {
30-
EXE = ".exe"
95+
suffix = ".exe"
3196
}
32-
protoc := "protoc" + EXE
97+
protoc := "protoc" + suffix
3398

3499
if path, err := exec.LookPath(protoc); err != nil {
35100
fmt.Println("Make sure that you have `" + protoc + "` in your system path or current path. To download it, please visit https://github.com/protocolbuffers/protobuf/releases")
@@ -64,11 +129,13 @@ func main() {
64129

65130
for _, files := range protoFilesMap {
66131
for _, relProtoFile := range files {
67-
var args []string
68-
if core.ProtoFilesUsingProtocGenGoFast[relProtoFile] {
69-
args = []string{"--gofast_out", pwd, "--gofast_opt", "paths=source_relative", "--plugin", "protoc-gen-gofast=" + GOBIN + "/protoc-gen-gofast" + EXE}
70-
} else {
71-
args = []string{"--go_out", pwd, "--go_opt", "paths=source_relative", "--go-grpc_out", pwd, "--go-grpc_opt", "paths=source_relative", "--plugin", "protoc-gen-go=" + GOBIN + "/protoc-gen-go" + EXE, "--plugin", "protoc-gen-go-grpc=" + GOBIN + "/protoc-gen-go-grpc" + EXE}
132+
args := []string{
133+
"--go_out", pwd,
134+
"--go_opt", "paths=source_relative",
135+
"--go-grpc_out", pwd,
136+
"--go-grpc_opt", "paths=source_relative",
137+
"--plugin", "protoc-gen-go=" + filepath.Join(GOBIN, "protoc-gen-go"+suffix),
138+
"--plugin", "protoc-gen-go-grpc=" + filepath.Join(GOBIN, "protoc-gen-go-grpc"+suffix),
72139
}
73140
args = append(args, relProtoFile)
74141
cmd := exec.Command(protoc, args...)

proto.go

-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package core
22

3-
import "path/filepath"
4-
53
//go:generate go install -v google.golang.org/protobuf/cmd/protoc-gen-go@latest
64
//go:generate go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
7-
//go:generate go install -v github.com/gogo/protobuf/protoc-gen-gofast@latest
85
//go:generate go run ./infra/vprotogen/
9-
10-
// ProtoFilesUsingProtocGenGoFast is the map of Proto files
11-
// that use `protoc-gen-gofast` to generate pb.go files
12-
var ProtoFilesUsingProtocGenGoFast = map[string]bool{filepath.Join("proxy", "vless", "encoding", "addons.proto"): true}

0 commit comments

Comments
 (0)