-
Notifications
You must be signed in to change notification settings - Fork 14
/
PluginFingerprinter.go
69 lines (65 loc) · 1.85 KB
/
PluginFingerprinter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package wpfinger
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"github.com/hashicorp/go-version"
"io"
"log"
"net/http"
)
type ComponentFingerprint struct {
Slug string `gorm:"primaryKey;autoIncrement:false"`
Type string `gorm:"primarykey;autoIncrement:false"`
Version string `gorm:"primarykey;autoIncrement:false"`
Filename string
Hash string `gorm:"index"`
Fuzzy bool
Downloaded int `gorm:"index"`
}
func GetPluginFingerprints(plugin PluginInfo, pluginVersion string) (*ComponentFingerprint, error) {
parseVersion, err := version.NewVersion(pluginVersion)
if err != nil {
return nil, err
}
if len(parseVersion.Prerelease()) > 0 {
return nil, errors.New("pre release")
}
var readmeHash string
var readmeFile string
for _, readmeFile = range []string{"readme.txt", "README.md", "README.txt", "readme.md"} {
readmeHash, err = getPluginReadmeHash(readmeFile, plugin.Slug, pluginVersion)
if err == nil {
break
}
}
if err != nil {
return nil, err
}
return &ComponentFingerprint{
Slug: plugin.Slug,
Type: "plugin",
Version: pluginVersion,
Filename: readmeFile,
Hash: readmeHash,
Downloaded: plugin.Downloaded,
}, nil
}
func getPluginReadmeHash(readmeFile, pluginName, pluginVersion string) (string, error) {
tracUrl := fmt.Sprintf("https://plugins.svn.wordpress.org/%s/tags/%s/%s", pluginName, pluginVersion, readmeFile)
resp, err := http.DefaultClient.Get(tracUrl)
if err != nil {
return "", fmt.Errorf("server error while loading %s", readmeFile)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if resp.StatusCode == 403 {
log.Fatalln(tracUrl)
}
return "", fmt.Errorf("file not found %s : %d on %s", readmeFile, resp.StatusCode, tracUrl)
}
hasher := sha256.New()
_, err = io.Copy(hasher, resp.Body)
return hex.EncodeToString(hasher.Sum(nil)), nil
}