-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
372 lines (326 loc) · 8.46 KB
/
main.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/google/go-github/v39/github"
)
type PullRequest struct {
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
HTMLURL string `json:"html_url"`
User struct {
Login string `json:"login"`
} `json:"user"`
Head struct {
SHA string `json:"sha"`
} `json:"head"`
}
type PullRequestData struct {
LastRun time.Time `json:"last_run"`
PRs []PullRequest `json:"prs"`
}
func main() {
owner := "projectdiscovery"
repo := "nuclei-templates"
outputFile := flag.String("output", "pull_requests.json", "Output file for pull requests")
silent := flag.Bool("silent", false, "Silent mode")
download := flag.Bool("download", false, "Download YAML files")
flag.Parse()
client := github.NewClient(nil)
ctx := context.Background()
data, err := loadPreviousPRs(*outputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading previous pull requests: %v\n", err)
data = &PullRequestData{LastRun: time.Now().AddDate(0, -1, 0), PRs: []PullRequest{}}
}
newPRs, err := fetchNewPullRequests(ctx, client, owner, repo, data.LastRun, *silent)
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching new pull requests: %v\n", err)
os.Exit(1)
}
if len(newPRs) > 0 {
fmt.Println("New pull requests:")
for _, pr := range newPRs {
fmt.Printf("- %s\n", pr.Title)
}
if *download {
if err := downloadYAMLFiles(newPRs); err != nil {
fmt.Fprintf(os.Stderr, "Error downloading YAML files: %v\n", err)
}
}
data.PRs = append(newPRs, data.PRs...)
data.LastRun = time.Now()
if err := writeToFile(data, *outputFile); err != nil {
fmt.Fprintf(os.Stderr, "Error saving pull requests: %v\n", err)
}
} else {
fmt.Println("No new pull requests found.")
}
}
func getPRSlice(prMap map[string]PullRequest) []PullRequest {
prSlice := make([]PullRequest, 0, len(prMap))
for _, pr := range prMap {
prSlice = append(prSlice, pr)
}
return prSlice
}
func fetchNewPullRequests(ctx context.Context, client *github.Client, owner, repo string, since time.Time, silent bool) ([]PullRequest, error) {
opts := &github.PullRequestListOptions{
State: "open",
Sort: "created",
Direction: "desc",
ListOptions: github.ListOptions{
PerPage: 100,
},
}
var newPRs []PullRequest
for {
prs, resp, err := client.PullRequests.List(ctx, owner, repo, opts)
if err != nil {
return nil, err
}
for _, pr := range prs {
if pr.CreatedAt.After(since) && strings.Contains(strings.ToLower(*pr.Title), "cve") {
newPR := PullRequest{
Title: *pr.Title,
CreatedAt: *pr.CreatedAt,
HTMLURL: *pr.HTMLURL,
User: struct {
Login string `json:"login"`
}{
Login: *pr.User.Login,
},
Head: struct {
SHA string `json:"sha"`
}{
SHA: *pr.Head.SHA,
},
}
newPRs = append(newPRs, newPR)
//if !silent {
// fmt.Printf("New PR: %s\n", newPR.Title)
//}
} else if pr.CreatedAt.Before(since) || pr.CreatedAt.Equal(since) {
// We've reached PRs that are older than or equal to our last run, so we can stop
return newPRs, nil
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return newPRs, nil
}
func loadPreviousPRs(filename string) (*PullRequestData, error) {
file, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
return &PullRequestData{LastRun: time.Now().AddDate(0, -1, 0), PRs: []PullRequest{}}, nil
}
return nil, err
}
defer file.Close()
var data PullRequestData
decoder := json.NewDecoder(file)
if err := decoder.Decode(&data); err != nil {
return nil, err
}
return &data, nil
}
//func fetchPullRequests(ctx context.Context, client *github.Client, owner, repo string, since time.Time, outputFile string, silent bool) error {
// opts := &github.PullRequestListOptions{
// State: "open",
// Sort: "created",
// Direction: "desc",
// ListOptions: github.ListOptions{
// PerPage: 100,
// },
// }
//
// var allPRs []PullRequest
//
// for {
// prs, resp, err := client.PullRequests.List(ctx, owner, repo, opts)
// if err != nil {
// return err
// }
//
// for _, pr := range prs {
// if pr.CreatedAt.Before(since) {
// break
// }
//
// if strings.Contains(strings.ToLower(*pr.Title), "cve") {
// allPRs = append(allPRs, PullRequest{
// Title: *pr.Title,
// CreatedAt: *pr.CreatedAt,
// HTMLURL: *pr.HTMLURL,
// User: struct {
// Login string `json:"login"`
// }{
// Login: *pr.User.Login,
// },
// Head: struct {
// SHA string `json:"sha"`
// }{
// SHA: *pr.Head.SHA,
// },
// })
//
// if !silent {
// fmt.Printf("New PR: %s\n", *pr.Title)
// }
// }
// }
//
// if resp.NextPage == 0 {
// break
// }
// opts.Page = resp.NextPage
// }
//
// return writeToFile(allPRs, outputFile)
//}
func writeToFile(data *PullRequestData, filename string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return encoder.Encode(data)
}
func downloadYAMLFiles(prs []PullRequest) error {
paths := []string{
"http/cves",
"network/cves",
"passive/cves",
"code/cves",
"headless/cves",
"dast/cves",
"javascript/cves",
}
for _, pr := range prs {
cve := extractCVE(pr.Title)
if cve == "" {
fmt.Printf("Skipping PR: %s (No CVE found in title)\n", pr.Title)
continue
}
year := extractYear(cve)
if year == "" {
fmt.Printf("Skipping PR: %s (No year found in CVE)\n", pr.Title)
continue
}
cveUpper := strings.ToUpper(strings.TrimSuffix(cve, ".yaml"))
filename := cveUpper + ".yaml"
lowercaseFilename := strings.ToLower(cveUpper) + ".yaml"
var downloadedURL string
var remoteContent []byte
for _, path := range paths {
url := fmt.Sprintf("https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/%s/%s/%s/%s",
pr.Head.SHA,
path,
year,
filename)
content, err := fetchFileContent(url)
if err == nil {
downloadedURL = url
remoteContent = content
break
}
}
// If standard paths fail, try root with uppercase filename
if downloadedURL == "" {
url := fmt.Sprintf("https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/%s/%s",
pr.Head.SHA,
filename)
content, err := fetchFileContent(url)
if err == nil {
downloadedURL = url
remoteContent = content
}
}
// If uppercase in root fails, try lowercase in root
if downloadedURL == "" {
url := fmt.Sprintf("https://raw.githubusercontent.com/projectdiscovery/nuclei-templates/%s/%s",
pr.Head.SHA,
lowercaseFilename)
content, err := fetchFileContent(url)
if err == nil {
downloadedURL = url
remoteContent = content
}
}
if downloadedURL != "" {
if shouldUpdateFile(filename, remoteContent) {
if err := os.WriteFile(filename, remoteContent, 0644); err != nil {
fmt.Printf("Failed to write file %s: %v\n", filename, err)
} else {
fmt.Printf("Updated: %s\n", downloadedURL)
}
} else {
fmt.Printf("Skipped: %s (No changes)\n", filename)
}
} else {
fmt.Printf("Failed to download %s from any path\n", filename)
}
}
return nil
}
func extractCVE(title string) string {
// First, try to find CVE pattern in the whole string
re := regexp.MustCompile(`(?i)CVE-\d{4}-\d{4,7}`)
match := re.FindString(title)
if match != "" {
return strings.TrimSuffix(match, ".yaml")
}
// If no match found, split by common delimiters and check each part
parts := strings.FieldsFunc(title, func(r rune) bool {
return r == ' ' || r == ',' || r == ';' || r == ':'
})
//parts := strings.Fields(title)
for _, part := range parts {
if strings.HasPrefix(strings.ToUpper(part), "CVE-") {
return strings.TrimSuffix(part, ".yaml")
}
}
return ""
}
func extractYear(cve string) string {
parts := strings.Split(cve, "-")
if len(parts) >= 2 {
return parts[1]
}
return ""
}
func fetchFileContent(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %s", resp.Status)
}
return io.ReadAll(resp.Body)
}
func shouldUpdateFile(filename string, remoteContent []byte) bool {
localContent, err := os.ReadFile(filename)
if err != nil {
// File doesn't exist locally, should download
return true
}
// Compare content
return !bytes.Equal(localContent, remoteContent)
}