1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-19 03:34:31 -04:00
v2fly/common/platform/securedload/embeddedhash.go

51 lines
1.5 KiB
Go
Raw Normal View History

2021-03-16 18:05:01 -04:00
package securedload
import (
"bytes"
"crypto/sha256"
"encoding/hex"
2021-04-03 05:14:23 -04:00
"path/filepath"
2021-03-27 20:44:04 -04:00
"strings"
2021-03-16 18:05:01 -04:00
"github.com/v2fly/VSign/insmgr"
"github.com/v2fly/VSign/signerVerify"
2021-03-27 20:44:04 -04:00
2021-03-16 18:05:01 -04:00
"github.com/v2fly/v2ray-core/v4/common/platform"
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
)
type EmbeddedHashProtectedLoader struct {
checkedFile map[string]string
}
func (e EmbeddedHashProtectedLoader) VerifyAndLoad(filename string) ([]byte, error) {
2021-04-03 05:14:23 -04:00
platformFileName := filepath.FromSlash(filename)
fileContent, err := filesystem.ReadFile(platform.GetAssetLocation(platformFileName))
2021-03-16 18:05:01 -04:00
if err != nil {
return nil, newError("Cannot find file", filename).Base(err)
}
fileHash := sha256.Sum256(fileContent)
2021-03-16 18:05:01 -04:00
fileHashAsString := hex.EncodeToString(fileHash[:])
if fileNameVerified, ok := e.checkedFile[fileHashAsString]; ok {
for _, filenameVerifiedIndividual := range strings.Split(fileNameVerified, ";") {
2021-03-16 18:05:01 -04:00
if strings.HasSuffix(filenameVerifiedIndividual, filename) {
return fileContent, nil
2021-03-16 18:05:01 -04:00
}
}
}
return nil, newError("Unrecognized file at ", filename, " can not be loaded for execution")
}
func NewEmbeddedHashProtectedLoader() *EmbeddedHashProtectedLoader {
instructions := insmgr.ReadAllIns(bytes.NewReader([]byte(allowedHashes)))
checkedFile, _, ok := signerVerify.CheckAsClient(instructions, "v2fly", true)
if !ok {
panic("Embedded Hash data is invalid")
}
return &EmbeddedHashProtectedLoader{checkedFile: checkedFile}
}
func init() {
RegisterProtectedLoader("embedded", NewEmbeddedHashProtectedLoader())
}