-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobileupdatecheck.go
122 lines (107 loc) · 2.76 KB
/
mobileupdatecheck.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/Masterminds/semver"
)
const (
actionNone = "NONE"
actionUpdate = "ADVICE"
actionForceUpdate = "FORCE"
)
var (
compiledRuleSets = make(map[string][]compiledRule)
)
// RuleSet represents a set of rules for a give os/product combination
type RuleSet struct {
Key string
Rules []Rule
}
// Rule represents a single rule that is part of a RuleSet
type Rule struct {
OSVersion string
ProductVersion string
Action string
}
type compiledRule struct {
osConstraints *semver.Constraints
productConstraints *semver.Constraints
action string
}
// handler checks if the request path is a valid rule set identifier and applies
// all rules in a set if there is a match
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path[0:1] == "/" && len(r.URL.Path) == 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
key := r.URL.Path[1:]
if _, ok := compiledRuleSets[key]; !ok {
w.WriteHeader(http.StatusNoContent)
return
}
osVersion, err := semver.NewVersion(r.URL.Query().Get("osVersion"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
productVersion, err := semver.NewVersion(r.URL.Query().Get("productVersion"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
for _, cr := range compiledRuleSets[key] {
if cr.osConstraints.Check(osVersion) &&
cr.productConstraints.Check(productVersion) {
w.Write([]byte(`{ "action": "` + cr.action + `" }`))
return
}
}
w.Write([]byte(`{ "action": "` + actionNone + `" }`))
}
func loadRuleSets(fname string) (map[string][]compiledRule, error) {
var ruleSets []RuleSet
// read rules file
buf, err := ioutil.ReadFile(fname)
if err != nil {
return nil, fmt.Errorf("read rules: %v", err)
}
// parse rules file
err = json.Unmarshal(buf, &ruleSets)
if err != nil {
return nil, fmt.Errorf("parse rules: %v", err)
}
// compile rules contraints
crs := make(map[string][]compiledRule)
for _, ruleSet := range ruleSets {
if _, ok := crs[ruleSet.Key]; !ok {
crs[ruleSet.Key] = []compiledRule{}
}
for _, rule := range ruleSet.Rules {
var cr compiledRule
var err error
cr.osConstraints, err = semver.NewConstraint(rule.OSVersion)
if err != nil {
return nil, fmt.Errorf("parse constraint: %v", err)
}
cr.productConstraints, err = semver.NewConstraint(rule.ProductVersion)
if err != nil {
return nil, fmt.Errorf("parse constraint: %v", err)
}
cr.action = rule.Action
crs[ruleSet.Key] = append(crs[ruleSet.Key], cr)
}
}
return crs, nil
}
func main() {
var err error
compiledRuleSets, err = loadRuleSets("rules.json")
if err != nil {
panic(err)
}
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}