others.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // +build !windows
  2. package platform
  3. import (
  4. "errors"
  5. "io/fs"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. func ExpandEnv(s string) string {
  11. return os.ExpandEnv(s)
  12. }
  13. func LineSeparator() string {
  14. return "\n"
  15. }
  16. func GetToolLocation(file string) string {
  17. const name = "v2ray.location.tool"
  18. toolPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
  19. return filepath.Join(toolPath, file)
  20. }
  21. // GetAssetLocation search for `file` in certain locations
  22. func GetAssetLocation(file string) string {
  23. filepathCleaned := filepath.Clean(file)
  24. if strings.HasPrefix("..", filepathCleaned) {
  25. newError("directory transversal is not allowed for assets. This will be forbidden in v5.").AtWarning().WriteToLog()
  26. }
  27. const name = "v2ray.location.asset"
  28. assetPath := NewEnvFlag(name).GetValue(getExecutableDir)
  29. defPath := filepath.Join(assetPath, file)
  30. for _, p := range []string{
  31. defPath,
  32. filepath.Join("/usr/local/share/v2ray/", file),
  33. filepath.Join("/usr/share/v2ray/", file),
  34. filepath.Join("/opt/share/v2ray/", file),
  35. } {
  36. if _, err := os.Stat(p); err != nil && errors.Is(err, fs.ErrNotExist) {
  37. continue
  38. }
  39. // asset found
  40. return p
  41. }
  42. // asset not found, let the caller throw out the error
  43. return defPath
  44. }