1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 19:45:24 +00:00
v2fly/common/platform/securedload/embeddedhash.go

51 lines
1.5 KiB
Go
Raw Normal View History

2021-03-16 22:05:01 +00:00
package securedload
import (
"bytes"
"crypto/sha256"
"encoding/hex"
2021-04-03 09:14:23 +00:00
"path/filepath"
2021-03-28 00:44:04 +00:00
"strings"
2021-03-16 22:05:01 +00:00
"github.com/v2fly/VSign/insmgr"
"github.com/v2fly/VSign/signerVerify"
2021-03-28 00:44:04 +00:00
2021-03-16 22:05:01 +00: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 09:14:23 +00:00
platformFileName := filepath.FromSlash(filename)
fileContent, err := filesystem.ReadFile(platform.GetAssetLocation(platformFileName))
2021-03-16 22:05:01 +00:00
if err != nil {
return nil, newError("Cannot find file", filename).Base(err)
}
fileHash := sha256.Sum256(fileContent)
2021-03-16 22:05:01 +00:00
fileHashAsString := hex.EncodeToString(fileHash[:])
if fileNameVerified, ok := e.checkedFile[fileHashAsString]; ok {
for _, filenameVerifiedIndividual := range strings.Split(fileNameVerified, ";") {
2021-03-16 22:05:01 +00:00
if strings.HasSuffix(filenameVerifiedIndividual, filename) {
return fileContent, nil
2021-03-16 22:05:01 +00: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())
}