|
1 | 1 | package handlers
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "archive/zip" |
5 |
| - "encoding/csv" |
6 |
| - "fmt" |
7 |
| - "io" |
8 | 4 | "net/http"
|
9 |
| - "net/url" |
10 |
| - "os" |
11 |
| - "path/filepath" |
12 |
| - "strings" |
13 |
| -) |
14 | 5 |
|
15 |
| -const ( |
16 |
| - fileURL = "https://s3-us-west-1.amazonaws.com/umbrella-static/top-1m.csv.zip" |
17 |
| - tempFilePath = "/tmp/top-1m.csv" |
| 6 | + "github.com/xray-web/web-check-api/checks" |
18 | 7 | )
|
19 | 8 |
|
20 |
| -type RankResponse struct { |
21 |
| - Domain string `json:"domain"` |
22 |
| - Rank string `json:"rank"` |
23 |
| - IsFound bool `json:"isFound"` |
24 |
| -} |
25 |
| - |
26 |
| -func checkLegacyRank(urlStr string) (RankResponse, error) { |
27 |
| - var domain string |
28 |
| - var err error |
29 |
| - |
30 |
| - // Parse the URL to extract the domain |
31 |
| - u, err := url.Parse(urlStr) |
32 |
| - if err != nil { |
33 |
| - return RankResponse{}, fmt.Errorf("invalid URL") |
34 |
| - } |
35 |
| - |
36 |
| - // Extract the domain from the parsed URL |
37 |
| - if u.Host != "" { |
38 |
| - domain = u.Host |
39 |
| - } else { |
40 |
| - // If Host is empty, try to extract the domain from the Path |
41 |
| - parts := strings.Split(u.Path, "/") |
42 |
| - if len(parts) > 0 { |
43 |
| - domain = parts[0] |
44 |
| - } else { |
45 |
| - return RankResponse{}, fmt.Errorf("unable to extract domain from URL") |
46 |
| - } |
47 |
| - } |
48 |
| - |
49 |
| - // Download and unzip the file if not in cache |
50 |
| - if _, err := os.Stat(tempFilePath); os.IsNotExist(err) { |
51 |
| - if err := downloadAndUnzip(fileURL); err != nil { |
52 |
| - return RankResponse{}, err |
53 |
| - } |
54 |
| - } |
55 |
| - |
56 |
| - // Parse the CSV and find the rank |
57 |
| - file, err := os.Open(tempFilePath) |
58 |
| - if err != nil { |
59 |
| - return RankResponse{}, fmt.Errorf("error opening CSV file: %s", err) |
60 |
| - } |
61 |
| - defer file.Close() |
62 |
| - |
63 |
| - reader := csv.NewReader(file) |
64 |
| - for { |
65 |
| - record, err := reader.Read() |
66 |
| - if err == io.EOF { |
67 |
| - break |
68 |
| - } |
69 |
| - if err != nil { |
70 |
| - return RankResponse{}, fmt.Errorf("error reading CSV record: %s", err) |
71 |
| - } |
72 |
| - |
73 |
| - if record[1] == domain { |
74 |
| - return RankResponse{ |
75 |
| - Domain: domain, |
76 |
| - Rank: record[0], |
77 |
| - IsFound: true, |
78 |
| - }, nil |
79 |
| - } |
80 |
| - } |
81 |
| - |
82 |
| - return RankResponse{ |
83 |
| - Domain: domain, |
84 |
| - IsFound: false, |
85 |
| - }, nil |
86 |
| -} |
87 |
| - |
88 |
| -func downloadAndUnzip(url string) error { |
89 |
| - resp, err := http.Get(url) |
90 |
| - if err != nil { |
91 |
| - return fmt.Errorf("error downloading file: %s", err) |
92 |
| - } |
93 |
| - defer resp.Body.Close() |
94 |
| - |
95 |
| - zipFile, err := os.Create(tempFilePath + ".zip") |
96 |
| - if err != nil { |
97 |
| - return fmt.Errorf("error creating zip file: %s", err) |
98 |
| - } |
99 |
| - defer zipFile.Close() |
100 |
| - |
101 |
| - _, err = io.Copy(zipFile, resp.Body) |
102 |
| - if err != nil { |
103 |
| - return fmt.Errorf("error writing zip file: %s", err) |
104 |
| - } |
105 |
| - |
106 |
| - err = unzip(tempFilePath+".zip", "/tmp") |
107 |
| - if err != nil { |
108 |
| - return fmt.Errorf("error unzipping file: %s", err) |
109 |
| - } |
110 |
| - |
111 |
| - return nil |
112 |
| -} |
113 |
| - |
114 |
| -func unzip(src, dest string) error { |
115 |
| - r, err := zip.OpenReader(src) |
116 |
| - if err != nil { |
117 |
| - return err |
118 |
| - } |
119 |
| - defer r.Close() |
120 |
| - |
121 |
| - for _, f := range r.File { |
122 |
| - rc, err := f.Open() |
123 |
| - if err != nil { |
124 |
| - return err |
125 |
| - } |
126 |
| - defer rc.Close() |
127 |
| - |
128 |
| - path := filepath.Join(dest, f.Name) |
129 |
| - if f.FileInfo().IsDir() { |
130 |
| - os.MkdirAll(path, f.Mode()) |
131 |
| - } else { |
132 |
| - os.MkdirAll(filepath.Dir(path), os.ModePerm) |
133 |
| - f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) |
134 |
| - if err != nil { |
135 |
| - return err |
136 |
| - } |
137 |
| - defer f.Close() |
138 |
| - |
139 |
| - _, err = io.Copy(f, rc) |
140 |
| - if err != nil { |
141 |
| - return err |
142 |
| - } |
143 |
| - } |
144 |
| - } |
145 |
| - |
146 |
| - return nil |
147 |
| -} |
148 |
| - |
149 |
| -func HandleLegacyRank() http.Handler { |
| 9 | +func HandleLegacyRank(l *checks.LegacyRank) http.Handler { |
150 | 10 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
151 | 11 | rawURL, err := extractURL(r)
|
152 | 12 | if err != nil {
|
153 | 13 | JSONError(w, ErrMissingURLParameter, http.StatusBadRequest)
|
154 | 14 | return
|
155 | 15 | }
|
156 | 16 |
|
157 |
| - result, err := checkLegacyRank(rawURL.String()) |
| 17 | + result, err := l.LegacyRank(rawURL.Hostname()) |
158 | 18 | if err != nil {
|
159 | 19 | JSONError(w, err, http.StatusInternalServerError)
|
160 | 20 | return
|
|
0 commit comments