common.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Package common contains common utilities that are shared among other packages.
  2. // See each sub-package for detail.
  3. package common
  4. import (
  5. "fmt"
  6. "go/build"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/xtls/xray-core/common/errors"
  11. )
  12. //go:generate go run github.com/xtls/xray-core/common/errors/errorgen
  13. // ErrNoClue is for the situation that existing information is not enough to make a decision. For example, Router may return this error when there is no suitable route.
  14. var ErrNoClue = errors.New("not enough information for making a decision")
  15. // Must panics if err is not nil.
  16. func Must(err error) {
  17. if err != nil {
  18. panic(err)
  19. }
  20. }
  21. // Must2 panics if the second parameter is not nil, otherwise returns the first parameter.
  22. func Must2(v interface{}, err error) interface{} {
  23. Must(err)
  24. return v
  25. }
  26. // Error2 returns the err from the 2nd parameter.
  27. func Error2(v interface{}, err error) error {
  28. return err
  29. }
  30. // envFile returns the name of the Go environment configuration file.
  31. // Copy from https://github.com/golang/go/blob/c4f2a9788a7be04daf931ac54382fbe2cb754938/src/cmd/go/internal/cfg/cfg.go#L150-L166
  32. func envFile() (string, error) {
  33. if file := os.Getenv("GOENV"); file != "" {
  34. if file == "off" {
  35. return "", fmt.Errorf("GOENV=off")
  36. }
  37. return file, nil
  38. }
  39. dir, err := os.UserConfigDir()
  40. if err != nil {
  41. return "", err
  42. }
  43. if dir == "" {
  44. return "", fmt.Errorf("missing user-config dir")
  45. }
  46. return filepath.Join(dir, "go", "env"), nil
  47. }
  48. // GetRuntimeEnv returns the value of runtime environment variable,
  49. // that is set by running following command: `go env -w key=value`.
  50. func GetRuntimeEnv(key string) (string, error) {
  51. file, err := envFile()
  52. if err != nil {
  53. return "", err
  54. }
  55. if file == "" {
  56. return "", fmt.Errorf("missing runtime env file")
  57. }
  58. var data []byte
  59. var runtimeEnv string
  60. data, readErr := os.ReadFile(file)
  61. if readErr != nil {
  62. return "", readErr
  63. }
  64. envStrings := strings.Split(string(data), "\n")
  65. for _, envItem := range envStrings {
  66. envItem = strings.TrimSuffix(envItem, "\r")
  67. envKeyValue := strings.Split(envItem, "=")
  68. if strings.EqualFold(strings.TrimSpace(envKeyValue[0]), key) {
  69. runtimeEnv = strings.TrimSpace(envKeyValue[1])
  70. }
  71. }
  72. return runtimeEnv, nil
  73. }
  74. // GetGOBIN returns GOBIN environment variable as a string. It will NOT be empty.
  75. func GetGOBIN() string {
  76. // The one set by user explicitly by `export GOBIN=/path` or `env GOBIN=/path command`
  77. GOBIN := os.Getenv("GOBIN")
  78. if GOBIN == "" {
  79. var err error
  80. // The one set by user by running `go env -w GOBIN=/path`
  81. GOBIN, err = GetRuntimeEnv("GOBIN")
  82. if err != nil {
  83. // The default one that Golang uses
  84. return filepath.Join(build.Default.GOPATH, "bin")
  85. }
  86. if GOBIN == "" {
  87. return filepath.Join(build.Default.GOPATH, "bin")
  88. }
  89. return GOBIN
  90. }
  91. return GOBIN
  92. }
  93. // GetGOPATH returns GOPATH environment variable as a string. It will NOT be empty.
  94. func GetGOPATH() string {
  95. // The one set by user explicitly by `export GOPATH=/path` or `env GOPATH=/path command`
  96. GOPATH := os.Getenv("GOPATH")
  97. if GOPATH == "" {
  98. var err error
  99. // The one set by user by running `go env -w GOPATH=/path`
  100. GOPATH, err = GetRuntimeEnv("GOPATH")
  101. if err != nil {
  102. // The default one that Golang uses
  103. return build.Default.GOPATH
  104. }
  105. if GOPATH == "" {
  106. return build.Default.GOPATH
  107. }
  108. return GOPATH
  109. }
  110. return GOPATH
  111. }
  112. // GetModuleName returns the value of module in `go.mod` file.
  113. func GetModuleName(pathToProjectRoot string) (string, error) {
  114. var moduleName string
  115. loopPath := pathToProjectRoot
  116. for {
  117. if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 {
  118. gomodPath := filepath.Join(loopPath, "go.mod")
  119. gomodBytes, err := os.ReadFile(gomodPath)
  120. if err != nil {
  121. loopPath = loopPath[:idx]
  122. continue
  123. }
  124. gomodContent := string(gomodBytes)
  125. moduleIdx := strings.Index(gomodContent, "module ")
  126. newLineIdx := strings.Index(gomodContent, "\n")
  127. if moduleIdx >= 0 {
  128. if newLineIdx >= 0 {
  129. moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx])
  130. moduleName = strings.TrimSuffix(moduleName, "\r")
  131. } else {
  132. moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:])
  133. }
  134. return moduleName, nil
  135. }
  136. return "", fmt.Errorf("can not get module path in `%s`", gomodPath)
  137. }
  138. break
  139. }
  140. return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot)
  141. }