package dnfbridge import ( "crypto/sha256" "encoding/hex" "fmt" "io" "os" "path/filepath" "strings" ) // 客户端产物指纹校验:影片与 90CN.dll 都必须与 client_build_id.go 里的值完全一致。 // 任何一个被改动或删除,服务端直接拒绝启动(与客户端补丁里的校验互为补充)。 // // 指纹常量由 rebuild_all.ps1 在构建后生成,不要手改。 // clientArtifactDir 返回客户端目录(= 触发文件所在目录)。 func clientArtifactDir() (string, error) { triggerPath, err := resolveClientCutsceneTriggerPath() if err != nil { return "", err } return filepath.Dir(triggerPath), nil } // verifyClientFile 校验单个文件的大小与 SHA-256。 func verifyClientFile(path string, wantSHA256 string, wantBytes int64) error { info, statErr := os.Stat(path) if statErr != nil { return fmt.Errorf("missing: %s: %w", path, statErr) } if info.IsDir() { return fmt.Errorf("is a directory: %s", path) } if info.Size() != wantBytes { return fmt.Errorf("size mismatch: %d != %d (%s)", info.Size(), wantBytes, path) } file, openErr := os.Open(path) if openErr != nil { return fmt.Errorf("open failed: %w", openErr) } defer file.Close() hasher := sha256.New() if _, copyErr := io.Copy(hasher, file); copyErr != nil { return fmt.Errorf("read failed: %w", copyErr) } digest := hex.EncodeToString(hasher.Sum(nil)) if !strings.EqualFold(digest, wantSHA256) { return fmt.Errorf("sha256 mismatch: %s (%s)", digest, path) } return nil } // verifyClientArtifacts 校验客户端影片与补丁 DLL。 func verifyClientArtifacts() error { dir, err := clientArtifactDir() if err != nil { return fmt.Errorf("client directory not resolved: %w", err) } if movieErr := verifyClientFile( filepath.Join(dir, filepath.FromSlash("Video/movie/demo_mjpeg.avi")), boundMovieSHA256, boundMovieBytes); movieErr != nil { return fmt.Errorf("movie: %w", movieErr) } if dllErr := verifyClientFile( filepath.Join(dir, "90CN.dll"), boundDllSHA256, boundDllBytes); dllErr != nil { return fmt.Errorf("client dll: %w", dllErr) } return nil }