From c6beeabf0444e1496bf3a0f081a776e92be0d3b3 Mon Sep 17 00:00:00 2001 From: patterniha Date: Mon, 24 Mar 2025 00:46:37 +0330 Subject: [PATCH 1/5] add "certificate" environment variable --- common/platform/filesystem/file.go | 8 ++++++++ common/platform/others.go | 5 +++++ common/platform/platform.go | 11 ++++++----- common/platform/windows.go | 5 +++++ infra/conf/transport_internet.go | 2 +- transport/internet/tls/config.go | 4 ++-- 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/common/platform/filesystem/file.go b/common/platform/filesystem/file.go index e10bfc11156e..958df8e640b9 100644 --- a/common/platform/filesystem/file.go +++ b/common/platform/filesystem/file.go @@ -3,6 +3,7 @@ package filesystem import ( "io" "os" + "path/filepath" "github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/platform" @@ -28,6 +29,13 @@ func ReadAsset(file string) ([]byte, error) { return ReadFile(platform.GetAssetLocation(file)) } +func ReadCertificate(file string) ([]byte, error) { + if filepath.IsAbs(file) { + return ReadFile(file) + } + return ReadFile(platform.GetCertificateLocation(file)) +} + func CopyFile(dst string, src string) error { bytes, err := ReadFile(src) if err != nil { diff --git a/common/platform/others.go b/common/platform/others.go index 7401a526a30d..3c4c4c9ebddd 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -42,3 +42,8 @@ func GetAssetLocation(file string) string { // asset not found, let the caller throw out the error return defPath } + +func GetCertificateLocation(file string) string { + certificatePath := NewEnvFlag(CertificateLocation).GetValue(getExecutableDir) + return filepath.Join(certificatePath, file) +} diff --git a/common/platform/platform.go b/common/platform/platform.go index 51e254475039..ce887b0920d3 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -8,11 +8,12 @@ import ( ) const ( - PluginLocation = "xray.location.plugin" - ConfigLocation = "xray.location.config" - ConfdirLocation = "xray.location.confdir" - ToolLocation = "xray.location.tool" - AssetLocation = "xray.location.asset" + PluginLocation = "xray.location.plugin" + ConfigLocation = "xray.location.config" + ConfdirLocation = "xray.location.confdir" + ToolLocation = "xray.location.tool" + AssetLocation = "xray.location.asset" + CertificateLocation = "xray.location.certificate" UseReadV = "xray.buf.readv" UseFreedomSplice = "xray.buf.splice" diff --git a/common/platform/windows.go b/common/platform/windows.go index 872e84615140..3768d2a52c12 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -24,3 +24,8 @@ func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) return filepath.Join(assetPath, file) } + +func GetCertificateLocation(file string) string { + certificatePath := NewEnvFlag(CertificateLocation).GetValue(getExecutableDir) + return filepath.Join(certificatePath, file) +} diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index 2d73e52408ae..f3afd2f9afbb 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -334,7 +334,7 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) { func readFileOrString(f string, s []string) ([]byte, error) { if len(f) > 0 { - return filesystem.ReadFile(f) + return filesystem.ReadCertificate(f) } if len(s) > 0 { return []byte(strings.Join(s, "\n")), nil diff --git a/transport/internet/tls/config.go b/transport/internet/tls/config.go index 171b30b677e3..197895cb1d13 100644 --- a/transport/internet/tls/config.go +++ b/transport/internet/tls/config.go @@ -109,12 +109,12 @@ func setupOcspTicker(entry *Certificate, callback func(isReloaded, isOcspstaplin for { var isReloaded bool if entry.CertificatePath != "" && entry.KeyPath != "" { - newCert, err := filesystem.ReadFile(entry.CertificatePath) + newCert, err := filesystem.ReadCertificate(entry.CertificatePath) if err != nil { errors.LogErrorInner(context.Background(), err, "failed to parse certificate") return } - newKey, err := filesystem.ReadFile(entry.KeyPath) + newKey, err := filesystem.ReadCertificate(entry.KeyPath) if err != nil { errors.LogErrorInner(context.Background(), err, "failed to parse key") return From 17f2dbcdb772218ff833082578f739d30646553e Mon Sep 17 00:00:00 2001 From: patterniha Date: Mon, 24 Mar 2025 14:25:29 +0330 Subject: [PATCH 2/5] rename --- common/platform/filesystem/file.go | 2 +- common/platform/others.go | 4 ++-- common/platform/platform.go | 12 ++++++------ common/platform/windows.go | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/common/platform/filesystem/file.go b/common/platform/filesystem/file.go index 958df8e640b9..7594395320ca 100644 --- a/common/platform/filesystem/file.go +++ b/common/platform/filesystem/file.go @@ -33,7 +33,7 @@ func ReadCertificate(file string) ([]byte, error) { if filepath.IsAbs(file) { return ReadFile(file) } - return ReadFile(platform.GetCertificateLocation(file)) + return ReadFile(platform.GetCertLocation(file)) } func CopyFile(dst string, src string) error { diff --git a/common/platform/others.go b/common/platform/others.go index 3c4c4c9ebddd..6b7738a74dc3 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -43,7 +43,7 @@ func GetAssetLocation(file string) string { return defPath } -func GetCertificateLocation(file string) string { - certificatePath := NewEnvFlag(CertificateLocation).GetValue(getExecutableDir) +func GetCertLocation(file string) string { + certificatePath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) return filepath.Join(certificatePath, file) } diff --git a/common/platform/platform.go b/common/platform/platform.go index ce887b0920d3..b865dc0db0fe 100644 --- a/common/platform/platform.go +++ b/common/platform/platform.go @@ -8,12 +8,12 @@ import ( ) const ( - PluginLocation = "xray.location.plugin" - ConfigLocation = "xray.location.config" - ConfdirLocation = "xray.location.confdir" - ToolLocation = "xray.location.tool" - AssetLocation = "xray.location.asset" - CertificateLocation = "xray.location.certificate" + PluginLocation = "xray.location.plugin" + ConfigLocation = "xray.location.config" + ConfdirLocation = "xray.location.confdir" + ToolLocation = "xray.location.tool" + AssetLocation = "xray.location.asset" + CertLocation = "xray.location.cert" UseReadV = "xray.buf.readv" UseFreedomSplice = "xray.buf.splice" diff --git a/common/platform/windows.go b/common/platform/windows.go index 3768d2a52c12..e251113d0464 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -25,7 +25,7 @@ func GetAssetLocation(file string) string { return filepath.Join(assetPath, file) } -func GetCertificateLocation(file string) string { - certificatePath := NewEnvFlag(CertificateLocation).GetValue(getExecutableDir) +func GetCertLocation(file string) string { + certificatePath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) return filepath.Join(certificatePath, file) } From 55c7874d5c69118599c103b72cb72e29b4d3d2d0 Mon Sep 17 00:00:00 2001 From: patterniha Date: Mon, 24 Mar 2025 15:42:18 +0330 Subject: [PATCH 3/5] rename --- common/platform/filesystem/file.go | 2 +- common/platform/others.go | 4 ++-- common/platform/windows.go | 4 ++-- infra/conf/transport_internet.go | 2 +- transport/internet/tls/config.go | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/common/platform/filesystem/file.go b/common/platform/filesystem/file.go index 7594395320ca..e4fe2a9aaea1 100644 --- a/common/platform/filesystem/file.go +++ b/common/platform/filesystem/file.go @@ -29,7 +29,7 @@ func ReadAsset(file string) ([]byte, error) { return ReadFile(platform.GetAssetLocation(file)) } -func ReadCertificate(file string) ([]byte, error) { +func ReadCert(file string) ([]byte, error) { if filepath.IsAbs(file) { return ReadFile(file) } diff --git a/common/platform/others.go b/common/platform/others.go index 6b7738a74dc3..3ebf0d6da00f 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -44,6 +44,6 @@ func GetAssetLocation(file string) string { } func GetCertLocation(file string) string { - certificatePath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) - return filepath.Join(certificatePath, file) + certPath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) + return filepath.Join(certPath, file) } diff --git a/common/platform/windows.go b/common/platform/windows.go index e251113d0464..8f98814ba8b4 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -26,6 +26,6 @@ func GetAssetLocation(file string) string { } func GetCertLocation(file string) string { - certificatePath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) - return filepath.Join(certificatePath, file) + certPath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) + return filepath.Join(certPath, file) } diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index f3afd2f9afbb..e32be32669da 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -334,7 +334,7 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) { func readFileOrString(f string, s []string) ([]byte, error) { if len(f) > 0 { - return filesystem.ReadCertificate(f) + return filesystem.ReadCert(f) } if len(s) > 0 { return []byte(strings.Join(s, "\n")), nil diff --git a/transport/internet/tls/config.go b/transport/internet/tls/config.go index 197895cb1d13..d6701a7d64cd 100644 --- a/transport/internet/tls/config.go +++ b/transport/internet/tls/config.go @@ -109,12 +109,12 @@ func setupOcspTicker(entry *Certificate, callback func(isReloaded, isOcspstaplin for { var isReloaded bool if entry.CertificatePath != "" && entry.KeyPath != "" { - newCert, err := filesystem.ReadCertificate(entry.CertificatePath) + newCert, err := filesystem.ReadCert(entry.CertificatePath) if err != nil { errors.LogErrorInner(context.Background(), err, "failed to parse certificate") return } - newKey, err := filesystem.ReadCertificate(entry.KeyPath) + newKey, err := filesystem.ReadCert(entry.KeyPath) if err != nil { errors.LogErrorInner(context.Background(), err, "failed to parse key") return From da7765959f67846d75072fe611e6dca411ce0961 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Mon, 24 Mar 2025 12:51:22 +0000 Subject: [PATCH 4/5] Update windows.go --- common/platform/windows.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/platform/windows.go b/common/platform/windows.go index 8f98814ba8b4..cb25a1ad9946 100644 --- a/common/platform/windows.go +++ b/common/platform/windows.go @@ -19,12 +19,13 @@ func GetToolLocation(file string) string { return filepath.Join(toolPath, file+".exe") } -// GetAssetLocation searches for `file` in the executable dir +// GetAssetLocation searches for `file` in the env dir and the executable dir func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) return filepath.Join(assetPath, file) } +// GetCertLocation searches for `file` in the env dir and the executable dir func GetCertLocation(file string) string { certPath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) return filepath.Join(certPath, file) From 72190c66cdce952629abf277826b625e67057350 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Mon, 24 Mar 2025 12:52:57 +0000 Subject: [PATCH 5/5] Update others.go --- common/platform/others.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/platform/others.go b/common/platform/others.go index 3ebf0d6da00f..a405ac480168 100644 --- a/common/platform/others.go +++ b/common/platform/others.go @@ -21,7 +21,7 @@ func GetToolLocation(file string) string { return filepath.Join(toolPath, file) } -// GetAssetLocation searches for `file` in certain locations +// GetAssetLocation searches for `file` in the env dir, the executable dir, and certain locations func GetAssetLocation(file string) string { assetPath := NewEnvFlag(AssetLocation).GetValue(getExecutableDir) defPath := filepath.Join(assetPath, file) @@ -43,6 +43,7 @@ func GetAssetLocation(file string) string { return defPath } +// GetCertLocation searches for `file` in the env dir and the executable dir func GetCertLocation(file string) string { certPath := NewEnvFlag(CertLocation).GetValue(getExecutableDir) return filepath.Join(certPath, file)