-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
executable file
·229 lines (198 loc) · 5.17 KB
/
api.go
File metadata and controls
executable file
·229 lines (198 loc) · 5.17 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
import _ "net/http/pprof"
type XMLOpt struct {
Packages []string `xml:"packages"`
Release int `xml:"release,attr"`
Product string `xml:"product,attr"`
References string `xml:"references,attr"`
Type string `xml:"type,attr"`
Topic string `xml:"topic,attr"`
OsRelease []int `xml:"os_release"`
OsArch []string `xml:"os_arch"`
Severity string `xml:"severity,attr"`
Solution string `xml:"solution,attr"`
Notes string `xml:"notes,attr"`
Synopsis string `xml:"synopsis,attr"`
Description string `xml:"description,attr"`
}
type XMLOpts struct {
Opt []XMLOpt `xml:",any"`
}
var mutex sync.RWMutex
var lastModified time.Time
func ShouldRefreshErrata() bool {
resp, err := http.Head("http://cefs.steve-meier.de/errata.latest.xml")
if err != nil {
fmt.Println("[~] Errata HEAD failed")
return false
}
defer resp.Body.Close()
// Example: Fri, 31 Oct 2014 09:40:46 GMT
const longForm = "Mon, 02 Jan 2006 03:04:05 MST"
_lastModified, err := time.Parse(longForm, resp.Header.Get("Last-Modified"))
if err != nil {
fmt.Println("[~] Time Parse failed: ", resp.Header.Get("Last-Modified"))
return false
}
// Dont compare Time objects, make sure they're something comparible first.
//if fmt.Sprintf("%s", _lastModified) != fmt.Sprintf("%s", lastModified) {
if !_lastModified.Equal(lastModified) {
fmt.Println(fmt.Sprintf("%s", _lastModified), "=", fmt.Sprintf("%s", lastModified))
lastModified = _lastModified
return true
} else {
return false
}
}
func GetSecurityErrata() []byte {
// http://cefs.steve-meier.de/errata.latest.xml
resp, err := http.Get("http://cefs.steve-meier.de/errata.latest.xml")
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
resp.Body.Close()
return body
}
func ParsePackageVersion(name string) int {
if !strings.Contains(name, "-") {
return 0
}
parts := strings.Split(name, "-")
version := strings.Split(parts[1], ".")
majorVersion, _ := strconv.ParseInt(version[0], 10, 0)
return int(majorVersion)
}
func ParseSecurityErrata() {
v := XMLOpts{}
errata := GetSecurityErrata()
versionLUT = map[int]packageLUT{}
err := xml.Unmarshal(errata, &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
for _, pkg := range v.Opt {
if pkg.Type == "Security Advisory" {
for _, ver := range pkg.OsRelease {
if versionLUT[ver] == nil {
versionLUT[ver] = packageLUT{}
}
for _, pack := range pkg.Packages {
major := ParsePackageVersion(pack)
if versionLUT[ver][major] == nil {
versionLUT[ver][major] = []XMLOpt{}
}
versionLUT[ver][major] = append(versionLUT[ver][major], pkg)
}
}
}
}
}
func CheckForUpdates() {
mutex.Lock()
if ShouldRefreshErrata() {
fmt.Println("!!!![ ]!!!! Refreshing errata....", time.Now())
ParseSecurityErrata()
fmt.Println("!!!![x]!!!! Refreshing errata....", time.Now())
}
mutex.Unlock()
}
type packageLUT map[int][]XMLOpt
var versionLUT map[int]packageLUT = map[int]packageLUT{}
func AppendIfMissing(slice []XMLOpt, x XMLOpt) []XMLOpt {
for _, ele := range slice {
if ele.Equal(x) {
return slice
}
}
return append(slice, x)
}
func (p *XMLOpt) Equal(o XMLOpt) bool {
if p.Release != o.Release {
return false
}
if len(p.OsRelease) != len(o.OsRelease) {
return false
}
for i, pr := range p.OsRelease {
if o.OsRelease[i] != pr {
return false
}
}
if len(p.Packages) != len(o.Packages) {
return false
}
for i, pp := range p.Packages {
if o.Packages[i] != pp {
return false
}
}
return true
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[len("/api/"):]
pathArr := strings.Split(path, "/")
_, osRelease, pkg, pkgVersion := pathArr[1], pathArr[2], pathArr[3], pathArr[4]
majorVer := strings.Split(pkgVersion, ".")[0]
version, _ := strconv.ParseInt(osRelease, 10, 0)
release, _ := strconv.ParseInt(majorVer, 10, 0)
//fmt.Println("pkgVersion =", pkgVersion)
//fmt.Println("osRelease =", osRelease)
//fmt.Println("majorVer =", majorVer)
//fmt.Println("pkg =", pkg)
mutex.RLock()
defer mutex.RUnlock()
xpkgs := versionLUT[int(version)][int(release)]
respPkgs := []XMLOpt{}
for _, xpkg := range xpkgs {
for _, vpkg := range xpkg.Packages {
if strings.Contains(vpkg, pkg) && strings.Contains(vpkg, pkgVersion) {
respPkgs = AppendIfMissing(respPkgs, xpkg)
}
}
}
err := json.NewEncoder(w).Encode(respPkgs)
if err != nil {
fmt.Println(err)
}
}
func apiUpdatedAt(w http.ResponseWriter, r *http.Request) {
resp := struct {
UpdatedAt time.Time
}{lastModified}
err := json.NewEncoder(w).Encode(resp)
if err != nil {
fmt.Println(err)
}
}
func main() {
CheckForUpdates()
ticker := time.NewTicker(60 * time.Second)
go func() {
for t := range ticker.C {
fmt.Println("[ ] Checking for updates....", t)
CheckForUpdates()
fmt.Println("[x] Checking for updates....", t)
}
}()
http.HandleFunc("/api/updated", apiUpdatedAt)
http.HandleFunc("/api/", apiHandler)
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic(err)
}
}