simplify again

This commit is contained in:
Meo597
2026-10-26 05:46:09 +08:00
parent 98d7b719c5
commit 75c08917e3
5 changed files with 43 additions and 126 deletions
+29 -45
View File
@@ -2,7 +2,6 @@ package filesystem
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
@@ -49,7 +48,15 @@ func getAssetFileLocation(file string) (string, os.FileInfo, error) {
if err != nil {
return "", nil, err
}
return ResolveRegularFile(platform.GetAssetLocation(local))
path := platform.GetAssetLocation(local)
info, err := os.Stat(path)
if err != nil {
return "", nil, err
}
if !info.Mode().IsRegular() {
return "", nil, errors.New("asset is not a regular file")
}
return path, info, nil
}
func ReadCert(file string) ([]byte, error) {
@@ -59,52 +66,29 @@ func ReadCert(file string) ([]byte, error) {
return ReadFile(platform.GetCertLocation(file))
}
// ResolveLuaFile finds a local .lua script and returns its resolved absolute path.
// ResolveLuaFile finds a local .lua script and returns its absolute path.
// Relative paths: XRAY_LOCATION_CONFDIR > XRAY_LOCATION_CONFIG > working dir > executable dir.
func ResolveLuaFile(path string) (resolved string, err error) {
func ResolveLuaFile(path string) (string, error) {
if filepath.Ext(path) != ".lua" {
return "", errors.New("Lua file extension must be .lua")
}
if filepath.IsAbs(path) {
resolved, _, err = ResolveRegularFile(path)
} else {
for _, dir := range []string{
platform.GetConfDirPath(),
platform.NewEnvFlag(platform.ConfigLocation).GetValue(func() string { return "" }),
".",
platform.GetExecutableDir(),
} {
if dir != "" {
if resolved, _, err = ResolveRegularFile(filepath.Join(dir, path)); err == nil || !errors.Is(err, os.ErrNotExist) {
break
}
return path, nil
}
for _, dir := range []string{
platform.GetConfDirPath(),
platform.NewEnvFlag(platform.ConfigLocation).GetValue(func() string { return "" }),
".",
platform.GetExecutableDir(),
} {
if dir != "" {
resolved := filepath.Join(dir, path)
if _, err := os.Stat(resolved); err == nil {
return filepath.Abs(resolved)
} else if !os.IsNotExist(err) {
return "", err
}
}
}
if err != nil {
return "", err
}
if filepath.Ext(resolved) != ".lua" {
return "", errors.New("Lua file extension must be .lua")
}
return resolved, nil
}
// ResolveRegularFile returns an absolute path and metadata of a regular file.
// Symlinks are resolved when possible; otherwise the original path is kept.
func ResolveRegularFile(path string) (string, os.FileInfo, error) {
path, err := filepath.Abs(path)
if err != nil {
return "", nil, fmt.Errorf("failed to resolve file path: %w", err)
}
// Best effort: EvalSymlinks may fail when Windows junctions are involved.
if resolved, err := filepath.EvalSymlinks(path); err == nil {
path = resolved
}
info, err := os.Stat(path)
if err != nil {
return "", nil, fmt.Errorf("failed to inspect file %q: %w", path, err)
}
if !info.Mode().IsRegular() {
return "", nil, fmt.Errorf("file is not a regular file: %s", path)
}
// Hard links are not handled.
return path, info, nil
return "", os.ErrNotExist
}
+3 -50
View File
@@ -4,8 +4,6 @@ import (
"errors"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/xtls/xray-core/common"
@@ -38,8 +36,9 @@ func TestResolveAssetRejectsInvalidPath(t *testing.T) {
}
func TestResolveLuaFile(t *testing.T) {
workingDir := t.TempDir()
t.Chdir(workingDir)
t.Chdir(t.TempDir())
workingDir, err := os.Getwd()
common.Must(err)
executable, err := os.Executable()
common.Must(err)
file, err := os.CreateTemp(filepath.Dir(executable), "lua-*.lua")
@@ -59,10 +58,6 @@ func TestResolveLuaFile(t *testing.T) {
for _, path := range paths[:3] {
common.Must(os.WriteFile(path, nil, 0o600))
}
for i, path := range paths {
paths[i], err = filepath.EvalSymlinks(path)
common.Must(err)
}
if got, err := ResolveLuaFile(paths[2]); err != nil || got != paths[2] {
t.Fatalf("absolute path = %q, %v; want %q", got, err, paths[2])
}
@@ -79,46 +74,4 @@ func TestResolveLuaFile(t *testing.T) {
if _, err := ResolveLuaFile(name); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("missing file error = %v", err)
}
t.Setenv(platform.ConfdirLocation, filepath.Dir(paths[0]))
t.Setenv(platform.ConfigLocation, filepath.Dir(paths[1]))
common.Must(os.Mkdir(paths[0], 0o700))
common.Must(os.WriteFile(paths[1], nil, 0o600))
for _, path := range []string{"", name, filepath.Join(t.TempDir(), name)} {
if _, err := ResolveLuaFile(path); err == nil {
t.Fatalf("accepted invalid path %q", path)
}
}
}
func TestResolveLuaFileSymlink(t *testing.T) {
for _, tc := range []struct {
link, target string
valid bool
}{
{"alias.lua", "data.dat", false},
{"alias.dat", "script.lua", true},
} {
t.Run(tc.link, func(t *testing.T) {
dir, err := filepath.EvalSymlinks(t.TempDir())
common.Must(err)
target := filepath.Join(dir, tc.target)
common.Must(os.WriteFile(target, nil, 0o600))
link := filepath.Join(dir, tc.link)
if err := os.Symlink(tc.target, link); err != nil {
if runtime.GOOS == "windows" {
t.Skipf("cannot create symlinks: %v", err)
}
t.Fatal(err)
}
got, err := ResolveLuaFile(link)
if tc.valid {
if err != nil || got != target {
t.Fatalf("ResolveLuaFile(%q) = %q, %v; want %q", link, got, err, target)
}
} else if err == nil || got != "" || !strings.Contains(err.Error(), "Lua file extension must be .lua") {
t.Fatalf("ResolveLuaFile(%q) = %q, %v; want .lua extension error", link, got, err)
}
})
}
}