-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
phishtank.go
67 lines (56 loc) · 1.35 KB
/
phishtank.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
package main
import (
"compress/gzip"
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"os"
)
var phishtankURLCache = "./.phishtankURLCache"
var phishtankURL = "http://data.phishtank.com/data/online-valid.csv.gz"
func savePhishtankDataset() error {
resp, err := http.Get(phishtankURL)
if err != nil {
return fmt.Errorf("failed to download file: %w", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case 404:
log.Println("Phishtank is down. Please try again later.")
os.Exit(-1)
case 429:
log.Println("Phishtank has rate-limited you. Please try again.")
os.Exit(-1)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
gzipReader, err := gzip.NewReader(resp.Body)
if err != nil {
return fmt.Errorf("failed to create gzip reader: %w", err)
}
defer gzipReader.Close()
reader := csv.NewReader(gzipReader)
outputFile, err := os.Create(phishtankURLCache)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer outputFile.Close()
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("error reading record: %w", err)
}
if len(record) >= 2 {
if _, err := outputFile.WriteString(record[1] + "\n"); err != nil {
return fmt.Errorf("failed to write to output file: %w", err)
}
}
}
return nil
}