|
| 1 | +package generators |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/gorilla/feeds" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type PentesterLandGenerator struct { |
| 14 | + filterFunc ItemFilterFunc |
| 15 | + itemModFunc ItemModifierFunc |
| 16 | + MaxLength int |
| 17 | +} |
| 18 | + |
| 19 | +type PentesterLandEntry struct { |
| 20 | + Links []struct { |
| 21 | + Title string |
| 22 | + Link string |
| 23 | + } |
| 24 | + Authors []string |
| 25 | + Programs []string |
| 26 | + Bugs []string |
| 27 | + Bounty string |
| 28 | + PublicationDate string |
| 29 | + AddedDate string |
| 30 | +} |
| 31 | + |
| 32 | +func (g *PentesterLandGenerator) fetch() ([]PentesterLandEntry, error) { |
| 33 | + url := "https://pentester.land/writeups.json" |
| 34 | + client := http.Client{ |
| 35 | + Timeout: 15 * time.Second, |
| 36 | + } |
| 37 | + |
| 38 | + res, err := client.Get(url) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + defer func() { _ = res.Body.Close() }() |
| 43 | + |
| 44 | + body, err := ioutil.ReadAll(res.Body) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + var entries struct { |
| 50 | + Data []PentesterLandEntry `json:"data"` |
| 51 | + } |
| 52 | + |
| 53 | + err = json.Unmarshal(body, &entries) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + return entries.Data, nil |
| 58 | +} |
| 59 | + |
| 60 | +func (g *PentesterLandGenerator) Feed() (*feeds.Feed, error) { |
| 61 | + entries, err := g.fetch() |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + out := feeds.Feed{} |
| 67 | + out.Title = "Pentester.Land Writeups" |
| 68 | + out.Link = &feeds.Link{Href: "https://pentester.land/writeups/"} |
| 69 | + out.Updated = time.Now() |
| 70 | + |
| 71 | + for _, e := range entries { |
| 72 | + item := &feeds.Item{ |
| 73 | + Title: e.Links[0].Title, |
| 74 | + Link: &feeds.Link{ |
| 75 | + Href: e.Links[0].Link, |
| 76 | + }, |
| 77 | + Author: &feeds.Author{ |
| 78 | + Name: strings.Join(e.Authors, ", "), |
| 79 | + }, |
| 80 | + Description: strings.Join(e.Bugs, ", "), |
| 81 | + Updated: time.Time{}, |
| 82 | + Created: time.Time{}, |
| 83 | + } |
| 84 | + |
| 85 | + if e.Bounty != "-" && e.Bounty != "" { |
| 86 | + item.Description += fmt.Sprintf("(%s Bounty)", e.Bounty) |
| 87 | + } |
| 88 | + |
| 89 | + t, err := time.Parse("2006-01-02", e.PublicationDate) |
| 90 | + if err != nil { |
| 91 | + continue |
| 92 | + } |
| 93 | + item.Created = t |
| 94 | + |
| 95 | + t, err = time.Parse("2006-01-02", e.AddedDate) |
| 96 | + if err != nil { |
| 97 | + continue |
| 98 | + } |
| 99 | + item.Updated = t |
| 100 | + |
| 101 | + if g.itemModFunc != nil { |
| 102 | + g.itemModFunc(item) |
| 103 | + } |
| 104 | + if g.filterFunc == nil || g.filterFunc(item) { |
| 105 | + out.Items = append(out.Items, item) |
| 106 | + } |
| 107 | + |
| 108 | + if len(out.Items) >= g.MaxLength { |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + if len(out.Items) > 0 { |
| 113 | + out.Updated = out.Items[0].Updated |
| 114 | + } |
| 115 | + |
| 116 | + return &out, nil |
| 117 | +} |
| 118 | + |
| 119 | +func (g *PentesterLandGenerator) RegisterItemFilter(callback ItemFilterFunc) { |
| 120 | + g.filterFunc = callback |
| 121 | +} |
| 122 | +func (g *PentesterLandGenerator) RegisterItemModifier(callback ItemModifierFunc) { |
| 123 | + g.itemModFunc = callback |
| 124 | +} |
0 commit comments