-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
205 lines (168 loc) · 6.41 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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"path/filepath"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// CustomTime represents a custom time format with JSON unmarshalling support.
type CustomTime struct {
time.Time
}
// Dateonly represents a date-only format with JSON unmarshalling support.
type Dateonly struct {
time.Time
}
// Response represents the JSON response structure from BugCrowd API.
type Response struct {
Results []struct {
Name string `json:"program_name"`
Code string `json:"program_code"`
Asset string `json:"target"`
Severity int `json:"priority"`
Reported CustomTime `json:"created_at"`
Accepted Dateonly `json:"accepted_at"`
Points int `json:"points"`
Bounty string `json:"amount"`
Hacker string `json:"researcher_username"`
Isprivate bool `json:"visibility_public"`
Submission_text string `json:"submission_state_text"`
} `json:"results"`
}
const reportedDateLayout = time.RFC3339
const acceptedLayout = "2 Jan 2006"
// UnmarshalJSON implements JSON unmarshalling for CustomTime.
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
ct.Time, err = time.Parse(reportedDateLayout, s)
return
}
// UnmarshalJSON implements JSON unmarshalling for Dateonly.
func (ct *Dateonly) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
ct.Time = time.Time{}
return
}
ct.Time, err = time.Parse(acceptedLayout, s)
return
}
// https://bugcrowd.com/crowdstream.json?page=1&filter_by=accepted%2Cdisclosures
func main() {
page := 1
// Encode the query parameters properly using url.Values
params := url.Values{}
params.Add("page", strconv.Itoa(page))
params.Add("filter_by", "accepted,disclosures")
// Use the encoded URL in the HTTP request
crowdstream := fmt.Sprintf("https://bugcrowd.com/crowdstream.json?%s", params.Encode())
fmt.Println("URL : ", crowdstream)
resp, err := http.Get(crowdstream)
if err != nil {
log.Fatalln(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
fmt.Println("JSON Response:")
fmt.Println(string(body))
var jsonbody Response
if err := json.Unmarshal(body, &jsonbody); err != nil {
log.Println("Error in JSON Unmarshalling:", err)
log.Println("JSON Response:", string(body))
log.Fatalln(err)
}
// Create a folder for the current year
year := time.Now().Year()
yearFolder := fmt.Sprintf("%d", year)
// Create a folder for the current month inside the year folder
month := time.Now().Month().String()
monthFolder := filepath.Join(yearFolder, month)
// Ensure that the year and month folders exist
foldercreate(yearFolder)
foldercreate(monthFolder)
// Create the new date-README.md file in the month folder
dateReadmeFile, err := os.Create(filepath.Join(monthFolder, fmt.Sprintf("%d-%02d-%02d-README.md", year, time.Now().Month(), time.Now().Day())))
if err != nil {
log.Println("Error in file creation", err)
}
// Write content to the date-README.md file
writeContent(dateReadmeFile, jsonbody)
// Create the new README.md file in the main directory
readmeFile, err := os.Create("README.md")
if err != nil {
log.Println("Error in file creation", err)
}
// Write content to the README.md file
writeContent(readmeFile, jsonbody)
// Move existing files to corresponding year/month folders
//moveFilesToYearMonthFolders("archive")
fmt.Println("Program Completed.")
}
// writeContent writes the program details to the provided file.
func writeContent(file *os.File, jsonbody Response) {
fmt.Fprintln(file, "[![schedule run](https://github.com/Linuxinet/bugcrowd-crowdstream/actions/workflows/actions.yml/badge.svg?branch=master)](https://github.com/Linuxinet/bugcrowd-crowdstream/actions/workflows/actions.yml)")
fmt.Fprintln(file, "## BugCrowd Crowdstream | Date: ", time.Now().Format("2006-January-02 15:04:05"))
for i, p := range jsonbody.Results {
// Write program details to the file
fmt.Fprintf(file, "### %d. Program Details : \n\n**Name:** %s \n\n **Link:** <https://bugcrowd.com/%s> \n\n **Severity:** P%d \n\n **Hacker:** %s \n\n **Points:** %d \n\n **Target:** ` %s` \n\n **Reported:** %s \n\n **Accepted:** %s \n\n **%s** \n\n", i+1, p.Name, p.Code, p.Severity, p.Hacker, p.Points, p.Asset, p.Reported, p.Accepted, p.Submission_text)
}
fmt.Fprintln(file, "## End of Crowdstream for", time.Now().Format("2006-January-02 15:04:05"))
file.Close()
}
// foldercreate creates the specified folder if it does not exist.
func foldercreate(folderPath string) {
err := os.MkdirAll(folderPath, 0700)
if err != nil {
log.Println("Error in Creating Directory: ", folderPath)
log.Fatalln(err)
}
}
// moveFilesToYearMonthFolders moves files from the source directory to corresponding year/month folders.
func moveFilesToYearMonthFolders(sourceDir string) {
// List all files in the source directory
files, err := os.ReadDir(sourceDir)
if err != nil {
log.Fatalln("error in listing archive directory")
}
// Iterate through each file and move it to the corresponding year/month folder
for _, file := range files {
if !file.IsDir() {
fileName := file.Name()
// Extract the date from the filename (assuming filename is in the format DD-MM-YYYY-README.md)
parts := strings.Split(fileName, "-")
if len(parts) == 4 {
// day, _ := strconv.Atoi(parts[0])
month, _ := strconv.Atoi(parts[1])
year, _ := strconv.Atoi(parts[2])
// Create a folder for the current year
yearFolder := fmt.Sprintf("%d", year)
// Create a folder for the current month inside the year folder
monthFolder := filepath.Join(yearFolder, time.Month(month).String())
// Ensure that the year and month folders exist
foldercreate(yearFolder)
foldercreate(monthFolder)
// Move the file to the year/month folder
sourceFilePath := filepath.Join(sourceDir, fileName)
destFilePath := filepath.Join(monthFolder, fileName)
err := os.Rename(sourceFilePath, destFilePath)
if err != nil {
log.Printf("Error moving file %s to %s: %s\n", sourceFilePath, destFilePath, err)
}
}
}
}
}