27 lines
649 B
Go
27 lines
649 B
Go
//go:build windows
|
|||
|
|
|
||
|
|
package platform
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
)
|
||
|
|
|
||
|
|
// GetHostsFilePath returns the absolute path to the system hosts file on Windows.
|
||
|
|
// Returns an error if the file does not exist or cannot be accessed.
|
||
|
|
func GetHostsFilePath() (string, error) {
|
||
|
|
var path string
|
||
|
|
if val, ok := os.LookupEnv("SystemRoot"); ok {
|
||
|
|
path = val + "\\System32\\drivers\\etc\\hosts"
|
||
|
|
} else {
|
||
|
|
path = "C:\\Windows\\System32\\drivers\\etc\\hosts"
|
||
|
|
}
|
||
|
|
if _, err := os.Stat(path); err != nil {
|
||
|
|
if os.IsNotExist(err) {
|
||
|
|
return "", fmt.Errorf("file does not exist: %s", path)
|
||
|
|
}
|
||
|
|
return "", fmt.Errorf("accessing hosts file: %w", err)
|
||
|
|
}
|
||
|
|
return path, nil
|
||
|
|
}
|