mirror of
https://github.com/bolucat/Archive.git
synced 2026-04-23 00:17:16 +08:00
Update On Sun May 4 20:34:17 CEST 2025
This commit is contained in:
@@ -55,48 +55,35 @@ type consoleLogWriter struct {
|
||||
logger *log.Logger // Standard logger
|
||||
}
|
||||
|
||||
// setEnvVariable safely sets an environment variable and logs any errors encountered.
|
||||
func setEnvVariable(key, value string) {
|
||||
if err := os.Setenv(key, value); err != nil {
|
||||
log.Printf("Failed to set environment variable %s: %v. Please check your configuration.", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
// InitCoreEnv initializes environment variables and file system handlers for the core
|
||||
// It sets up asset path, certificate path, XUDP base key and customizes the file reader
|
||||
// to support Android asset system
|
||||
func InitCoreEnv(envPath string, key string) {
|
||||
// Set asset/cert paths
|
||||
if len(envPath) > 0 {
|
||||
if err := os.Setenv(coreAsset, envPath); err != nil {
|
||||
log.Printf("Failed to set %s: %v", coreAsset, err)
|
||||
}
|
||||
if err := os.Setenv(coreCert, envPath); err != nil {
|
||||
log.Printf("Failed to set %s: %v", coreCert, err)
|
||||
}
|
||||
setEnvVariable(coreAsset, envPath)
|
||||
setEnvVariable(coreCert, envPath)
|
||||
}
|
||||
|
||||
// Set XUDP encryption key
|
||||
if len(key) > 0 {
|
||||
if err := os.Setenv(xudpBaseKey, key); err != nil {
|
||||
log.Printf("Failed to set %s: %v", xudpBaseKey, err)
|
||||
}
|
||||
setEnvVariable(xudpBaseKey, key)
|
||||
}
|
||||
|
||||
// Custom file reader with path validation
|
||||
corefilesystem.NewFileReader = func(path string) (io.ReadCloser, error) {
|
||||
// G304 Fix - Path sanitization
|
||||
baseDir := envPath
|
||||
cleanPath := filepath.Clean(path)
|
||||
fullPath := filepath.Join(baseDir, cleanPath)
|
||||
|
||||
// Prevent directory traversal
|
||||
if baseDir != "" && !strings.HasPrefix(fullPath, baseDir) {
|
||||
return nil, fmt.Errorf("unauthorized path: %s", path)
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
_, file := filepath.Split(path)
|
||||
return mobasset.Open(file)
|
||||
}
|
||||
|
||||
// Check file existence
|
||||
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
||||
_, file := filepath.Split(fullPath)
|
||||
return mobasset.Open(file) // Fallback to assets
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("file access error: %w", err)
|
||||
}
|
||||
|
||||
return os.Open(fullPath) // #nosec G304 - Validated path
|
||||
return os.Open(path)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user